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