source: logging.c @ 421c286f

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 421c286f was 421c286f, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
Normalize and downcase aim names before logging
  • Property mode set to 100644
File size: 11.1 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  /* check for nolog */
206  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
207      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
208
209  /* this is kind of a mess */
210  if (owl_message_is_type_zephyr(m)) {
211    if (owl_message_is_personal(m)) {
212      personal=1;
213      if (!owl_global_is_logging(&g)) return;
214    } else {
215      personal=0;
216      if (!owl_global_is_classlogging(&g)) return;
217    }
218  } else {
219    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
220      personal=1;
221      if (!owl_global_is_logging(&g)) return;
222    } else {
223      personal=0;
224      if (!owl_global_is_classlogging(&g)) return;
225    }
226  }
227
228  if (owl_message_is_type_zephyr(m)) {
229    /* chop off a local realm for personal zephyr messages */
230    if (personal) {
231      if (owl_message_is_type_zephyr(m)) {
232        from=frombuff=owl_strdup(owl_message_get_sender(m));
233        ptr=strchr(frombuff, '@');
234        if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
235          *ptr='\0';
236        }
237      }
238    } else {
239      /* we assume zephyr for now */
240      from=frombuff=owl_strdup(owl_message_get_class(m));
241    }
242  } else if (owl_message_is_type_aim(m)) {
243    /* we do not yet handle chat rooms */
244    char *normalto;
245    normalto=owl_aim_normalize_screenname(owl_message_get_sender(m));
246    downstr(normalto);
247    from=frombuff=owl_sprintf("aim:%s", normalto);
248    owl_free(normalto);
249  } else if (owl_message_is_type_loopback(m)) {
250    from=frombuff=owl_strdup("loopback");
251  } else {
252    from=frombuff=owl_strdup("unknown");
253  }
254 
255  /* check for malicious sender formats */
256  len=strlen(frombuff);
257  if (len<1 || len>35) from="weird";
258  if (strchr(frombuff, '/')) from="weird";
259
260  ch=frombuff[0];
261  if (!isalnum(ch)) from="weird";
262
263  for (i=0; i<len; i++) {
264    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
265  }
266
267  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
268
269  if (!personal) {
270    if (strcmp(from, "weird")) downstr(from);
271  }
272
273  /* create the filename (expanding ~ in path names) */
274  if (personal) {
275    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
276                                  owl_global_get_homedir(&g));
277    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
278    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
279
280  } else {
281    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", 
282                                owl_global_get_homedir(&g));
283
284    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
285  }
286  owl_free(logpath);
287 
288  file=fopen(filename, "a");
289  if (!file) {
290    owl_function_error("Unable to open file for incoming logging");
291    return;
292  }
293
294  allfile=NULL;
295  if (personal) {
296    allfile=fopen(allfilename, "a");
297    if (!allfile) {
298      owl_function_error("Unable to open file for incoming logging");
299      fclose(file);
300      return;
301    }
302  }
303
304  /* write to the main file */
305  if (owl_message_is_type_zephyr(m)) {
306    char *tmp;
307   
308    tmp=short_zuser(owl_message_get_sender(m));
309    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
310    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
311    fprintf(file, "\n");
312    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
313    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
314    buff=owl_malloc(i+10);
315    memcpy(buff, ptr, i);
316    buff[i]='\0';
317    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
318    fprintf(file, "%s\n\n", owl_message_get_body(m));
319    owl_free(tmp);
320  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
321    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
322    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
323    fprintf(file, "%s\n\n", owl_message_get_body(m));
324  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
325    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
326    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
327    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
328    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
329  } else {
330    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
331    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
332    fprintf(file, "%s\n\n", owl_message_get_body(m));
333  }
334
335  fclose(file);
336
337  /* if it's a personal message, also write to the 'all' file */
338  if (personal) {
339    if (owl_message_is_type_zephyr(m)) {
340      char *tmp;
341
342      tmp=short_zuser(owl_message_get_sender(m));
343      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
344      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
345      fprintf(allfile, "\n");
346      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
347      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
348      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
349      owl_free(tmp);
350    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
351      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
352      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
353      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
354    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
355      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
356      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
357      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
358      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
359    } else {
360      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
361      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
362      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
363    }
364    fclose(allfile);
365  }
366
367  if (owl_message_is_type_zephyr(m)) {
368    owl_free(buff);
369  }
370  owl_free(frombuff);
371}
Note: See TracBrowser for help on using the repository browser.