source: logging.c @ fe6f1d3

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since fe6f1d3 was e6449bc, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
Owl should build now without zephyr, enabling it to act as a standalone AIM client. Cleaned up some warnings from linux gcc.
  • 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  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, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
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=NULL, *from=NULL, *buff=NULL, *ptr;
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, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
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  } else {
143    from=frombuff=owl_strdup("unknown");
144  }
145 
146  /* check for malicious sender formats */
147  len=strlen(frombuff);
148  if (len<1 || len>35) from="weird";
149  if (strchr(frombuff, '/')) from="weird";
150
151  ch=frombuff[0];
152  if (!isalnum(ch)) from="weird";
153
154  for (i=0; i<len; i++) {
155    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
156  }
157
158  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
159
160  if (!personal) {
161    if (strcmp(from, "weird")) downstr(from);
162  }
163
164  /* create the filename (expanding ~ in path names) */
165  if (personal) {
166    logpath = owl_util_substitute(owl_global_get_logpath(&g), "~", 
167                                  owl_global_get_homedir(&g));
168    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
169    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
170
171  } else {
172    logpath = owl_util_substitute(owl_global_get_classlogpath(&g), "~", 
173                                owl_global_get_homedir(&g));
174
175    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
176  }
177  owl_free(logpath);
178 
179  file=fopen(filename, "a");
180  if (!file) {
181    owl_function_makemsg("Unable to open file for incoming logging");
182    return;
183  }
184
185  allfile=NULL;
186  if (personal) {
187    allfile=fopen(allfilename, "a");
188    if (!allfile) {
189      owl_function_makemsg("Unable to open file for incoming logging");
190      fclose(file);
191      return;
192    }
193  }
194
195  /* write to the main file */
196  if (owl_message_is_type_zephyr(m)) {
197    char *tmp;
198   
199    tmp=short_zuser(owl_message_get_sender(m));
200    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
201    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
202    fprintf(file, "\n");
203    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
204    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
205    buff=owl_malloc(i+10);
206    memcpy(buff, ptr, i);
207    buff[i]='\0';
208    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
209    fprintf(file, "%s\n", owl_message_get_body(m));
210    owl_free(tmp);
211  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
212    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
213    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
214    fprintf(file, "%s\n\n", owl_message_get_body(m));
215  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
216    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
217    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
218    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
219    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
220  }
221  fclose(file);
222
223  /* if it's a personal message, also write to the 'all' file */
224  if (personal) {
225    if (owl_message_is_type_zephyr(m)) {
226      char *tmp;
227
228      tmp=short_zuser(owl_message_get_sender(m));
229      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
230      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
231      fprintf(allfile, "\n");
232      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
233      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
234      fprintf(allfile, "%s\n", owl_message_get_body(m));
235      owl_free(tmp);
236    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
237      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
238      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
239      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
240    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
241      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
242      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
243      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
244      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
245    }
246    fclose(allfile);
247  }
248
249  if (owl_message_is_type_zephyr(m)) {
250    owl_free(buff);
251  }
252  owl_free(frombuff);
253}
Note: See TracBrowser for help on using the repository browser.