ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/include/tools.h
Revision: 1.1
Committed: Thu Jul 19 08:24:51 2007 UTC (16 years, 10 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Log Message:
initial import. the most important changes since Atheme are:
- fixed many memory leaks
- fixed many bugs
- converted to C++ and use more STL containers
- added a (not very enhanced yet) perl module
- greatly improved XML-RPC speed
- added a JSON-RPC module with code from json-cpp
- added a valgrind memcheck module to operserv
- added a more object oriented base64 implementation
- added a specialised unit test framework
- improved stability
- use gettimeofday() if available
- reworked adding/removing commands
- MemoServ IGNORE DEL can now remove indices

File Contents

# User Rev Content
1 pippijn 1.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/LICENSE.
5     *
6     * Misc tools
7     *
8     * $Id: tools.h 8363 2007-06-02 22:17:28Z jilles $
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 soft_assert, 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, const char *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     /* arc4random.c */
128     #ifndef HAVE_ARC4RANDOM
129     E void arc4random_stir (void);
130     E void arc4random_addrandom (unsigned char *dat, int datlen);
131     E unsigned int arc4random (void);
132     #endif /* !HAVE_ARC4RANDOM */
133    
134     struct logfile_t;
135    
136     /* logstreams API --nenolod */
137     typedef void (*log_write_func_t) (logfile_t *lf, const char *buf);
138    
139     /* logger.c */
140     struct logfile_t
141     {
142     object_t parent;
143     node_t node;
144    
145     void *log_file; /* opaque: can either be mychan_t or FILE --nenolod */
146     char *log_path;
147     unsigned int log_mask;
148    
149     log_write_func_t write_func;
150     };
151    
152     E char *log_path; /* contains path to default log. */
153     E int log_force;
154    
155     E logfile_t *logfile_new (const char *log_path_, unsigned int log_mask);
156     E void logfile_write (logfile_t *lf, const char *buf);
157     E void logfile_register (logfile_t *lf);
158     E void logfile_unregister (logfile_t *lf);
159    
160     /* general */
161     #define LG_NONE 0x00000001 /* don't log */
162     #define LG_INFO 0x00000002 /* log general info */
163     #define LG_ERROR 0x00000004 /* log real important stuff */
164     #define LG_IOERROR 0x00000008 /* log I/O errors. */
165     #define LG_DEBUG 0x00000010 /* log debugging stuff */
166     /* commands */
167     #define LG_CMD_ADMIN 0x00000100 /* oper-only commands */
168     #define LG_CMD_REGISTER 0x00000200 /* register/drop */
169     #define LG_CMD_SET 0x00000400 /* change properties of static data */
170     #define LG_CMD_DO 0x00000800 /* change properties of dynamic data */
171     #define LG_CMD_LOGIN 0x00001000 /* login/logout */
172     #define LG_CMD_GET 0x00002000 /* query information */
173     /* other */
174     #define LG_NETWORK 0x00010000 /* netsplit/netjoin */
175     #define LG_WALLOPS 0x00020000 /* NOTYET wallops from opers/other servers */
176     #define LG_RAWDATA 0x00040000 /* all data sent/received */
177     #define LG_REGISTER 0x00080000 /* all registration related messages */
178     #define LG_WARN1 0x00100000 /* NOTYET messages formerly walloped */
179     #define LG_WARN2 0x00100000 /* NOTYET messages formerly snooped */
180    
181     #define LG_CMD_ALL 0x0000FF00
182     #define LG_ALL 0x7FFFFFFF /* XXX cannot use bit 31 as it would then be unequal to TOKEN_UNMATCHED */
183    
184     /* aliases for use with logcommand() */
185     #define CMDLOG_ADMIN LG_CMD_ADMIN
186     #define CMDLOG_REGISTER (LG_CMD_REGISTER | LG_REGISTER)
187     #define CMDLOG_SET LG_CMD_SET
188     #define CMDLOG_DO LG_CMD_DO
189     #define CMDLOG_LOGIN LG_CMD_LOGIN
190     #define CMDLOG_GET LG_CMD_GET
191    
192     E void log_open (void);
193     E void log_shutdown (void);
194     E bool log_debug_enabled (void);
195     E void log_master_set_mask (unsigned int mask);
196     E void slog (unsigned int level, const char *fmt, ...);
197     E void logcommand (sourceinfo_t *si, int level, const char *fmt, ...);
198     E void logcommand_user (service_t *svs, user_t *source, int level, const char *fmt, ...);
199     E void logcommand_external (service_t *svs, const char *type, connection_t *source, const char *sourcedesc, myuser_t *login, int level, const char *fmt, ...);
200    
201     /* function.c */
202     /* misc string stuff */
203     E char *gen_pw (int sz);
204     E void tb2sp (char *line);
205     E char *replace (char *s, int size, const char *oldstr, const char *newstr);
206     E char *itoa (int num);
207     E int validemail (char *email);
208     E bool validhostmask (char *host);
209     E char *sbytes (float x);
210     E float bytes (float x);
211    
212     E unsigned long makekey (void);
213    
214     /* the hash function */
215     E unsigned int shash (const unsigned char *text);
216    
217     /* time stuff */
218     #if HAVE_GETTIMEOFDAY
219     E void s_time (struct timeval *sttime);
220     E void e_time (struct timeval sttime, struct timeval *ttime);
221     E int tv2ms (struct timeval *tv);
222     #endif
223     E char *time_ago (time_t event);
224     E char *timediff (time_t seconds);
225    
226     #ifndef timersub
227     #define timersub(tvp, uvp, vvp) \
228     do { \
229     (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
230     (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
231     if ((vvp)->tv_usec < 0) { \
232     (vvp)->tv_sec--; \
233     (vvp)->tv_usec += 1000000; \
234     } \
235     } while (0)
236     #endif
237    
238     /* tokenize.c */
239     E int sjtoken (char *message, char delimiter, char **parv);
240     E int tokenize (char *message, char **parv);
241    
242     /* ubase64.c */
243     E const char *uinttobase64 (char *buf, uint64_t v, int64_t count);
244     E unsigned int base64touint (char *buf);
245    
246     #endif