1 |
/* |
2 |
* Copyright © 2003-2004 E. Will et al. |
3 |
* Copyright © 2005-2006 Atheme Development Group |
4 |
* Rights to this code are as documented in doc/pod/license.pod. |
5 |
* |
6 |
* Misc tools |
7 |
* |
8 |
* $Id: tools.h,v 1.7 2007-08-30 19:56:20 pippijn Exp $ |
9 |
*/ |
10 |
|
11 |
#ifndef _TOOLS_H |
12 |
#define _TOOLS_H |
13 |
|
14 |
#include <object.h> |
15 |
|
16 |
/** |
17 |
* Warns operators and console users that an error occurred |
18 |
*/ |
19 |
#define warn(msg) \ |
20 |
do \ |
21 |
{ \ |
22 |
slog (LG_INFO, "%s(%d) [%s]: critical: " msg, \ |
23 |
__FILE__, __LINE__, CURFUNC); \ |
24 |
wallops ("%s(%d) [%s]: critical: " msg, \ |
25 |
__FILE__, __LINE__, CURFUNC);\ |
26 |
} while (0) |
27 |
|
28 |
/** |
29 |
* Performs a soft assertion. If the assertion fails, we wallops () and log. |
30 |
*/ |
31 |
#define soft_assert(cond) \ |
32 |
if (!(cond)) \ |
33 |
{ \ |
34 |
slog (LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
35 |
__FILE__, __LINE__, CURFUNC, #cond); \ |
36 |
wallops ("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
37 |
__FILE__, __LINE__, CURFUNC, #cond); \ |
38 |
} |
39 |
|
40 |
/** |
41 |
* Same as soft_assert, but returns if an assertion fails. |
42 |
*/ |
43 |
#define return_if_fail(cond) \ |
44 |
if (!(cond)) \ |
45 |
{ \ |
46 |
slog (LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
47 |
__FILE__, __LINE__, CURFUNC, #cond); \ |
48 |
wallops ("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
49 |
__FILE__, __LINE__, CURFUNC, #cond); \ |
50 |
return; \ |
51 |
} |
52 |
|
53 |
/** |
54 |
* Same as return_if_fail, but returns a given value if an assertion fails. |
55 |
*/ |
56 |
#define return_val_if_fail(cond, val) \ |
57 |
if (!(cond)) \ |
58 |
{ \ |
59 |
slog (LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
60 |
__FILE__, __LINE__, CURFUNC, #cond); \ |
61 |
wallops ("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
62 |
__FILE__, __LINE__, CURFUNC, #cond); \ |
63 |
return (val); \ |
64 |
} |
65 |
|
66 |
/* email stuff */ |
67 |
/* the following struct is not used yet */ |
68 |
struct email_t |
69 |
{ |
70 |
char *sender; |
71 |
char *reciever; |
72 |
char *subject; |
73 |
char *body; |
74 |
char **headers; |
75 |
|
76 |
void *miscellaneous; /* module defined data */ |
77 |
void (*callback_sent) (email_t *); /* callback on email send */ |
78 |
}; |
79 |
|
80 |
E int sendemail (user_t *from, int type, myuser_t *mu, char const * const param); |
81 |
|
82 |
/* email types (meaning of param argument) */ |
83 |
enum email_type |
84 |
{ |
85 |
EMAIL_REGISTER = 1, /* register an account/nick (verification code) */ |
86 |
EMAIL_SENDPASS = 2, /* send a password to a user (password) */ |
87 |
EMAIL_SETEMAIL = 3, /* change email address (verification code) */ |
88 |
EMAIL_MEMO = 4, /* emailed memos (memo text) */ |
89 |
EMAIL_SETPASS = 5 /* send a password change key (verification code) */ |
90 |
}; |
91 |
|
92 |
/* function.c */ |
93 |
/* misc string stuff */ |
94 |
E char *gen_pw (int sz); |
95 |
E void tb2sp (char *line); |
96 |
E char *replace (char *s, int size, char const * const oldstr, char const * const newstr); |
97 |
|
98 |
/* reverse of atoi() */ |
99 |
template<typename T> |
100 |
static inline char * |
101 |
itoa (T num) |
102 |
{ |
103 |
static char ret[32]; |
104 |
itoa (num, ret); |
105 |
return ret; |
106 |
} |
107 |
|
108 |
E int validemail (char *email); |
109 |
E bool validhostmask (char *host); |
110 |
E char const *sbytes (float x); |
111 |
E float bytes (float x); |
112 |
|
113 |
E unsigned long makekey (void); |
114 |
|
115 |
/* time stuff */ |
116 |
#if HAVE_GETTIMEOFDAY |
117 |
E void s_time (struct timeval *sttime); |
118 |
E void e_time (struct timeval sttime, struct timeval *ttime); |
119 |
E int tv2ms (struct timeval *tv); |
120 |
#endif |
121 |
E char *time_ago (time_t event); |
122 |
E char *timediff (time_t seconds); |
123 |
|
124 |
#ifdef timersub |
125 |
#undef timersub |
126 |
inline void |
127 |
timersub (timeval *tvp, timeval *uvp, timeval *vvp) |
128 |
{ |
129 |
vvp->tv_sec = tvp->tv_sec - uvp->tv_sec; |
130 |
vvp->tv_usec = tvp->tv_usec - uvp->tv_usec; |
131 |
if (vvp->tv_usec < 0) |
132 |
{ |
133 |
vvp->tv_sec--; |
134 |
vvp->tv_usec += 1000000; |
135 |
} |
136 |
} |
137 |
#endif |
138 |
|
139 |
/* tokenize.c */ |
140 |
E int sjtoken (char *message, char delimiter, char **parv, int limit = 256); |
141 |
E int tokenize (char *message, char **parv); |
142 |
|
143 |
inline int |
144 |
sjtoken (char const * const message, char delimiter, char **parv, int limit = 256) |
145 |
{ |
146 |
char *tmp = sstrdup (message); |
147 |
int parc = sjtoken (tmp, delimiter, parv, limit); |
148 |
gc.insert (tmp); |
149 |
|
150 |
return parc; |
151 |
} |
152 |
|
153 |
/* ubase64.c */ |
154 |
E char const * const uinttobase64 (char *buf, uint64_t v, int64_t count); |
155 |
E unsigned int base64touint (char *buf); |
156 |
|
157 |
#endif |