source: logging.c @ 180cd15

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 180cd15 was 180cd15, checked in by James M. Kretchmar <kretch@mit.edu>, 16 years ago
Now deals with the special case of logging failed zwrites correctly
  • Property mode set to 100644
File size: 13.0 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <sys/param.h>
6
7static const char fileIdent[] = "$Id$";
8
9/* This is now the one function that should be called to log a
10 * message.  It will do all the work necessary by calling the other
11 * functions in this file as necessary.
12 */
13void owl_log_message(owl_message *m) {
14  owl_function_debugmsg("owl_log_message: entering");
15
16  /* should we be logging this message? */
17  if (!owl_log_shouldlog_message(m)) {
18    owl_function_debugmsg("owl_log_message: not logging message");
19    return;
20  }
21
22  /* handle incmoing messages */
23  if (owl_message_is_direction_in(m)) {
24    owl_log_incoming(m);
25    owl_function_debugmsg("owl_log_message: leaving");
26    return;
27  }
28
29  /* handle outgoing messages */
30  if (owl_message_is_type_aim(m)) {
31    owl_log_outgoing_aim(m);
32  } else if (owl_message_is_type_zephyr(m)) {
33    owl_log_outgoing_zephyr(m);
34  } else if (owl_message_is_type_loopback(m)) {
35    owl_log_outgoing_loopback(m);
36  } else {
37    owl_function_error("Unknown message type for logging");
38  }
39  owl_function_debugmsg("owl_log_message: leaving");
40}
41
42/* Return 1 if we should log the given message, otherwise return 0 */
43int owl_log_shouldlog_message(owl_message *m) {
44  owl_filter *f;
45
46  /* If there's a logfilter and this message matches it, log */
47  f=owl_global_get_filter(&g, owl_global_get_logfilter(&g));
48  if (f && owl_filter_message_match(f, m)) return(1);
49
50  /* otherwise we do things based on the logging variables */
51
52  /* skip login/logout messages if appropriate */
53  if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0);
54     
55  /* check for nolog */
56  if (!strcasecmp(owl_message_get_opcode(m), "nolog") || !strcasecmp(owl_message_get_instance(m), "nolog")) 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    }
75  }
76  return(1);
77}
78
79void owl_log_outgoing_zephyr(owl_message *m)
80{
81  FILE *file;
82  char filename[MAXPATHLEN], *logpath;
83  char *to, *text;
84
85  /* strip local realm */
86  to=short_zuser(owl_message_get_recipient(m));
87
88  /* expand ~ in path names */
89  logpath=owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
90
91  text=owl_message_get_body(m);
92
93  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, to);
94  file=fopen(filename, "a");
95  if (!file) {
96    owl_function_error("Unable to open file for outgoing logging");
97    owl_free(logpath);
98    owl_free(to);
99    return;
100  }
101  fprintf(file, "OUTGOING (owl): %s\n%s\n", to, text);
102  if (text[strlen(text)-1]!='\n') {
103    fprintf(file, "\n");
104  }
105  fclose(file);
106
107  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
108  owl_free(logpath);
109  file=fopen(filename, "a");
110  if (!file) {
111    owl_function_error("Unable to open file for outgoing logging");
112    owl_free(to);
113    return;
114  }
115  fprintf(file, "OUTGOING (owl): %s\n%s\n", to, text);
116  if (text[strlen(text)-1]!='\n') {
117    fprintf(file, "\n");
118  }
119  fclose(file);
120
121  owl_free(to);
122}
123
124void owl_log_outgoing_zephyr_error(char *to, char *text)
125{
126  FILE *file;
127  char filename[MAXPATHLEN], *logpath;
128  char *tobuff, *zwriteline;
129  owl_message *m;
130
131  /* create a present message so we can pass it to
132   * owl_log_shouldlog_message()
133   */
134  zwriteline=owl_sprintf("zwrite %s", to);
135  m=owl_function_make_outgoing_zephyr(text, zwriteline, "");
136  owl_free(zwriteline);
137  if (!owl_log_shouldlog_message(m)) {
138    owl_message_free(m);
139    return;
140  }
141  owl_message_free(m);
142
143  /* chop off a local realm */
144  tobuff=short_zuser(to);
145
146  /* expand ~ in path names */
147  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
148
149  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
150  file=fopen(filename, "a");
151  if (!file) {
152    owl_function_error("Unable to open file for outgoing logging");
153    owl_free(logpath);
154    owl_free(tobuff);
155    return;
156  }
157  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
158  if (text[strlen(text)-1]!='\n') {
159    fprintf(file, "\n");
160  }
161  fclose(file);
162
163  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
164  owl_free(logpath);
165  file=fopen(filename, "a");
166  if (!file) {
167    owl_function_error("Unable to open file for outgoing logging");
168    owl_free(tobuff);
169    return;
170  }
171  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
172  if (text[strlen(text)-1]!='\n') {
173    fprintf(file, "\n");
174  }
175  fclose(file);
176
177  owl_free(tobuff);
178}
179
180void owl_log_outgoing_aim(owl_message *m)
181{
182  FILE *file;
183  char filename[MAXPATHLEN], *logpath;
184  char *tobuff, *normalto, *text;
185
186  owl_function_debugmsg("owl_log_outgoing_aim: entering");
187
188  /* normalize and downcase the screenname */
189  normalto=owl_aim_normalize_screenname(owl_message_get_recipient(m));
190  downstr(normalto);
191  tobuff=owl_sprintf("aim:%s", normalto);
192  owl_free(normalto);
193
194  /* expand ~ in path names */
195  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
196
197  text=owl_message_get_body(m);
198 
199  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
200  file=fopen(filename, "a");
201  if (!file) {
202    owl_function_error("Unable to open file for outgoing logging");
203    owl_free(logpath);
204    owl_free(tobuff);
205    return;
206  }
207  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
208  if (text[strlen(text)-1]!='\n') {
209    fprintf(file, "\n");
210  }
211  fclose(file);
212
213  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
214  owl_free(logpath);
215  file=fopen(filename, "a");
216  if (!file) {
217    owl_function_error("Unable to open file for outgoing logging");
218    owl_free(tobuff);
219    return;
220  }
221  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
222  if (text[strlen(text)-1]!='\n') {
223    fprintf(file, "\n");
224  }
225  fclose(file);
226
227  owl_free(tobuff);
228}
229
230void owl_log_outgoing_loopback(owl_message *m)
231{
232  FILE *file;
233  char filename[MAXPATHLEN], *logpath;
234  char *tobuff, *text;
235
236  tobuff=owl_sprintf("loopback");
237
238  /* expand ~ in path names */
239  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
240
241  text=owl_message_get_body(m);
242
243  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
244  file=fopen(filename, "a");
245  if (!file) {
246    owl_function_error("Unable to open file for outgoing logging");
247    owl_free(logpath);
248    owl_free(tobuff);
249    return;
250  }
251  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
252  if (text[strlen(text)-1]!='\n') {
253    fprintf(file, "\n");
254  }
255  fclose(file);
256
257  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
258  owl_free(logpath);
259  file=fopen(filename, "a");
260  if (!file) {
261    owl_function_error("Unable to open file for outgoing logging");
262    owl_free(tobuff);
263    return;
264  }
265  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
266  if (text[strlen(text)-1]!='\n') {
267    fprintf(file, "\n");
268  }
269  fclose(file);
270
271  owl_free(tobuff);
272}
273
274void owl_log_incoming(owl_message *m)
275{
276  FILE *file, *allfile;
277  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
278  char *frombuff=NULL, *from=NULL, *buff=NULL, *ptr;
279  int len, ch, i, personal;
280
281  /* figure out if it's a "personal" message or not */
282  if (owl_message_is_type_zephyr(m)) {
283    if (owl_message_is_personal(m)) {
284      personal=1;
285    } else {
286      personal=0;
287    }
288  } else {
289    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
290      personal=1;
291    } else {
292      personal=0;
293    }
294  }
295
296  if (owl_message_is_type_zephyr(m)) {
297    if (personal) {
298      if (owl_message_is_type_zephyr(m)) {
299        from=frombuff=short_zuser(owl_message_get_sender(m));
300      }
301    } else {
302      from=frombuff=owl_strdup(owl_message_get_class(m));
303    }
304  } else if (owl_message_is_type_aim(m)) {
305    /* we do not yet handle chat rooms */
306    char *normalto;
307    normalto=owl_aim_normalize_screenname(owl_message_get_sender(m));
308    downstr(normalto);
309    from=frombuff=owl_sprintf("aim:%s", normalto);
310    owl_free(normalto);
311  } else if (owl_message_is_type_loopback(m)) {
312    from=frombuff=owl_strdup("loopback");
313  } else {
314    from=frombuff=owl_strdup("unknown");
315  }
316 
317  /* check for malicious sender formats */
318  len=strlen(frombuff);
319  if (len<1 || len>35) from="weird";
320  if (strchr(frombuff, '/')) from="weird";
321
322  ch=frombuff[0];
323  if (!isalnum(ch)) from="weird";
324
325  for (i=0; i<len; i++) {
326    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
327  }
328
329  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
330
331  if (!personal) {
332    if (strcmp(from, "weird")) downstr(from);
333  }
334
335  /* create the filename (expanding ~ in path names) */
336  if (personal) {
337    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
338    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
339    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
340
341  } else {
342    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", owl_global_get_homedir(&g));
343    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
344  }
345  owl_free(logpath);
346 
347  file=fopen(filename, "a");
348  if (!file) {
349    owl_function_error("Unable to open file for incoming logging");
350    owl_free(frombuff);
351    return;
352  }
353
354  allfile=NULL;
355  if (personal) {
356    allfile=fopen(allfilename, "a");
357    if (!allfile) {
358      owl_function_error("Unable to open file for incoming logging");
359      owl_free(frombuff);
360      fclose(file);
361      return;
362    }
363  }
364
365  /* write to the main file */
366  if (owl_message_is_type_zephyr(m)) {
367    char *tmp;
368   
369    tmp=short_zuser(owl_message_get_sender(m));
370    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
371    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
372    fprintf(file, "\n");
373    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
374    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
375    buff=owl_malloc(i+10);
376    memcpy(buff, ptr, i);
377    buff[i]='\0';
378    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
379    fprintf(file, "%s\n\n", owl_message_get_body(m));
380    owl_free(tmp);
381  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
382    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
383    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
384    fprintf(file, "%s\n\n", owl_message_get_body(m));
385  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
386    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
387    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
388    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
389    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
390  } else {
391    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
392    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
393    fprintf(file, "%s\n\n", owl_message_get_body(m));
394  }
395
396  fclose(file);
397
398  /* if it's a personal message, also write to the 'all' file */
399  if (personal) {
400    if (owl_message_is_type_zephyr(m)) {
401      char *tmp;
402
403      tmp=short_zuser(owl_message_get_sender(m));
404      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
405      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
406      fprintf(allfile, "\n");
407      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
408      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
409      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
410      owl_free(tmp);
411    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
412      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
413      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
414      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
415    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
416      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
417      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
418      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
419      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
420    } else {
421      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
422      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
423      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
424    }
425    fclose(allfile);
426  }
427
428  if (owl_message_is_type_zephyr(m)) {
429    owl_free(buff);
430  }
431  owl_free(frombuff);
432}
Note: See TracBrowser for help on using the repository browser.