log.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * log.c -- handles:
  22. *
  23. * What else?!
  24. *
  25. */
  26. #include "common.h"
  27. #include "log.h"
  28. #include "tandem.h"
  29. #include "color.h"
  30. #include "userrec.h"
  31. #include "botnet.h"
  32. #include "botmsg.h"
  33. #include "dcc.h"
  34. #include "dccutil.h"
  35. #include "rfc1459.h"
  36. #include "users.h"
  37. #include "misc.h"
  38. #include "main.h"
  39. #include <ctype.h>
  40. #include <stdarg.h>
  41. #include <fcntl.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <unistd.h>
  45. int conmask = LOG_MODES | LOG_CMDS | LOG_MISC; /* Console mask */
  46. bool debug_output = 1; /* Disply output to server to LOG_SERVEROUT */
  47. typedef struct {
  48. int flag;
  49. char *type;
  50. unsigned char c;
  51. } logmode_mapping_t;
  52. static logmode_mapping_t logmode_mappings[] = {
  53. {LOG_MSGS, "msgs", 'm'},
  54. {LOG_PUBLIC, "public", 'p'},
  55. {LOG_JOIN, "joins", 'j'},
  56. {LOG_MODES, "kicks/modes", 'k'},
  57. {LOG_CMDS, "cmds", 'c'},
  58. {LOG_MISC, "misc", 'o'},
  59. {LOG_BOTS, "bots", 'b'},
  60. {LOG_RAW, "raw", 'r'},
  61. {LOG_WALL, "wallops", 'w'},
  62. {LOG_FILES, "files", 'x'},
  63. {LOG_SERV, "server", 's'},
  64. {LOG_DEBUG, "debug", 'd'},
  65. {LOG_SRVOUT, "server output", 'v'},
  66. {LOG_BOTNET, "botnet traffic", 't'},
  67. {LOG_BOTSHARE, "share traffic", 'h'},
  68. {LOG_ERRORS, "errors", 'e'},
  69. {LOG_GETIN, "getin", 'g'},
  70. {LOG_WARN, "warnings", 'u'},
  71. {0, NULL, 0}
  72. };
  73. #define LOG_LEVELS 18 /* change this if you change the levels */
  74. #define NEEDS_DEBUG_OUTPUT (LOG_RAW|LOG_SRVOUT|LOG_BOTNET|LOG_BOTSHARE)
  75. int logmodes(const char *s)
  76. {
  77. logmode_mapping_t *mapping = NULL;
  78. int modes = 0;
  79. while (*s) {
  80. if (*s == '*') return(LOG_ALL);
  81. for (mapping = logmode_mappings; mapping->type; mapping++) {
  82. if (mapping->c == tolower(*s)) break;
  83. }
  84. if (mapping->type) modes |= mapping->flag;
  85. s++;
  86. }
  87. return(modes);
  88. }
  89. char *masktype(int x)
  90. {
  91. static char s[LOG_LEVELS + 1];
  92. char *p = s;
  93. logmode_mapping_t *mapping = NULL;
  94. for (mapping = logmode_mappings; mapping->type; mapping++) {
  95. if (x & mapping->flag) {
  96. if ((mapping->flag & NEEDS_DEBUG_OUTPUT) && !debug_output) continue;
  97. *p++ = mapping->c;
  98. }
  99. }
  100. if (p == s) *p++ = '-';
  101. *p = 0;
  102. return(s);
  103. }
  104. char *maskname(int x)
  105. {
  106. static char s[1024] = "";
  107. logmode_mapping_t *mapping = NULL;
  108. int len;
  109. *s = 0;
  110. for (mapping = logmode_mappings; mapping->type; mapping++) {
  111. if (x & mapping->flag) {
  112. if ((mapping->flag & NEEDS_DEBUG_OUTPUT) && !debug_output) continue;
  113. strcat(s, mapping->type);
  114. strcat(s, ", ");
  115. }
  116. }
  117. len = strlen(s);
  118. if (len) s[len-2] = 0;
  119. else strcpy(s, "none");
  120. return(s);
  121. }
  122. /*
  123. * Logging functions
  124. */
  125. void logidx(int idx, const char *format, ...)
  126. {
  127. char va_out[LOGLINEMAX + 1];
  128. va_list va;
  129. va_start(va, format);
  130. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  131. va_end(va);
  132. if (idx < 0)
  133. putlog(LOG_DEBUG, "*", "%s", va_out);
  134. else
  135. dprintf(idx, "%s\n", va_out);
  136. }
  137. #ifdef no
  138. /* CURRENTLY SPAWNS TONS OF FILES */
  139. FILE *logf;
  140. bool flush_log = 1;
  141. bool init_log_exit = 0;
  142. char log_last[LOGLINEMAX + 1] = "";
  143. int repeats = 0;
  144. void logfile_close(void)
  145. {
  146. if (!logf)
  147. return;
  148. char date[50] = "";
  149. egg_strftime(date, sizeof date, "%c %Z", gmtime(&now));
  150. fprintf(logf, "--- Log session end: %s ---\n", date);
  151. fclose(logf);
  152. logf = NULL;
  153. }
  154. bool logfile_open()
  155. {
  156. if (!(logf = fopen(".l", "a")))
  157. return 0;
  158. if (!init_log_exit) {
  159. init_log_exit = 1;
  160. atexit(logfile_close);
  161. }
  162. char date[50] = "";
  163. egg_strftime(date, sizeof date, "%c %Z", gmtime(&now));
  164. fprintf(logf, "--- Log session begin: %s ---\n", date);
  165. return 1;
  166. }
  167. bool logfile_stat(const char *fname)
  168. {
  169. struct stat st;
  170. int fd = open(fname, O_RDONLY);
  171. if (fd == -1 || fstat(fd, &st) < 0) {
  172. if (fd != -1)
  173. close(fd);
  174. fclose(logf);
  175. logf = NULL;
  176. if (!logfile_open())
  177. return 0;
  178. }
  179. return 1;
  180. }
  181. void logfile(int type, const char *msg)
  182. {
  183. if (!logf && !logfile_open())
  184. return;
  185. if (!logfile_stat(".l"))
  186. return;
  187. if (!egg_strncasecmp(msg, log_last, sizeof(log_last))) {
  188. repeats++;
  189. return;
  190. }
  191. if (repeats) {
  192. fprintf(logf, "Last message repeated %d times.\n", repeats);
  193. repeats = 0;
  194. }
  195. strlcpy(log_last, msg, sizeof(log_last));
  196. fprintf(logf, "%s\n", msg);
  197. if (flush_log)
  198. fflush(logf);
  199. }
  200. #endif
  201. /* Log something
  202. * putlog(level,channel_name,format,...);
  203. * Broadcast the log if chname is not '@'
  204. */
  205. char last_log[LOGLINEMAX + 1] = "";
  206. int log_repeats = 0;
  207. char last_chname[25] = "";
  208. int last_type;
  209. bool log_repeated;
  210. void putlog(int type, const char *chname, const char *format, ...)
  211. {
  212. char va_out[LOGLINEMAX + 1] = "";
  213. va_list va;
  214. va_start(va, format);
  215. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  216. va_end(va);
  217. if (!va_out[0]) {
  218. putlog(LOG_ERRORS, "*", "Empty putlog() detected");
  219. return;
  220. }
  221. if (!log_repeated) {
  222. if (type == last_type &&
  223. !egg_strncasecmp(chname, last_chname, sizeof(last_chname)) &&
  224. !egg_strncasecmp(va_out, last_log, sizeof(last_log))) {
  225. log_repeats++;
  226. return;
  227. }
  228. if (log_repeats) {
  229. log_repeated = 1;
  230. putlog(type, last_chname, "Last message repeated %d times.\n", log_repeats);
  231. log_repeats = 0;
  232. }
  233. strlcpy(last_log, va_out, sizeof(last_log));
  234. last_type = type;
  235. strlcpy(last_chname, chname, sizeof(last_chname));
  236. } else
  237. log_repeated = 0;
  238. char *p = NULL;
  239. if ((p = strchr(va_out, '\n'))) /* make sure no trailing newline */
  240. *p = 0;
  241. int idx = 0;
  242. char out[LOGLINEMAX + 1] = "";
  243. if (conf.bot && conf.bot->hub) {
  244. char stamp[34] = "";
  245. struct tm *t = gmtime(&now);
  246. egg_strftime(stamp, sizeof(stamp), LOG_TS, t);
  247. /* Place the timestamp in the string to be printed */
  248. simple_snprintf(out, sizeof out, "%s %s", stamp, va_out);
  249. } else
  250. simple_snprintf(out, sizeof out, "%s", va_out);
  251. /* strcat(out, "\n"); */
  252. #ifdef no
  253. /* FIXME: WRITE LOG HERE */
  254. int logfile_masks = LOG_CMDS|LOG_ERRORS|LOG_WARN|LOG_BOTS|LOG_MISC;
  255. if (logfile_masks && (logfile_masks & type))
  256. logfile(type, out);
  257. #endif
  258. /* broadcast to hubs */
  259. if (chname[0] == '*' && conf.bot && conf.bot->nick)
  260. botnet_send_log(-1, conf.bot->nick, type, out);
  261. for (idx = 0; idx < dcc_total; idx++) {
  262. if (dcc[idx].type && (dcc[idx].type == &DCC_CHAT && dcc[idx].simul == -1) && (dcc[idx].u.chat->con_flags & type)) {
  263. if ((chname[0] == '@') || (chname[0] == '*') || (dcc[idx].u.chat->con_chan[0] == '*') ||
  264. (!rfc_casecmp(chname, dcc[idx].u.chat->con_chan)))
  265. dprintf(idx, "%s\n", out);
  266. }
  267. }
  268. if ((!backgrd) && (!term_z)) {
  269. dprintf(DP_STDOUT, "%s\n", out);
  270. } else if ((type & LOG_ERRORS || type & LOG_MISC) && use_stderr) {
  271. dprintf(DP_STDERR, "%s\n", va_out);
  272. }
  273. }
  274. void
  275. irc_log(struct chanset_t *chan, const char *format, ...)
  276. {
  277. #ifdef NOTHANKS
  278. char va_out[LOGLINEMAX + 1];
  279. va_list va;
  280. va_start(va, format);
  281. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  282. va_end(va);
  283. if ((chan && egg_strcasecmp(chan->dname, "#!obs")) || !chan)
  284. dprintf(DP_HELP, "PRIVMSG #!obs :[%s] %s\n", chan ? chan->dname : "*" , va_out);
  285. /*
  286. chanout_but(-1, 1, "[%s] %s\n", chan->dname, va_out);
  287. botnet_send_chan(-1, conf.bot->nick, chan->dname, 1, va_out);
  288. if (chan)
  289. putlog(LOG_PUBLIC, "*", "[%s] %s", chan->dname, va_out);
  290. else
  291. putlog(LOG_PUBLIC, "*", "%s", va_out);
  292. sdprintf("%s", va_out);
  293. */
  294. #endif
  295. }