source: logging.c @ 8c92848

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