log.c 4.9 KB

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