Sat, 12 Oct 2002 18:48:33 +0000
[gaim-migrate @ 3784]
history plugin segfault fix courtesy of jstuart
committer: Luke Schierer <lschiere@pidgin.im>
| 3598 | 1 | /* Puts last 4k of log in new conversations a la Everybuddy (and then |
| 2 | * stolen by Trillian "Pro") */ | |
| 3 | ||
| 4 | #define GAIM_PLUGINS | |
| 5 | #include "gaim.h" | |
| 6 | #include "gtkimhtml.h" | |
| 7 | #include <sys/stat.h> | |
| 8 | #include <unistd.h> | |
| 9 | ||
| 10 | #define HISTORY_SIZE (4 * 1024) | |
| 11 | ||
| 12 | GModule *handle; | |
| 13 | ||
| 14 | void historize (char *name, void *data) | |
| 15 | { | |
| 16 | struct conversation *c = find_conversation(name); | |
| 17 | struct stat st; | |
| 18 | FILE *fd; | |
| 3655 | 19 | char *userdir = g_strdup(gaim_user_dir()); |
| 3598 | 20 | char *logfile = g_strdup_printf("%s.log", normalize(name)); |
| 21 | char *path = g_build_filename(userdir, "logs", logfile, NULL); | |
| 22 | char buf[HISTORY_SIZE+1]; | |
| 23 | char *tmp; | |
| 24 | int size; | |
| 3602 | 25 | GtkIMHtmlOptions options = GTK_IMHTML_NO_COLOURS; |
| 3598 | 26 | |
| 27 | if (stat(path, &st) || S_ISDIR(st.st_mode) || st.st_size == 0 || | |
| 28 | !(fd = fopen(path, "r"))) { | |
| 29 | g_free(userdir); | |
| 30 | g_free(logfile); | |
| 31 | g_free(path); | |
| 32 | return; | |
| 33 | } | |
| 34 | ||
| 35 | fseek(fd, st.st_size > HISTORY_SIZE ? st.st_size - HISTORY_SIZE : st.st_size, SEEK_SET); | |
| 36 | size = fread(buf, 1, HISTORY_SIZE, fd); | |
| 37 | tmp = buf; | |
| 38 | tmp[size] = 0; | |
| 39 | ||
| 40 | /* start the history at a newline */ | |
| 41 | while (*tmp && *tmp != '\n') | |
| 42 | tmp++; | |
| 43 | ||
| 44 | if (*tmp) tmp++; | |
| 3602 | 45 | |
| 46 | if(*tmp == '<') | |
| 47 | options |= GTK_IMHTML_NO_NEWLINE; | |
| 48 | ||
| 49 | gtk_imhtml_append_text(GTK_IMHTML(c->text), tmp, strlen(tmp), options); | |
| 3598 | 50 | |
| 51 | g_free(userdir); | |
| 52 | g_free(logfile); | |
| 53 | g_free(path); | |
| 54 | } | |
| 55 | ||
| 56 | char *gaim_plugin_init(GModule *h) { | |
| 57 | handle = h; | |
| 58 | ||
| 59 | gaim_signal_connect(handle, event_new_conversation, historize, NULL); | |
| 60 | ||
| 61 | return NULL; | |
| 62 | } | |
| 63 | ||
| 64 | struct gaim_plugin_description desc; | |
| 65 | struct gaim_plugin_description *gaim_plugin_desc() { | |
| 66 | desc.api_version = PLUGIN_API_VERSION; | |
| 67 | desc.name = g_strdup("History"); | |
| 68 | desc.version = g_strdup(VERSION); | |
| 69 | desc.description = g_strdup("Shows recently logged conversations in new conversations "); | |
| 70 | desc.authors = g_strdup("Sean Egan <bj91704@binghamton.edu>"); | |
| 71 | desc.url = g_strdup(WEBSITE); | |
| 72 | return &desc; | |
| 73 | } |