log.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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, LOGLINEMAX, 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. /* No need to check if out should be null-terminated here,
  125. * just do it! <cybah>
  126. */
  127. egg_vsnprintf(va_out, LOGLINEMAX, format, va);
  128. va_end(va);
  129. if (!va_out[0]) {
  130. putlog(LOG_ERRORS, "*", "Empty putlog() detected");
  131. return;
  132. }
  133. char *p = NULL;
  134. if ((p = strchr(va_out, '\n'))) /* make sure no trailing newline */
  135. *p = 0;
  136. int idx = 0;
  137. char out[LOGLINEMAX + 1] = "";
  138. #ifdef HUB
  139. char stamp[34] = "";
  140. struct tm *t = gmtime(&now);
  141. egg_strftime(stamp, sizeof(stamp), LOG_TS, t);
  142. /* Place the timestamp in the string to be printed */
  143. egg_snprintf(out, sizeof out, "%s %s", stamp, va_out);
  144. #endif /* HUB */
  145. #ifdef LEAF
  146. egg_snprintf(out, sizeof out, "%s", va_out);
  147. #endif /* LEAF */
  148. /* strcat(out, "\n"); */
  149. /* FIXME: WRITE LOG HERE */
  150. /* broadcast to hubs */
  151. if (chname[0] == '*' && conf.bot && conf.bot->nick) {
  152. char outbuf[LOGLINEMAX + 1] = "";
  153. egg_snprintf(outbuf, sizeof outbuf, "hl %d %s", type, out);
  154. if (userlist && !loading) {
  155. tand_t *bot = NULL;
  156. struct userrec *ubot = NULL;
  157. for (bot = tandbot; bot; bot = bot->next) {
  158. if ((ubot = get_user_by_handle(userlist, bot->bot))) {
  159. if (bot_hublevel(ubot) < 999)
  160. putbot(ubot->handle, outbuf);
  161. }
  162. }
  163. } else {
  164. putallbots(outbuf);
  165. }
  166. }
  167. for (idx = 0; idx < dcc_total; idx++) {
  168. if (dcc[idx].type && (dcc[idx].type == &DCC_CHAT && !dcc[idx].simul) && (dcc[idx].u.chat->con_flags & type)) {
  169. if ((chname[0] == '@') || (chname[0] == '*') || (dcc[idx].u.chat->con_chan[0] == '*') ||
  170. (!rfc_casecmp(chname, dcc[idx].u.chat->con_chan)))
  171. dprintf(idx, "%s\n", out);
  172. }
  173. }
  174. if ((!backgrd) && (!term_z)) {
  175. dprintf(DP_STDOUT, "%s\n", out);
  176. } else if ((type & LOG_ERRORS || type & LOG_MISC) && use_stderr) {
  177. dprintf(DP_STDERR, "%s\n", va_out);
  178. }
  179. }
  180. void
  181. irc_log(struct chanset_t *chan, const char *format, ...)
  182. {
  183. #ifdef NOTHANKS
  184. char va_out[LOGLINEMAX + 1];
  185. va_list va;
  186. va_start(va, format);
  187. egg_vsnprintf(va_out, LOGLINEMAX, format, va);
  188. va_end(va);
  189. if ((chan && egg_strcasecmp(chan->dname, "#!obs")) || !chan)
  190. dprintf(DP_HELP, "PRIVMSG #!obs :[%s] %s\n", chan ? chan->dname : "" , va_out);
  191. /*
  192. chanout_but(-1, 1, "[%s] %s\n", chan->dname, va_out);
  193. botnet_send_chan(-1, conf.bot->nick, chan->dname, 1, va_out);
  194. if (chan)
  195. putlog(LOG_PUBLIC, "*", "[%s] %s", chan->dname, va_out);
  196. else
  197. putlog(LOG_PUBLIC, "*", "%s", va_out);
  198. sdprintf("%s", va_out);
  199. */
  200. #endif
  201. }