log.c 6.0 KB

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