ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/include/tools.h
Revision: 1.6
Committed: Wed Aug 29 21:01:18 2007 UTC (16 years, 9 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.5: +95 -126 lines
Log Message:
- reduced ifdefs by moving __GNUC__ and friends to svsconfig.h
- #define to enum { } in tools.h
- corrected log levels a bit
- made timersub an inline function instead of a macro
- added a simple garbage collection mechanism for postponed freeing of lost
  memory chunks
- enhanced type_traits
- merged inspircd1.2 support with upstream
- reformatting
- renamed TTP to a more "standard" PRItime and STP to PRIsize

File Contents

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