source: logging.c @ e22f27c

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since e22f27c was e22f27c, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
The 'loglogins' variable now controls whether login/logout messages are logged. It is off by default. For now this affects only AIM messages, later zephyr login/logout messages will also be logged if this is set to 'on'
  • Property mode set to 100644
File size: 11.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
7static const char fileIdent[] = "$Id$";
8
9void owl_log_outgoing_zephyr(char *to, char *text)
10{
11  FILE *file;
12  char filename[MAXPATHLEN], *logpath;
13  char *tobuff, *ptr="";
14
15  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return;
16
17  tobuff=owl_malloc(strlen(to)+20);
18  strcpy(tobuff, to);
19
20  /* chop off a local realm */
21  ptr=strchr(tobuff, '@');
22  if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
23    *ptr='\0';
24  }
25
26  /* expand ~ in path names */
27  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
28                                owl_global_get_homedir(&g));
29
30  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
31  file=fopen(filename, "a");
32  if (!file) {
33    owl_function_error("Unable to open file for outgoing logging");
34    owl_free(logpath);
35    return;
36  }
37  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
38  if (text[strlen(text)-1]!='\n') {
39    fprintf(file, "\n");
40  }
41  fclose(file);
42
43  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
44  owl_free(logpath);
45  file=fopen(filename, "a");
46  if (!file) {
47    owl_function_error("Unable to open file for outgoing logging");
48    return;
49  }
50  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
51  if (text[strlen(text)-1]!='\n') {
52    fprintf(file, "\n");
53  }
54  fclose(file);
55
56  owl_free(tobuff);
57}
58
59void owl_log_outgoing_zephyr_error(char *to, char *text)
60{
61  FILE *file;
62  char filename[MAXPATHLEN], *logpath;
63  char *tobuff, *ptr="";
64
65  tobuff=owl_malloc(strlen(to)+20);
66  strcpy(tobuff, to);
67
68  /* chop off a local realm */
69  ptr=strchr(tobuff, '@');
70  if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
71    *ptr='\0';
72  }
73
74  /* expand ~ in path names */
75  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
76                                owl_global_get_homedir(&g));
77
78  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
79  file=fopen(filename, "a");
80  if (!file) {
81    owl_function_error("Unable to open file for outgoing logging");
82    owl_free(logpath);
83    return;
84  }
85  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
86  if (text[strlen(text)-1]!='\n') {
87    fprintf(file, "\n");
88  }
89  fclose(file);
90
91  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
92  owl_free(logpath);
93  file=fopen(filename, "a");
94  if (!file) {
95    owl_function_error("Unable to open file for outgoing logging");
96    return;
97  }
98  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
99  if (text[strlen(text)-1]!='\n') {
100    fprintf(file, "\n");
101  }
102  fclose(file);
103
104  owl_free(tobuff);
105}
106
107void owl_log_outgoing_aim(char *to, char *text)
108{
109  FILE *file;
110  char filename[MAXPATHLEN], *logpath;
111  char *tobuff, *normalto;
112
113  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return;
114
115  normalto=owl_aim_normalize_screenname(to);
116  downstr(normalto);
117  tobuff=owl_sprintf("aim:%s", normalto);
118  owl_free(normalto);
119
120  /* expand ~ in path names */
121  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
122                                owl_global_get_homedir(&g));
123
124  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
125  file=fopen(filename, "a");
126  if (!file) {
127    owl_function_error("Unable to open file for outgoing logging");
128    owl_free(logpath);
129    return;
130  }
131  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
132  if (text[strlen(text)-1]!='\n') {
133    fprintf(file, "\n");
134  }
135  fclose(file);
136
137  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
138  owl_free(logpath);
139  file=fopen(filename, "a");
140  if (!file) {
141    owl_function_error("Unable to open file for outgoing logging");
142    return;
143  }
144  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
145  if (text[strlen(text)-1]!='\n') {
146    fprintf(file, "\n");
147  }
148  fclose(file);
149
150  owl_free(tobuff);
151}
152
153void owl_log_outgoing_loopback(char *text)
154{
155  FILE *file;
156  char filename[MAXPATHLEN], *logpath;
157  char *tobuff;
158
159  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return;
160
161  tobuff=owl_sprintf("loopback");
162
163  /* expand ~ in path names */
164  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
165                                owl_global_get_homedir(&g));
166
167  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
168  file=fopen(filename, "a");
169  if (!file) {
170    owl_function_error("Unable to open file for outgoing logging");
171    owl_free(logpath);
172    return;
173  }
174  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
175  if (text[strlen(text)-1]!='\n') {
176    fprintf(file, "\n");
177  }
178  fclose(file);
179
180  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
181  owl_free(logpath);
182  file=fopen(filename, "a");
183  if (!file) {
184    owl_function_error("Unable to open file for outgoing logging");
185    return;
186  }
187  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
188  if (text[strlen(text)-1]!='\n') {
189    fprintf(file, "\n");
190  }
191  fclose(file);
192
193  owl_free(tobuff);
194}
195
196void owl_log_incoming(owl_message *m)
197{
198  FILE *file, *allfile;
199  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
200  char *frombuff=NULL, *from=NULL, *buff=NULL, *ptr;
201  int len, ch, i, personal;
202
203  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) return;
204
205  /* skip login/logout messages if appropriate */
206  if (!owl_global_is__loglogins(&g) && owl_message_is_loginout(m)) return;
207     
208  /* check for nolog */
209  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
210      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
211
212  /* this is kind of a mess */
213  if (owl_message_is_type_zephyr(m)) {
214    if (owl_message_is_personal(m)) {
215      personal=1;
216      if (!owl_global_is_logging(&g)) return;
217    } else {
218      personal=0;
219      if (!owl_global_is_classlogging(&g)) return;
220    }
221  } else {
222    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
223      personal=1;
224      if (!owl_global_is_logging(&g)) return;
225    } else {
226      personal=0;
227      if (!owl_global_is_classlogging(&g)) return;
228    }
229  }
230
231  if (owl_message_is_type_zephyr(m)) {
232    /* chop off a local realm for personal zephyr messages */
233    if (personal) {
234      if (owl_message_is_type_zephyr(m)) {
235        from=frombuff=owl_strdup(owl_message_get_sender(m));
236        ptr=strchr(frombuff, '@');
237        if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
238          *ptr='\0';
239        }
240      }
241    } else {
242      /* we assume zephyr for now */
243      from=frombuff=owl_strdup(owl_message_get_class(m));
244    }
245  } else if (owl_message_is_type_aim(m)) {
246    /* we do not yet handle chat rooms */
247    char *normalto;
248    normalto=owl_aim_normalize_screenname(owl_message_get_sender(m));
249    downstr(normalto);
250    from=frombuff=owl_sprintf("aim:%s", normalto);
251    owl_free(normalto);
252  } else if (owl_message_is_type_loopback(m)) {
253    from=frombuff=owl_strdup("loopback");
254  } else {
255    from=frombuff=owl_strdup("unknown");
256  }
257 
258  /* check for malicious sender formats */
259  len=strlen(frombuff);
260  if (len<1 || len>35) from="weird";
261  if (strchr(frombuff, '/')) from="weird";
262
263  ch=frombuff[0];
264  if (!isalnum(ch)) from="weird";
265
266  for (i=0; i<len; i++) {
267    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
268  }
269
270  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
271
272  if (!personal) {
273    if (strcmp(from, "weird")) downstr(from);
274  }
275
276  /* create the filename (expanding ~ in path names) */
277  if (personal) {
278    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
279                                  owl_global_get_homedir(&g));
280    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
281    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
282
283  } else {
284    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", 
285                                owl_global_get_homedir(&g));
286
287    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
288  }
289  owl_free(logpath);
290 
291  file=fopen(filename, "a");
292  if (!file) {
293    owl_function_error("Unable to open file for incoming logging");
294    return;
295  }
296
297  allfile=NULL;
298  if (personal) {
299    allfile=fopen(allfilename, "a");
300    if (!allfile) {
301      owl_function_error("Unable to open file for incoming logging");
302      fclose(file);
303      return;
304    }
305  }
306
307  /* write to the main file */
308  if (owl_message_is_type_zephyr(m)) {
309    char *tmp;
310   
311    tmp=short_zuser(owl_message_get_sender(m));
312    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
313    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
314    fprintf(file, "\n");
315    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
316    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
317    buff=owl_malloc(i+10);
318    memcpy(buff, ptr, i);
319    buff[i]='\0';
320    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
321    fprintf(file, "%s\n\n", owl_message_get_body(m));
322    owl_free(tmp);
323  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
324    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
325    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
326    fprintf(file, "%s\n\n", owl_message_get_body(m));
327  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
328    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
329    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
330    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
331    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
332  } else {
333    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
334    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
335    fprintf(file, "%s\n\n", owl_message_get_body(m));
336  }
337
338  fclose(file);
339
340  /* if it's a personal message, also write to the 'all' file */
341  if (personal) {
342    if (owl_message_is_type_zephyr(m)) {
343      char *tmp;
344
345      tmp=short_zuser(owl_message_get_sender(m));
346      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
347      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
348      fprintf(allfile, "\n");
349      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
350      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
351      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
352      owl_free(tmp);
353    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
354      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
355      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
356      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
357    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
358      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
359      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
360      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
361      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
362    } else {
363      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
364      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
365      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
366    }
367    fclose(allfile);
368  }
369
370  if (owl_message_is_type_zephyr(m)) {
371    owl_free(buff);
372  }
373  owl_free(frombuff);
374}
Note: See TracBrowser for help on using the repository browser.