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