source: logging.c @ c453ada

barnowl_perlaim
Last change on this file since c453ada was c453ada, checked in by Geoffrey Thomas <geofft@mit.edu>, 17 years ago
Remove aim.c. buddylist.c, buddy.c, libfaim, and everything that uses them.
  • Property mode set to 100644
File size: 9.3 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
9/* This is now the one function that should be called to log a
10 * message.  It will do all the work necessary by calling the other
11 * functions in this file as necessary.
12 */
13void owl_log_message(owl_message *m) {
14  owl_function_debugmsg("owl_log_message: entering");
15
16  if (m == NULL) {
17    owl_function_debugmsg("owl_log_message: passed null message");
18    return;
19  }
20
21  /* should we be logging this message? */
22  if (!owl_log_shouldlog_message(m)) {
23    owl_function_debugmsg("owl_log_message: not logging message");
24    return;
25  }
26
27  /* handle incmoing messages */
28  if (owl_message_is_direction_in(m)) {
29    owl_log_incoming(m);
30    owl_function_debugmsg("owl_log_message: leaving");
31    return;
32  }
33
34  /* handle outgoing messages */
35  owl_log_outgoing(m);
36
37  owl_function_debugmsg("owl_log_message: leaving");
38}
39
40/* Return 1 if we should log the given message, otherwise return 0 */
41int owl_log_shouldlog_message(owl_message *m) {
42  owl_filter *f;
43
44  /* If there's a logfilter and this message matches it, log */
45  f=owl_global_get_filter(&g, owl_global_get_logfilter(&g));
46  if (f && owl_filter_message_match(f, m)) return(1);
47
48  /* otherwise we do things based on the logging variables */
49
50  /* skip login/logout messages if appropriate */
51  if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0);
52     
53  /* check for nolog */
54  if (!strcasecmp(owl_message_get_opcode(m), "nolog") || !strcasecmp(owl_message_get_instance(m), "nolog")) return(0);
55
56  /* check direction */
57  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) {
58    return(0);
59  }
60  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) && owl_message_is_direction_in(m)) {
61    return(0);
62  }
63
64  if (owl_message_is_type_zephyr(m)) {
65    if (owl_message_is_personal(m) && !owl_global_is_logging(&g)) return(0);
66    if (!owl_message_is_personal(m) && !owl_global_is_classlogging(&g)) return(0);
67  } else {
68    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
69      if (!owl_global_is_logging(&g)) return(0);
70    } else {
71      if (!owl_global_is_classlogging(&g)) return(0);
72    }
73  }
74  return(1);
75}
76
77void owl_log_zephyr(owl_message *m, FILE *file) {
78    char *tmp;
79    tmp=short_zuser(owl_message_get_sender(m));
80    fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m));
81    if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m));
82    fprintf(file, "\n");
83    fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m));
84    fprintf(file, "From: %s <%s>\n\n", owl_message_get_zsig(m), tmp);
85    fprintf(file, "%s\n\n", owl_message_get_body(m));
86    owl_free(tmp);
87}
88
89void owl_log_jabber(owl_message *m, FILE *file) {
90    fprintf(file, "From: <%s> To: <%s>\n",owl_message_get_sender(m), owl_message_get_recipient(m));
91    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
92    fprintf(file, "%s\n\n",owl_message_get_body(m));
93}
94
95void owl_log_generic(owl_message *m, FILE *file) {
96    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
97    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
98    fprintf(file, "%s\n\n", owl_message_get_body(m));
99}
100
101void owl_log_append(owl_message *m, char *filename) {
102    FILE *file;
103    file=fopen(filename, "a");
104    if (!file) {
105        owl_function_error("Unable to open file for logging");
106        return;
107    }
108    if (owl_message_is_type_zephyr(m)) {
109        owl_log_zephyr(m, file);
110    } else if (owl_message_is_type_jabber(m)) {
111        owl_log_jabber(m, file);
112    } else {
113        owl_log_generic(m, file);
114    }
115    fclose(file);
116}
117
118void owl_log_outgoing(owl_message *m)
119{
120  char filename[MAXPATHLEN], *logpath;
121  char *to, *temp;
122
123  /* expand ~ in path names */
124  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
125
126  /* Figure out what path to log to */
127  if (owl_message_is_type_zephyr(m)) {
128    /* If this has CC's, do all but the "recipient" which we'll do below */
129    to = owl_message_get_cc_without_recipient(m);
130    if (to != NULL) {
131      temp = strtok(to, " ");
132      while (temp != NULL) {
133          temp = short_zuser(temp);
134          snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp);
135          owl_log_append(m, filename);
136          temp = strtok(NULL, " ");
137      }
138      owl_free(to);
139    }
140    to = short_zuser(owl_message_get_recipient(m));
141  } else if (owl_message_is_type_jabber(m)) {
142    to = owl_sprintf("jabber:%s", owl_message_get_recipient(m));
143  } else {
144    to = owl_sprintf("loopback");
145  }
146
147  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, to);
148  owl_log_append(m, filename);
149  owl_free(to);
150
151  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
152  owl_log_append(m, filename);
153  owl_free(logpath);
154}
155
156
157void owl_log_outgoing_zephyr_error(char *to, char *text)
158{
159  FILE *file;
160  char filename[MAXPATHLEN], *logpath;
161  char *tobuff, *zwriteline;
162  owl_message *m;
163
164  /* create a present message so we can pass it to
165   * owl_log_shouldlog_message()
166   */
167  zwriteline=owl_sprintf("zwrite %s", to);
168  m=owl_function_make_outgoing_zephyr(text, zwriteline, "");
169  owl_free(zwriteline);
170  if (!owl_log_shouldlog_message(m)) {
171    owl_message_free(m);
172    return;
173  }
174  owl_message_free(m);
175
176  /* chop off a local realm */
177  tobuff=short_zuser(to);
178
179  /* expand ~ in path names */
180  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
181
182  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
183  file=fopen(filename, "a");
184  if (!file) {
185    owl_function_error("Unable to open file for outgoing logging");
186    owl_free(logpath);
187    owl_free(tobuff);
188    return;
189  }
190  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
191  if (text[strlen(text)-1]!='\n') {
192    fprintf(file, "\n");
193  }
194  fclose(file);
195
196  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
197  owl_free(logpath);
198  file=fopen(filename, "a");
199  if (!file) {
200    owl_function_error("Unable to open file for outgoing logging");
201    owl_free(tobuff);
202    return;
203  }
204  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
205  if (text[strlen(text)-1]!='\n') {
206    fprintf(file, "\n");
207  }
208  fclose(file);
209
210  owl_free(tobuff);
211}
212
213void owl_log_incoming(owl_message *m)
214{
215  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
216  char *frombuff=NULL, *from=NULL;
217  int len, ch, i, personal;
218
219  /* figure out if it's a "personal" message or not */
220  if (owl_message_is_type_zephyr(m)) {
221    if (owl_message_is_personal(m)) {
222      personal = 1;
223    } else {
224      personal = 0;
225    }
226  } else if (owl_message_is_type_jabber(m)) {
227    /* This needs to be fixed to handle groupchat */
228    char* msgtype = owl_message_get_attribute_value(m,"jtype");
229    if (msgtype && !strcmp(msgtype,"groupchat")) {
230      personal = 0;
231    } else {
232      personal = 1;
233    }
234  } else {
235    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
236      personal = 1;
237    } else {
238      personal = 0;
239    }
240  }
241
242
243  if (owl_message_is_type_zephyr(m)) {
244    if (personal) {
245      from=frombuff=short_zuser(owl_message_get_sender(m));
246    } else {
247      from=frombuff=owl_strdup(owl_message_get_class(m));
248    }
249  } else if (owl_message_is_type_loopback(m)) {
250    from=frombuff=owl_strdup("loopback");
251  } else if (owl_message_is_type_jabber(m)) {
252    if (personal) {
253      from=frombuff=owl_sprintf("jabber:%s",owl_message_get_sender(m));
254    } else {
255      from=frombuff=owl_sprintf("jabber:%s",owl_message_get_recipient(m));
256    }
257  } else {
258    from=frombuff=owl_strdup("unknown");
259  }
260 
261  /* check for malicious sender formats */
262  len=strlen(frombuff);
263  if (len<1 || len>35) from="weird";
264  if (strchr(frombuff, '/')) from="weird";
265
266  ch=frombuff[0];
267  if (!g_ascii_isalnum(ch)) from="weird";
268
269  for (i=0; i<len; i++) {
270    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
271  }
272
273  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
274
275  if (!personal) {
276    if (strcmp(from, "weird")) {
277      char* temp = g_utf8_strdown(frombuff, -1);
278      if (temp) {
279        owl_free(frombuff);
280        from = frombuff = temp;
281      }
282    }
283  }
284
285  /* create the filename (expanding ~ in path names) */
286  if (personal) {
287    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
288    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
289    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
290    owl_log_append(m, allfilename);
291
292  } else {
293    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", owl_global_get_homedir(&g));
294    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
295  }
296
297  owl_log_append(m, filename);
298
299  if (personal && owl_message_is_type_zephyr(m)) {
300    /* We want to log to all of the CC'd people who were not us, or
301     * the sender, as well.
302     */
303    char *cc, *temp;
304    cc = owl_message_get_cc_without_recipient(m);
305    if (cc != NULL) {
306      temp = strtok(cc, " ");
307      while (temp != NULL) {
308        temp = short_zuser(temp);
309        if (strcasecmp(temp, frombuff) != 0) {
310          snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp);
311          owl_log_append(m, filename);
312        }
313        temp = strtok(NULL, " ");
314      }
315      owl_free(cc);
316    }
317  }
318
319  owl_free(frombuff);
320  owl_free(logpath);
321}
Note: See TracBrowser for help on using the repository browser.