source: logging.c @ e5da3fe

release-1.10release-1.8release-1.9
Last change on this file since e5da3fe was e5da3fe, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Create per-recipient copies of outgoing messages for non-CC'd personals Fixes bug #63. [davidben@mit.edu: Fix up comment, rebase to master] [davidben@mit.edu: Address nitpicks]
  • Property mode set to 100644
File size: 10.2 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <sys/param.h>
6
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 */
11void owl_log_message(const owl_message *m) {
12  owl_function_debugmsg("owl_log_message: entering");
13
14  if (m == NULL) {
15    owl_function_debugmsg("owl_log_message: passed null message");
16    return;
17  }
18
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 */
33  owl_log_outgoing(m);
34
35  owl_function_debugmsg("owl_log_message: leaving");
36}
37
38/* Return 1 if we should log the given message, otherwise return 0 */
39int owl_log_shouldlog_message(const owl_message *m) {
40  const owl_filter *f;
41
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);
45
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    }
68  }
69  return(1);
70}
71
72void owl_log_zephyr(const owl_message *m, FILE *file) {
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));
81    g_free(tmp);
82}
83
84void owl_log_aim(const owl_message *m, FILE *file) {
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
95void owl_log_jabber(const owl_message *m, FILE *file) {
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
101void owl_log_generic(const owl_message *m, FILE *file) {
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
107void owl_log_append(const owl_message *m, const char *filename) {
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
126void owl_log_outgoing(const owl_message *m)
127{
128  char *filename, *logpath;
129  char *to, *temp;
130  GList *cc;
131
132  /* expand ~ in path names */
133  logpath = owl_util_makepath(owl_global_get_logpath(&g));
134
135  /* Figure out what path to log to */
136  if (owl_message_is_type_zephyr(m)) {
137    /* If this has CC's, do all but the "recipient" which we'll do below */
138    cc = owl_message_get_cc_without_recipient(m);
139    while (cc != NULL) {
140      temp = short_zuser(cc->data);
141      filename = g_strdup_printf("%s/%s", logpath, temp);
142      owl_log_append(m, filename);
143
144      g_free(filename);
145      g_free(temp);
146      g_free(cc->data);
147      cc = g_list_delete_link(cc, cc);
148    }
149
150    to = short_zuser(owl_message_get_recipient(m));
151  } else if (owl_message_is_type_jabber(m)) {
152    to = g_strdup_printf("jabber:%s", owl_message_get_recipient(m));
153    owl_text_tr(to, '/', '_');
154  } else if (owl_message_is_type_aim(m)) {
155    char *temp2;
156    temp = owl_aim_normalize_screenname(owl_message_get_recipient(m));
157    temp2 = g_utf8_strdown(temp,-1);
158    to = g_strdup_printf("aim:%s", temp2);
159    g_free(temp2);
160    g_free(temp);
161  } else {
162    to = g_strdup("loopback");
163  }
164
165  filename = g_strdup_printf("%s/%s", logpath, to);
166  owl_log_append(m, filename);
167  g_free(to);
168  g_free(filename);
169
170  filename = g_strdup_printf("%s/all", logpath);
171  owl_log_append(m, filename);
172  g_free(logpath);
173  g_free(filename);
174}
175
176
177void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
178{
179  FILE *file;
180  char *filename, *logpath;
181  char *tobuff;
182  owl_message *m;
183
184  /* create a present message so we can pass it to
185   * owl_log_shouldlog_message(void)
186   */
187  m = g_new(owl_message, 1);
188  /* recip_index = 0 because there can only be one recipient anyway */
189  owl_message_create_from_zwrite(m, zw, text, 0);
190  if (!owl_log_shouldlog_message(m)) {
191    owl_message_delete(m);
192    return;
193  }
194  owl_message_delete(m);
195
196  /* chop off a local realm */
197  tobuff = short_zuser(owl_list_get_element(&(zw->recips), 0));
198
199  /* expand ~ in path names */
200  logpath = owl_util_makepath(owl_global_get_logpath(&g));
201
202  filename = g_strdup_printf("%s/%s", logpath, tobuff);
203  file=fopen(filename, "a");
204  g_free(filename);
205  if (!file) {
206    owl_function_error("Unable to open file for outgoing logging");
207    g_free(logpath);
208    g_free(tobuff);
209    return;
210  }
211  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
212  if (text[strlen(text)-1]!='\n') {
213    fprintf(file, "\n");
214  }
215  fclose(file);
216
217  filename = g_strdup_printf("%s/all", logpath);
218  g_free(logpath);
219  file=fopen(filename, "a");
220  g_free(filename);
221  if (!file) {
222    owl_function_error("Unable to open file for outgoing logging");
223    g_free(tobuff);
224    return;
225  }
226  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
227  if (text[strlen(text)-1]!='\n') {
228    fprintf(file, "\n");
229  }
230  fclose(file);
231
232  g_free(tobuff);
233}
234
235void owl_log_incoming(const owl_message *m)
236{
237  char *filename, *allfilename, *logpath;
238  const char *from=NULL;
239  char *frombuff=NULL;
240  int len, ch, i, personal;
241
242  /* figure out if it's a "personal" message or not */
243  if (owl_message_is_type_zephyr(m)) {
244    if (owl_message_is_personal(m)) {
245      personal = 1;
246    } else {
247      personal = 0;
248    }
249  } else if (owl_message_is_type_jabber(m)) {
250    /* This needs to be fixed to handle groupchat */
251    const char* msgtype = owl_message_get_attribute_value(m,"jtype");
252    if (msgtype && !strcmp(msgtype,"groupchat")) {
253      personal = 0;
254    } else {
255      personal = 1;
256    }
257  } else {
258    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
259      personal = 1;
260    } else {
261      personal = 0;
262    }
263  }
264
265
266  if (owl_message_is_type_zephyr(m)) {
267    if (personal) {
268      from=frombuff=short_zuser(owl_message_get_sender(m));
269    } else {
270      from=frombuff=g_strdup(owl_message_get_class(m));
271    }
272  } else if (owl_message_is_type_aim(m)) {
273    /* we do not yet handle chat rooms */
274    char *normalto, *temp;
275    temp = owl_aim_normalize_screenname(owl_message_get_sender(m));
276    normalto = g_utf8_strdown(temp, -1);
277    from=frombuff=g_strdup_printf("aim:%s", normalto);
278    g_free(normalto);
279    g_free(temp);
280  } else if (owl_message_is_type_loopback(m)) {
281    from=frombuff=g_strdup("loopback");
282  } else if (owl_message_is_type_jabber(m)) {
283    if (personal) {
284      from=frombuff=g_strdup_printf("jabber:%s",owl_message_get_sender(m));
285    } else {
286      from=frombuff=g_strdup_printf("jabber:%s",owl_message_get_recipient(m));
287    }
288  } else {
289    from=frombuff=g_strdup("unknown");
290  }
291 
292  /* check for malicious sender formats */
293  len=strlen(frombuff);
294  if (len<1 || len>35) from="weird";
295  if (strchr(frombuff, '/')) from="weird";
296
297  ch=frombuff[0];
298  if (!g_ascii_isalnum(ch)) from="weird";
299
300  for (i=0; i<len; i++) {
301    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
302  }
303
304  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
305
306  if (!personal) {
307    if (strcmp(from, "weird")) {
308      char* temp = g_utf8_strdown(frombuff, -1);
309      if (temp) {
310        g_free(frombuff);
311        from = frombuff = temp;
312      }
313    }
314  }
315
316  /* create the filename (expanding ~ in path names) */
317  if (personal) {
318    logpath = owl_util_makepath(owl_global_get_logpath(&g));
319    filename = g_strdup_printf("%s/%s", logpath, from);
320    allfilename = g_strdup_printf("%s/all", logpath);
321    owl_log_append(m, allfilename);
322    g_free(allfilename);
323  } else {
324    logpath = owl_util_makepath(owl_global_get_classlogpath(&g));
325    filename = g_strdup_printf("%s/%s", logpath, from);
326  }
327
328  owl_log_append(m, filename);
329  g_free(filename);
330
331  if (personal && owl_message_is_type_zephyr(m)) {
332    /* We want to log to all of the CC'd people who were not us, or
333     * the sender, as well.
334     */
335    char *temp;
336    GList *cc;
337    cc = owl_message_get_cc_without_recipient(m);
338    while (cc != NULL) {
339      temp = short_zuser(cc->data);
340      if (strcasecmp(temp, frombuff) != 0) {
341        filename = g_strdup_printf("%s/%s", logpath, temp);
342        owl_log_append(m, filename);
343        g_free(filename);
344      }
345
346      g_free(temp);
347      g_free(cc->data);
348      cc = g_list_delete_link(cc, cc);
349    }
350  }
351
352  g_free(frombuff);
353  g_free(logpath);
354}
Note: See TracBrowser for help on using the repository browser.