1 | #include "owl.h" |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <ctype.h> |
---|
5 | #include <sys/param.h> |
---|
6 | |
---|
7 | static const char fileIdent[] = "$Id$"; |
---|
8 | |
---|
9 | void owl_log_outgoing_zephyr(char *to, char *text) |
---|
10 | { |
---|
11 | FILE *file; |
---|
12 | char filename[MAXPATHLEN], *logpath; |
---|
13 | char *tobuff, *ptr=""; |
---|
14 | |
---|
15 | if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return; |
---|
16 | |
---|
17 | tobuff=owl_malloc(strlen(to)+20); |
---|
18 | strcpy(tobuff, to); |
---|
19 | |
---|
20 | /* chop off a local realm */ |
---|
21 | ptr=strchr(tobuff, '@'); |
---|
22 | if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) { |
---|
23 | *ptr='\0'; |
---|
24 | } |
---|
25 | |
---|
26 | /* expand ~ in path names */ |
---|
27 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", |
---|
28 | owl_global_get_homedir(&g)); |
---|
29 | |
---|
30 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff); |
---|
31 | file=fopen(filename, "a"); |
---|
32 | if (!file) { |
---|
33 | owl_function_error("Unable to open file for outgoing logging"); |
---|
34 | owl_free(logpath); |
---|
35 | return; |
---|
36 | } |
---|
37 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
38 | if (text[strlen(text)-1]!='\n') { |
---|
39 | fprintf(file, "\n"); |
---|
40 | } |
---|
41 | fclose(file); |
---|
42 | |
---|
43 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
44 | owl_free(logpath); |
---|
45 | file=fopen(filename, "a"); |
---|
46 | if (!file) { |
---|
47 | owl_function_error("Unable to open file for outgoing logging"); |
---|
48 | return; |
---|
49 | } |
---|
50 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
51 | if (text[strlen(text)-1]!='\n') { |
---|
52 | fprintf(file, "\n"); |
---|
53 | } |
---|
54 | fclose(file); |
---|
55 | |
---|
56 | owl_free(tobuff); |
---|
57 | } |
---|
58 | |
---|
59 | void owl_log_outgoing_aim(char *to, char *text) |
---|
60 | { |
---|
61 | FILE *file; |
---|
62 | char filename[MAXPATHLEN], *logpath; |
---|
63 | char *tobuff; |
---|
64 | |
---|
65 | if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return; |
---|
66 | |
---|
67 | tobuff=owl_sprintf("aim:%s", to); |
---|
68 | |
---|
69 | /* expand ~ in path names */ |
---|
70 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", |
---|
71 | owl_global_get_homedir(&g)); |
---|
72 | |
---|
73 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff); |
---|
74 | file=fopen(filename, "a"); |
---|
75 | if (!file) { |
---|
76 | owl_function_error("Unable to open file for outgoing logging"); |
---|
77 | owl_free(logpath); |
---|
78 | return; |
---|
79 | } |
---|
80 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
81 | if (text[strlen(text)-1]!='\n') { |
---|
82 | fprintf(file, "\n"); |
---|
83 | } |
---|
84 | fclose(file); |
---|
85 | |
---|
86 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
87 | owl_free(logpath); |
---|
88 | file=fopen(filename, "a"); |
---|
89 | if (!file) { |
---|
90 | owl_function_error("Unable to open file for outgoing logging"); |
---|
91 | return; |
---|
92 | } |
---|
93 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
94 | if (text[strlen(text)-1]!='\n') { |
---|
95 | fprintf(file, "\n"); |
---|
96 | } |
---|
97 | fclose(file); |
---|
98 | |
---|
99 | owl_free(tobuff); |
---|
100 | } |
---|
101 | |
---|
102 | void owl_log_outgoing_loopback(char *text) |
---|
103 | { |
---|
104 | FILE *file; |
---|
105 | char filename[MAXPATHLEN], *logpath; |
---|
106 | char *tobuff; |
---|
107 | |
---|
108 | if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return; |
---|
109 | |
---|
110 | tobuff=owl_sprintf("loopback"); |
---|
111 | |
---|
112 | /* expand ~ in path names */ |
---|
113 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", |
---|
114 | owl_global_get_homedir(&g)); |
---|
115 | |
---|
116 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff); |
---|
117 | file=fopen(filename, "a"); |
---|
118 | if (!file) { |
---|
119 | owl_function_error("Unable to open file for outgoing logging"); |
---|
120 | owl_free(logpath); |
---|
121 | return; |
---|
122 | } |
---|
123 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
124 | if (text[strlen(text)-1]!='\n') { |
---|
125 | fprintf(file, "\n"); |
---|
126 | } |
---|
127 | fclose(file); |
---|
128 | |
---|
129 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
130 | owl_free(logpath); |
---|
131 | file=fopen(filename, "a"); |
---|
132 | if (!file) { |
---|
133 | owl_function_error("Unable to open file for outgoing logging"); |
---|
134 | return; |
---|
135 | } |
---|
136 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
137 | if (text[strlen(text)-1]!='\n') { |
---|
138 | fprintf(file, "\n"); |
---|
139 | } |
---|
140 | fclose(file); |
---|
141 | |
---|
142 | owl_free(tobuff); |
---|
143 | } |
---|
144 | |
---|
145 | void owl_log_incoming(owl_message *m) |
---|
146 | { |
---|
147 | FILE *file, *allfile; |
---|
148 | char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath; |
---|
149 | char *frombuff=NULL, *from=NULL, *buff=NULL, *ptr; |
---|
150 | int len, ch, i, personal; |
---|
151 | |
---|
152 | if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) return; |
---|
153 | |
---|
154 | /* check for nolog */ |
---|
155 | if (!strcasecmp(owl_message_get_opcode(m), "nolog") || |
---|
156 | !strcasecmp(owl_message_get_instance(m), "nolog")) return; |
---|
157 | |
---|
158 | /* this is kind of a mess */ |
---|
159 | if (owl_message_is_type_zephyr(m)) { |
---|
160 | if (owl_message_is_personal(m)) { |
---|
161 | personal=1; |
---|
162 | if (!owl_global_is_logging(&g)) return; |
---|
163 | } else { |
---|
164 | personal=0; |
---|
165 | if (!owl_global_is_classlogging(&g)) return; |
---|
166 | } |
---|
167 | } else { |
---|
168 | if (owl_message_is_private(m) || owl_message_is_loginout(m)) { |
---|
169 | personal=1; |
---|
170 | if (!owl_global_is_logging(&g)) return; |
---|
171 | } else { |
---|
172 | personal=0; |
---|
173 | if (!owl_global_is_classlogging(&g)) return; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | if (owl_message_is_type_zephyr(m)) { |
---|
178 | /* chop off a local realm for personal zephyr messages */ |
---|
179 | if (personal) { |
---|
180 | if (owl_message_is_type_zephyr(m)) { |
---|
181 | from=frombuff=owl_strdup(owl_message_get_sender(m)); |
---|
182 | ptr=strchr(frombuff, '@'); |
---|
183 | if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) { |
---|
184 | *ptr='\0'; |
---|
185 | } |
---|
186 | } |
---|
187 | } else { |
---|
188 | /* we assume zephyr for now */ |
---|
189 | from=frombuff=owl_strdup(owl_message_get_class(m)); |
---|
190 | } |
---|
191 | } else if (owl_message_is_type_aim(m)) { |
---|
192 | /* we do not yet handle chat rooms */ |
---|
193 | from=frombuff=owl_sprintf("aim:%s", owl_message_get_sender(m)); |
---|
194 | } else if (owl_message_is_type_loopback(m)) { |
---|
195 | from=frombuff=owl_strdup("loopback"); |
---|
196 | } else { |
---|
197 | from=frombuff=owl_strdup("unknown"); |
---|
198 | } |
---|
199 | |
---|
200 | /* check for malicious sender formats */ |
---|
201 | len=strlen(frombuff); |
---|
202 | if (len<1 || len>35) from="weird"; |
---|
203 | if (strchr(frombuff, '/')) from="weird"; |
---|
204 | |
---|
205 | ch=frombuff[0]; |
---|
206 | if (!isalnum(ch)) from="weird"; |
---|
207 | |
---|
208 | for (i=0; i<len; i++) { |
---|
209 | if (frombuff[i]<'!' || frombuff[i]>='~') from="weird"; |
---|
210 | } |
---|
211 | |
---|
212 | if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird"; |
---|
213 | |
---|
214 | if (!personal) { |
---|
215 | if (strcmp(from, "weird")) downstr(from); |
---|
216 | } |
---|
217 | |
---|
218 | /* create the filename (expanding ~ in path names) */ |
---|
219 | if (personal) { |
---|
220 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", |
---|
221 | owl_global_get_homedir(&g)); |
---|
222 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from); |
---|
223 | snprintf(allfilename, MAXPATHLEN, "%s/all", logpath); |
---|
224 | |
---|
225 | } else { |
---|
226 | logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", |
---|
227 | owl_global_get_homedir(&g)); |
---|
228 | |
---|
229 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from); |
---|
230 | } |
---|
231 | owl_free(logpath); |
---|
232 | |
---|
233 | file=fopen(filename, "a"); |
---|
234 | if (!file) { |
---|
235 | owl_function_error("Unable to open file for incoming logging"); |
---|
236 | return; |
---|
237 | } |
---|
238 | |
---|
239 | allfile=NULL; |
---|
240 | if (personal) { |
---|
241 | allfile=fopen(allfilename, "a"); |
---|
242 | if (!allfile) { |
---|
243 | owl_function_error("Unable to open file for incoming logging"); |
---|
244 | fclose(file); |
---|
245 | return; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | /* write to the main file */ |
---|
250 | if (owl_message_is_type_zephyr(m)) { |
---|
251 | char *tmp; |
---|
252 | |
---|
253 | tmp=short_zuser(owl_message_get_sender(m)); |
---|
254 | fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m)); |
---|
255 | if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m)); |
---|
256 | fprintf(file, "\n"); |
---|
257 | fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m)); |
---|
258 | ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i); |
---|
259 | buff=owl_malloc(i+10); |
---|
260 | memcpy(buff, ptr, i); |
---|
261 | buff[i]='\0'; |
---|
262 | fprintf(file, "From: %s <%s>\n\n", buff, tmp); |
---|
263 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
264 | owl_free(tmp); |
---|
265 | } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) { |
---|
266 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
267 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
268 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
269 | } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) { |
---|
270 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
271 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
272 | if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n"); |
---|
273 | if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n"); |
---|
274 | } else { |
---|
275 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
276 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
277 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
278 | } |
---|
279 | |
---|
280 | fclose(file); |
---|
281 | |
---|
282 | /* if it's a personal message, also write to the 'all' file */ |
---|
283 | if (personal) { |
---|
284 | if (owl_message_is_type_zephyr(m)) { |
---|
285 | char *tmp; |
---|
286 | |
---|
287 | tmp=short_zuser(owl_message_get_sender(m)); |
---|
288 | fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m)); |
---|
289 | if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m)); |
---|
290 | fprintf(allfile, "\n"); |
---|
291 | fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m)); |
---|
292 | fprintf(allfile, "From: %s <%s>\n\n", buff, tmp); |
---|
293 | fprintf(allfile, "%s\n\n", owl_message_get_body(m)); |
---|
294 | owl_free(tmp); |
---|
295 | } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) { |
---|
296 | fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
297 | fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
298 | fprintf(allfile, "%s\n\n", owl_message_get_body(m)); |
---|
299 | } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) { |
---|
300 | fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
301 | fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
302 | if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n"); |
---|
303 | if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n"); |
---|
304 | } else { |
---|
305 | fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
306 | fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
307 | fprintf(allfile, "%s\n\n", owl_message_get_body(m)); |
---|
308 | } |
---|
309 | fclose(allfile); |
---|
310 | } |
---|
311 | |
---|
312 | if (owl_message_is_type_zephyr(m)) { |
---|
313 | owl_free(buff); |
---|
314 | } |
---|
315 | owl_free(frombuff); |
---|
316 | } |
---|