source: logging.c @ 8ba37ec

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 8ba37ec was 421c286f, checked in by James M. Kretchmar <kretch@mit.edu>, 20 years ago
Normalize and downcase aim names before logging
  • Property mode set to 100644
File size: 11.1 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
[15283bb]9void owl_log_outgoing_zephyr(char *to, char *text)
10{
[7d4fbcd]11  FILE *file;
[e1c4636]12  char filename[MAXPATHLEN], *logpath;
[e6449bc]13  char *tobuff, *ptr="";
[7d4fbcd]14
[12c35df]15  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return;
16
[7d4fbcd]17  tobuff=owl_malloc(strlen(to)+20);
18  strcpy(tobuff, to);
19
20  /* chop off a local realm */
21  ptr=strchr(tobuff, '@');
[09489b89]22  if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
[7d4fbcd]23    *ptr='\0';
24  }
25
[e1c4636]26  /* expand ~ in path names */
[e3d9c77]27  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
[e1c4636]28                                owl_global_get_homedir(&g));
29
30  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
[7d4fbcd]31  file=fopen(filename, "a");
32  if (!file) {
[836ea3a3]33    owl_function_error("Unable to open file for outgoing logging");
[e1c4636]34    owl_free(logpath);
[7d4fbcd]35    return;
36  }
37  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
38  if (text[strlen(text)-1]!='\n') {
39    fprintf(file, "\n");
40  }
41  fclose(file);
42
[e1c4636]43  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
44  owl_free(logpath);
[7d4fbcd]45  file=fopen(filename, "a");
46  if (!file) {
[836ea3a3]47    owl_function_error("Unable to open file for outgoing logging");
[7d4fbcd]48    return;
49  }
50  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
51  if (text[strlen(text)-1]!='\n') {
52    fprintf(file, "\n");
53  }
54  fclose(file);
55
56  owl_free(tobuff);
57}
58
[2b86d14]59void owl_log_outgoing_zephyr_error(char *to, char *text)
60{
61  FILE *file;
62  char filename[MAXPATHLEN], *logpath;
63  char *tobuff, *ptr="";
64
65  tobuff=owl_malloc(strlen(to)+20);
66  strcpy(tobuff, to);
67
68  /* chop off a local realm */
69  ptr=strchr(tobuff, '@');
70  if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
71    *ptr='\0';
72  }
73
74  /* expand ~ in path names */
75  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
76                                owl_global_get_homedir(&g));
77
78  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
79  file=fopen(filename, "a");
80  if (!file) {
81    owl_function_error("Unable to open file for outgoing logging");
82    owl_free(logpath);
83    return;
84  }
85  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
86  if (text[strlen(text)-1]!='\n') {
87    fprintf(file, "\n");
88  }
89  fclose(file);
90
91  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
92  owl_free(logpath);
93  file=fopen(filename, "a");
94  if (!file) {
95    owl_function_error("Unable to open file for outgoing logging");
96    return;
97  }
98  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
99  if (text[strlen(text)-1]!='\n') {
100    fprintf(file, "\n");
101  }
102  fclose(file);
103
104  owl_free(tobuff);
105}
106
[37eab7f]107void owl_log_outgoing_aim(char *to, char *text)
108{
[aac889a]109  FILE *file;
110  char filename[MAXPATHLEN], *logpath;
[421c286f]111  char *tobuff, *normalto;
[aac889a]112
[12c35df]113  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return;
114
[421c286f]115  normalto=owl_aim_normalize_screenname(to);
116  downstr(normalto);
117  tobuff=owl_sprintf("aim:%s", normalto);
118  owl_free(normalto);
[aac889a]119
120  /* expand ~ in path names */
[e3d9c77]121  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
[aac889a]122                                owl_global_get_homedir(&g));
123
124  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
125  file=fopen(filename, "a");
126  if (!file) {
[836ea3a3]127    owl_function_error("Unable to open file for outgoing logging");
[aac889a]128    owl_free(logpath);
129    return;
130  }
131  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
132  if (text[strlen(text)-1]!='\n') {
133    fprintf(file, "\n");
134  }
135  fclose(file);
136
137  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
138  owl_free(logpath);
139  file=fopen(filename, "a");
140  if (!file) {
[836ea3a3]141    owl_function_error("Unable to open file for outgoing logging");
[aac889a]142    return;
143  }
144  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
145  if (text[strlen(text)-1]!='\n') {
146    fprintf(file, "\n");
147  }
148  fclose(file);
149
150  owl_free(tobuff);
151}
152
[37eab7f]153void owl_log_outgoing_loopback(char *text)
154{
155  FILE *file;
156  char filename[MAXPATHLEN], *logpath;
157  char *tobuff;
158
[12c35df]159  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return;
160
161  tobuff=owl_sprintf("loopback");
[37eab7f]162
163  /* expand ~ in path names */
[e3d9c77]164  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
[37eab7f]165                                owl_global_get_homedir(&g));
166
167  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
168  file=fopen(filename, "a");
169  if (!file) {
170    owl_function_error("Unable to open file for outgoing logging");
171    owl_free(logpath);
172    return;
173  }
174  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
175  if (text[strlen(text)-1]!='\n') {
176    fprintf(file, "\n");
177  }
178  fclose(file);
179
180  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
181  owl_free(logpath);
182  file=fopen(filename, "a");
183  if (!file) {
184    owl_function_error("Unable to open file for outgoing logging");
185    return;
186  }
187  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
188  if (text[strlen(text)-1]!='\n') {
189    fprintf(file, "\n");
190  }
191  fclose(file);
192
193  owl_free(tobuff);
194}
195
[15283bb]196void owl_log_incoming(owl_message *m)
197{
[7d4fbcd]198  FILE *file, *allfile;
[e1c4636]199  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
[e6449bc]200  char *frombuff=NULL, *from=NULL, *buff=NULL, *ptr;
[7d4fbcd]201  int len, ch, i, personal;
[12c35df]202
203  if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) return;
[d09e5a1]204     
[7d4fbcd]205  /* check for nolog */
206  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
207      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
208
[aac889a]209  /* this is kind of a mess */
210  if (owl_message_is_type_zephyr(m)) {
211    if (owl_message_is_personal(m)) {
212      personal=1;
213      if (!owl_global_is_logging(&g)) return;
214    } else {
215      personal=0;
216      if (!owl_global_is_classlogging(&g)) return;
217    }
[7d4fbcd]218  } else {
[79a0e82]219    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
[aac889a]220      personal=1;
221      if (!owl_global_is_logging(&g)) return;
222    } else {
223      personal=0;
224      if (!owl_global_is_classlogging(&g)) return;
225    }
[7d4fbcd]226  }
227
[aac889a]228  if (owl_message_is_type_zephyr(m)) {
229    /* chop off a local realm for personal zephyr messages */
230    if (personal) {
231      if (owl_message_is_type_zephyr(m)) {
232        from=frombuff=owl_strdup(owl_message_get_sender(m));
233        ptr=strchr(frombuff, '@');
[09489b89]234        if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
[aac889a]235          *ptr='\0';
236        }
237      }
238    } else {
239      /* we assume zephyr for now */
240      from=frombuff=owl_strdup(owl_message_get_class(m));
[7d4fbcd]241    }
[aac889a]242  } else if (owl_message_is_type_aim(m)) {
243    /* we do not yet handle chat rooms */
[421c286f]244    char *normalto;
245    normalto=owl_aim_normalize_screenname(owl_message_get_sender(m));
246    downstr(normalto);
247    from=frombuff=owl_sprintf("aim:%s", normalto);
248    owl_free(normalto);
[37eab7f]249  } else if (owl_message_is_type_loopback(m)) {
250    from=frombuff=owl_strdup("loopback");
[e6449bc]251  } else {
252    from=frombuff=owl_strdup("unknown");
[7d4fbcd]253  }
254 
255  /* check for malicious sender formats */
256  len=strlen(frombuff);
257  if (len<1 || len>35) from="weird";
258  if (strchr(frombuff, '/')) from="weird";
259
260  ch=frombuff[0];
261  if (!isalnum(ch)) from="weird";
262
263  for (i=0; i<len; i++) {
[e1c4636]264    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
[7d4fbcd]265  }
266
267  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
268
269  if (!personal) {
270    if (strcmp(from, "weird")) downstr(from);
271  }
272
[e1c4636]273  /* create the filename (expanding ~ in path names) */
[7d4fbcd]274  if (personal) {
[e3d9c77]275    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
[e1c4636]276                                  owl_global_get_homedir(&g));
277    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
278    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
279
[7d4fbcd]280  } else {
[e3d9c77]281    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", 
[e1c4636]282                                owl_global_get_homedir(&g));
283
284    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
[7d4fbcd]285  }
[e1c4636]286  owl_free(logpath);
[7d4fbcd]287 
288  file=fopen(filename, "a");
289  if (!file) {
[836ea3a3]290    owl_function_error("Unable to open file for incoming logging");
[7d4fbcd]291    return;
292  }
293
294  allfile=NULL;
295  if (personal) {
296    allfile=fopen(allfilename, "a");
297    if (!allfile) {
[836ea3a3]298      owl_function_error("Unable to open file for incoming logging");
[79a0e82]299      fclose(file);
[7d4fbcd]300      return;
301    }
302  }
303
[aac889a]304  /* write to the main file */
305  if (owl_message_is_type_zephyr(m)) {
[e6449bc]306    char *tmp;
307   
[aac889a]308    tmp=short_zuser(owl_message_get_sender(m));
309    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
310    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
311    fprintf(file, "\n");
312    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
313    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
314    buff=owl_malloc(i+10);
315    memcpy(buff, ptr, i);
316    buff[i]='\0';
317    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
[269ed34]318    fprintf(file, "%s\n\n", owl_message_get_body(m));
[e6449bc]319    owl_free(tmp);
[79a0e82]320  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
[aac889a]321    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
322    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
323    fprintf(file, "%s\n\n", owl_message_get_body(m));
[79a0e82]324  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
325    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
326    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
327    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
328    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
[37eab7f]329  } else {
330    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
331    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
332    fprintf(file, "%s\n\n", owl_message_get_body(m));
[aac889a]333  }
[37eab7f]334
[7d4fbcd]335  fclose(file);
336
[aac889a]337  /* if it's a personal message, also write to the 'all' file */
[7d4fbcd]338  if (personal) {
[aac889a]339    if (owl_message_is_type_zephyr(m)) {
[e6449bc]340      char *tmp;
341
342      tmp=short_zuser(owl_message_get_sender(m));
[aac889a]343      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
344      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
345      fprintf(allfile, "\n");
346      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
347      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
[269ed34]348      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
[e6449bc]349      owl_free(tmp);
[79a0e82]350    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
351      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
352      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
353      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
354    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
355      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
356      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
357      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
358      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
[37eab7f]359    } else {
[eec69e1]360      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
361      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
362      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
[aac889a]363    }
[723c427]364    fclose(allfile);
[7d4fbcd]365  }
366
[aac889a]367  if (owl_message_is_type_zephyr(m)) {
368    owl_free(buff);
369  }
[7d4fbcd]370  owl_free(frombuff);
371}
Note: See TracBrowser for help on using the repository browser.