… | |
… | |
3 | * Copyright © 2005-2006 Atheme Development Group |
3 | * Copyright © 2005-2006 Atheme Development Group |
4 | * Rights to this code are as documented in doc/pod/license.pod. |
4 | * Rights to this code are as documented in doc/pod/license.pod. |
5 | * |
5 | * |
6 | * Misc tools |
6 | * Misc tools |
7 | * |
7 | * |
8 | * $Id: tools.h,v 1.5 2007/08/29 08:04:32 pippijn Exp $ |
8 | * $Id: tools.h,v 1.6 2007/08/29 21:01:18 pippijn Exp $ |
9 | */ |
9 | */ |
10 | |
10 | |
11 | #ifndef _TOOLS_H |
11 | #ifndef _TOOLS_H |
12 | #define _TOOLS_H |
12 | #define _TOOLS_H |
13 | |
13 | |
14 | /* |
14 | /** |
15 | * Warns operators and console users that an error occurred |
15 | * Warns operators and console users that an error occurred |
16 | */ |
16 | */ |
17 | #ifdef __GNUC__ |
|
|
18 | #define warn(x) \ |
17 | #define warn(msg) \ |
19 | do \ |
18 | do \ |
20 | { \ |
19 | { \ |
21 | slog (LG_INFO, "%s(%d) [%s]: critical: " x, \ |
20 | slog (LG_INFO, "%s(%d) [%s]: critical: " msg, \ |
22 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ |
21 | __FILE__, __LINE__, CURFUNC); \ |
23 | wallops ("%s(%d) [%s]: critical: " x, \ |
22 | wallops ("%s(%d) [%s]: critical: " msg, \ |
24 | __FILE__, __LINE__, __PRETTY_FUNCTION__);\ |
23 | __FILE__, __LINE__, CURFUNC);\ |
25 | } while (0) |
24 | } while (0) |
26 | #else |
|
|
27 | #define warn(x) \ |
|
|
28 | do \ |
|
|
29 | { \ |
|
|
30 | slog (LG_INFO, "%s(%d): critical: " x, \ |
|
|
31 | __FILE__, __LINE__); \ |
|
|
32 | wallops ("%s(%d): critical: " x, \ |
|
|
33 | __FILE__, __LINE__); \ |
|
|
34 | } while (0) |
|
|
35 | #endif |
|
|
36 | |
25 | |
37 | /* |
26 | /** |
38 | * Performs a soft assertion. If the assertion fails, we wallops() and log. |
27 | * Performs a soft assertion. If the assertion fails, we wallops () and log. |
39 | */ |
28 | */ |
40 | #ifdef __GNUC__ |
29 | #define soft_assert(cond) \ |
41 | #define soft_assert(x) \ |
30 | if (!(cond)) \ |
42 | if (!(x)) { \ |
31 | { \ |
43 | slog(LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
32 | slog (LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
44 | __FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
33 | __FILE__, __LINE__, CURFUNC, #cond); \ |
45 | wallops("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
34 | wallops ("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
46 | __FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
35 | __FILE__, __LINE__, CURFUNC, #cond); \ |
47 | } |
36 | } |
48 | #else |
|
|
49 | #define soft_assert(x) \ |
|
|
50 | if (!(x)) { \ |
|
|
51 | slog(LG_INFO, "%s(%d): critical: Assertion '%s' failed.", \ |
|
|
52 | __FILE__, __LINE__, #x); \ |
|
|
53 | wallops("%s(%d): critical: Assertion '%s' failed.", \ |
|
|
54 | __FILE__, __LINE__, #x); \ |
|
|
55 | } |
|
|
56 | #endif |
|
|
57 | |
37 | |
58 | /* |
38 | /** |
59 | * Same as soft_assert, but returns if an assertion fails. |
39 | * Same as soft_assert, but returns if an assertion fails. |
60 | */ |
40 | */ |
61 | #ifdef __GNUC__ |
41 | #define return_if_fail(cond) \ |
62 | #define return_if_fail(x) \ |
42 | if (!(cond)) \ |
63 | if (!(x)) { \ |
43 | { \ |
64 | slog(LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
44 | slog (LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
65 | __FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
45 | __FILE__, __LINE__, CURFUNC, #cond); \ |
66 | wallops("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
46 | wallops ("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
67 | __FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
47 | __FILE__, __LINE__, CURFUNC, #cond); \ |
68 | return; \ |
48 | return; \ |
69 | } |
49 | } |
70 | #else |
|
|
71 | #define return_if_fail(x) \ |
|
|
72 | if (!(x)) { \ |
|
|
73 | slog(LG_INFO, "%s(%d): critical: Assertion '%s' failed.", \ |
|
|
74 | __FILE__, __LINE__, #x); \ |
|
|
75 | wallops("%s(%d): critical: Assertion '%s' failed.", \ |
|
|
76 | __FILE__, __LINE__, #x); \ |
|
|
77 | return; \ |
|
|
78 | } |
|
|
79 | #endif |
|
|
80 | |
50 | |
81 | /* |
51 | /** |
82 | * Same as return_if_fail, but returns a given value if an assertion fails. |
52 | * Same as return_if_fail, but returns a given value if an assertion fails. |
83 | */ |
53 | */ |
84 | #ifdef __GNUC__ |
54 | #define return_val_if_fail(cond, val) \ |
85 | #define return_val_if_fail(x, y) \ |
55 | if (!(cond)) \ |
86 | if (!(x)) { \ |
56 | { \ |
87 | slog(LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
57 | slog (LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
88 | __FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
58 | __FILE__, __LINE__, CURFUNC, #cond); \ |
89 | wallops("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
59 | wallops ("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
90 | __FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
60 | __FILE__, __LINE__, CURFUNC, #cond); \ |
91 | return (y); \ |
61 | return (val); \ |
92 | } |
62 | } |
93 | #else |
|
|
94 | #define return_val_if_fail(x, y) \ |
|
|
95 | if (!(x)) { \ |
|
|
96 | slog(LG_INFO, "%s(%d): critical: Assertion '%s' failed.", \ |
|
|
97 | __FILE__, __LINE__, #x); \ |
|
|
98 | wallops("%s(%d): critical: Assertion '%s' failed.", \ |
|
|
99 | __FILE__, __LINE__, #x); \ |
|
|
100 | return (y); \ |
|
|
101 | } |
|
|
102 | #endif |
|
|
103 | |
63 | |
104 | /* email stuff */ |
64 | /* email stuff */ |
105 | /* the following struct is not used yet */ |
65 | /* the following struct is not used yet */ |
106 | struct email_t |
66 | struct email_t |
107 | { |
67 | { |
… | |
… | |
116 | }; |
76 | }; |
117 | |
77 | |
118 | E int sendemail (user_t *from, int type, myuser_t *mu, char const * const param); |
78 | E int sendemail (user_t *from, int type, myuser_t *mu, char const * const param); |
119 | |
79 | |
120 | /* email types (meaning of param argument) */ |
80 | /* email types (meaning of param argument) */ |
|
|
81 | enum email_type |
|
|
82 | { |
121 | #define EMAIL_REGISTER 1 /* register an account/nick (verification code) */ |
83 | EMAIL_REGISTER = 1, /* register an account/nick (verification code) */ |
122 | #define EMAIL_SENDPASS 2 /* send a password to a user (password) */ |
84 | EMAIL_SENDPASS = 2, /* send a password to a user (password) */ |
123 | #define EMAIL_SETEMAIL 3 /* change email address (verification code) */ |
85 | EMAIL_SETEMAIL = 3, /* change email address (verification code) */ |
124 | #define EMAIL_MEMO 4 /* emailed memos (memo text) */ |
86 | EMAIL_MEMO = 4, /* emailed memos (memo text) */ |
125 | #define EMAIL_SETPASS 5 /* send a password change key (verification code) */ |
87 | EMAIL_SETPASS = 5 /* send a password change key (verification code) */ |
|
|
88 | }; |
126 | |
89 | |
127 | /* logstreams API --nenolod */ |
90 | /* logstreams API --nenolod */ |
128 | struct logfile_t; |
91 | struct logfile_t; |
129 | |
92 | |
130 | typedef void (*log_write_func_t) (logfile_t *lf, char const * const buf); |
93 | typedef void (*log_write_func_t) (logfile_t *lf, char const * const buf); |
… | |
… | |
133 | struct logfile_t : zero_initialised |
96 | struct logfile_t : zero_initialised |
134 | { |
97 | { |
135 | object_t parent; |
98 | object_t parent; |
136 | node_t node; |
99 | node_t node; |
137 | |
100 | |
138 | void *log_file; /* opaque: can either be mychan_t or FILE --nenolod */ |
101 | void *log_file; /* opaque: can either be mychan_t or FILE --nenolod */ |
139 | char *log_path; |
102 | char *log_path; |
140 | unsigned int log_mask; |
103 | unsigned int log_mask; |
141 | |
104 | |
142 | log_write_func_t write_func; |
105 | log_write_func_t write_func; |
143 | }; |
106 | }; |
144 | |
107 | |
145 | E char *log_path; /* contains path to default log. */ |
108 | E char *log_path; /* contains path to default log. */ |
146 | E int log_force; |
109 | E int log_force; |
147 | |
110 | |
148 | E logfile_t *logfile_new (char const * const log_path_, unsigned int log_mask); |
111 | E logfile_t *logfile_new (char const * const log_path_, unsigned int log_mask); |
149 | E void logfile_write (logfile_t *lf, char const * const buf); |
112 | E void logfile_write (logfile_t *lf, char const * const buf); |
150 | E void logfile_register (logfile_t *lf); |
113 | E void logfile_register (logfile_t *lf); |
151 | E void logfile_unregister (logfile_t *lf); |
114 | E void logfile_unregister (logfile_t *lf); |
152 | |
115 | |
|
|
116 | enum loglevel |
|
|
117 | { |
153 | /* general */ |
118 | /* general */ |
154 | #define LG_NONE 0x00000001 /* don't log */ |
119 | LG_NONE = 1 << 0, /* don't log */ |
155 | #define LG_INFO 0x00000002 /* log general info */ |
120 | LG_INFO = 1 << 1, /* log general info */ |
156 | #define LG_ERROR 0x00000004 /* log real important stuff */ |
121 | LG_ERROR = 1 << 2, /* log real important stuff */ |
157 | #define LG_IOERROR 0x00000008 /* log I/O errors. */ |
122 | LG_IOERROR = 1 << 3, /* log I/O errors. */ |
158 | #define LG_DEBUG 0x00000010 /* log debugging stuff */ |
123 | LG_DEBUG = 1 << 4, /* log debugging stuff */ |
159 | /* commands */ |
124 | /* commands */ |
160 | #define LG_CMD_ADMIN 0x00000100 /* oper-only commands */ |
125 | LG_CMD_ADMIN = 1 << 5, /* oper-only commands */ |
161 | #define LG_CMD_REGISTER 0x00000200 /* register/drop */ |
126 | LG_CMD_REGISTER = 1 << 6, /* register/drop */ |
162 | #define LG_CMD_SET 0x00000400 /* change properties of static data */ |
127 | LG_CMD_SET = 1 << 7, /* change properties of static data */ |
163 | #define LG_CMD_DO 0x00000800 /* change properties of dynamic data */ |
128 | LG_CMD_DO = 1 << 8, /* change properties of dynamic data */ |
164 | #define LG_CMD_LOGIN 0x00001000 /* login/logout */ |
129 | LG_CMD_LOGIN = 1 << 9, /* login/logout */ |
165 | #define LG_CMD_GET 0x00002000 /* query information */ |
130 | LG_CMD_GET = 1 << 10, /* query information */ |
166 | /* other */ |
131 | /* other */ |
167 | #define LG_NETWORK 0x00010000 /* netsplit/netjoin */ |
132 | LG_NETWORK = 1 << 11, /* netsplit/netjoin */ |
168 | #define LG_WALLOPS 0x00020000 /* NOTYET wallops from opers/other servers */ |
133 | LG_WALLOPS = 1 << 12, /* NOTYET wallops from opers/other servers */ |
169 | #define LG_RAWDATA 0x00040000 /* all data sent/received */ |
134 | LG_RAWDATA = 1 << 13, /* all data sent/received */ |
170 | #define LG_REGISTER 0x00080000 /* all registration related messages */ |
135 | LG_REGISTER = 1 << 14, /* all registration related messages */ |
171 | #define LG_WARN1 0x00100000 /* NOTYET messages formerly walloped */ |
136 | LG_WARN1 = 1 << 15, /* NOTYET messages formerly walloped */ |
172 | #define LG_WARN2 0x00100000 /* NOTYET messages formerly snooped */ |
137 | LG_WARN2 = 1 << 16, /* NOTYET messages formerly snooped */ |
173 | |
138 | |
174 | #define LG_CMD_ALL 0x0000FF00 |
139 | LG_CMD_ALL = LG_CMD_ADMIN | LG_CMD_REGISTER | LG_CMD_SET | LG_CMD_DO | LG_CMD_LOGIN | LG_CMD_GET, |
175 | #define LG_ALL 0x7FFFFFFF /* XXX cannot use bit 31 as it would then be unequal to TOKEN_UNMATCHED */ |
140 | LG_ALL = 1 << 31 - 1,/* XXX cannot use bit 31 as it would then be unequal to TOKEN_UNMATCHED */ |
176 | |
141 | |
177 | /* aliases for use with logcommand() */ |
142 | /* aliases for use with logcommand() */ |
178 | #define CMDLOG_ADMIN LG_CMD_ADMIN |
143 | CMDLOG_ADMIN = LG_CMD_ADMIN, |
179 | #define CMDLOG_REGISTER (LG_CMD_REGISTER | LG_REGISTER) |
144 | CMDLOG_REGISTER = LG_CMD_REGISTER | LG_REGISTER, |
180 | #define CMDLOG_SET LG_CMD_SET |
145 | CMDLOG_SET = LG_CMD_SET, |
181 | #define CMDLOG_DO LG_CMD_DO |
146 | CMDLOG_DO = LG_CMD_DO, |
182 | #define CMDLOG_LOGIN LG_CMD_LOGIN |
147 | CMDLOG_LOGIN = LG_CMD_LOGIN, |
183 | #define CMDLOG_GET LG_CMD_GET |
148 | CMDLOG_GET = LG_CMD_GET |
|
|
149 | }; |
184 | |
150 | |
185 | E void log_open (void); |
151 | E void log_open (void); |
186 | E void log_shutdown (void); |
152 | E void log_shutdown (void); |
187 | E bool log_debug_enabled (void); |
153 | E bool log_debug_enabled (void); |
188 | E void log_master_set_mask (unsigned int mask); |
154 | E void log_master_set_mask (unsigned int mask); |
… | |
… | |
211 | E int tv2ms (struct timeval *tv); |
177 | E int tv2ms (struct timeval *tv); |
212 | #endif |
178 | #endif |
213 | E char *time_ago (time_t event); |
179 | E char *time_ago (time_t event); |
214 | E char *timediff (time_t seconds); |
180 | E char *timediff (time_t seconds); |
215 | |
181 | |
216 | #ifndef timersub |
182 | #ifdef timersub |
217 | #define timersub(tvp, uvp, vvp) \ |
183 | #undef timersub |
218 | do { \ |
184 | inline void |
219 | (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ |
185 | timersub (timeval *tvp, timeval *uvp, timeval *vvp) |
|
|
186 | { |
|
|
187 | vvp->tv_sec = tvp->tv_sec - uvp->tv_sec; |
220 | (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ |
188 | vvp->tv_usec = tvp->tv_usec - uvp->tv_usec; |
221 | if ((vvp)->tv_usec < 0) { \ |
189 | if (vvp->tv_usec < 0) |
222 | (vvp)->tv_sec--; \ |
190 | { |
223 | (vvp)->tv_usec += 1000000; \ |
191 | vvp->tv_sec--; |
224 | } \ |
192 | vvp->tv_usec += 1000000; |
225 | } while (0) |
193 | } |
|
|
194 | } |
226 | #endif |
195 | #endif |
227 | |
196 | |
228 | /* tokenize.c */ |
197 | /* tokenize.c */ |
229 | E int sjtoken (char *message, char delimiter, char **parv, int limit = 256); |
198 | E int sjtoken (char *message, char delimiter, char **parv, int limit = 256); |
230 | E int tokenize (char *message, char **parv); |
199 | E int tokenize (char *message, char **parv); |
… | |
… | |
232 | inline int |
201 | inline int |
233 | sjtoken (char const * const message, char delimiter, char **parv, int limit = 256) |
202 | sjtoken (char const * const message, char delimiter, char **parv, int limit = 256) |
234 | { |
203 | { |
235 | char *tmp = sstrdup (message); |
204 | char *tmp = sstrdup (message); |
236 | int parc = sjtoken (tmp, delimiter, parv, limit); |
205 | int parc = sjtoken (tmp, delimiter, parv, limit); |
237 | free (tmp); |
206 | gc.insert (tmp); |
238 | |
207 | |
239 | return parc; |
208 | return parc; |
240 | } |
209 | } |
241 | |
210 | |
242 | /* ubase64.c */ |
211 | /* ubase64.c */ |