log.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. int conmask = LOG_MODES | LOG_CMDS | LOG_MISC; /* Console mask */
  23. bool debug_output = 1; /* Disply output to server to LOG_SERVEROUT */
  24. bool use_console_r = 1; /* Allow users to set their console +r */
  25. typedef struct {
  26. int flag;
  27. char *type;
  28. unsigned char c;
  29. } logmode_mapping_t;
  30. static logmode_mapping_t logmode_mappings[] = {
  31. {LOG_MSGS, "msgs", 'm'},
  32. {LOG_PUBLIC, "public", 'p'},
  33. {LOG_JOIN, "joins", 'j'},
  34. {LOG_MODES, "kicks/modes", 'k'},
  35. {LOG_CMDS, "cmds", 'c'},
  36. {LOG_MISC, "misc", 'o'},
  37. {LOG_BOTS, "bots", 'b'},
  38. {LOG_RAW, "raw", 'r'},
  39. {LOG_WALL, "wallops", 'w'},
  40. {LOG_FILES, "files", 'x'},
  41. {LOG_SERV, "server", 's'},
  42. {LOG_DEBUG, "debug", 'd'},
  43. {LOG_SRVOUT, "server output", 'v'},
  44. {LOG_BOTNET, "botnet traffic", 't'},
  45. {LOG_BOTSHARE, "share traffic", 'h'},
  46. {LOG_ERRORS, "errors", 'e'},
  47. {LOG_GETIN, "getin", 'g'},
  48. {LOG_WARN, "warnings", 'u'},
  49. {0, NULL, 0}
  50. };
  51. #define LOG_LEVELS 18 /* change this if you change the levels */
  52. #define NEEDS_DEBUG_OUTPUT (LOG_RAW|LOG_SRVOUT|LOG_BOTNET|LOG_BOTSHARE)
  53. int logmodes(const char *s)
  54. {
  55. logmode_mapping_t *mapping = NULL;
  56. int modes = 0;
  57. while (*s) {
  58. if (*s == '*') return(LOG_ALL);
  59. for (mapping = logmode_mappings; mapping->type; mapping++) {
  60. if (mapping->c == tolower(*s)) break;
  61. }
  62. if (mapping->type) modes |= mapping->flag;
  63. s++;
  64. }
  65. return(modes);
  66. }
  67. char *masktype(int x)
  68. {
  69. static char s[LOG_LEVELS + 1];
  70. char *p = s;
  71. logmode_mapping_t *mapping = NULL;
  72. for (mapping = logmode_mappings; mapping->type; mapping++) {
  73. if (x & mapping->flag) {
  74. if ((mapping->flag & NEEDS_DEBUG_OUTPUT) && !debug_output) continue;
  75. *p++ = mapping->c;
  76. }
  77. }
  78. if (p == s) *p++ = '-';
  79. *p = 0;
  80. return(s);
  81. }
  82. char *maskname(int x)
  83. {
  84. static char s[1024] = "";
  85. logmode_mapping_t *mapping = NULL;
  86. int len;
  87. *s = 0;
  88. for (mapping = logmode_mappings; mapping->type; mapping++) {
  89. if (x & mapping->flag) {
  90. if ((mapping->flag & NEEDS_DEBUG_OUTPUT) && !debug_output) continue;
  91. strcat(s, mapping->type);
  92. strcat(s, ", ");
  93. }
  94. }
  95. len = strlen(s);
  96. if (len) s[len-2] = 0;
  97. else strcpy(s, "none");
  98. return(s);
  99. }
  100. /*
  101. * Logging functions
  102. */
  103. void logidx(int idx, const char *format, ...)
  104. {
  105. char va_out[LOGLINEMAX + 1];
  106. va_list va;
  107. va_start(va, format);
  108. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  109. va_end(va);
  110. if (idx < 0)
  111. putlog(LOG_DEBUG, "*", "%s", va_out);
  112. else
  113. dprintf(idx, "%s\n", va_out);
  114. }
  115. /* Log something
  116. * putlog(level,channel_name,format,...);
  117. * Broadcast the log if chname is not '@'
  118. */
  119. void putlog(int type, const char *chname, const char *format, ...)
  120. {
  121. char va_out[LOGLINEMAX + 1] = "";
  122. va_list va;
  123. va_start(va, format);
  124. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  125. va_end(va);
  126. if (!va_out[0]) {
  127. putlog(LOG_ERRORS, "*", "Empty putlog() detected");
  128. return;
  129. }
  130. char *p = NULL;
  131. if ((p = strchr(va_out, '\n'))) /* make sure no trailing newline */
  132. *p = 0;
  133. int idx = 0;
  134. char out[LOGLINEMAX + 1] = "";
  135. if (conf.bot && conf.bot->hub) {
  136. char stamp[34] = "";
  137. struct tm *t = gmtime(&now);
  138. egg_strftime(stamp, sizeof(stamp), LOG_TS, t);
  139. /* Place the timestamp in the string to be printed */
  140. simple_snprintf(out, sizeof out, "%s %s", stamp, va_out);
  141. } else
  142. simple_snprintf(out, sizeof out, "%s", va_out);
  143. /* strcat(out, "\n"); */
  144. /* FIXME: WRITE LOG HERE */
  145. /* broadcast to hubs */
  146. if (chname[0] == '*' && conf.bot && conf.bot->nick)
  147. botnet_send_log(-1, conf.bot->nick, type, out);
  148. for (idx = 0; idx < dcc_total; idx++) {
  149. if (dcc[idx].type && (dcc[idx].type == &DCC_CHAT && dcc[idx].simul == -1) && (dcc[idx].u.chat->con_flags & type)) {
  150. if ((chname[0] == '@') || (chname[0] == '*') || (dcc[idx].u.chat->con_chan[0] == '*') ||
  151. (!rfc_casecmp(chname, dcc[idx].u.chat->con_chan)))
  152. dprintf(idx, "%s\n", out);
  153. }
  154. }
  155. if ((!backgrd) && (!term_z)) {
  156. dprintf(DP_STDOUT, "%s\n", out);
  157. } else if ((type & LOG_ERRORS || type & LOG_MISC) && use_stderr) {
  158. dprintf(DP_STDERR, "%s\n", va_out);
  159. }
  160. }
  161. void
  162. irc_log(struct chanset_t *chan, const char *format, ...)
  163. {
  164. #ifdef NOTHANKS
  165. char va_out[LOGLINEMAX + 1];
  166. va_list va;
  167. va_start(va, format);
  168. egg_vsnprintf(va_out, sizeof(va_out), format, va);
  169. va_end(va);
  170. if ((chan && egg_strcasecmp(chan->dname, "#!obs")) || !chan)
  171. dprintf(DP_HELP, "PRIVMSG #!obs :[%s] %s\n", chan ? chan->dname : "" , va_out);
  172. /*
  173. chanout_but(-1, 1, "[%s] %s\n", chan->dname, va_out);
  174. botnet_send_chan(-1, conf.bot->nick, chan->dname, 1, va_out);
  175. if (chan)
  176. putlog(LOG_PUBLIC, "*", "[%s] %s", chan->dname, va_out);
  177. else
  178. putlog(LOG_PUBLIC, "*", "%s", va_out);
  179. sdprintf("%s", va_out);
  180. */
  181. #endif
  182. }