source: logging.c @ ee310eb

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since ee310eb was ee310eb, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 16 years ago
Merged revisions 909-923 via svnmerge from file:///afs/sipb.mit.edu/project/barnowl/src/svn/trunk ........ r910 | nelhage | 2008-01-29 20:47:41 -0500 (Tue, 29 Jan 2008) | 1 line Fix IRC -- I forgot to add one ->conn in the last patch ........ r911 | asedeno | 2008-01-30 15:49:35 -0500 (Wed, 30 Jan 2008) | 5 lines Jabber Buddy Lists: * Query the jabber:show_offline_buddies once when invoking onGetBuddyList() * Don't bold online roster entries when hiding offline ones ........ r922 | asedeno | 2008-02-03 00:49:46 -0500 (Sun, 03 Feb 2008) | 1 line Bounds checking. ........ r923 | asedeno | 2008-02-03 01:01:07 -0500 (Sun, 03 Feb 2008) | 1 line Portability - removing C++ style comments. ........
  • Property mode set to 100644
File size: 10.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_aim(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    if (owl_message_is_login(m))
93        fprintf(file, "LOGIN\n\n");
94    else if (owl_message_is_logout(m))
95        fprintf(file, "LOGOUT\n\n");
96    else
97        fprintf(file, "%s\n\n", owl_message_get_body(m));
98}
99
100void owl_log_jabber(owl_message *m, FILE *file) {
101    fprintf(file, "From: <%s> To: <%s>\n",owl_message_get_sender(m), owl_message_get_recipient(m));
102    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
103    fprintf(file, "%s\n\n",owl_message_get_body(m));
104}
105
106void owl_log_generic(owl_message *m, FILE *file) {
107    fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m));
108    fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m));
109    fprintf(file, "%s\n\n", owl_message_get_body(m));
110}
111
112void owl_log_append(owl_message *m, char *filename) {
113    FILE *file;
114    file=fopen(filename, "a");
115    if (!file) {
116        owl_function_error("Unable to open file for logging");
117        return;
118    }
119    if (owl_message_is_type_zephyr(m)) {
120        owl_log_zephyr(m, file);
121    } else if (owl_message_is_type_jabber(m)) {
122        owl_log_jabber(m, file);
123    } else if (owl_message_is_type_aim(m)) {
124        owl_log_aim(m, file);
125    } else {
126        owl_log_generic(m, file);
127    }
128    fclose(file);
129}
130
131void owl_log_outgoing(owl_message *m)
132{
133  char filename[MAXPATHLEN], *logpath;
134  char *to, *temp;
135
136  /* expand ~ in path names */
137  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
138
139  /* Figure out what path to log to */
140  if (owl_message_is_type_zephyr(m)) {
141    /* If this has CC's, do all but the "recipient" which we'll do below */
142    to = owl_message_get_cc_without_recipient(m);
143    if (to != NULL) {
144      temp = strtok(to, " ");
145      while (temp != NULL) {
146          temp = short_zuser(temp);
147          snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp);
148          owl_log_append(m, filename);
149          temp = strtok(NULL, " ");
150      }
151      owl_free(to);
152    }
153    to = short_zuser(owl_message_get_recipient(m));
154  } else if (owl_message_is_type_jabber(m)) {
155    to = owl_sprintf("jabber:%s", owl_message_get_recipient(m));
156  } else if (owl_message_is_type_aim(m)) {
157    char *temp2;
158    temp = owl_aim_normalize_screenname(owl_message_get_recipient(m));
159    temp2 = g_utf8_strdown(temp,-1);
160    to = owl_sprintf("aim:%s", temp2);
161    owl_free(temp2);
162    owl_free(temp);
163  } else {
164    to = owl_sprintf("loopback");
165  }
166
167  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, to);
168  owl_log_append(m, filename);
169  owl_free(to);
170
171  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
172  owl_log_append(m, filename);
173  owl_free(logpath);
174}
175
176
177void owl_log_outgoing_zephyr_error(char *to, char *text)
178{
179  FILE *file;
180  char filename[MAXPATHLEN], *logpath;
181  char *tobuff, *zwriteline;
182  owl_message *m;
183
184  /* create a present message so we can pass it to
185   * owl_log_shouldlog_message()
186   */
187  zwriteline=owl_sprintf("zwrite %s", to);
188  m=owl_function_make_outgoing_zephyr(text, zwriteline, "");
189  owl_free(zwriteline);
190  if (!owl_log_shouldlog_message(m)) {
191    owl_message_free(m);
192    return;
193  }
194  owl_message_free(m);
195
196  /* chop off a local realm */
197  tobuff=short_zuser(to);
198
199  /* expand ~ in path names */
200  logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
201
202  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
203  file=fopen(filename, "a");
204  if (!file) {
205    owl_function_error("Unable to open file for outgoing logging");
206    owl_free(logpath);
207    owl_free(tobuff);
208    return;
209  }
210  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
211  if (text[strlen(text)-1]!='\n') {
212    fprintf(file, "\n");
213  }
214  fclose(file);
215
216  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
217  owl_free(logpath);
218  file=fopen(filename, "a");
219  if (!file) {
220    owl_function_error("Unable to open file for outgoing logging");
221    owl_free(tobuff);
222    return;
223  }
224  fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text);
225  if (text[strlen(text)-1]!='\n') {
226    fprintf(file, "\n");
227  }
228  fclose(file);
229
230  owl_free(tobuff);
231}
232
233void owl_log_incoming(owl_message *m)
234{
235  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
236  char *frombuff=NULL, *from=NULL;
237  int len, ch, i, personal;
238
239  /* figure out if it's a "personal" message or not */
240  if (owl_message_is_type_zephyr(m)) {
241    if (owl_message_is_personal(m)) {
242      personal = 1;
243    } else {
244      personal = 0;
245    }
246  } else if (owl_message_is_type_jabber(m)) {
247    /* This needs to be fixed to handle groupchat */
248    char* msgtype = owl_message_get_attribute_value(m,"jtype");
249    if (msgtype && !strcmp(msgtype,"groupchat")) {
250      personal = 0;
251    } else {
252      personal = 1;
253    }
254  } else {
255    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
256      personal = 1;
257    } else {
258      personal = 0;
259    }
260  }
261
262
263  if (owl_message_is_type_zephyr(m)) {
264    if (personal) {
265      from=frombuff=short_zuser(owl_message_get_sender(m));
266    } else {
267      from=frombuff=owl_strdup(owl_message_get_class(m));
268    }
269  } else if (owl_message_is_type_aim(m)) {
270    /* we do not yet handle chat rooms */
271    char *normalto, *temp;
272    temp = owl_aim_normalize_screenname(owl_message_get_sender(m));
273    normalto = g_utf8_strdown(temp, -1);
274    from=frombuff=owl_sprintf("aim:%s", normalto);
275    owl_free(normalto);
276    owl_free(temp);
277  } else if (owl_message_is_type_loopback(m)) {
278    from=frombuff=owl_strdup("loopback");
279  } else if (owl_message_is_type_jabber(m)) {
280    if (personal) {
281      from=frombuff=owl_sprintf("jabber:%s",owl_message_get_sender(m));
282    } else {
283      from=frombuff=owl_sprintf("jabber:%s",owl_message_get_recipient(m));
284    }
285  } else {
286    from=frombuff=owl_strdup("unknown");
287  }
288 
289  /* check for malicious sender formats */
290  len=strlen(frombuff);
291  if (len<1 || len>35) from="weird";
292  if (strchr(frombuff, '/')) from="weird";
293
294  ch=frombuff[0];
295  if (!g_ascii_isalnum(ch)) from="weird";
296
297  for (i=0; i<len; i++) {
298    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
299  }
300
301  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
302
303  if (!personal) {
304    if (strcmp(from, "weird")) {
305      char* temp = g_utf8_strdown(frombuff, -1);
306      if (temp) {
307        owl_free(frombuff);
308        from = frombuff = temp;
309      }
310    }
311  }
312
313  /* create the filename (expanding ~ in path names) */
314  if (personal) {
315    logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g));
316    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
317    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
318    owl_log_append(m, allfilename);
319
320  } else {
321    logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", owl_global_get_homedir(&g));
322    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
323  }
324
325  owl_log_append(m, filename);
326
327  if (personal && owl_message_is_type_zephyr(m)) {
328    /* We want to log to all of the CC'd people who were not us, or
329     * the sender, as well.
330     */
331    char *cc, *temp;
332    cc = owl_message_get_cc_without_recipient(m);
333    if (cc != NULL) {
334      temp = strtok(cc, " ");
335      while (temp != NULL) {
336        temp = short_zuser(temp);
337        if (strcasecmp(temp, frombuff) != 0) {
338          snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp);
339          owl_log_append(m, filename);
340        }
341        temp = strtok(NULL, " ");
342      }
343      owl_free(cc);
344    }
345  }
346
347  owl_free(frombuff);
348  owl_free(logpath);
349}
Note: See TracBrowser for help on using the repository browser.