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