source: logging.c @ eec69e1

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since eec69e1 was eec69e1, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
Fixed bug in loopback logging
  • Property mode set to 100644
File size: 9.4 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{
59  FILE *file;
60  char filename[MAXPATHLEN], *logpath;
61  char *tobuff;
62
63  tobuff=owl_sprintf("aim:%s", to);
64
65  /* expand ~ in path names */
66  logpath = owl_util_substitute(owl_global_get_logpath(&g), "~", 
67                                owl_global_get_homedir(&g));
68
69  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
70  file=fopen(filename, "a");
71  if (!file) {
72    owl_function_error("Unable to open file for outgoing logging");
73    owl_free(logpath);
74    return;
75  }
76  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
77  if (text[strlen(text)-1]!='\n') {
78    fprintf(file, "\n");
79  }
80  fclose(file);
81
82  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
83  owl_free(logpath);
84  file=fopen(filename, "a");
85  if (!file) {
86    owl_function_error("Unable to open file for outgoing logging");
87    return;
88  }
89  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
90  if (text[strlen(text)-1]!='\n') {
91    fprintf(file, "\n");
92  }
93  fclose(file);
94
95  owl_free(tobuff);
96}
97
98void owl_log_outgoing_loopback(char *text)
99{
100  FILE *file;
101  char filename[MAXPATHLEN], *logpath;
102  char *tobuff;
103
104  tobuff=owl_sprintf("loopback:%s", "loppback");
105
106  /* expand ~ in path names */
107  logpath = owl_util_substitute(owl_global_get_logpath(&g), "~", 
108                                owl_global_get_homedir(&g));
109
110  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
111  file=fopen(filename, "a");
112  if (!file) {
113    owl_function_error("Unable to open file for outgoing logging");
114    owl_free(logpath);
115    return;
116  }
117  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
118  if (text[strlen(text)-1]!='\n') {
119    fprintf(file, "\n");
120  }
121  fclose(file);
122
123  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
124  owl_free(logpath);
125  file=fopen(filename, "a");
126  if (!file) {
127    owl_function_error("Unable to open file for outgoing logging");
128    return;
129  }
130  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
131  if (text[strlen(text)-1]!='\n') {
132    fprintf(file, "\n");
133  }
134  fclose(file);
135
136  owl_free(tobuff);
137}
138
139void owl_log_incoming(owl_message *m)
140{
141  FILE *file, *allfile;
142  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
143  char *frombuff=NULL, *from=NULL, *buff=NULL, *ptr;
144  int len, ch, i, personal;
145     
146  /* check for nolog */
147  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
148      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
149
150  /* this is kind of a mess */
151  if (owl_message_is_type_zephyr(m)) {
152    if (owl_message_is_personal(m)) {
153      personal=1;
154      if (!owl_global_is_logging(&g)) return;
155    } else {
156      personal=0;
157      if (!owl_global_is_classlogging(&g)) return;
158    }
159  } else {
160    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
161      personal=1;
162      if (!owl_global_is_logging(&g)) return;
163    } else {
164      personal=0;
165      if (!owl_global_is_classlogging(&g)) return;
166    }
167  }
168
169  if (owl_message_is_type_zephyr(m)) {
170    /* chop off a local realm for personal zephyr messages */
171    if (personal) {
172      if (owl_message_is_type_zephyr(m)) {
173        from=frombuff=owl_strdup(owl_message_get_sender(m));
174        ptr=strchr(frombuff, '@');
175        if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
176          *ptr='\0';
177        }
178      }
179    } else {
180      /* we assume zephyr for now */
181      from=frombuff=owl_strdup(owl_message_get_class(m));
182    }
183  } else if (owl_message_is_type_aim(m)) {
184    /* we do not yet handle chat rooms */
185    from=frombuff=owl_sprintf("aim:%s", owl_message_get_sender(m));
186  } else if (owl_message_is_type_loopback(m)) {
187    from=frombuff=owl_strdup("loopback");
188  } else {
189    from=frombuff=owl_strdup("unknown");
190  }
191 
192  /* check for malicious sender formats */
193  len=strlen(frombuff);
194  if (len<1 || len>35) from="weird";
195  if (strchr(frombuff, '/')) from="weird";
196
197  ch=frombuff[0];
198  if (!isalnum(ch)) from="weird";
199
200  for (i=0; i<len; i++) {
201    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
202  }
203
204  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
205
206  if (!personal) {
207    if (strcmp(from, "weird")) downstr(from);
208  }
209
210  /* create the filename (expanding ~ in path names) */
211  if (personal) {
212    logpath = owl_util_substitute(owl_global_get_logpath(&g), "~", 
213                                  owl_global_get_homedir(&g));
214    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
215    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
216
217  } else {
218    logpath = owl_util_substitute(owl_global_get_classlogpath(&g), "~", 
219                                owl_global_get_homedir(&g));
220
221    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
222  }
223  owl_free(logpath);
224 
225  file=fopen(filename, "a");
226  if (!file) {
227    owl_function_error("Unable to open file for incoming logging");
228    return;
229  }
230
231  allfile=NULL;
232  if (personal) {
233    allfile=fopen(allfilename, "a");
234    if (!allfile) {
235      owl_function_error("Unable to open file for incoming logging");
236      fclose(file);
237      return;
238    }
239  }
240
241  /* write to the main file */
242  if (owl_message_is_type_zephyr(m)) {
243    char *tmp;
244   
245    tmp=short_zuser(owl_message_get_sender(m));
246    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
247    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
248    fprintf(file, "\n");
249    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
250    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
251    buff=owl_malloc(i+10);
252    memcpy(buff, ptr, i);
253    buff[i]='\0';
254    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
255    fprintf(file, "%s\n\n", owl_message_get_body(m));
256    owl_free(tmp);
257  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
258    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
259    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
260    fprintf(file, "%s\n\n", owl_message_get_body(m));
261  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
262    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
263    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
264    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
265    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
266  } else {
267    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
268    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
269    fprintf(file, "%s\n\n", owl_message_get_body(m));
270  }
271
272  fclose(file);
273
274  /* if it's a personal message, also write to the 'all' file */
275  if (personal) {
276    if (owl_message_is_type_zephyr(m)) {
277      char *tmp;
278
279      tmp=short_zuser(owl_message_get_sender(m));
280      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
281      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
282      fprintf(allfile, "\n");
283      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
284      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
285      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
286      owl_free(tmp);
287    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
288      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
289      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
290      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
291    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
292      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
293      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
294      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
295      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
296    } else {
297      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
298      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
299      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
300    }
301    fclose(allfile);
302  }
303
304  if (owl_message_is_type_zephyr(m)) {
305    owl_free(buff);
306  }
307  owl_free(frombuff);
308}
Note: See TracBrowser for help on using the repository browser.