source: logging.c @ 7d4fbcd

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7d4fbcd was 7d4fbcd, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Initial check in
  • Property mode set to 100644
File size: 4.0 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <sys/param.h>
6
7void owl_log_outgoing(char *to, char *text) {
8  FILE *file;
9  char filename[MAXPATHLEN];
10  char *tobuff, *ptr;
11
12  tobuff=owl_malloc(strlen(to)+20);
13  strcpy(tobuff, to);
14
15  /* chop off a local realm */
16  ptr=strchr(tobuff, '@');
17  if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
18    *ptr='\0';
19  }
20
21  sprintf(filename, "%s/zlog/people/%s", owl_global_get_homedir(&g), tobuff);
22  file=fopen(filename, "a");
23  if (!file) {
24    owl_function_makemsg("Unable to open file for outgoing logging");
25    return;
26  }
27  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
28  if (text[strlen(text)-1]!='\n') {
29    fprintf(file, "\n");
30  }
31  fclose(file);
32
33  sprintf(filename, "%s/zlog/people/all", owl_global_get_homedir(&g));
34  file=fopen(filename, "a");
35  if (!file) {
36    owl_function_makemsg("Unable to open file for outgoing logging");
37    return;
38  }
39  fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text);
40  if (text[strlen(text)-1]!='\n') {
41    fprintf(file, "\n");
42  }
43  fclose(file);
44
45  owl_free(tobuff);
46}
47
48void owl_log_incoming(owl_message *m) {
49  FILE *file, *allfile;
50  char filename[MAXPATHLEN], allfilename[MAXPATHLEN];
51  char *frombuff, *ptr, *from, *buff, *tmp;
52  int len, ch, i, personal;
53
54  /* check for nolog */
55  if (!strcasecmp(owl_message_get_opcode(m), "nolog") ||
56      !strcasecmp(owl_message_get_instance(m), "nolog")) return;
57
58  if (owl_message_is_personal(m)) {
59    personal=1;
60    if (!owl_global_is_logging(&g)) return;
61  } else {
62    personal=0;
63    if (!owl_global_is_classlogging(&g)) return;
64  }
65
66  if (personal) {
67    from=frombuff=owl_strdup(owl_message_get_sender(m));
68    /* chop off a local realm */
69    ptr=strchr(frombuff, '@');
70    if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) {
71      *ptr='\0';
72    }
73  } else {
74    from=frombuff=owl_strdup(owl_message_get_class(m));
75  }
76 
77  /* check for malicious sender formats */
78  len=strlen(frombuff);
79  if (len<1 || len>35) from="weird";
80  if (strchr(frombuff, '/')) from="weird";
81
82  ch=frombuff[0];
83  if (!isalnum(ch)) from="weird";
84
85  for (i=0; i<len; i++) {
86    if (frombuff[i]<'!' || frombuff[i]>'~') from="weird";
87  }
88
89  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
90
91  if (!personal) {
92    if (strcmp(from, "weird")) downstr(from);
93  }
94
95  /* create the filename */
96  if (personal) {
97    sprintf(filename, "%s/%s", owl_global_get_logpath(&g), from);
98    sprintf(allfilename, "%s/all", owl_global_get_logpath(&g));
99  } else {
100    sprintf(filename, "%s/%s", owl_global_get_classlogpath(&g), from);
101  }
102 
103  file=fopen(filename, "a");
104  if (!file) {
105    owl_function_makemsg("Unable to open file for incoming logging");
106    return;
107  }
108
109  allfile=NULL;
110  if (personal) {
111    allfile=fopen(allfilename, "a");
112    if (!allfile) {
113      owl_function_makemsg("Unable to open file for incoming logging");
114      return;
115    }
116  }
117
118  tmp=pretty_sender(owl_message_get_sender(m));
119 
120  fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
121  if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
122  fprintf(file, "\n");
123  fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
124  ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i);
125  buff=owl_malloc(i+10);
126  memcpy(buff, ptr, i);
127  buff[i]='\0';
128  fprintf(file, "From: %s <%s>\n\n", buff, tmp);
129  fprintf(file, "%s\n", owl_message_get_body(m));
130  fclose(file);
131
132  if (personal) {
133    fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
134    if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m));
135    fprintf(allfile, "\n");
136    fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
137    fprintf(allfile, "From: %s <%s>\n\n", buff, tmp);
138    fprintf(allfile, "%s\n", owl_message_get_body(m));
139    fclose(allfile);
140  }
141
142  owl_free(tmp);
143  owl_free(buff);
144  owl_free(frombuff);
145}
Note: See TracBrowser for help on using the repository browser.