source: logging.c @ fe67f1f

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since fe67f1f was debb15d, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
Added 'show license'
  • Property mode set to 100644
File size: 11.2 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;
[e22f27c]204
205  /* skip login/logout messages if appropriate */
[debb15d]206  if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return;
[d09e5a1]207     
[7d4fbcd]208  /* check for nolog */
209  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
210      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
211
[aac889a]212  /* this is kind of a mess */
213  if (owl_message_is_type_zephyr(m)) {
214    if (owl_message_is_personal(m)) {
215      personal=1;
216      if (!owl_global_is_logging(&g)) return;
217    } else {
218      personal=0;
219      if (!owl_global_is_classlogging(&g)) return;
220    }
[7d4fbcd]221  } else {
[79a0e82]222    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
[aac889a]223      personal=1;
224      if (!owl_global_is_logging(&g)) return;
225    } else {
226      personal=0;
227      if (!owl_global_is_classlogging(&g)) return;
228    }
[7d4fbcd]229  }
230
[aac889a]231  if (owl_message_is_type_zephyr(m)) {
232    /* chop off a local realm for personal zephyr messages */
233    if (personal) {
234      if (owl_message_is_type_zephyr(m)) {
235        from=frombuff=owl_strdup(owl_message_get_sender(m));
236        ptr=strchr(frombuff, '@');
[09489b89]237        if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) {
[aac889a]238          *ptr='\0';
239        }
240      }
241    } else {
242      /* we assume zephyr for now */
243      from=frombuff=owl_strdup(owl_message_get_class(m));
[7d4fbcd]244    }
[aac889a]245  } else if (owl_message_is_type_aim(m)) {
246    /* we do not yet handle chat rooms */
[421c286f]247    char *normalto;
248    normalto=owl_aim_normalize_screenname(owl_message_get_sender(m));
249    downstr(normalto);
250    from=frombuff=owl_sprintf("aim:%s", normalto);
251    owl_free(normalto);
[37eab7f]252  } else if (owl_message_is_type_loopback(m)) {
253    from=frombuff=owl_strdup("loopback");
[e6449bc]254  } else {
255    from=frombuff=owl_strdup("unknown");
[7d4fbcd]256  }
257 
258  /* check for malicious sender formats */
259  len=strlen(frombuff);
260  if (len<1 || len>35) from="weird";
261  if (strchr(frombuff, '/')) from="weird";
262
263  ch=frombuff[0];
264  if (!isalnum(ch)) from="weird";
265
266  for (i=0; i<len; i++) {
[e1c4636]267    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
[7d4fbcd]268  }
269
270  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
271
272  if (!personal) {
273    if (strcmp(from, "weird")) downstr(from);
274  }
275
[e1c4636]276  /* create the filename (expanding ~ in path names) */
[7d4fbcd]277  if (personal) {
[e3d9c77]278    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", 
[e1c4636]279                                  owl_global_get_homedir(&g));
280    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
281    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
282
[7d4fbcd]283  } else {
[e3d9c77]284    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", 
[e1c4636]285                                owl_global_get_homedir(&g));
286
287    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
[7d4fbcd]288  }
[e1c4636]289  owl_free(logpath);
[7d4fbcd]290 
291  file=fopen(filename, "a");
292  if (!file) {
[836ea3a3]293    owl_function_error("Unable to open file for incoming logging");
[7d4fbcd]294    return;
295  }
296
297  allfile=NULL;
298  if (personal) {
299    allfile=fopen(allfilename, "a");
300    if (!allfile) {
[836ea3a3]301      owl_function_error("Unable to open file for incoming logging");
[79a0e82]302      fclose(file);
[7d4fbcd]303      return;
304    }
305  }
306
[aac889a]307  /* write to the main file */
308  if (owl_message_is_type_zephyr(m)) {
[e6449bc]309    char *tmp;
310   
[aac889a]311    tmp=short_zuser(owl_message_get_sender(m));
312    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
313    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
314    fprintf(file, "\n");
315    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
316    ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
317    buff=owl_malloc(i+10);
318    memcpy(buff, ptr, i);
319    buff[i]='\0';
320    fprintf(file, "From: %s <%s>\n\n", buff, tmp);
[269ed34]321    fprintf(file, "%s\n\n", owl_message_get_body(m));
[e6449bc]322    owl_free(tmp);
[79a0e82]323  } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
[aac889a]324    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
325    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
326    fprintf(file, "%s\n\n", owl_message_get_body(m));
[79a0e82]327  } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
328    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
329    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
330    if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n");
331    if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n");
[37eab7f]332  } else {
333    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
334    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
335    fprintf(file, "%s\n\n", owl_message_get_body(m));
[aac889a]336  }
[37eab7f]337
[7d4fbcd]338  fclose(file);
339
[aac889a]340  /* if it's a personal message, also write to the 'all' file */
[7d4fbcd]341  if (personal) {
[aac889a]342    if (owl_message_is_type_zephyr(m)) {
[e6449bc]343      char *tmp;
344
345      tmp=short_zuser(owl_message_get_sender(m));
[aac889a]346      fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
347      if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
348      fprintf(allfile, "\n");
349      fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
350      fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
[269ed34]351      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
[e6449bc]352      owl_free(tmp);
[79a0e82]353    } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) {
354      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
355      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
356      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
357    } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) {
358      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
359      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
360      if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n");
361      if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n");
[37eab7f]362    } else {
[eec69e1]363      fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
364      fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m));
365      fprintf(allfile, "%s\n\n", owl_message_get_body(m));
[aac889a]366    }
[723c427]367    fclose(allfile);
[7d4fbcd]368  }
369
[aac889a]370  if (owl_message_is_type_zephyr(m)) {
371    owl_free(buff);
372  }
[7d4fbcd]373  owl_free(frombuff);
374}
Note: See TracBrowser for help on using the repository browser.