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.4 2007-08-28 17:08:07 pippijn Exp $ |
9 |
*/ |
10 |
|
11 |
#ifndef _TOOLS_H |
12 |
#define _TOOLS_H |
13 |
|
14 |
/* |
15 |
* Warns operators and console users that an error occurred |
16 |
*/ |
17 |
#ifdef __GNUC__ |
18 |
#define warn(x) \ |
19 |
do \ |
20 |
{ \ |
21 |
slog (LG_INFO, "%s(%d) [%s]: critical: " x, \ |
22 |
__FILE__, __LINE__, __PRETTY_FUNCTION__); \ |
23 |
wallops ("%s(%d) [%s]: critical: " x, \ |
24 |
__FILE__, __LINE__, __PRETTY_FUNCTION__);\ |
25 |
} 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 |
|
37 |
/* |
38 |
* Performs a soft assertion. If the assertion fails, we wallops() and log. |
39 |
*/ |
40 |
#ifdef __GNUC__ |
41 |
#define soft_assert(x) \ |
42 |
if (!(x)) { \ |
43 |
slog(LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
44 |
__FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
45 |
wallops("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
46 |
__FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
47 |
} |
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 |
|
58 |
/* |
59 |
* Same as soft_assert, but returns if an assertion fails. |
60 |
*/ |
61 |
#ifdef __GNUC__ |
62 |
#define return_if_fail(x) \ |
63 |
if (!(x)) { \ |
64 |
slog(LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
65 |
__FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
66 |
wallops("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
67 |
__FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
68 |
return; \ |
69 |
} |
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 |
|
81 |
/* |
82 |
* Same as return_if_fail, but returns a given value if an assertion fails. |
83 |
*/ |
84 |
#ifdef __GNUC__ |
85 |
#define return_val_if_fail(x, y) \ |
86 |
if (!(x)) { \ |
87 |
slog(LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
88 |
__FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
89 |
wallops("%s(%d) [%s]: critical: Assertion '%s' failed.", \ |
90 |
__FILE__, __LINE__, __PRETTY_FUNCTION__, #x); \ |
91 |
return (y); \ |
92 |
} |
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 |
|
104 |
/* email stuff */ |
105 |
/* the following struct is not used yet */ |
106 |
struct email_t |
107 |
{ |
108 |
char *sender; |
109 |
char *reciever; |
110 |
char *subject; |
111 |
char *body; |
112 |
char **headers; |
113 |
|
114 |
void *miscellaneous; /* module defined data */ |
115 |
void (*callback_sent) (email_t *); /* callback on email send */ |
116 |
}; |
117 |
|
118 |
E int sendemail (user_t *from, int type, myuser_t *mu, char const * const param); |
119 |
|
120 |
/* email types (meaning of param argument) */ |
121 |
#define EMAIL_REGISTER 1 /* register an account/nick (verification code) */ |
122 |
#define EMAIL_SENDPASS 2 /* send a password to a user (password) */ |
123 |
#define EMAIL_SETEMAIL 3 /* change email address (verification code) */ |
124 |
#define EMAIL_MEMO 4 /* emailed memos (memo text) */ |
125 |
#define EMAIL_SETPASS 5 /* send a password change key (verification code) */ |
126 |
|
127 |
/* logstreams API --nenolod */ |
128 |
struct logfile_t; |
129 |
|
130 |
typedef void (*log_write_func_t) (logfile_t *lf, char const * const buf); |
131 |
|
132 |
/* logger.c */ |
133 |
struct logfile_t : zero_initialised |
134 |
{ |
135 |
object_t parent; |
136 |
node_t node; |
137 |
|
138 |
void *log_file; /* opaque: can either be mychan_t or FILE --nenolod */ |
139 |
char *log_path; |
140 |
unsigned int log_mask; |
141 |
|
142 |
log_write_func_t write_func; |
143 |
}; |
144 |
|
145 |
E char *log_path; /* contains path to default log. */ |
146 |
E int log_force; |
147 |
|
148 |
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); |
150 |
E void logfile_register (logfile_t *lf); |
151 |
E void logfile_unregister (logfile_t *lf); |
152 |
|
153 |
/* general */ |
154 |
#define LG_NONE 0x00000001 /* don't log */ |
155 |
#define LG_INFO 0x00000002 /* log general info */ |
156 |
#define LG_ERROR 0x00000004 /* log real important stuff */ |
157 |
#define LG_IOERROR 0x00000008 /* log I/O errors. */ |
158 |
#define LG_DEBUG 0x00000010 /* log debugging stuff */ |
159 |
/* commands */ |
160 |
#define LG_CMD_ADMIN 0x00000100 /* oper-only commands */ |
161 |
#define LG_CMD_REGISTER 0x00000200 /* register/drop */ |
162 |
#define LG_CMD_SET 0x00000400 /* change properties of static data */ |
163 |
#define LG_CMD_DO 0x00000800 /* change properties of dynamic data */ |
164 |
#define LG_CMD_LOGIN 0x00001000 /* login/logout */ |
165 |
#define LG_CMD_GET 0x00002000 /* query information */ |
166 |
/* other */ |
167 |
#define LG_NETWORK 0x00010000 /* netsplit/netjoin */ |
168 |
#define LG_WALLOPS 0x00020000 /* NOTYET wallops from opers/other servers */ |
169 |
#define LG_RAWDATA 0x00040000 /* all data sent/received */ |
170 |
#define LG_REGISTER 0x00080000 /* all registration related messages */ |
171 |
#define LG_WARN1 0x00100000 /* NOTYET messages formerly walloped */ |
172 |
#define LG_WARN2 0x00100000 /* NOTYET messages formerly snooped */ |
173 |
|
174 |
#define LG_CMD_ALL 0x0000FF00 |
175 |
#define LG_ALL 0x7FFFFFFF /* XXX cannot use bit 31 as it would then be unequal to TOKEN_UNMATCHED */ |
176 |
|
177 |
/* aliases for use with logcommand() */ |
178 |
#define CMDLOG_ADMIN LG_CMD_ADMIN |
179 |
#define CMDLOG_REGISTER (LG_CMD_REGISTER | LG_REGISTER) |
180 |
#define CMDLOG_SET LG_CMD_SET |
181 |
#define CMDLOG_DO LG_CMD_DO |
182 |
#define CMDLOG_LOGIN LG_CMD_LOGIN |
183 |
#define CMDLOG_GET LG_CMD_GET |
184 |
|
185 |
E void log_open (void); |
186 |
E void log_shutdown (void); |
187 |
E bool log_debug_enabled (void); |
188 |
E void log_master_set_mask (unsigned int mask); |
189 |
E void slog (unsigned int level, char const * const fmt, ...); |
190 |
E void logcommand (sourceinfo_t *si, int level, char const * const fmt, ...); |
191 |
E void logcommand_user (service_t *svs, user_t *source, int level, char const * const fmt, ...); |
192 |
E void logcommand_external (service_t *svs, char const * const type, connection_t *source, char const * const sourcedesc, myuser_t *login, int level, char const * const fmt, ...); |
193 |
|
194 |
/* function.c */ |
195 |
/* misc string stuff */ |
196 |
E char *gen_pw (int sz); |
197 |
E void tb2sp (char *line); |
198 |
E char *replace (char *s, int size, char const * const oldstr, char const * const newstr); |
199 |
E char *itoa (int num); |
200 |
E int validemail (char *email); |
201 |
E bool validhostmask (char *host); |
202 |
E char const *sbytes (float x); |
203 |
E float bytes (float x); |
204 |
|
205 |
E unsigned long makekey (void); |
206 |
|
207 |
/* time stuff */ |
208 |
#if HAVE_GETTIMEOFDAY |
209 |
E void s_time (struct timeval *sttime); |
210 |
E void e_time (struct timeval sttime, struct timeval *ttime); |
211 |
E int tv2ms (struct timeval *tv); |
212 |
#endif |
213 |
E char *time_ago (time_t event); |
214 |
E char *timediff (time_t seconds); |
215 |
|
216 |
#ifndef timersub |
217 |
#define timersub(tvp, uvp, vvp) \ |
218 |
do { \ |
219 |
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ |
220 |
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ |
221 |
if ((vvp)->tv_usec < 0) { \ |
222 |
(vvp)->tv_sec--; \ |
223 |
(vvp)->tv_usec += 1000000; \ |
224 |
} \ |
225 |
} while (0) |
226 |
#endif |
227 |
|
228 |
/* tokenize.c */ |
229 |
E int sjtoken (char *message, char delimiter, char **parv, int limit = 256); |
230 |
E int tokenize (char *message, char **parv); |
231 |
|
232 |
inline int |
233 |
sjtoken (char const * const message, char delimiter, char **parv, int limit = 256) |
234 |
{ |
235 |
char *tmp = sstrdup (message); |
236 |
int parc = sjtoken (tmp, delimiter, parv, limit); |
237 |
free (tmp); |
238 |
|
239 |
return parc; |
240 |
} |
241 |
|
242 |
/* ubase64.c */ |
243 |
E char const * const uinttobase64 (char *buf, uint64_t v, int64_t count); |
244 |
E unsigned int base64touint (char *buf); |
245 |
|
246 |
#endif |