source: logging.c @ 723c427

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 723c427 was 723c427, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
another logging fix
  • Property mode set to 100644
File size: 7.7 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  FILE *file;
11  char filename[MAXPATHLEN], *logpath;
12  char *tobuff, *ptr;
13
14  tobuff=owl_malloc(strlen(to)+20);
15  strcpy(tobuff, to);
16
17  /* chop off a local realm */
18  ptr=strchr(tobuff, '@');
19  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
20    *ptr='\0';
21  }
22
23  /* expand ~ in path names */
24  logpath = owl_util_substitute(owl_global_get_logpath(&g), "~", 
25                                owl_global_get_homedir(&g));
26
27  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
28  file=fopen(filename, "a");
29  if (!file) {
30    owl_function_makemsg("Unable to open file for outgoing logging");
31    owl_free(logpath);
32    return;
33  }
34  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
35  if (text[strlen(text)-1]!='\n') {
36    fprintf(file, "\n");
37  }
38  fclose(file);
39
40  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
41  owl_free(logpath);
42  file=fopen(filename, "a");
43  if (!file) {
44    owl_function_makemsg("Unable to open file for outgoing logging");
45    return;
46  }
47  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
48  if (text[strlen(text)-1]!='\n') {
49    fprintf(file, "\n");
50  }
51  fclose(file);
52
53  owl_free(tobuff);
54}
55
56void owl_log_outgoing_aim(char *to, char *text) {
57  FILE *file;
58  char filename[MAXPATHLEN], *logpath;
59  char *tobuff;
60
61  tobuff=owl_sprintf("aim:%s", to);
62
63  /* expand ~ in path names */
64  logpath = owl_util_substitute(owl_global_get_logpath(&g), "~", 
65                                owl_global_get_homedir(&g));
66
67  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
68  file=fopen(filename, "a");
69  if (!file) {
70    owl_function_makemsg("Unable to open file for outgoing logging");
71    owl_free(logpath);
72    return;
73  }
74  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
75  if (text[strlen(text)-1]!='\n') {
76    fprintf(file, "\n");
77  }
78  fclose(file);
79
80  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
81  owl_free(logpath);
82  file=fopen(filename, "a");
83  if (!file) {
84    owl_function_makemsg("Unable to open file for outgoing logging");
85    return;
86  }
87  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
88  if (text[strlen(text)-1]!='\n') {
89    fprintf(file, "\n");
90  }
91  fclose(file);
92
93  owl_free(tobuff);
94}
95
96void owl_log_incoming(owl_message *m) {
97  FILE *file, *allfile;
98  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
99  char *frombuff, *ptr, *from, *buff, *tmp;
100  int len, ch, i, personal;
101     
102  /* check for nolog */
103  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
104      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
105
106  /* this is kind of a mess */
107  if (owl_message_is_type_zephyr(m)) {
108    if (owl_message_is_personal(m)) {
109      personal=1;
110      if (!owl_global_is_logging(&g)) return;
111    } else {
112      personal=0;
113      if (!owl_global_is_classlogging(&g)) return;
114    }
115  } else {
116    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
117      personal=1;
118      if (!owl_global_is_logging(&g)) return;
119    } else {
120      personal=0;
121      if (!owl_global_is_classlogging(&g)) return;
122    }
123  }
124
125  if (owl_message_is_type_zephyr(m)) {
126    /* chop off a local realm for personal zephyr messages */
127    if (personal) {
128      if (owl_message_is_type_zephyr(m)) {
129        from=frombuff=owl_strdup(owl_message_get_sender(m));
130        ptr=strchr(frombuff, '@');
131        if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
132          *ptr='\0';
133        }
134      }
135    } else {
136      /* we assume zephyr for now */
137      from=frombuff=owl_strdup(owl_message_get_class(m));
138    }
139  } else if (owl_message_is_type_aim(m)) {
140    /* we do not yet handle chat rooms */
141    from=frombuff=owl_sprintf("aim:%s", owl_message_get_sender(m));
142  }
143 
144  /* check for malicious sender formats */
145  len=strlen(frombuff);
146  if (len<1 || len>35) from="weird";
147  if (strchr(frombuff, '/')) from="weird";
148
149  ch=frombuff[0];
150  if (!isalnum(ch)) from="weird";
151
152  for (i=0; i<len; i++) {
153    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
154  }
155
156  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
157
158  if (!personal) {
159    if (strcmp(from, "weird")) downstr(from);
160  }
161
162  /* create the filename (expanding ~ in path names) */
163  if (personal) {
164    logpath = owl_util_substitute(owl_global_get_logpath(&g), "~", 
165                                  owl_global_get_homedir(&g));
166    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
167    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
168
169  } else {
170    logpath = owl_util_substitute(owl_global_get_classlogpath(&g), "~", 
171                                owl_global_get_homedir(&g));
172
173    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
174  }
175  owl_free(logpath);
176 
177  file=fopen(filename, "a");
178  if (!file) {
179    owl_function_makemsg("Unable to open file for incoming logging");
180    return;
181  }
182
183  allfile=NULL;
184  if (personal) {
185    allfile=fopen(allfilename, "a");
186    if (!allfile) {
187      owl_function_makemsg("Unable to open file for incoming logging");
188      fclose(file);
189      return;
190    }
191  }
192
193  /* write to the main file */
194  if (owl_message_is_type_zephyr(m)) {
195    tmp=short_zuser(owl_message_get_sender(m));
196    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
197    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
198    fprintf(file, "\n");
199    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
200    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
201    buff=owl_malloc(i+10);
202    memcpy(buff, ptr, i);
203    buff[i]='\0';
204    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
205    fprintf(file, "%s\n", owl_message_get_body(m));
206  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
207    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
208    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
209    fprintf(file, "%s\n\n", owl_message_get_body(m));
210  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
211    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
212    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
213    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
214    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
215  }
216  fclose(file);
217
218  /* if it's a personal message, also write to the 'all' file */
219  if (personal) {
220    if (owl_message_is_type_zephyr(m)) {
221      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
222      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
223      fprintf(allfile, "\n");
224      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
225      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
226      fprintf(allfile, "%s\n", owl_message_get_body(m));
227    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
228      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
229      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
230      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
231    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
232      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
233      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
234      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
235      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
236    }
237    fclose(allfile);
238  }
239
240  if (owl_message_is_type_zephyr(m)) {
241    owl_free(tmp);
242    owl_free(buff);
243  }
244  owl_free(frombuff);
245}
Note: See TracBrowser for help on using the repository browser.