source: logging.c @ 4b464a4

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 4b464a4 was 4b464a4, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Messages now have a direciton (in, out or none). Filters can this direction Outbound messages are no longer type 'admin' but are of the appropriate message type (i.e. 'zephyr') and are direction 'out'. Smartnarrow now works on outgoing messages 'info' updated to show more information for admin and outgoing messages Renamed pretty_sender to short_zuser and renamed long_sender to long_zuser
  • Property mode set to 100644
File size: 4.5 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(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_incoming(owl_message *m) {
57  FILE *file, *allfile;
58  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
59  char *frombuff, *ptr, *from, *buff, *tmp;
60  int len, ch, i, personal;
61
62  /* check for nolog */
63  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
64      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
65
66  if (owl_message_is_personal(m)) {
67    personal=1;
68    if (!owl_global_is_logging(&g)) return;
69  } else {
70    personal=0;
71    if (!owl_global_is_classlogging(&g)) return;
72  }
73
74  if (personal) {
75    from=frombuff=owl_strdup(owl_message_get_sender(m));
76    /* chop off a local realm */
77    ptr=strchr(frombuff, '@');
78    if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
79      *ptr='\0';
80    }
81  } else {
82    from=frombuff=owl_strdup(owl_message_get_class(m));
83  }
84 
85  /* check for malicious sender formats */
86  len=strlen(frombuff);
87  if (len<1 || len>35) from="weird";
88  if (strchr(frombuff, '/')) from="weird";
89
90  ch=frombuff[0];
91  if (!isalnum(ch)) from="weird";
92
93  for (i=0; i<len; i++) {
94    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
95  }
96
97  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
98
99  if (!personal) {
100    if (strcmp(from, "weird")) downstr(from);
101  }
102
103  /* create the filename (expanding ~ in path names) */
104  if (personal) {
105    logpath = owl_util_substitute(owl_global_get_logpath(&g), "~", 
106                                  owl_global_get_homedir(&g));
107    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
108    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
109
110  } else {
111    logpath = owl_util_substitute(owl_global_get_classlogpath(&g), "~", 
112                                owl_global_get_homedir(&g));
113
114    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
115  }
116  owl_free(logpath);
117 
118  file=fopen(filename, "a");
119  if (!file) {
120    owl_function_makemsg("Unable to open file for incoming logging");
121    return;
122  }
123
124  allfile=NULL;
125  if (personal) {
126    allfile=fopen(allfilename, "a");
127    if (!allfile) {
128      owl_function_makemsg("Unable to open file for incoming logging");
129      return;
130    }
131  }
132
133  tmp=short_zuser(owl_message_get_sender(m));
134 
135  fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
136  if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
137  fprintf(file, "\n");
138  fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
139  ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
140  buff=owl_malloc(i+10);
141  memcpy(buff, ptr, i);
142  buff[i]='\0';
143  fprintf(file, "From: %s <%s>\n\n", buff, tmp);
144  fprintf(file, "%s\n", owl_message_get_body(m));
145  fclose(file);
146
147  if (personal) {
148    fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
149    if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
150    fprintf(allfile, "\n");
151    fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
152    fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
153    fprintf(allfile, "%s\n", owl_message_get_body(m));
154    fclose(allfile);
155  }
156
157  owl_free(tmp);
158  owl_free(buff);
159  owl_free(frombuff);
160}
Note: See TracBrowser for help on using the repository browser.