log.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 "chanprog.h"
  40. #include <ctype.h>
  41. #include <stdarg.h>
  42. #include <fcntl.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <unistd.h>
  46. int conmask = LOG_MODES | LOG_CMDS | LOG_MISC; /* Console mask */
  47. bool debug_output = 1; /* Disply output to server to LOG_SERVEROUT */
  48. typedef struct {
  49. int flag;
  50. char *type;
  51. unsigned char c;
  52. } logmode_mapping_t;
  53. static logmode_mapping_t logmode_mappings[] = {
  54. {LOG_MSGS, "msgs", 'm'},
  55. {LOG_PUBLIC, "public", 'p'},
  56. {LOG_JOIN, "joins", 'j'},
  57. {LOG_MODES, "kicks/modes", 'k'},
  58. {LOG_CMDS, "cmds", 'c'},
  59. {LOG_MISC, "misc", 'o'},
  60. {LOG_BOTS, "bots", 'b'},
  61. {LOG_RAW, "raw", 'r'},
  62. {LOG_WALL, "wallops", 'w'},
  63. {LOG_FILES, "files", 'x'},
  64. {LOG_SERV, "server", 's'},
  65. {LOG_DEBUG, "debug", 'd'},
  66. {LOG_SRVOUT, "server output", 'v'},
  67. {LOG_BOTNET, "botnet traffic", 't'},
  68. {LOG_BOTSHARE, "share traffic", 'h'},
  69. {LOG_ERRORS, "errors", 'e'},
  70. {LOG_GETIN, "getin", 'g'},
  71. {LOG_WARN, "warnings", 'u'},
  72. {0, NULL, 0}
  73. };
  74. #define LOG_LEVELS 18 /* change this if you change the levels */
  75. #define NEEDS_DEBUG_OUTPUT (LOG_RAW|LOG_SRVOUT|LOG_BOTNET|LOG_BOTSHARE)
  76. int logmodes(const char *s)
  77. {
  78. logmode_mapping_t *mapping = NULL;
  79. int modes = 0;
  80. while (*s) {
  81. if (*s == '*') return(LOG_ALL);
  82. for (mapping = logmode_mappings; mapping->type; mapping++) {
  83. if (mapping->c == tolower(*s)) break;
  84. }
  85. if (mapping->type) modes |= mapping->flag;
  86. s++;
  87. }
  88. return(modes);
  89. }
  90. char *masktype(int x)
  91. {
  92. static char s[LOG_LEVELS + 1];
  93. char *p = s;
  94. logmode_mapping_t *mapping = NULL;
  95. for (mapping = logmode_mappings; mapping->type; mapping++) {
  96. if (x & mapping->flag) {
  97. if ((mapping->flag & NEEDS_DEBUG_OUTPUT) && !debug_output) continue;
  98. *p++ = mapping->c;
  99. }
  100. }
  101. if (p == s) *p++ = '-';
  102. *p = 0;
  103. return(s);
  104. }
  105. char *maskname(int x)
  106. {
  107. static char s[1024] = "";
  108. logmode_mapping_t *mapping = NULL;
  109. int len;
  110. *s = 0;
  111. for (mapping = logmode_mappings; mapping->type; mapping++) {
  112. if (x & mapping->flag) {
  113. if ((mapping->flag & NEEDS_DEBUG_OUTPUT) && !debug_output) continue;
  114. strlcat(s, mapping->type, sizeof(s));
  115. strlcat(s, ", ", sizeof(s));
  116. }
  117. }
  118. len = strlen(s);
  119. if (len) s[len-2] = 0;
  120. else strlcpy(s, "none", sizeof(s));
  121. return(s);
  122. }
  123. /*
  124. * Logging functions
  125. */
  126. void logidx(int idx, const char *format, ...)
  127. {
  128. char va_out[LOGLINEMAX + 1];
  129. va_list va;
  130. va_start(va, format);
  131. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  132. va_end(va);
  133. if (idx < 0)
  134. putlog(LOG_MISC, "*", "%s", va_out);
  135. else
  136. dprintf(idx, "%s\n", va_out);
  137. }
  138. #ifdef DEBUG
  139. /* CURRENTLY SPAWNS TONS OF FILES */
  140. FILE *log_f;
  141. bool flush_log = 1;
  142. bool init_log_exit = 0;
  143. char log_last[LOGLINEMAX + 1] = "";
  144. int repeats = 0;
  145. void logfile_close(void)
  146. {
  147. if (!log_f)
  148. return;
  149. char date[50] = "";
  150. strftime(date, sizeof date, "%c %Z", gmtime(&now));
  151. fprintf(log_f, "--- Log session end: %s ---\n", date);
  152. fclose(log_f);
  153. log_f = NULL;
  154. }
  155. bool logfile_open()
  156. {
  157. if (!conf.bot || !conf.bot->nick) return 0;
  158. char filename[20] = "";
  159. simple_snprintf(filename, sizeof(filename), ".l-%s", conf.bot->nick);
  160. if (!(log_f = fopen(filename, "w")))
  161. return 0;
  162. if (!init_log_exit) {
  163. init_log_exit = 1;
  164. atexit(logfile_close);
  165. }
  166. char date[50] = "";
  167. strftime(date, sizeof date, "%c %Z", gmtime(&now));
  168. fprintf(log_f, "--- Log session begin: %s ---\n", date);
  169. return 1;
  170. }
  171. bool logfile_stat(const char *fname)
  172. {
  173. struct stat st;
  174. int fd = open(fname, O_RDONLY);
  175. if (fd == -1 || fstat(fd, &st) < 0) {
  176. if (fd != -1)
  177. close(fd);
  178. fclose(log_f);
  179. log_f = NULL;
  180. if (!logfile_open())
  181. return 0;
  182. }
  183. return 1;
  184. }
  185. void logfile(int type, const char *msg)
  186. {
  187. if (!log_f && !logfile_open())
  188. return;
  189. // if (!logfile_stat(".l"))
  190. // return;
  191. if (!strncasecmp(msg, log_last, sizeof(log_last))) {
  192. repeats++;
  193. return;
  194. }
  195. if (repeats) {
  196. fprintf(log_f, "Last message repeated %d times.\n", repeats);
  197. repeats = 0;
  198. }
  199. strlcpy(log_last, msg, sizeof(log_last));
  200. fprintf(log_f, "%s\n", msg);
  201. if (flush_log)
  202. fflush(log_f);
  203. }
  204. #endif
  205. /* Log something
  206. * putlog(level,channel_name,format,...);
  207. * Broadcast the log if chname is not '@'
  208. */
  209. char last_log[LOGLINEMAX + 1] = "";
  210. int log_repeats = 0;
  211. char last_chname[25] = "";
  212. int last_type;
  213. bool log_repeated;
  214. void putlog(int type, const char *chname, const char *format, ...)
  215. {
  216. char va_out[LOGLINEMAX + 1] = "";
  217. va_list va;
  218. va_start(va, format);
  219. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  220. va_end(va);
  221. if (!va_out[0]) {
  222. putlog(LOG_ERRORS, "*", "Empty putlog() detected");
  223. return;
  224. }
  225. if (!log_repeated) {
  226. if (type == last_type &&
  227. !strncasecmp(chname, last_chname, sizeof(last_chname)) &&
  228. !strncasecmp(va_out, last_log, sizeof(last_log))) {
  229. ++log_repeats;
  230. return;
  231. }
  232. if (log_repeats) {
  233. log_repeated = 1;
  234. putlog(type, last_chname, "Last message repeated %d times.\n", log_repeats);
  235. log_repeats = 0;
  236. }
  237. strlcpy(last_log, va_out, sizeof(last_log));
  238. last_type = type;
  239. strlcpy(last_chname, chname, sizeof(last_chname));
  240. } else
  241. log_repeated = 0;
  242. char *p = NULL;
  243. if ((p = strchr(va_out, '\n'))) /* make sure no trailing newline */
  244. *p = 0;
  245. int idx = 0;
  246. char out[LOGLINEMAX + 1] = "";
  247. if (conf.bot && conf.bot->hub) {
  248. char stamp[34] = "";
  249. time_t synced = now + timesync;
  250. struct tm *t = gmtime(&synced);
  251. strftime(stamp, sizeof(stamp), LOG_TS, t);
  252. /* Place the timestamp in the string to be printed */
  253. strlcpy(out, stamp, sizeof(out));
  254. strlcat(out, " ", sizeof(out));
  255. strlcat(out, va_out, sizeof(out));
  256. } else
  257. strlcpy(out, va_out, sizeof(out));
  258. /* strcat(out, "\n"); */
  259. #ifdef DEBUG
  260. /* FIXME: WRITE LOG HERE */
  261. int logfile_masks = LOG_ALL;
  262. if ((logfile_masks && (logfile_masks & type)) || 1)
  263. logfile(type, out);
  264. #endif
  265. /* broadcast to hubs */
  266. if (chname[0] == '*' && conf.bot && conf.bot->nick)
  267. botnet_send_log(-1, conf.bot->nick, type, out, (conf.bot->hub ? 1 : 0));
  268. for (idx = 0; idx < dcc_total; idx++) {
  269. if (dcc[idx].type && (dcc[idx].type == &DCC_CHAT && dcc[idx].simul == -1) && (dcc[idx].u.chat->con_flags & type)) {
  270. if ((chname[0] == '@') || (chname[0] == '*') || (dcc[idx].u.chat->con_chan[0] == '*') ||
  271. (!rfc_casecmp(chname, dcc[idx].u.chat->con_chan)))
  272. dprintf(idx, "%s\n", out);
  273. }
  274. }
  275. if ((!backgrd) && (!term_z)) {
  276. dprintf(DP_STDOUT, "%s\n", out);
  277. } else if ((type & LOG_ERRORS || type & LOG_MISC) && use_stderr) {
  278. dprintf(DP_STDERR, "%s\n", va_out);
  279. }
  280. }
  281. void
  282. irc_log(struct chanset_t *chan, const char *format, ...)
  283. {
  284. #ifdef NOTHANKS
  285. char va_out[LOGLINEMAX + 1];
  286. va_list va;
  287. va_start(va, format);
  288. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  289. va_end(va);
  290. if ((chan && strcasecmp(chan->dname, relay_chan)) || !chan) {
  291. bd::String msg;
  292. msg.printf("[%s] %s", chan ? chan->dname : "*" , va_out);
  293. privmsg(relay_chan, msg.c_str(), DP_HELP);
  294. }
  295. /*
  296. chanout_but(-1, 1, "[%s] %s\n", chan->dname, va_out);
  297. botnet_send_chan(-1, conf.bot->nick, chan->dname, 1, va_out);
  298. if (chan)
  299. putlog(LOG_PUBLIC, "*", "[%s] %s", chan->dname, va_out);
  300. else
  301. putlog(LOG_PUBLIC, "*", "%s", va_out);
  302. sdprintf("%s", va_out);
  303. */
  304. #endif
  305. }