comparison src/log.c @ 4184:af2eeb7f7cf8

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