Mercurial > pidgin
annotate src/log.c @ 4192:17187504bfc2
[gaim-migrate @ 4423]
include win32dep.h
committer: Tailor Script <tailor@pidgin.im>
author | Herman Bloggs <hermanator12002@yahoo.com> |
---|---|
date | Fri, 03 Jan 2003 20:49:12 +0000 |
parents | af2eeb7f7cf8 |
children | 4a0837c49d85 |
rev | line source |
---|---|
4184 | 1 /* --------------------------------------------------- |
2 * Function to remove a log file entry | |
3 * --------------------------------------------------- | |
4 */ | |
5 #include "gaim.h" | |
6 #include "core.h" | |
7 #include "multi.h" | |
8 #include "prpl.h" | |
9 #include <sys/stat.h> | |
10 | |
4192
17187504bfc2
[gaim-migrate @ 4423]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4184
diff
changeset
|
11 #ifdef _WIN32 |
17187504bfc2
[gaim-migrate @ 4423]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4184
diff
changeset
|
12 #include "win32dep.h" |
17187504bfc2
[gaim-migrate @ 4423]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4184
diff
changeset
|
13 #endif |
17187504bfc2
[gaim-migrate @ 4423]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4184
diff
changeset
|
14 |
4184 | 15 void rm_log(struct log_conversation *a) |
16 { | |
17 struct conversation *cnv = find_conversation(a->name); | |
18 | |
19 log_conversations = g_list_remove(log_conversations, a); | |
20 | |
21 save_prefs(); | |
22 | |
23 if (cnv && !(im_options & OPT_IM_ONE_WINDOW)) | |
24 set_convo_title(cnv); | |
25 } | |
26 | |
27 struct log_conversation *find_log_info(const char *name) | |
28 { | |
29 char *pname = g_malloc(BUF_LEN); | |
30 GList *lc = log_conversations; | |
31 struct log_conversation *l; | |
32 | |
33 | |
34 strcpy(pname, normalize(name)); | |
35 | |
36 while (lc) { | |
37 l = (struct log_conversation *)lc->data; | |
38 if (!g_strcasecmp(pname, normalize(l->name))) { | |
39 g_free(pname); | |
40 return l; | |
41 } | |
42 lc = lc->next; | |
43 } | |
44 g_free(pname); | |
45 return NULL; | |
46 } | |
47 | |
48 void update_log_convs() | |
49 { | |
50 GSList *C = connections; | |
51 struct gaim_connection *g; | |
52 GSList *bcs; | |
53 GList *cnv = conversations; | |
54 struct conversation *c; | |
55 | |
56 while (cnv) { | |
57 c = (struct conversation *)cnv->data; | |
58 if (c->log_button) { | |
59 if (c->is_chat) | |
60 gtk_widget_set_sensitive(GTK_WIDGET(c->log_button), | |
61 ((logging_options & OPT_LOG_CHATS)) ? FALSE : TRUE); | |
62 else | |
63 gtk_widget_set_sensitive(GTK_WIDGET(c->log_button), | |
64 ((logging_options & OPT_LOG_CONVOS)) ? FALSE : TRUE); | |
65 } | |
66 | |
67 cnv = cnv->next; | |
68 } | |
69 | |
70 while (C) { | |
71 g = (struct gaim_connection *)C->data; | |
72 bcs = g->buddy_chats; | |
73 while (bcs) { | |
74 c = (struct conversation *)bcs->data; | |
75 if (c->log_button) { | |
76 if (c->is_chat) | |
77 gtk_widget_set_sensitive(GTK_WIDGET(c->log_button), | |
78 ((logging_options & OPT_LOG_CHATS)) ? FALSE : | |
79 TRUE); | |
80 else | |
81 gtk_widget_set_sensitive(GTK_WIDGET(c->log_button), | |
82 ((logging_options & OPT_LOG_CONVOS)) ? FALSE : | |
83 TRUE); | |
84 } | |
85 | |
86 bcs = bcs->next; | |
87 } | |
88 C = C->next; | |
89 } | |
90 } | |
91 | |
92 static void do_save_convo(GtkObject *obj, GtkWidget *wid) | |
93 { | |
94 struct conversation *c = gtk_object_get_user_data(obj); | |
95 const char *filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(wid)); | |
96 FILE *f; | |
97 if (file_is_dir(filename, wid)) | |
98 return; | |
99 if (!((!c->is_chat && g_list_find(conversations, c)) || | |
100 (c->is_chat && g_slist_find(connections, c->gc) && g_slist_find(c->gc->buddy_chats, c)))) | |
101 filename = NULL; | |
102 gtk_widget_destroy(wid); | |
103 if (!filename) | |
104 return; | |
105 f = fopen(filename, "w+"); | |
106 if (!f) | |
107 return; | |
108 fprintf(f, "%s", c->history->str); | |
109 fclose(f); | |
110 } | |
111 | |
112 | |
113 void save_convo(GtkWidget *save, struct conversation *c) | |
114 { | |
115 char buf[BUF_LONG]; | |
116 GtkWidget *window = gtk_file_selection_new(_("Gaim - Save Conversation")); | |
117 g_snprintf(buf, sizeof(buf), "%s" G_DIR_SEPARATOR_S "%s.log", gaim_home_dir(), normalize(c->name)); | |
118 gtk_file_selection_set_filename(GTK_FILE_SELECTION(window), buf); | |
119 gtk_object_set_user_data(GTK_OBJECT(GTK_FILE_SELECTION(window)->ok_button), c); | |
120 g_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(window)->ok_button), | |
121 "clicked", G_CALLBACK(do_save_convo), window); | |
122 g_signal_connect_swapped(GTK_OBJECT(GTK_FILE_SELECTION(window)->cancel_button), | |
123 "clicked", G_CALLBACK(gtk_widget_destroy), (gpointer)window); | |
124 gtk_widget_show(window); | |
125 } | |
126 | |
127 char *html_logize(char *p) | |
128 { | |
129 char *temp_p = p; | |
130 char *buffer_p; | |
131 char *buffer_start; | |
132 int num_cr = 0; | |
133 int char_len = 0; | |
134 | |
135 while (*temp_p != '\0') { | |
136 char_len++; | |
137 if ((*temp_p == '\n') || ((*temp_p == '<') && (*(temp_p + 1) == '!'))) | |
138 num_cr++; | |
139 ++temp_p; | |
140 } | |
141 | |
142 temp_p = p; | |
143 buffer_p = g_malloc(char_len + (4 * num_cr) + 1); | |
144 buffer_start = buffer_p; | |
145 | |
146 while (*temp_p != '\0') { | |
147 if (*temp_p == '\n') { | |
148 *buffer_p++ = '<'; | |
149 *buffer_p++ = 'B'; | |
150 *buffer_p++ = 'R'; | |
151 *buffer_p++ = '>'; | |
152 *buffer_p++ = '\n'; | |
153 } else if ((*temp_p == '<') && (*(temp_p + 1) == '!')) { | |
154 *buffer_p++ = '&'; | |
155 *buffer_p++ = 'l'; | |
156 *buffer_p++ = 't'; | |
157 *buffer_p++ = ';'; | |
158 } else | |
159 *buffer_p++ = *temp_p; | |
160 ++temp_p; | |
161 } | |
162 *buffer_p = '\0'; | |
163 | |
164 return buffer_start; | |
165 } | |
166 | |
167 static FILE *open_gaim_log_file(const char *name, int *flag) | |
168 { | |
169 char *buf; | |
170 char *buf2; | |
171 char log_all_file[256]; | |
172 struct stat st; | |
173 FILE *fd; | |
174 #ifndef _WIN32 | |
175 int res; | |
176 #endif | |
177 gchar *gaim_dir; | |
178 | |
179 buf = g_malloc(BUF_LONG); | |
180 buf2 = g_malloc(BUF_LONG); | |
181 gaim_dir = gaim_user_dir(); | |
182 | |
183 /* Dont log yourself */ | |
184 strncpy(log_all_file, gaim_dir, 256); | |
185 | |
186 #ifndef _WIN32 | |
187 stat(log_all_file, &st); | |
188 if (!S_ISDIR(st.st_mode)) | |
189 unlink(log_all_file); | |
190 | |
191 fd = fopen(log_all_file, "r"); | |
192 | |
193 if (!fd) { | |
194 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); | |
195 if (res < 0) { | |
196 g_snprintf(buf, BUF_LONG, _("Unable to make directory %s for logging"), | |
197 log_all_file); | |
198 do_error_dialog(buf, NULL, GAIM_ERROR); | |
199 g_free(buf); | |
200 g_free(buf2); | |
201 return NULL; | |
202 } | |
203 } else | |
204 fclose(fd); | |
205 | |
206 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs", gaim_dir); | |
207 | |
208 if (stat(log_all_file, &st) < 0) | |
209 *flag = 1; | |
210 if (!S_ISDIR(st.st_mode)) | |
211 unlink(log_all_file); | |
212 | |
213 fd = fopen(log_all_file, "r"); | |
214 if (!fd) { | |
215 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); | |
216 if (res < 0) { | |
217 g_snprintf(buf, BUF_LONG, _("Unable to make directory %s for logging"), | |
218 log_all_file); | |
219 do_error_dialog(buf, NULL, GAIM_ERROR); | |
220 g_free(buf); | |
221 g_free(buf2); | |
222 return NULL; | |
223 } | |
224 } else | |
225 fclose(fd); | |
226 #else /* _WIN32 */ | |
227 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs", gaim_dir); | |
228 | |
229 if( _mkdir(log_all_file) < 0 && errno != EEXIST ) { | |
230 g_snprintf(buf, BUF_LONG, _("Unable to make directory %s for logging"), log_all_file); | |
231 do_error_dialog(buf, NULL, GAIM_ERROR); | |
232 g_free(buf); | |
233 g_free(buf2); | |
234 return NULL; | |
235 } | |
236 #endif | |
237 | |
238 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs" G_DIR_SEPARATOR_S "%s", gaim_dir, name); | |
239 if (stat(log_all_file, &st) < 0) | |
240 *flag = 1; | |
241 | |
242 debug_printf("Logging to: \"%s\"\n", log_all_file); | |
243 | |
244 fd = fopen(log_all_file, "a"); | |
245 | |
246 g_free(buf); | |
247 g_free(buf2); | |
248 return fd; | |
249 } | |
250 | |
251 static FILE *open_system_log_file(char *name) | |
252 { | |
253 int x; | |
254 | |
255 if (name) | |
256 return open_log_file(name, 2); | |
257 else | |
258 return open_gaim_log_file("system", &x); | |
259 } | |
260 | |
261 FILE *open_log_file(const char *name, int is_chat) | |
262 { | |
263 struct stat st; | |
264 char realname[256]; | |
265 struct log_conversation *l; | |
266 FILE *fd; | |
267 int flag = 0; | |
268 | |
269 if (((is_chat == 2) && !(logging_options & OPT_LOG_INDIVIDUAL)) | |
270 || ((is_chat == 1) && !(logging_options & OPT_LOG_CHATS)) | |
271 || ((is_chat == 0) && !(logging_options & OPT_LOG_CONVOS))) { | |
272 | |
273 l = find_log_info(name); | |
274 if (!l) | |
275 return NULL; | |
276 | |
277 if (stat(l->filename, &st) < 0) | |
278 flag = 1; | |
279 | |
280 fd = fopen(l->filename, "a"); | |
281 | |
282 if (flag) { /* is a new file */ | |
283 if (logging_options & OPT_LOG_STRIP_HTML) { | |
284 fprintf(fd, _("IM Sessions with %s\n"), name); | |
285 } else { | |
286 fprintf(fd, "<HTML><HEAD><TITLE>"); | |
287 fprintf(fd, _("IM Sessions with %s"), name); | |
288 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); | |
289 } | |
290 } | |
291 | |
292 return fd; | |
293 } | |
294 | |
295 g_snprintf(realname, sizeof(realname), "%s.log", normalize(name)); | |
296 fd = open_gaim_log_file(realname, &flag); | |
297 | |
298 if (fd && flag) { /* is a new file */ | |
299 if (logging_options & OPT_LOG_STRIP_HTML) { | |
300 fprintf(fd, _("IM Sessions with %s\n"), name); | |
301 } else { | |
302 fprintf(fd, "<HTML><HEAD><TITLE>"); | |
303 fprintf(fd, _("IM Sessions with %s"), name); | |
304 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); | |
305 } | |
306 } | |
307 | |
308 return fd; | |
309 } | |
310 | |
311 void system_log(enum log_event what, struct gaim_connection *gc, | |
312 struct buddy *who, int why) | |
313 { | |
314 FILE *fd; | |
315 char text[256], html[256]; | |
316 | |
317 if ((logging_options & why) != why) | |
318 return; | |
319 | |
320 if (logging_options & OPT_LOG_INDIVIDUAL) { | |
321 if (why & OPT_LOG_MY_SIGNON) | |
322 fd = open_system_log_file(gc ? gc->username : NULL); | |
323 else | |
324 fd = open_system_log_file(who->name); | |
325 } else | |
326 fd = open_system_log_file(NULL); | |
327 | |
328 if (!fd) | |
329 return; | |
330 | |
331 if (why & OPT_LOG_MY_SIGNON) { | |
332 switch (what) { | |
333 case log_signon: | |
334 g_snprintf(text, sizeof(text), "+++ %s (%s) signed on @ %s", | |
335 gc->username, gc->prpl->name, full_date()); | |
336 g_snprintf(html, sizeof(html), "<B>%s</B>", text); | |
337 break; | |
338 case log_signoff: | |
339 g_snprintf(text, sizeof(text), "+++ %s (%s) signed off @ %s", | |
340 gc->username, gc->prpl->name, full_date()); | |
341 g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); | |
342 break; | |
343 case log_away: | |
344 g_snprintf(text, sizeof(text), "+++ %s (%s) changed away state @ %s", | |
345 gc->username, gc->prpl->name, full_date()); | |
346 g_snprintf(html, sizeof(html), "<FONT COLOR=OLIVE>%s</FONT>", text); | |
347 break; | |
348 case log_back: | |
349 g_snprintf(text, sizeof(text), "+++ %s (%s) came back @ %s", | |
350 gc->username, gc->prpl->name, full_date()); | |
351 g_snprintf(html, sizeof(html), "%s", text); | |
352 break; | |
353 case log_idle: | |
354 g_snprintf(text, sizeof(text), "+++ %s (%s) became idle @ %s", | |
355 gc->username, gc->prpl->name, full_date()); | |
356 g_snprintf(html, sizeof(html), "<FONT COLOR=GRAY>%s</FONT>", text); | |
357 break; | |
358 case log_unidle: | |
359 g_snprintf(text, sizeof(text), "+++ %s (%s) returned from idle @ %s", | |
360 gc->username, gc->prpl->name, full_date()); | |
361 g_snprintf(html, sizeof(html), "%s", text); | |
362 break; | |
363 case log_quit: | |
364 g_snprintf(text, sizeof(text), "+++ Program exit @ %s", full_date()); | |
365 g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); | |
366 break; | |
367 } | |
368 } else if (strcmp(who->name, who->show)) { | |
369 switch (what) { | |
370 case log_signon: | |
371 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) signed on @ %s", | |
372 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
373 g_snprintf(html, sizeof(html), "<B>%s</B>", text); | |
374 break; | |
375 case log_signoff: | |
376 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) signed off @ %s", | |
377 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
378 g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); | |
379 break; | |
380 case log_away: | |
381 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) went away @ %s", | |
382 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
383 g_snprintf(html, sizeof(html), "<FONT COLOR=OLIVE>%s</FONT>", text); | |
384 break; | |
385 case log_back: | |
386 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) came back @ %s", | |
387 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
388 g_snprintf(html, sizeof(html), "%s", text); | |
389 break; | |
390 case log_idle: | |
391 g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) became idle @ %s", | |
392 gc->username, gc->prpl->name, who->show, who->name, full_date()); | |
393 g_snprintf(html, sizeof(html), "<FONT COLOR=GRAY>%s</FONT>", text); | |
394 break; | |
395 case log_unidle: | |
396 g_snprintf(text, sizeof(text), | |
397 "%s (%s) reported that %s (%s) returned from idle @ %s", gc->username, | |
398 gc->prpl->name, who->show, who->name, full_date()); | |
399 g_snprintf(html, sizeof(html), "%s", text); | |
400 break; | |
401 default: | |
402 fclose(fd); | |
403 return; | |
404 break; | |
405 } | |
406 } else { | |
407 switch (what) { | |
408 case log_signon: | |
409 g_snprintf(text, sizeof(text), "%s (%s) reported that %s signed on @ %s", | |
410 gc->username, gc->prpl->name, who->name, full_date()); | |
411 g_snprintf(html, sizeof(html), "<B>%s</B>", text); | |
412 break; | |
413 case log_signoff: | |
414 g_snprintf(text, sizeof(text), "%s (%s) reported that %s signed off @ %s", | |
415 gc->username, gc->prpl->name, who->name, full_date()); | |
416 g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); | |
417 break; | |
418 case log_away: | |
419 g_snprintf(text, sizeof(text), "%s (%s) reported that %s went away @ %s", | |
420 gc->username, gc->prpl->name, who->name, full_date()); | |
421 g_snprintf(html, sizeof(html), "<FONT COLOR=OLIVE>%s</FONT>", text); | |
422 break; | |
423 case log_back: | |
424 g_snprintf(text, sizeof(text), "%s (%s) reported that %s came back @ %s", | |
425 gc->username, gc->prpl->name, who->name, full_date()); | |
426 g_snprintf(html, sizeof(html), "%s", text); | |
427 break; | |
428 case log_idle: | |
429 g_snprintf(text, sizeof(text), "%s (%s) reported that %s became idle @ %s", | |
430 gc->username, gc->prpl->name, who->name, full_date()); | |
431 g_snprintf(html, sizeof(html), "<FONT COLOR=GRAY>%s</FONT>", text); | |
432 break; | |
433 case log_unidle: | |
434 g_snprintf(text, sizeof(text), | |
435 "%s (%s) reported that %s returned from idle @ %s", gc->username, | |
436 gc->prpl->name, who->name, full_date()); | |
437 g_snprintf(html, sizeof(html), "%s", text); | |
438 break; | |
439 default: | |
440 fclose(fd); | |
441 return; | |
442 break; | |
443 } | |
444 } | |
445 | |
446 if (logging_options & OPT_LOG_STRIP_HTML) { | |
447 fprintf(fd, "---- %s ----\n", text); | |
448 } else { | |
449 if (logging_options & OPT_LOG_INDIVIDUAL) | |
450 fprintf(fd, "<HR>%s<BR><HR><BR>\n", html); | |
451 else | |
452 fprintf(fd, "%s<BR>\n", html); | |
453 } | |
454 | |
455 fclose(fd); | |
456 } |