source: logging.c @ ab31454

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since ab31454 was 723c427, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
another logging fix
  • Property mode set to 100644
File size: 7.7 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <sys/param.h>
6
[1aee7d9]7static const char fileIdent[] = "$Id$";
8
[aac889a]9void owl_log_outgoing_zephyr(char *to, char *text) {
[7d4fbcd]10  FILE *file;
[e1c4636]11  char filename[MAXPATHLEN], *logpath;
[7d4fbcd]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
[e1c4636]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);
[7d4fbcd]28  file=fopen(filename, "a");
29  if (!file) {
30    owl_function_makemsg("Unable to open file for outgoing logging");
[e1c4636]31    owl_free(logpath);
[7d4fbcd]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
[e1c4636]40  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
41  owl_free(logpath);
[7d4fbcd]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
[aac889a]56void owl_log_outgoing_aim(char *to, char *text) {
57  FILE *file;
58  char filename[MAXPATHLEN], *logpath;
[d559df9]59  char *tobuff;
[aac889a]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
[7d4fbcd]96void owl_log_incoming(owl_message *m) {
97  FILE *file, *allfile;
[e1c4636]98  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
[7d4fbcd]99  char *frombuff, *ptr, *from, *buff, *tmp;
100  int len, ch, i, personal;
[d09e5a1]101     
[7d4fbcd]102  /* check for nolog */
103  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
104      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
105
[aac889a]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    }
[7d4fbcd]115  } else {
[79a0e82]116    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
[aac889a]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    }
[7d4fbcd]123  }
124
[aac889a]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));
[7d4fbcd]138    }
[aac889a]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));
[7d4fbcd]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++) {
[e1c4636]153    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
[7d4fbcd]154  }
155
156  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
157
158  if (!personal) {
159    if (strcmp(from, "weird")) downstr(from);
160  }
161
[e1c4636]162  /* create the filename (expanding ~ in path names) */
[7d4fbcd]163  if (personal) {
[e1c4636]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
[7d4fbcd]169  } else {
[e1c4636]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);
[7d4fbcd]174  }
[e1c4636]175  owl_free(logpath);
[7d4fbcd]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");
[79a0e82]188      fclose(file);
[7d4fbcd]189      return;
190    }
191  }
192
[aac889a]193  /* write to the main file */
194  if (owl_message_is_type_zephyr(m)) {
195    tmp=short_zuser(owl_message_get_sender(m));
196    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
197    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
198    fprintf(file, "\n");
199    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
200    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
201    buff=owl_malloc(i+10);
202    memcpy(buff, ptr, i);
203    buff[i]='\0';
204    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
205    fprintf(file, "%s\n", owl_message_get_body(m));
[79a0e82]206  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
[aac889a]207    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
208    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
209    fprintf(file, "%s\n\n", owl_message_get_body(m));
[79a0e82]210  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
211    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
212    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
213    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
214    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
[aac889a]215  }
[7d4fbcd]216  fclose(file);
217
[aac889a]218  /* if it's a personal message, also write to the 'all' file */
[7d4fbcd]219  if (personal) {
[aac889a]220    if (owl_message_is_type_zephyr(m)) {
221      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
222      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
223      fprintf(allfile, "\n");
224      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
225      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
226      fprintf(allfile, "%s\n", owl_message_get_body(m));
[79a0e82]227    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
228      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
229      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
230      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
231    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
232      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
233      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
234      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
235      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
[aac889a]236    }
[723c427]237    fclose(allfile);
[7d4fbcd]238  }
239
[aac889a]240  if (owl_message_is_type_zephyr(m)) {
241    owl_free(tmp);
242    owl_free(buff);
243  }
[7d4fbcd]244  owl_free(frombuff);
245}
Note: See TracBrowser for help on using the repository browser.