source: logging.c @ 7659079

release-1.10release-1.8release-1.9
Last change on this file since 7659079 was 60d7935, checked in by Anders Kaseorg <andersk@mit.edu>, 14 years ago
Use owl_util_makepath instead of faking ~ expansion by owl_text_substitute Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 10.0 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <sys/param.h>
6
[15b34fd]7/* This is now the one function that should be called to log a
8 * message.  It will do all the work necessary by calling the other
9 * functions in this file as necessary.
10 */
[c08c70a]11void owl_log_message(const owl_message *m) {
[15b34fd]12  owl_function_debugmsg("owl_log_message: entering");
13
[3c7d086a]14  if (m == NULL) {
15    owl_function_debugmsg("owl_log_message: passed null message");
16    return;
17  }
18
[15b34fd]19  /* should we be logging this message? */
20  if (!owl_log_shouldlog_message(m)) {
21    owl_function_debugmsg("owl_log_message: not logging message");
22    return;
23  }
24
25  /* handle incmoing messages */
26  if (owl_message_is_direction_in(m)) {
27    owl_log_incoming(m);
28    owl_function_debugmsg("owl_log_message: leaving");
29    return;
30  }
31
32  /* handle outgoing messages */
[42947f1]33  owl_log_outgoing(m);
34
[15b34fd]35  owl_function_debugmsg("owl_log_message: leaving");
36}
[7d4fbcd]37
[15b34fd]38/* Return 1 if we should log the given message, otherwise return 0 */
[c08c70a]39int owl_log_shouldlog_message(const owl_message *m) {
[4542047]40  const owl_filter *f;
[12c35df]41
[15b34fd]42  /* If there's a logfilter and this message matches it, log */
43  f=owl_global_get_filter(&g, owl_global_get_logfilter(&g));
44  if (f && owl_filter_message_match(f, m)) return(1);
[7d4fbcd]45
[15b34fd]46  /* otherwise we do things based on the logging variables */
47
48  /* skip login/logout messages if appropriate */
49  if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0);
50     
51  /* check direction */
52  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) {
53    return(0);
54  }
55  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) && owl_message_is_direction_in(m)) {
56    return(0);
57  }
58
59  if (owl_message_is_type_zephyr(m)) {
60    if (owl_message_is_personal(m) && !owl_global_is_logging(&g)) return(0);
61    if (!owl_message_is_personal(m) && !owl_global_is_classlogging(&g)) return(0);
62  } else {
63    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
64      if (!owl_global_is_logging(&g)) return(0);
65    } else {
66      if (!owl_global_is_classlogging(&g)) return(0);
67    }
[7d4fbcd]68  }
[15b34fd]69  return(1);
70}
71
[c08c70a]72void owl_log_zephyr(const owl_message *m, FILE *file) {
[42947f1]73    char *tmp;
74    tmp=short_zuser(owl_message_get_sender(m));
75    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
76    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
77    fprintf(file, "\n");
78    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
79    fprintf(file, "From: %s <%s>\n\n", owl_message_get_zsig(m), tmp);
80    fprintf(file, "%s\n\n", owl_message_get_body(m));
[ddbbcffa]81    g_free(tmp);
[42947f1]82}
83
[c08c70a]84void owl_log_aim(const owl_message *m, FILE *file) {
[42947f1]85    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
86    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
87    if (owl_message_is_login(m))
88        fprintf(file, "LOGIN\n\n");
89    else if (owl_message_is_logout(m))
90        fprintf(file, "LOGOUT\n\n");
91    else
92        fprintf(file, "%s\n\n", owl_message_get_body(m));
93}
94
[c08c70a]95void owl_log_jabber(const owl_message *m, FILE *file) {
[42947f1]96    fprintf(file, "From: <%s> To: <%s>\n",owl_message_get_sender(m), owl_message_get_recipient(m));
97    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
98    fprintf(file, "%s\n\n",owl_message_get_body(m));
99}
100
[c08c70a]101void owl_log_generic(const owl_message *m, FILE *file) {
[42947f1]102    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
103    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
104    fprintf(file, "%s\n\n", owl_message_get_body(m));
105}
106
[c08c70a]107void owl_log_append(const owl_message *m, const char *filename) {
[42947f1]108    FILE *file;
109    file=fopen(filename, "a");
110    if (!file) {
111        owl_function_error("Unable to open file for logging");
112        return;
113    }
114    if (owl_message_is_type_zephyr(m)) {
115        owl_log_zephyr(m, file);
116    } else if (owl_message_is_type_jabber(m)) {
117        owl_log_jabber(m, file);
118    } else if (owl_message_is_type_aim(m)) {
119        owl_log_aim(m, file);
120    } else {
121        owl_log_generic(m, file);
122    }
123    fclose(file);
124}
125
[c08c70a]126void owl_log_outgoing(const owl_message *m)
[15b34fd]127{
128  char filename[MAXPATHLEN], *logpath;
[9c590d4]129  char *to, *temp;
[839697d]130  GList *cc;
[9c590d4]131
132  /* expand ~ in path names */
[60d7935]133  logpath = owl_util_makepath(owl_global_get_logpath(&g));
[15b34fd]134
[42947f1]135  /* Figure out what path to log to */
136  if (owl_message_is_type_zephyr(m)) {
[af1920fd]137    /* If this has CC's, do all but the "recipient" which we'll do below */
[839697d]138    cc = owl_message_get_cc_without_recipient(m);
139    while (cc != NULL) {
140      temp = short_zuser(cc->data);
141      snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp);
142      owl_log_append(m, filename);
143
[ddbbcffa]144      g_free(temp);
145      g_free(cc->data);
[839697d]146      cc = g_list_delete_link(cc, cc);
[9c590d4]147    }
[839697d]148
[9c590d4]149    to = short_zuser(owl_message_get_recipient(m));
[42947f1]150  } else if (owl_message_is_type_jabber(m)) {
[3472845]151    to = g_strdup_printf("jabber:%s", owl_message_get_recipient(m));
[d8671a1]152    owl_text_tr(to, '/', '_');
[42947f1]153  } else if (owl_message_is_type_aim(m)) {
[28ee32b]154    char *temp2;
[9c590d4]155    temp = owl_aim_normalize_screenname(owl_message_get_recipient(m));
[28ee32b]156    temp2 = g_utf8_strdown(temp,-1);
[3472845]157    to = g_strdup_printf("aim:%s", temp2);
[ddbbcffa]158    g_free(temp2);
159    g_free(temp);
[42947f1]160  } else {
[d4927a7]161    to = g_strdup("loopback");
[42947f1]162  }
[7d4fbcd]163
[15b34fd]164  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, to);
[42947f1]165  owl_log_append(m, filename);
[ddbbcffa]166  g_free(to);
[7d4fbcd]167
[e1c4636]168  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
[42947f1]169  owl_log_append(m, filename);
[ddbbcffa]170  g_free(logpath);
[7d4fbcd]171}
172
[42947f1]173
[24ccc01]174void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
[2b86d14]175{
176  FILE *file;
177  char filename[MAXPATHLEN], *logpath;
[e2ebf39]178  char *tobuff;
[180cd15]179  owl_message *m;
180
181  /* create a present message so we can pass it to
[c79a047]182   * owl_log_shouldlog_message(void)
[180cd15]183   */
[96828e4]184  m = g_new(owl_message, 1);
[24ccc01]185  owl_message_create_from_zwrite(m, zw, text);
[180cd15]186  if (!owl_log_shouldlog_message(m)) {
[91634ec]187    owl_message_delete(m);
[180cd15]188    return;
189  }
[91634ec]190  owl_message_delete(m);
[2b86d14]191
[180cd15]192  /* chop off a local realm */
[24ccc01]193  tobuff = short_zuser(owl_list_get_element(&(zw->recips), 0));
[2b86d14]194
195  /* expand ~ in path names */
[60d7935]196  logpath = owl_util_makepath(owl_global_get_logpath(&g));
[2b86d14]197
198  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
199  file=fopen(filename, "a");
200  if (!file) {
201    owl_function_error("Unable to open file for outgoing logging");
[ddbbcffa]202    g_free(logpath);
203    g_free(tobuff);
[2b86d14]204    return;
205  }
206  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
207  if (text[strlen(text)-1]!='\n') {
208    fprintf(file, "\n");
209  }
210  fclose(file);
211
212  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
[ddbbcffa]213  g_free(logpath);
[2b86d14]214  file=fopen(filename, "a");
215  if (!file) {
216    owl_function_error("Unable to open file for outgoing logging");
[ddbbcffa]217    g_free(tobuff);
[2b86d14]218    return;
219  }
220  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
221  if (text[strlen(text)-1]!='\n') {
222    fprintf(file, "\n");
223  }
224  fclose(file);
225
[ddbbcffa]226  g_free(tobuff);
[2b86d14]227}
228
[c08c70a]229void owl_log_incoming(const owl_message *m)
[15283bb]230{
[e1c4636]231  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
[e19eb97]232  const char *from=NULL;
[65b2173]233  char *frombuff=NULL;
[7d4fbcd]234  int len, ch, i, personal;
[12c35df]235
[15b34fd]236  /* figure out if it's a "personal" message or not */
[aac889a]237  if (owl_message_is_type_zephyr(m)) {
238    if (owl_message_is_personal(m)) {
[42947f1]239      personal = 1;
[aac889a]240    } else {
[42947f1]241      personal = 0;
[aac889a]242    }
[2182be3]243  } else if (owl_message_is_type_jabber(m)) {
[42947f1]244    /* This needs to be fixed to handle groupchat */
[e19eb97]245    const char* msgtype = owl_message_get_attribute_value(m,"jtype");
[42947f1]246    if (msgtype && !strcmp(msgtype,"groupchat")) {
247      personal = 0;
248    } else {
249      personal = 1;
250    }
[7d4fbcd]251  } else {
[79a0e82]252    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
[42947f1]253      personal = 1;
[aac889a]254    } else {
[42947f1]255      personal = 0;
[aac889a]256    }
[7d4fbcd]257  }
258
[2182be3]259
[aac889a]260  if (owl_message_is_type_zephyr(m)) {
261    if (personal) {
[3066d23]262      from=frombuff=short_zuser(owl_message_get_sender(m));
[aac889a]263    } else {
[d4927a7]264      from=frombuff=g_strdup(owl_message_get_class(m));
[7d4fbcd]265    }
[aac889a]266  } else if (owl_message_is_type_aim(m)) {
267    /* we do not yet handle chat rooms */
[28ee32b]268    char *normalto, *temp;
269    temp = owl_aim_normalize_screenname(owl_message_get_sender(m));
270    normalto = g_utf8_strdown(temp, -1);
[3472845]271    from=frombuff=g_strdup_printf("aim:%s", normalto);
[ddbbcffa]272    g_free(normalto);
273    g_free(temp);
[37eab7f]274  } else if (owl_message_is_type_loopback(m)) {
[d4927a7]275    from=frombuff=g_strdup("loopback");
[2182be3]276  } else if (owl_message_is_type_jabber(m)) {
[5c30091]277    if (personal) {
[3472845]278      from=frombuff=g_strdup_printf("jabber:%s",owl_message_get_sender(m));
[5c30091]279    } else {
[3472845]280      from=frombuff=g_strdup_printf("jabber:%s",owl_message_get_recipient(m));
[5c30091]281    }
[e6449bc]282  } else {
[d4927a7]283    from=frombuff=g_strdup("unknown");
[7d4fbcd]284  }
285 
286  /* check for malicious sender formats */
287  len=strlen(frombuff);
288  if (len<1 || len>35) from="weird";
289  if (strchr(frombuff, '/')) from="weird";
290
291  ch=frombuff[0];
[28ee32b]292  if (!g_ascii_isalnum(ch)) from="weird";
[7d4fbcd]293
294  for (i=0; i<len; i++) {
[e1c4636]295    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
[7d4fbcd]296  }
297
298  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
299
300  if (!personal) {
[28ee32b]301    if (strcmp(from, "weird")) {
302      char* temp = g_utf8_strdown(frombuff, -1);
303      if (temp) {
[ddbbcffa]304        g_free(frombuff);
[28ee32b]305        from = frombuff = temp;
306      }
307    }
[7d4fbcd]308  }
309
[e1c4636]310  /* create the filename (expanding ~ in path names) */
[7d4fbcd]311  if (personal) {
[60d7935]312    logpath = owl_util_makepath(owl_global_get_logpath(&g));
[e1c4636]313    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
314    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
[d0961fe]315    owl_log_append(m, allfilename);
[e1c4636]316
[7d4fbcd]317  } else {
[60d7935]318    logpath = owl_util_makepath(owl_global_get_classlogpath(&g));
[e1c4636]319    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
[7d4fbcd]320  }
[37eab7f]321
[42947f1]322  owl_log_append(m, filename);
[7d4fbcd]323
[d0961fe]324  if (personal && owl_message_is_type_zephyr(m)) {
[af1920fd]325    /* We want to log to all of the CC'd people who were not us, or
326     * the sender, as well.
327     */
[839697d]328    char *temp;
329    GList *cc;
[d0961fe]330    cc = owl_message_get_cc_without_recipient(m);
[839697d]331    while (cc != NULL) {
332      temp = short_zuser(cc->data);
333      if (strcasecmp(temp, frombuff) != 0) {
334        snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp);
335        owl_log_append(m, filename);
[d0961fe]336      }
[839697d]337
[ddbbcffa]338      g_free(temp);
339      g_free(cc->data);
[839697d]340      cc = g_list_delete_link(cc, cc);
[d0961fe]341    }
342  }
[7d4fbcd]343
[ddbbcffa]344  g_free(frombuff);
345  g_free(logpath);
[7d4fbcd]346}
Note: See TracBrowser for help on using the repository browser.