log.c 7.1 KB

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