log.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 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. #ifdef unused
  172. bool logfile_stat(const char *fname)
  173. {
  174. struct stat st;
  175. int fd = open(fname, O_RDONLY);
  176. if (fd == -1 || fstat(fd, &st) < 0) {
  177. if (fd != -1)
  178. close(fd);
  179. fclose(log_f);
  180. log_f = NULL;
  181. if (!logfile_open())
  182. return 0;
  183. }
  184. return 1;
  185. }
  186. #endif
  187. void logfile(int type, const char *msg)
  188. {
  189. // Only log if this is an actual bot and not a startup/cron proc
  190. if (!safe_to_log || (!log_f && !logfile_open()))
  191. return;
  192. // if (!logfile_stat(".l"))
  193. // return;
  194. if (!strncasecmp(msg, log_last, sizeof(log_last))) {
  195. repeats++;
  196. return;
  197. }
  198. if (repeats) {
  199. fprintf(log_f, "Last message repeated %d times.\n", repeats);
  200. repeats = 0;
  201. }
  202. strlcpy(log_last, msg, sizeof(log_last));
  203. fprintf(log_f, "%s\n", msg);
  204. if (flush_log)
  205. fflush(log_f);
  206. }
  207. #endif
  208. /* Log something
  209. * putlog(level,channel_name,format,...);
  210. * Broadcast the log if chname is not '@'
  211. */
  212. char last_log[LOGLINEMAX + 1] = "";
  213. int log_repeats = 0;
  214. char last_chname[25] = "";
  215. int last_type;
  216. bool log_repeated;
  217. void putlog(int type, const char *chname, const char *format, ...)
  218. {
  219. char va_out[LOGLINEMAX + 1] = "";
  220. va_list va;
  221. va_start(va, format);
  222. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  223. va_end(va);
  224. if (!va_out[0]) {
  225. putlog(LOG_ERRORS, "*", "Empty putlog() detected");
  226. return;
  227. }
  228. if (!log_repeated) {
  229. if (type == last_type &&
  230. !strncasecmp(chname, last_chname, sizeof(last_chname)) &&
  231. !strncasecmp(va_out, last_log, sizeof(last_log))) {
  232. ++log_repeats;
  233. return;
  234. }
  235. if (log_repeats) {
  236. log_repeated = 1;
  237. putlog(type, last_chname, "Last message repeated %d times.\n", log_repeats);
  238. log_repeats = 0;
  239. }
  240. strlcpy(last_log, va_out, sizeof(last_log));
  241. last_type = type;
  242. strlcpy(last_chname, chname, sizeof(last_chname));
  243. } else
  244. log_repeated = 0;
  245. char *p = NULL;
  246. if ((p = strchr(va_out, '\n'))) /* make sure no trailing newline */
  247. *p = 0;
  248. int idx = 0;
  249. char out[LOGLINEMAX + 1] = "";
  250. if (conf.bot && conf.bot->hub) {
  251. char stamp[34] = "";
  252. time_t synced = now + timesync;
  253. struct tm *t = gmtime(&synced);
  254. strftime(stamp, sizeof(stamp), LOG_TS, t);
  255. /* Place the timestamp in the string to be printed */
  256. strlcpy(out, stamp, sizeof(out));
  257. strlcat(out, " ", sizeof(out));
  258. strlcat(out, va_out, sizeof(out));
  259. } else
  260. strlcpy(out, va_out, sizeof(out));
  261. /* strcat(out, "\n"); */
  262. #ifdef DEBUG
  263. /* FIXME: WRITE LOG HERE */
  264. int logfile_masks = LOG_ALL;
  265. if ((logfile_masks && (logfile_masks & type)) || 1)
  266. logfile(type, out);
  267. #endif
  268. /* broadcast to hubs */
  269. if (chname[0] == '*' && conf.bot && conf.bot->nick)
  270. botnet_send_log(-1, conf.bot->nick, type, out, (conf.bot->hub ? 1 : 0));
  271. for (idx = 0; idx < dcc_total; idx++) {
  272. if (dcc[idx].type && (dcc[idx].type == &DCC_CHAT && dcc[idx].simul == -1) && (dcc[idx].u.chat->con_flags & type)) {
  273. if ((chname[0] == '@') || (chname[0] == '*') || (dcc[idx].u.chat->con_chan[0] == '*') ||
  274. (!rfc_casecmp(chname, dcc[idx].u.chat->con_chan)))
  275. dprintf(idx, "%s\n", out);
  276. }
  277. }
  278. if ((!backgrd) && (!term_z)) {
  279. dprintf(DP_STDOUT, "%s\n", out);
  280. } else if ((type & LOG_ERRORS || type & LOG_MISC) && use_stderr) {
  281. dprintf(DP_STDERR, "%s\n", va_out);
  282. }
  283. }
  284. void
  285. irc_log(struct chanset_t *chan, const char *format, ...)
  286. {
  287. #ifdef NOTHANKS
  288. char va_out[LOGLINEMAX + 1];
  289. va_list va;
  290. va_start(va, format);
  291. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  292. va_end(va);
  293. if ((chan && strcasecmp(chan->dname, relay_chan)) || !chan) {
  294. bd::String msg;
  295. msg.printf("[%s] %s", chan ? chan->dname : "*" , va_out);
  296. privmsg(relay_chan, msg.c_str(), DP_HELP);
  297. }
  298. /*
  299. chanout_but(-1, 1, "[%s] %s\n", chan->dname, va_out);
  300. botnet_send_chan(-1, conf.bot->nick, chan->dname, 1, va_out);
  301. if (chan)
  302. putlog(LOG_PUBLIC, "*", "[%s] %s", chan->dname, va_out);
  303. else
  304. putlog(LOG_PUBLIC, "*", "%s", va_out);
  305. sdprintf("%s", va_out);
  306. */
  307. #endif
  308. }