/** * tools.h: Misc tools * * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in COPYING. * * * Portions of this file were derived from sources bearing the following license: * Copyright © 2003-2004 E. Will et al. * Copyright © 2005-2006 Atheme Development Group * Rights to this code are as documented in doc/pod/license.pod. * * $Id: tools.h,v 1.9 2007/09/16 18:54:42 pippijn Exp $ */ #ifndef _TOOLS_H #define _TOOLS_H #include /** * Warns operators and console users that an error occurred */ #define warn(msg) \ do \ { \ slog (LG_INFO, "%s(%d) [%s]: critical: " msg, \ __FILE__, __LINE__, CURFUNC); \ wallops ("%s(%d) [%s]: critical: " msg, \ __FILE__, __LINE__, CURFUNC);\ } while (0) /** * Performs a soft assertion. If the assertion fails, we wallops () and log. */ #define soft_assert(cond) \ if (!(cond)) \ { \ slog (LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ __FILE__, __LINE__, CURFUNC, #cond); \ wallops ("%s(%d) [%s]: critical: Assertion '%s' failed.", \ __FILE__, __LINE__, CURFUNC, #cond); \ } /** * Same as soft_assert, but returns if an assertion fails. */ #define return_if_fail(cond) \ if (!(cond)) \ { \ slog (LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ __FILE__, __LINE__, CURFUNC, #cond); \ wallops ("%s(%d) [%s]: critical: Assertion '%s' failed.", \ __FILE__, __LINE__, CURFUNC, #cond); \ return; \ } /** * Same as return_if_fail, but returns a given value if an assertion fails. */ #define return_val_if_fail(cond, val) \ if (!(cond)) \ { \ slog (LG_INFO, "%s(%d) [%s]: critical: Assertion '%s' failed.", \ __FILE__, __LINE__, CURFUNC, #cond); \ wallops ("%s(%d) [%s]: critical: Assertion '%s' failed.", \ __FILE__, __LINE__, CURFUNC, #cond); \ return (val); \ } /* email stuff */ /* the following struct is not used yet */ struct email_t { char *sender; char *reciever; char *subject; char *body; char **headers; void *miscellaneous; /* module defined data */ void (*callback_sent) (email_t *); /* callback on email send */ }; E int sendemail (user_t *from, int type, myuser_t *mu, char const * const param); /* email types (meaning of param argument) */ enum email_type { EMAIL_REGISTER = 1, /* register an account/nick (verification code) */ EMAIL_SENDPASS = 2, /* send a password to a user (password) */ EMAIL_SETEMAIL = 3, /* change email address (verification code) */ EMAIL_MEMO = 4, /* emailed memos (memo text) */ EMAIL_SETPASS = 5 /* send a password change key (verification code) */ }; /* function.c */ /* misc string stuff */ E char *gen_pw (int sz); E void tb2sp (char *line); E char *replace (char *s, int size, char const * const oldstr, char const * const newstr); /* reverse of atoi() */ template static inline char * itoa (T num) { static char ret[32]; itoa (num, ret); return ret; } E int validemail (char *email); E bool validhostmask (char *host); E char const *sbytes (float x); E float bytes (float x); E unsigned long makekey (void); /* time stuff */ #if HAVE_GETTIMEOFDAY E void s_time (struct timeval *sttime); E void e_time (struct timeval sttime, struct timeval *ttime); E int tv2ms (struct timeval *tv); #endif E char *time_ago (time_t event); E char *timediff (time_t seconds); #ifdef timersub #undef timersub inline void timersub (timeval *tvp, timeval *uvp, timeval *vvp) { vvp->tv_sec = tvp->tv_sec - uvp->tv_sec; vvp->tv_usec = tvp->tv_usec - uvp->tv_usec; if (vvp->tv_usec < 0) { vvp->tv_sec--; vvp->tv_usec += 1000000; } } #endif /* tokenize.c */ E int sjtoken (char *message, char delimiter, char **parv, int limit = 256); E int tokenize (char *message, char **parv); inline int sjtoken (char const * const message, char delimiter, char **parv, int limit = 256) { char *tmp = sstrdup (message); int parc = sjtoken (tmp, delimiter, parv, limit); gc.insert (tmp); return parc; } /* ubase64.c */ E char const * const uinttobase64 (char *buf, uint64_t v, int64_t count); E unsigned int base64touint (char *buf); #endif