/* * 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. * * Misc tools * * $Id: tools.h,v 1.6 2007/08/29 21:01:18 pippijn Exp $ */ #ifndef _TOOLS_H #define _TOOLS_H /** * 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) */ }; /* logstreams API --nenolod */ struct logfile_t; typedef void (*log_write_func_t) (logfile_t *lf, char const * const buf); /* logger.c */ struct logfile_t : zero_initialised { object_t parent; node_t node; void *log_file; /* opaque: can either be mychan_t or FILE --nenolod */ char *log_path; unsigned int log_mask; log_write_func_t write_func; }; E char *log_path; /* contains path to default log. */ E int log_force; E logfile_t *logfile_new (char const * const log_path_, unsigned int log_mask); E void logfile_write (logfile_t *lf, char const * const buf); E void logfile_register (logfile_t *lf); E void logfile_unregister (logfile_t *lf); enum loglevel { /* general */ LG_NONE = 1 << 0, /* don't log */ LG_INFO = 1 << 1, /* log general info */ LG_ERROR = 1 << 2, /* log real important stuff */ LG_IOERROR = 1 << 3, /* log I/O errors. */ LG_DEBUG = 1 << 4, /* log debugging stuff */ /* commands */ LG_CMD_ADMIN = 1 << 5, /* oper-only commands */ LG_CMD_REGISTER = 1 << 6, /* register/drop */ LG_CMD_SET = 1 << 7, /* change properties of static data */ LG_CMD_DO = 1 << 8, /* change properties of dynamic data */ LG_CMD_LOGIN = 1 << 9, /* login/logout */ LG_CMD_GET = 1 << 10, /* query information */ /* other */ LG_NETWORK = 1 << 11, /* netsplit/netjoin */ LG_WALLOPS = 1 << 12, /* NOTYET wallops from opers/other servers */ LG_RAWDATA = 1 << 13, /* all data sent/received */ LG_REGISTER = 1 << 14, /* all registration related messages */ LG_WARN1 = 1 << 15, /* NOTYET messages formerly walloped */ LG_WARN2 = 1 << 16, /* NOTYET messages formerly snooped */ LG_CMD_ALL = LG_CMD_ADMIN | LG_CMD_REGISTER | LG_CMD_SET | LG_CMD_DO | LG_CMD_LOGIN | LG_CMD_GET, LG_ALL = 1 << 31 - 1,/* XXX cannot use bit 31 as it would then be unequal to TOKEN_UNMATCHED */ /* aliases for use with logcommand() */ CMDLOG_ADMIN = LG_CMD_ADMIN, CMDLOG_REGISTER = LG_CMD_REGISTER | LG_REGISTER, CMDLOG_SET = LG_CMD_SET, CMDLOG_DO = LG_CMD_DO, CMDLOG_LOGIN = LG_CMD_LOGIN, CMDLOG_GET = LG_CMD_GET }; E void log_open (void); E void log_shutdown (void); E bool log_debug_enabled (void); E void log_master_set_mask (unsigned int mask); E void slog (unsigned int level, char const * const fmt, ...); E void logcommand (sourceinfo_t *si, int level, char const * const fmt, ...); E void logcommand_user (service_t *svs, user_t *source, int level, char const * const fmt, ...); 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, ...); /* 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); E char *itoa (int num); 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