source: logging.c @ 15b34fd

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 15b34fd was 15b34fd, checked in by James M. Kretchmar <kretch@mit.edu>, 16 years ago
Fixed some small memory leaks in logging if files unwriteable If the variable logfilter is set it names a filter. Any messages matching this filter are logged. This is an independent mechanism from the other logging variables. If you want to control all logging with logfilter the other variables must be set to their default (off) settings. [BZ 37] Relatively substantial changes made under the hood to support filter logging. Now have more consistent interfaces to creating messages etc. Still needs more work though.
  • Property mode set to 100644
File size: 12.6 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;
129
130  tobuff=short_zuser(to);
131
132  /* expand ~ in path names */
133  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
134
135  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
136  file=fopen(filename, "a");
137  if (!file) {
138    owl_function_error("Unable to open file for outgoing logging");
139    owl_free(logpath);
140    owl_free(tobuff);
141    return;
142  }
143  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
144  if (text[strlen(text)-1]!='\n') {
145    fprintf(file, "\n");
146  }
147  fclose(file);
148
149  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
150  owl_free(logpath);
151  file=fopen(filename, "a");
152  if (!file) {
153    owl_function_error("Unable to open file for outgoing logging");
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  owl_free(tobuff);
164}
165
166void owl_log_outgoing_aim(owl_message *m)
167{
168  FILE *file;
169  char filename[MAXPATHLEN], *logpath;
170  char *tobuff, *normalto, *text;
171
172  owl_function_debugmsg("owl_log_outgoing_aim: entering");
173
174  /* normalize and downcase the screenname */
175  normalto=owl_aim_normalize_screenname(owl_message_get_recipient(m));
176  downstr(normalto);
177  tobuff=owl_sprintf("aim:%s", normalto);
178  owl_free(normalto);
179
180  /* expand ~ in path names */
181  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
182
183  text=owl_message_get_body(m);
184 
185  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
186  file=fopen(filename, "a");
187  if (!file) {
188    owl_function_error("Unable to open file for outgoing logging");
189    owl_free(logpath);
190    owl_free(tobuff);
191    return;
192  }
193  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
194  if (text[strlen(text)-1]!='\n') {
195    fprintf(file, "\n");
196  }
197  fclose(file);
198
199  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
200  owl_free(logpath);
201  file=fopen(filename, "a");
202  if (!file) {
203    owl_function_error("Unable to open file for outgoing logging");
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  owl_free(tobuff);
214}
215
216void owl_log_outgoing_loopback(owl_message *m)
217{
218  FILE *file;
219  char filename[MAXPATHLEN], *logpath;
220  char *tobuff, *text;
221
222  tobuff=owl_sprintf("loopback");
223
224  /* expand ~ in path names */
225  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
226
227  text=owl_message_get_body(m);
228
229  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
230  file=fopen(filename, "a");
231  if (!file) {
232    owl_function_error("Unable to open file for outgoing logging");
233    owl_free(logpath);
234    owl_free(tobuff);
235    return;
236  }
237  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
238  if (text[strlen(text)-1]!='\n') {
239    fprintf(file, "\n");
240  }
241  fclose(file);
242
243  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
244  owl_free(logpath);
245  file=fopen(filename, "a");
246  if (!file) {
247    owl_function_error("Unable to open file for outgoing logging");
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  owl_free(tobuff);
258}
259
260void owl_log_incoming(owl_message *m)
261{
262  FILE *file, *allfile;
263  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
264  char *frombuff=NULL, *from=NULL, *buff=NULL, *ptr;
265  int len, ch, i, personal;
266
267  /* figure out if it's a "personal" message or not */
268  if (owl_message_is_type_zephyr(m)) {
269    if (owl_message_is_personal(m)) {
270      personal=1;
271    } else {
272      personal=0;
273    }
274  } else {
275    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
276      personal=1;
277    } else {
278      personal=0;
279    }
280  }
281
282  if (owl_message_is_type_zephyr(m)) {
283    if (personal) {
284      if (owl_message_is_type_zephyr(m)) {
285        from=frombuff=short_zuser(owl_message_get_sender(m));
286      }
287    } else {
288      from=frombuff=owl_strdup(owl_message_get_class(m));
289    }
290  } else if (owl_message_is_type_aim(m)) {
291    /* we do not yet handle chat rooms */
292    char *normalto;
293    normalto=owl_aim_normalize_screenname(owl_message_get_sender(m));
294    downstr(normalto);
295    from=frombuff=owl_sprintf("aim:%s", normalto);
296    owl_free(normalto);
297  } else if (owl_message_is_type_loopback(m)) {
298    from=frombuff=owl_strdup("loopback");
299  } else {
300    from=frombuff=owl_strdup("unknown");
301  }
302 
303  /* check for malicious sender formats */
304  len=strlen(frombuff);
305  if (len<1 || len>35) from="weird";
306  if (strchr(frombuff, '/')) from="weird";
307
308  ch=frombuff[0];
309  if (!isalnum(ch)) from="weird";
310
311  for (i=0; i<len; i++) {
312    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
313  }
314
315  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
316
317  if (!personal) {
318    if (strcmp(from, "weird")) downstr(from);
319  }
320
321  /* create the filename (expanding ~ in path names) */
322  if (personal) {
323    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
324    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
325    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
326
327  } else {
328    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", owl_global_get_homedir(&g));
329    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
330  }
331  owl_free(logpath);
332 
333  file=fopen(filename, "a");
334  if (!file) {
335    owl_function_error("Unable to open file for incoming logging");
336    owl_free(frombuff);
337    return;
338  }
339
340  allfile=NULL;
341  if (personal) {
342    allfile=fopen(allfilename, "a");
343    if (!allfile) {
344      owl_function_error("Unable to open file for incoming logging");
345      owl_free(frombuff);
346      fclose(file);
347      return;
348    }
349  }
350
351  /* write to the main file */
352  if (owl_message_is_type_zephyr(m)) {
353    char *tmp;
354   
355    tmp=short_zuser(owl_message_get_sender(m));
356    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
357    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
358    fprintf(file, "\n");
359    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
360    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
361    buff=owl_malloc(i+10);
362    memcpy(buff, ptr, i);
363    buff[i]='\0';
364    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
365    fprintf(file, "%s\n\n", owl_message_get_body(m));
366    owl_free(tmp);
367  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
368    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
369    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
370    fprintf(file, "%s\n\n", owl_message_get_body(m));
371  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
372    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
373    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
374    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
375    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
376  } else {
377    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
378    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
379    fprintf(file, "%s\n\n", owl_message_get_body(m));
380  }
381
382  fclose(file);
383
384  /* if it's a personal message, also write to the 'all' file */
385  if (personal) {
386    if (owl_message_is_type_zephyr(m)) {
387      char *tmp;
388
389      tmp=short_zuser(owl_message_get_sender(m));
390      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
391      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
392      fprintf(allfile, "\n");
393      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
394      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
395      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
396      owl_free(tmp);
397    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
398      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
399      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
400      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
401    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
402      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
403      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
404      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
405      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
406    } else {
407      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
408      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
409      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
410    }
411    fclose(allfile);
412  }
413
414  if (owl_message_is_type_zephyr(m)) {
415    owl_free(buff);
416  }
417  owl_free(frombuff);
418}
Note: See TracBrowser for help on using the repository browser.