source: logging.c @ d559df9

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since d559df9 was d559df9, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
AIM messages now go through format_msg() in .owlconf if it exists. Additional Perl variables will need to be added so users can distinguish AIM login/logout messages from normal messages.
  • Property mode set to 100644
File size: 6.8 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)) {
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      return;
189    }
190  }
191
192  /* write to the main file */
193  if (owl_message_is_type_zephyr(m)) {
194    tmp=short_zuser(owl_message_get_sender(m));
195    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
196    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
197    fprintf(file, "\n");
198    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
199    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
200    buff=owl_malloc(i+10);
201    memcpy(buff, ptr, i);
202    buff[i]='\0';
203    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
204    fprintf(file, "%s\n", owl_message_get_body(m));
205  } else if (owl_message_is_type_aim(m)) {
206    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
207    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
208    fprintf(file, "%s\n\n", owl_message_get_body(m));
209  }
210  fclose(file);
211
212  /* if it's a personal message, also write to the 'all' file */
213  if (personal) {
214    if (owl_message_is_type_zephyr(m)) {
215      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
216      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
217      fprintf(allfile, "\n");
218      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
219      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
220      fprintf(allfile, "%s\n", owl_message_get_body(m));
221      fclose(allfile);
222    } else {
223      fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
224      fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
225      fprintf(file, "%s\n\n", owl_message_get_body(m));
226    }
227  }
228
229  if (owl_message_is_type_zephyr(m)) {
230    owl_free(tmp);
231    owl_free(buff);
232  }
233  owl_free(frombuff);
234}
Note: See TracBrowser for help on using the repository browser.