source: logging.c @ a0c06dc

release-1.10release-1.9
Last change on this file since a0c06dc was f271129, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Fix up headers The additions to owl.h and some of the removals were done by Alejandro Sedeño <asedeno@mit.edu> in commit 77a0258b3919468fc9d7f7602588ac427ab36e6c. Notes: * I think owl.c lost the need for sys/time.h when we punted select() in favor of glib's main loop. * We don't actually need to include things like stdarg.h, stdio.h, glib/gstdio.h, glib-object.h. I think they get indirectly included via owl.h and/or glib.h. They're left in (or added in to) the files that use functions/types from them. * I'm not entirely sure what sys/socket.h is doing in message.c. It is there from the initial commit. I suspect it might have had something to do with the call to getnameinfo. message.c compiles without it, but http://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html suggests that we're supposed to include it? *shrugs* I'm leaving it in, for now. (Rather, I'll leave one copy of the #include in.)
  • Property mode set to 100644
File size: 13.1 KB
RevLine 
[7d4fbcd]1#include "owl.h"
[f271129]2#include <stdio.h>
[7d4fbcd]3
[cc305b5]4typedef struct _owl_log_entry { /* noproto */
5  char *filename;
6  char *message;
7} owl_log_entry;
8
9
10static GMainContext *log_context;
11static GMainLoop *log_loop;
12static GThread *logging_thread;
13
[15b34fd]14/* This is now the one function that should be called to log a
15 * message.  It will do all the work necessary by calling the other
16 * functions in this file as necessary.
17 */
[c08c70a]18void owl_log_message(const owl_message *m) {
[15b34fd]19  owl_function_debugmsg("owl_log_message: entering");
20
[3c7d086a]21  if (m == NULL) {
22    owl_function_debugmsg("owl_log_message: passed null message");
23    return;
24  }
25
[15b34fd]26  /* should we be logging this message? */
27  if (!owl_log_shouldlog_message(m)) {
28    owl_function_debugmsg("owl_log_message: not logging message");
29    return;
30  }
31
32  /* handle incmoing messages */
33  if (owl_message_is_direction_in(m)) {
34    owl_log_incoming(m);
35    owl_function_debugmsg("owl_log_message: leaving");
36    return;
37  }
38
39  /* handle outgoing messages */
[42947f1]40  owl_log_outgoing(m);
41
[15b34fd]42  owl_function_debugmsg("owl_log_message: leaving");
43}
[7d4fbcd]44
[15b34fd]45/* Return 1 if we should log the given message, otherwise return 0 */
[c08c70a]46int owl_log_shouldlog_message(const owl_message *m) {
[4542047]47  const owl_filter *f;
[12c35df]48
[15b34fd]49  /* If there's a logfilter and this message matches it, log */
50  f=owl_global_get_filter(&g, owl_global_get_logfilter(&g));
51  if (f && owl_filter_message_match(f, m)) return(1);
[7d4fbcd]52
[15b34fd]53  /* otherwise we do things based on the logging variables */
54
55  /* skip login/logout messages if appropriate */
56  if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0);
57     
58  /* check direction */
59  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) {
60    return(0);
61  }
62  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) && owl_message_is_direction_in(m)) {
63    return(0);
64  }
65
66  if (owl_message_is_type_zephyr(m)) {
67    if (owl_message_is_personal(m) && !owl_global_is_logging(&g)) return(0);
68    if (!owl_message_is_personal(m) && !owl_global_is_classlogging(&g)) return(0);
69  } else {
70    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
71      if (!owl_global_is_logging(&g)) return(0);
72    } else {
73      if (!owl_global_is_classlogging(&g)) return(0);
74    }
[7d4fbcd]75  }
[15b34fd]76  return(1);
77}
78
[6829afc]79CALLER_OWN char *owl_log_zephyr(const owl_message *m)
[d427f08]80{
[cc305b5]81    char *tmp = NULL;
82    GString *buffer = NULL;
83    buffer = g_string_new("");
84    tmp = short_zuser(owl_message_get_sender(m));
85    g_string_append_printf(buffer, "Class: %s Instance: %s", 
86                           owl_message_get_class(m), 
87                           owl_message_get_instance(m));
88    if (strcmp(owl_message_get_opcode(m), "")) {
89      g_string_append_printf(buffer, " Opcode: %s", 
90                             owl_message_get_opcode(m));
91    }
92    g_string_append_printf(buffer, "\n");
93    g_string_append_printf(buffer, "Time: %s Host: %s\n", 
94                           owl_message_get_timestr(m), 
95                           owl_message_get_hostname(m));
96    g_string_append_printf(buffer, "From: %s <%s>\n\n", 
97                           owl_message_get_zsig(m), tmp);
98    g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
[ddbbcffa]99    g_free(tmp);
[cc305b5]100    return g_string_free(buffer, FALSE);
101}
102
[6829afc]103CALLER_OWN char *owl_log_aim(const owl_message *m)
[d427f08]104{
[cc305b5]105    GString *buffer = NULL;
106    buffer = g_string_new("");
107    g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 
108                           owl_message_get_sender(m), owl_message_get_recipient(m));
109    g_string_append_printf(buffer, "Time: %s\n\n", 
110                           owl_message_get_timestr(m));
111    if (owl_message_is_login(m)) {
112        g_string_append_printf(buffer, "LOGIN\n\n");
113    } else if (owl_message_is_logout(m)) {
114        g_string_append_printf(buffer, "LOGOUT\n\n");
115    } else {
116        g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
117    }
118    return g_string_free(buffer, FALSE);
119}
120
[6829afc]121CALLER_OWN char *owl_log_jabber(const owl_message *m)
[d427f08]122{
[cc305b5]123    GString *buffer = NULL;
124    buffer = g_string_new("");
125    g_string_append_printf(buffer, "From: <%s> To: <%s>\n",
126                           owl_message_get_sender(m), 
127                           owl_message_get_recipient(m));
128    g_string_append_printf(buffer, "Time: %s\n\n", 
129                           owl_message_get_timestr(m));
130    g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
131    return g_string_free(buffer, FALSE);
132}
133
[6829afc]134CALLER_OWN char *owl_log_generic(const owl_message *m)
[d427f08]135{
[cc305b5]136    GString *buffer;
137    buffer = g_string_new("");
138    g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 
139                           owl_message_get_sender(m), 
140                           owl_message_get_recipient(m));
141    g_string_append_printf(buffer, "Time: %s\n\n", 
142                           owl_message_get_timestr(m));
143    g_string_append_printf(buffer, "%s\n\n", 
144                           owl_message_get_body(m));
145    return g_string_free(buffer, FALSE);
146}
147
148static void owl_log_error_main_thread(gpointer data)
149{
150  owl_function_error("%s", (const char*)data);
[42947f1]151}
152
[cc305b5]153static void owl_log_error(const char *message)
154{
155  char *data = g_strdup(message);
156  owl_select_post_task(owl_log_error_main_thread,
157                       data, g_free, g_main_context_default());
[42947f1]158}
159
[cc305b5]160static void owl_log_write_entry(gpointer data)
161{
162  owl_log_entry *msg = (owl_log_entry*)data;
163  FILE *file = NULL;
164  file = fopen(msg->filename, "a");
165  if (!file) {
166    owl_log_error("Unable to open file for logging");
167    return;
168  }
169  fprintf(file, "%s", msg->message);
170  fclose(file);
[42947f1]171}
172
[cc305b5]173static void owl_log_entry_free(void *data)
174{
175  owl_log_entry *msg = (owl_log_entry*)data;
176  if (msg) {
177    g_free(msg->message);
178    g_free(msg->filename);
179    g_free(msg);
180  }
181}
182
183void owl_log_enqueue_message(const char *buffer, const char *filename)
184{
185  owl_log_entry *log_msg = NULL; 
186  log_msg = g_new(owl_log_entry,1);
187  log_msg->message = g_strdup(buffer);
188  log_msg->filename = g_strdup(filename);
189  owl_select_post_task(owl_log_write_entry, log_msg, 
190                       owl_log_entry_free, log_context);
[42947f1]191}
192
[c08c70a]193void owl_log_append(const owl_message *m, const char *filename) {
[cc305b5]194  char *buffer = NULL;
195  if (owl_message_is_type_zephyr(m)) {
196    buffer = owl_log_zephyr(m);
197  } else if (owl_message_is_type_jabber(m)) {
198    buffer = owl_log_jabber(m);
199  } else if (owl_message_is_type_aim(m)) {
200    buffer = owl_log_aim(m);
201  } else {
202    buffer = owl_log_generic(m);
203  }
204  owl_log_enqueue_message(buffer, filename);
205  g_free(buffer);
[42947f1]206}
207
[c08c70a]208void owl_log_outgoing(const owl_message *m)
[15b34fd]209{
[e3a75ed]210  char *filename, *logpath;
[9c590d4]211  char *to, *temp;
[839697d]212  GList *cc;
[9c590d4]213
214  /* expand ~ in path names */
[60d7935]215  logpath = owl_util_makepath(owl_global_get_logpath(&g));
[15b34fd]216
[42947f1]217  /* Figure out what path to log to */
218  if (owl_message_is_type_zephyr(m)) {
[af1920fd]219    /* If this has CC's, do all but the "recipient" which we'll do below */
[839697d]220    cc = owl_message_get_cc_without_recipient(m);
221    while (cc != NULL) {
222      temp = short_zuser(cc->data);
[dde1b4d]223      filename = g_build_filename(logpath, temp, NULL);
[839697d]224      owl_log_append(m, filename);
225
[e3a75ed]226      g_free(filename);
[ddbbcffa]227      g_free(temp);
228      g_free(cc->data);
[839697d]229      cc = g_list_delete_link(cc, cc);
[9c590d4]230    }
[839697d]231
[9c590d4]232    to = short_zuser(owl_message_get_recipient(m));
[42947f1]233  } else if (owl_message_is_type_jabber(m)) {
[3472845]234    to = g_strdup_printf("jabber:%s", owl_message_get_recipient(m));
[7865479]235    g_strdelimit(to, "/", '_');
[42947f1]236  } else if (owl_message_is_type_aim(m)) {
[28ee32b]237    char *temp2;
[9c590d4]238    temp = owl_aim_normalize_screenname(owl_message_get_recipient(m));
[28ee32b]239    temp2 = g_utf8_strdown(temp,-1);
[3472845]240    to = g_strdup_printf("aim:%s", temp2);
[ddbbcffa]241    g_free(temp2);
242    g_free(temp);
[42947f1]243  } else {
[d4927a7]244    to = g_strdup("loopback");
[42947f1]245  }
[7d4fbcd]246
[dde1b4d]247  filename = g_build_filename(logpath, to, NULL);
[42947f1]248  owl_log_append(m, filename);
[ddbbcffa]249  g_free(to);
[e3a75ed]250  g_free(filename);
[7d4fbcd]251
[dde1b4d]252  filename = g_build_filename(logpath, "all", NULL);
[42947f1]253  owl_log_append(m, filename);
[ddbbcffa]254  g_free(logpath);
[e3a75ed]255  g_free(filename);
[7d4fbcd]256}
257
[42947f1]258
[24ccc01]259void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
[2b86d14]260{
[e3a75ed]261  char *filename, *logpath;
[fe3b017]262  char *tobuff, *recip;
[180cd15]263  owl_message *m;
[cc305b5]264  GString *msgbuf;
[180cd15]265  /* create a present message so we can pass it to
[c79a047]266   * owl_log_shouldlog_message(void)
[180cd15]267   */
[96828e4]268  m = g_new(owl_message, 1);
[e5da3fe]269  /* recip_index = 0 because there can only be one recipient anyway */
270  owl_message_create_from_zwrite(m, zw, text, 0);
[180cd15]271  if (!owl_log_shouldlog_message(m)) {
[91634ec]272    owl_message_delete(m);
[180cd15]273    return;
274  }
[91634ec]275  owl_message_delete(m);
[2b86d14]276
[180cd15]277  /* chop off a local realm */
[fe3b017]278  recip = owl_zwrite_get_recip_n_with_realm(zw, 0);
279  tobuff = short_zuser(recip);
280  g_free(recip);
[2b86d14]281
282  /* expand ~ in path names */
[60d7935]283  logpath = owl_util_makepath(owl_global_get_logpath(&g));
[dde1b4d]284  filename = g_build_filename(logpath, tobuff, NULL);
[cc305b5]285  msgbuf = g_string_new("");
286  g_string_printf(msgbuf, "ERROR (owl): %s\n%s\n", tobuff, text);
287  if (text[strlen(text)-1] != '\n') {
288    g_string_append_printf(msgbuf, "\n");
[2b86d14]289  }
[cc305b5]290  owl_log_enqueue_message(msgbuf->str, filename);
291  g_string_free(msgbuf, TRUE);
[2b86d14]292
[dde1b4d]293  filename = g_build_filename(logpath, "all", NULL);
[ddbbcffa]294  g_free(logpath);
[cc305b5]295  msgbuf = g_string_new("");
296  g_string_printf(msgbuf, "ERROR (owl): %s\n%s\n", tobuff, text);
297  if (text[strlen(text)-1] != '\n') {
298    g_string_append_printf(msgbuf, "\n");
[2b86d14]299  }
[cc305b5]300  owl_log_enqueue_message(msgbuf->str, filename);
301  g_string_free(msgbuf, TRUE);
[2b86d14]302
[ddbbcffa]303  g_free(tobuff);
[2b86d14]304}
305
[c08c70a]306void owl_log_incoming(const owl_message *m)
[15283bb]307{
[e3a75ed]308  char *filename, *allfilename, *logpath;
[e19eb97]309  const char *from=NULL;
[65b2173]310  char *frombuff=NULL;
[7d4fbcd]311  int len, ch, i, personal;
[12c35df]312
[15b34fd]313  /* figure out if it's a "personal" message or not */
[aac889a]314  if (owl_message_is_type_zephyr(m)) {
315    if (owl_message_is_personal(m)) {
[42947f1]316      personal = 1;
[aac889a]317    } else {
[42947f1]318      personal = 0;
[aac889a]319    }
[2182be3]320  } else if (owl_message_is_type_jabber(m)) {
[42947f1]321    /* This needs to be fixed to handle groupchat */
[e19eb97]322    const char* msgtype = owl_message_get_attribute_value(m,"jtype");
[42947f1]323    if (msgtype && !strcmp(msgtype,"groupchat")) {
324      personal = 0;
325    } else {
326      personal = 1;
327    }
[7d4fbcd]328  } else {
[79a0e82]329    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
[42947f1]330      personal = 1;
[aac889a]331    } else {
[42947f1]332      personal = 0;
[aac889a]333    }
[7d4fbcd]334  }
335
[2182be3]336
[aac889a]337  if (owl_message_is_type_zephyr(m)) {
338    if (personal) {
[3066d23]339      from=frombuff=short_zuser(owl_message_get_sender(m));
[aac889a]340    } else {
[d4927a7]341      from=frombuff=g_strdup(owl_message_get_class(m));
[7d4fbcd]342    }
[aac889a]343  } else if (owl_message_is_type_aim(m)) {
344    /* we do not yet handle chat rooms */
[28ee32b]345    char *normalto, *temp;
346    temp = owl_aim_normalize_screenname(owl_message_get_sender(m));
347    normalto = g_utf8_strdown(temp, -1);
[3472845]348    from=frombuff=g_strdup_printf("aim:%s", normalto);
[ddbbcffa]349    g_free(normalto);
350    g_free(temp);
[37eab7f]351  } else if (owl_message_is_type_loopback(m)) {
[d4927a7]352    from=frombuff=g_strdup("loopback");
[2182be3]353  } else if (owl_message_is_type_jabber(m)) {
[5c30091]354    if (personal) {
[cc305b5]355      from=frombuff=g_strdup_printf("jabber:%s", 
356                                    owl_message_get_sender(m));
[5c30091]357    } else {
[cc305b5]358      from=frombuff=g_strdup_printf("jabber:%s", 
359                                    owl_message_get_recipient(m));
[5c30091]360    }
[e6449bc]361  } else {
[d4927a7]362    from=frombuff=g_strdup("unknown");
[7d4fbcd]363  }
364 
365  /* check for malicious sender formats */
366  len=strlen(frombuff);
367  if (len<1 || len>35) from="weird";
368  if (strchr(frombuff, '/')) from="weird";
369
370  ch=frombuff[0];
[28ee32b]371  if (!g_ascii_isalnum(ch)) from="weird";
[7d4fbcd]372
373  for (i=0; i<len; i++) {
[e1c4636]374    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
[7d4fbcd]375  }
376
377  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
378
379  if (!personal) {
[28ee32b]380    if (strcmp(from, "weird")) {
381      char* temp = g_utf8_strdown(frombuff, -1);
382      if (temp) {
[ddbbcffa]383        g_free(frombuff);
[28ee32b]384        from = frombuff = temp;
385      }
386    }
[7d4fbcd]387  }
388
[e1c4636]389  /* create the filename (expanding ~ in path names) */
[7d4fbcd]390  if (personal) {
[60d7935]391    logpath = owl_util_makepath(owl_global_get_logpath(&g));
[dde1b4d]392    filename = g_build_filename(logpath, from, NULL);
393    allfilename = g_build_filename(logpath, "all", NULL);
[d0961fe]394    owl_log_append(m, allfilename);
[e3a75ed]395    g_free(allfilename);
[7d4fbcd]396  } else {
[60d7935]397    logpath = owl_util_makepath(owl_global_get_classlogpath(&g));
[dde1b4d]398    filename = g_build_filename(logpath, from, NULL);
[7d4fbcd]399  }
[37eab7f]400
[42947f1]401  owl_log_append(m, filename);
[e3a75ed]402  g_free(filename);
[7d4fbcd]403
[d0961fe]404  if (personal && owl_message_is_type_zephyr(m)) {
[af1920fd]405    /* We want to log to all of the CC'd people who were not us, or
406     * the sender, as well.
407     */
[839697d]408    char *temp;
409    GList *cc;
[d0961fe]410    cc = owl_message_get_cc_without_recipient(m);
[839697d]411    while (cc != NULL) {
412      temp = short_zuser(cc->data);
413      if (strcasecmp(temp, frombuff) != 0) {
[dde1b4d]414        filename = g_build_filename(logpath, temp, NULL);
[839697d]415        owl_log_append(m, filename);
[e3a75ed]416        g_free(filename);
[d0961fe]417      }
[839697d]418
[ddbbcffa]419      g_free(temp);
420      g_free(cc->data);
[839697d]421      cc = g_list_delete_link(cc, cc);
[d0961fe]422    }
423  }
[7d4fbcd]424
[ddbbcffa]425  g_free(frombuff);
426  g_free(logpath);
[7d4fbcd]427}
[cc305b5]428
429static gpointer owl_log_thread_func(gpointer data)
430{
431  log_context = g_main_context_new();
432  log_loop = g_main_loop_new(log_context, FALSE);
433  g_main_loop_run(log_loop);
434  return NULL;
435}
436
437void owl_log_init(void) 
438{
439  GError *error = NULL;
440  logging_thread = g_thread_create(owl_log_thread_func,
441                                   NULL,
442                                   TRUE,
443                                   &error);
444  if (error) {
445    endwin();
446    fprintf(stderr, "Error spawning logging thread: %s\n", error->message);
447    fflush(stderr);
448    exit(1);
449  }
450 
451}
452
453static void owl_log_quit_func(gpointer data)
454{
455  g_main_loop_quit(log_loop);
456}
457
458void owl_log_shutdown(void)
459{
460  owl_select_post_task(owl_log_quit_func, NULL,
461                       NULL, log_context);
462  g_thread_join(logging_thread);
463}
Note: See TracBrowser for help on using the repository browser.