log.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. strlcat(s, mapping->type, sizeof(s));
  114. strlcat(s, ", ", sizeof(s));
  115. }
  116. }
  117. len = strlen(s);
  118. if (len) s[len-2] = 0;
  119. else strlcpy(s, "none", sizeof(s));
  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 DEBUG
  138. /* CURRENTLY SPAWNS TONS OF FILES */
  139. FILE *log_f;
  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 (!log_f)
  147. return;
  148. char date[50] = "";
  149. strftime(date, sizeof date, "%c %Z", gmtime(&now));
  150. fprintf(log_f, "--- Log session end: %s ---\n", date);
  151. fclose(log_f);
  152. log_f = NULL;
  153. }
  154. bool logfile_open()
  155. {
  156. if (!conf.bot || !conf.bot->nick) return 0;
  157. char filename[20] = "";
  158. simple_snprintf(filename, sizeof(filename), ".l-%s", conf.bot->nick);
  159. if (!(log_f = fopen(filename, "w")))
  160. return 0;
  161. if (!init_log_exit) {
  162. init_log_exit = 1;
  163. atexit(logfile_close);
  164. }
  165. char date[50] = "";
  166. strftime(date, sizeof date, "%c %Z", gmtime(&now));
  167. fprintf(log_f, "--- Log session begin: %s ---\n", date);
  168. return 1;
  169. }
  170. bool logfile_stat(const char *fname)
  171. {
  172. struct stat st;
  173. int fd = open(fname, O_RDONLY);
  174. if (fd == -1 || fstat(fd, &st) < 0) {
  175. if (fd != -1)
  176. close(fd);
  177. fclose(log_f);
  178. log_f = NULL;
  179. if (!logfile_open())
  180. return 0;
  181. }
  182. return 1;
  183. }
  184. void logfile(int type, const char *msg)
  185. {
  186. if (!log_f && !logfile_open())
  187. return;
  188. // if (!logfile_stat(".l"))
  189. // return;
  190. if (!strncasecmp(msg, log_last, sizeof(log_last))) {
  191. repeats++;
  192. return;
  193. }
  194. if (repeats) {
  195. fprintf(log_f, "Last message repeated %d times.\n", repeats);
  196. repeats = 0;
  197. }
  198. strlcpy(log_last, msg, sizeof(log_last));
  199. fprintf(log_f, "%s\n", msg);
  200. if (flush_log)
  201. fflush(log_f);
  202. }
  203. #endif
  204. /* Log something
  205. * putlog(level,channel_name,format,...);
  206. * Broadcast the log if chname is not '@'
  207. */
  208. char last_log[LOGLINEMAX + 1] = "";
  209. int log_repeats = 0;
  210. char last_chname[25] = "";
  211. int last_type;
  212. bool log_repeated;
  213. void putlog(int type, const char *chname, const char *format, ...)
  214. {
  215. char va_out[LOGLINEMAX + 1] = "";
  216. va_list va;
  217. va_start(va, format);
  218. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  219. va_end(va);
  220. if (!va_out[0]) {
  221. putlog(LOG_ERRORS, "*", "Empty putlog() detected");
  222. return;
  223. }
  224. if (!log_repeated) {
  225. if (type == last_type &&
  226. !strncasecmp(chname, last_chname, sizeof(last_chname)) &&
  227. !strncasecmp(va_out, last_log, sizeof(last_log))) {
  228. ++log_repeats;
  229. return;
  230. }
  231. if (log_repeats) {
  232. log_repeated = 1;
  233. putlog(type, last_chname, "Last message repeated %d times.\n", log_repeats);
  234. log_repeats = 0;
  235. }
  236. strlcpy(last_log, va_out, sizeof(last_log));
  237. last_type = type;
  238. strlcpy(last_chname, chname, sizeof(last_chname));
  239. } else
  240. log_repeated = 0;
  241. char *p = NULL;
  242. if ((p = strchr(va_out, '\n'))) /* make sure no trailing newline */
  243. *p = 0;
  244. int idx = 0;
  245. char out[LOGLINEMAX + 1] = "";
  246. if (conf.bot && conf.bot->hub) {
  247. char stamp[34] = "";
  248. time_t synced = now + timesync;
  249. struct tm *t = gmtime(&synced);
  250. strftime(stamp, sizeof(stamp), LOG_TS, t);
  251. /* Place the timestamp in the string to be printed */
  252. strlcpy(out, stamp, sizeof(out));
  253. strlcat(out, " ", sizeof(out));
  254. strlcat(out, va_out, sizeof(out));
  255. } else
  256. strlcpy(out, va_out, sizeof(out));
  257. /* strcat(out, "\n"); */
  258. #ifdef DEBUG
  259. /* FIXME: WRITE LOG HERE */
  260. int logfile_masks = LOG_ALL;
  261. if ((logfile_masks && (logfile_masks & type)) || 1)
  262. logfile(type, out);
  263. #endif
  264. /* broadcast to hubs */
  265. if (chname[0] == '*' && conf.bot && conf.bot->nick)
  266. botnet_send_log(-1, conf.bot->nick, type, out, (conf.bot->hub ? 1 : 0));
  267. for (idx = 0; idx < dcc_total; idx++) {
  268. if (dcc[idx].type && (dcc[idx].type == &DCC_CHAT && dcc[idx].simul == -1) && (dcc[idx].u.chat->con_flags & type)) {
  269. if ((chname[0] == '@') || (chname[0] == '*') || (dcc[idx].u.chat->con_chan[0] == '*') ||
  270. (!rfc_casecmp(chname, dcc[idx].u.chat->con_chan)))
  271. dprintf(idx, "%s\n", out);
  272. }
  273. }
  274. if ((!backgrd) && (!term_z)) {
  275. dprintf(DP_STDOUT, "%s\n", out);
  276. } else if ((type & LOG_ERRORS || type & LOG_MISC) && use_stderr) {
  277. dprintf(DP_STDERR, "%s\n", va_out);
  278. }
  279. }
  280. void
  281. irc_log(struct chanset_t *chan, const char *format, ...)
  282. {
  283. #ifdef NOTHANKS
  284. char va_out[LOGLINEMAX + 1];
  285. va_list va;
  286. va_start(va, format);
  287. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  288. va_end(va);
  289. if ((chan && strcasecmp(chan->dname, "#!obs")) || !chan)
  290. dprintf(DP_HELP, "PRIVMSG %s :[%s] %s\n", relay_chan, chan ? chan->dname : "*" , va_out);
  291. /*
  292. chanout_but(-1, 1, "[%s] %s\n", chan->dname, va_out);
  293. botnet_send_chan(-1, conf.bot->nick, chan->dname, 1, va_out);
  294. if (chan)
  295. putlog(LOG_PUBLIC, "*", "[%s] %s", chan->dname, va_out);
  296. else
  297. putlog(LOG_PUBLIC, "*", "%s", va_out);
  298. sdprintf("%s", va_out);
  299. */
  300. #endif
  301. }