source: logging.c @ d09e5a1

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since d09e5a1 was d09e5a1, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
Added libfaim Added basic AIM support, including the "aimlogin", "aimwrite" and "aimlogout" commands
  • 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  /* we only do zephyrs right now */
63  if (!owl_message_is_type_zephyr(m)) return;
64     
65  /* check for nolog */
66  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
67      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
68
69  if (owl_message_is_personal(m)) {
70    personal=1;
71    if (!owl_global_is_logging(&g)) return;
72  } else {
73    personal=0;
74    if (!owl_global_is_classlogging(&g)) return;
75  }
76
77  if (personal) {
78    from=frombuff=owl_strdup(owl_message_get_sender(m));
79    /* chop off a local realm */
80    ptr=strchr(frombuff, '@');
81    if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
82      *ptr='\0';
83    }
84  } else {
85    from=frombuff=owl_strdup(owl_message_get_class(m));
86  }
87 
88  /* check for malicious sender formats */
89  len=strlen(frombuff);
90  if (len<1 || len>35) from="weird";
91  if (strchr(frombuff, '/')) from="weird";
92
93  ch=frombuff[0];
94  if (!isalnum(ch)) from="weird";
95
96  for (i=0; i<len; i++) {
97    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
98  }
99
100  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
101
102  if (!personal) {
103    if (strcmp(from, "weird")) downstr(from);
104  }
105
106  /* create the filename (expanding ~ in path names) */
107  if (personal) {
108    logpath = owl_util_substitute(owl_global_get_logpath(&g), "~", 
109                                  owl_global_get_homedir(&g));
110    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
111    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
112
113  } else {
114    logpath = owl_util_substitute(owl_global_get_classlogpath(&g), "~", 
115                                owl_global_get_homedir(&g));
116
117    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
118  }
119  owl_free(logpath);
120 
121  file=fopen(filename, "a");
122  if (!file) {
123    owl_function_makemsg("Unable to open file for incoming logging");
124    return;
125  }
126
127  allfile=NULL;
128  if (personal) {
129    allfile=fopen(allfilename, "a");
130    if (!allfile) {
131      owl_function_makemsg("Unable to open file for incoming logging");
132      return;
133    }
134  }
135
136  tmp=short_zuser(owl_message_get_sender(m));
137 
138  fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
139  if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
140  fprintf(file, "\n");
141  fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
142  ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
143  buff=owl_malloc(i+10);
144  memcpy(buff, ptr, i);
145  buff[i]='\0';
146  fprintf(file, "From: %s <%s>\n\n", buff, tmp);
147  fprintf(file, "%s\n", owl_message_get_body(m));
148  fclose(file);
149
150  if (personal) {
151    fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
152    if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
153    fprintf(allfile, "\n");
154    fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
155    fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
156    fprintf(allfile, "%s\n", owl_message_get_body(m));
157    fclose(allfile);
158  }
159
160  owl_free(tmp);
161  owl_free(buff);
162  owl_free(frombuff);
163}
Note: See TracBrowser for help on using the repository browser.