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