source: logging.c @ 2b86d14

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2b86d14 was 2b86d14, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
Log when outgoing personal message fails
  • Property mode set to 100644
File size: 10.9 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;
112
113  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return;
114
115  tobuff=owl_sprintf("aim:%s", to);
116
117  /* expand ~ in path names */
118  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
119                                owl_global_get_homedir(&g));
120
121  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
122  file=fopen(filename, "a");
123  if (!file) {
124    owl_function_error("Unable to open file for outgoing logging");
125    owl_free(logpath);
126    return;
127  }
128  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
129  if (text[strlen(text)-1]!='\n') {
130    fprintf(file, "\n");
131  }
132  fclose(file);
133
134  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
135  owl_free(logpath);
136  file=fopen(filename, "a");
137  if (!file) {
138    owl_function_error("Unable to open file for outgoing logging");
139    return;
140  }
141  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
142  if (text[strlen(text)-1]!='\n') {
143    fprintf(file, "\n");
144  }
145  fclose(file);
146
147  owl_free(tobuff);
148}
149
150void owl_log_outgoing_loopback(char *text)
151{
152  FILE *file;
153  char filename[MAXPATHLEN], *logpath;
154  char *tobuff;
155
156  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return;
157
158  tobuff=owl_sprintf("loopback");
159
160  /* expand ~ in path names */
161  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
162                                owl_global_get_homedir(&g));
163
164  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
165  file=fopen(filename, "a");
166  if (!file) {
167    owl_function_error("Unable to open file for outgoing logging");
168    owl_free(logpath);
169    return;
170  }
171  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
172  if (text[strlen(text)-1]!='\n') {
173    fprintf(file, "\n");
174  }
175  fclose(file);
176
177  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
178  owl_free(logpath);
179  file=fopen(filename, "a");
180  if (!file) {
181    owl_function_error("Unable to open file for outgoing logging");
182    return;
183  }
184  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
185  if (text[strlen(text)-1]!='\n') {
186    fprintf(file, "\n");
187  }
188  fclose(file);
189
190  owl_free(tobuff);
191}
192
193void owl_log_incoming(owl_message *m)
194{
195  FILE *file, *allfile;
196  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
197  char *frombuff=NULL, *from=NULL, *buff=NULL, *ptr;
198  int len, ch, i, personal;
199
200  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) return;
201     
202  /* check for nolog */
203  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
204      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
205
206  /* this is kind of a mess */
207  if (owl_message_is_type_zephyr(m)) {
208    if (owl_message_is_personal(m)) {
209      personal=1;
210      if (!owl_global_is_logging(&g)) return;
211    } else {
212      personal=0;
213      if (!owl_global_is_classlogging(&g)) return;
214    }
215  } else {
216    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
217      personal=1;
218      if (!owl_global_is_logging(&g)) return;
219    } else {
220      personal=0;
221      if (!owl_global_is_classlogging(&g)) return;
222    }
223  }
224
225  if (owl_message_is_type_zephyr(m)) {
226    /* chop off a local realm for personal zephyr messages */
227    if (personal) {
228      if (owl_message_is_type_zephyr(m)) {
229        from=frombuff=owl_strdup(owl_message_get_sender(m));
230        ptr=strchr(frombuff, '@');
231        if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
232          *ptr='\0';
233        }
234      }
235    } else {
236      /* we assume zephyr for now */
237      from=frombuff=owl_strdup(owl_message_get_class(m));
238    }
239  } else if (owl_message_is_type_aim(m)) {
240    /* we do not yet handle chat rooms */
241    from=frombuff=owl_sprintf("aim:%s", owl_message_get_sender(m));
242  } else if (owl_message_is_type_loopback(m)) {
243    from=frombuff=owl_strdup("loopback");
244  } else {
245    from=frombuff=owl_strdup("unknown");
246  }
247 
248  /* check for malicious sender formats */
249  len=strlen(frombuff);
250  if (len<1 || len>35) from="weird";
251  if (strchr(frombuff, '/')) from="weird";
252
253  ch=frombuff[0];
254  if (!isalnum(ch)) from="weird";
255
256  for (i=0; i<len; i++) {
257    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
258  }
259
260  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
261
262  if (!personal) {
263    if (strcmp(from, "weird")) downstr(from);
264  }
265
266  /* create the filename (expanding ~ in path names) */
267  if (personal) {
268    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
269                                  owl_global_get_homedir(&g));
270    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
271    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
272
273  } else {
274    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", 
275                                owl_global_get_homedir(&g));
276
277    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
278  }
279  owl_free(logpath);
280 
281  file=fopen(filename, "a");
282  if (!file) {
283    owl_function_error("Unable to open file for incoming logging");
284    return;
285  }
286
287  allfile=NULL;
288  if (personal) {
289    allfile=fopen(allfilename, "a");
290    if (!allfile) {
291      owl_function_error("Unable to open file for incoming logging");
292      fclose(file);
293      return;
294    }
295  }
296
297  /* write to the main file */
298  if (owl_message_is_type_zephyr(m)) {
299    char *tmp;
300   
301    tmp=short_zuser(owl_message_get_sender(m));
302    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
303    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
304    fprintf(file, "\n");
305    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
306    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
307    buff=owl_malloc(i+10);
308    memcpy(buff, ptr, i);
309    buff[i]='\0';
310    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
311    fprintf(file, "%s\n\n", owl_message_get_body(m));
312    owl_free(tmp);
313  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
314    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
315    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
316    fprintf(file, "%s\n\n", owl_message_get_body(m));
317  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
318    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
319    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
320    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
321    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
322  } else {
323    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
324    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
325    fprintf(file, "%s\n\n", owl_message_get_body(m));
326  }
327
328  fclose(file);
329
330  /* if it's a personal message, also write to the 'all' file */
331  if (personal) {
332    if (owl_message_is_type_zephyr(m)) {
333      char *tmp;
334
335      tmp=short_zuser(owl_message_get_sender(m));
336      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
337      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
338      fprintf(allfile, "\n");
339      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
340      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
341      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
342      owl_free(tmp);
343    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
344      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
345      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
346      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
347    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
348      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
349      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
350      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
351      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
352    } else {
353      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
354      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
355      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
356    }
357    fclose(allfile);
358  }
359
360  if (owl_message_is_type_zephyr(m)) {
361    owl_free(buff);
362  }
363  owl_free(frombuff);
364}
Note: See TracBrowser for help on using the repository browser.