Mercurial > pidgin
annotate plugins/chkmail.c @ 10462:f7b32dd67bdf
[gaim-migrate @ 11735]
Fix for bug 1027454: Blank "Unable to open socket" window if locale is not UTF-8
committer: Tailor Script <tailor@pidgin.im>
| author | Stu Tomlinson <stu@nosnilmot.com> |
|---|---|
| date | Fri, 31 Dec 2004 15:34:18 +0000 |
| parents | 61852117568f |
| children |
| rev | line source |
|---|---|
| 114 | 1 /* This is some funky code. It is still being developed by Rob Flynn - rob@linuxpimps.com |
| 2 * I recommend not using this code right now. :) | |
| 3 */ | |
| 4 | |
| 105 | 5 #define GAIM_PLUGINS |
| 6 | |
| 7 #include <stdio.h> | |
| 114 | 8 #include <stdlib.h> |
| 9 #include <unistd.h> | |
| 10 #include <string.h> | |
| 11 #include <netinet/in.h> | |
| 12 #include <arpa/inet.h> | |
| 13 #include <sys/socket.h> | |
| 14 #include <netdb.h> | |
| 15 #include <netinet/in.h> | |
| 127 | 16 #include <pthread.h> |
| 9821 | 17 |
| 18 #include "internal.h" | |
| 19 #include "gtkgaim.h" | |
| 20 | |
| 114 | 21 char username[] = ""; |
| 22 char password[] = ""; | |
| 23 char mailhost[] = ""; | |
| 24 int mailport = 110; | |
| 127 | 25 int state = 0; |
| 114 | 26 |
| 105 | 27 static void *handle = NULL; |
| 124 | 28 extern GtkWidget *buddies; |
| 114 | 29 |
| 30 int lastnum = 0; | |
| 31 int orig = 0; | |
| 32 int mytimer; | |
| 33 | |
| 34 void update_mail(); | |
| 127 | 35 void check_mail(); |
| 114 | 36 |
| 37 int num_msgs() | |
| 38 { | |
| 6308 | 39 struct in_addr *sin; |
| 40 char recv[1024]; | |
| 41 char command[256]; | |
| 42 int fd; | |
| 43 int num = 0; | |
| 44 int step = 0; | |
| 45 int len; | |
| 114 | 46 |
| 6308 | 47 sin = (struct in_addr *)get_address(mailhost); |
| 48 fd = connect_address(sin->s_addr, mailport); | |
| 114 | 49 while ((len = read(fd, recv, 1023))>0) { |
| 50 recv[len] = 0; | |
| 6308 | 51 if (!strncmp(recv, "-ERR", strlen("-ERR"))) { |
| 52 step = 4; | |
| 53 break; | |
| 54 } else if (!strncmp(recv, "+OK", strlen("+OK"))) { | |
| 55 if (step == 3) { | |
| 56 if (sscanf(recv, "+OK %d %d\n", &num, &step) != 2) | |
| 57 break; | |
| 58 g_snprintf(command, sizeof(command), "QUIT\n"); | |
| 59 write(fd, command, strlen(command)); | |
| 114 | 60 close(fd); |
| 6308 | 61 return num; |
| 62 } | |
| 114 | 63 |
| 6308 | 64 if (step == 0) { |
| 65 g_snprintf(command, sizeof(command), "USER %s\n", username); | |
| 66 write(fd, command, strlen(command)); | |
| 67 step = 1; | |
| 68 } else if (step == 1) { | |
| 69 g_snprintf(command, sizeof(command), "PASS %s\n", password); | |
| 70 write(fd, command, strlen(command)); | |
| 71 step = 2; | |
| 72 } else if (step == 2) { | |
| 73 g_snprintf(command, sizeof(command), "STAT\n"); | |
| 74 write(fd, command, strlen(command)); | |
| 75 step = 3; | |
| 76 } | |
| 77 } | |
| 78 } | |
| 79 close(fd); | |
| 80 | |
| 114 | 81 return 0; |
| 82 } | |
| 105 | 83 |
| 124 | 84 void destroy_mail_list() |
| 85 { | |
| 6308 | 86 GList *list; |
| 87 GtkWidget *w; | |
| 124 | 88 |
| 6308 | 89 list = GTK_TREE(buddies)->children; |
| 90 while (list) { | |
| 91 w = (GtkWidget *)list->data; | |
| 92 if (!strcmp(GTK_LABEL(GTK_BIN(w)->child)->label, _("Mail Server"))) { | |
| 93 gtk_tree_remove_items(GTK_TREE(buddies), list); | |
|
126
b4cd83f1d0b8
[gaim-migrate @ 136]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
124
diff
changeset
|
94 list = GTK_TREE(buddies)->children; |
| 6308 | 95 if (!list) |
| 96 break; | |
| 97 } | |
| 98 list = list->next; | |
| 99 } | |
| 124 | 100 } |
| 101 | |
| 102 void setup_mail_list() | |
| 103 { | |
| 104 GList *list; | |
| 105 GtkWidget *w; | |
| 106 GtkWidget *item; | |
| 107 GtkWidget *tree; | |
| 108 gchar *buf; | |
| 109 | |
| 110 list = GTK_TREE(buddies)->children; | |
| 111 | |
| 112 while (list) { | |
| 113 w = (GtkWidget *)list->data; | |
| 5116 | 114 if (!strcmp(GTK_LABEL(GTK_BIN(w)->child)->label, _("Mail Server"))) { |
| 124 | 115 gtk_tree_remove_items(GTK_TREE(buddies), list); |
|
126
b4cd83f1d0b8
[gaim-migrate @ 136]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
124
diff
changeset
|
116 list = GTK_TREE(buddies)->children; |
| 124 | 117 if (!list) |
| 118 break; | |
| 119 } | |
| 120 list = list->next; | |
| 121 } | |
| 122 | |
| 5116 | 123 item = gtk_tree_item_new_with_label(_("Mail Server")); |
| 124 | 124 tree = gtk_tree_new(); |
| 125 gtk_widget_show(item); | |
| 126 gtk_widget_show(tree); | |
| 127 gtk_tree_append(GTK_TREE(buddies), item); | |
| 128 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), tree); | |
| 129 gtk_tree_item_expand(GTK_TREE_ITEM(item)); | |
| 130 | |
| 10320 | 131 /* XXX - This needs to use ngettext() */ |
| 132 buf = g_strdup_printf(_("%s (%d new/%d total)"), mailhost, lastnum - orig, lastnum); | |
| 124 | 133 item = gtk_tree_item_new_with_label(buf); |
| 134 g_free(buf); | |
| 135 | |
| 136 gtk_tree_append(GTK_TREE(tree), item); | |
| 137 gtk_widget_show(item); | |
| 138 } | |
| 10320 | 139 |
| 105 | 140 void gaim_plugin_init(void *h) { |
| 141 handle = h; | |
| 142 | |
| 114 | 143 orig = num_msgs(); |
| 144 lastnum = orig; | |
| 145 | |
| 124 | 146 gaim_signal_connect(handle, event_blist_update, setup_mail_list, NULL); |
| 147 setup_mail_list(); | |
| 10320 | 148 |
| 4168 | 149 mytimer = g_timeout_add(30000, check_mail, NULL); |
| 127 | 150 } |
| 151 | |
| 152 void check_mail() { | |
| 153 pthread_t mail_thread; | |
| 154 pthread_attr_t attr; | |
| 155 | |
| 156 if (state == 0) { | |
| 157 state = 1; | |
| 158 pthread_attr_init(&attr); | |
| 159 pthread_create(&mail_thread, &attr, (void *)&update_mail, NULL); | |
| 160 } | |
| 105 | 161 } |
| 162 | |
| 114 | 163 void update_mail () { |
| 164 int newnum; | |
| 165 | |
| 4168 | 166 g_source_remove(mytimer); |
| 114 | 167 |
| 168 newnum = num_msgs(); | |
| 169 | |
| 6308 | 170 if ((newnum >= lastnum) && (newnum > 0)) { |
| 127 | 171 newnum = newnum - lastnum; |
| 114 | 172 } else { |
| 124 | 173 newnum = 0; |
| 114 | 174 } |
| 175 | |
| 176 if (newnum < lastnum) { | |
| 127 | 177 orig = lastnum; |
| 114 | 178 } |
| 179 | |
| 180 lastnum = newnum; | |
| 4168 | 181 mytimer = g_timeout_add(30000, check_mail, NULL); |
| 124 | 182 setup_mail_list(); |
| 127 | 183 state = 0; |
| 114 | 184 } |
| 185 | |
| 105 | 186 void gaim_plugin_remove() { |
| 4168 | 187 g_source_remove(mytimer); |
| 127 | 188 while (state == 1) { } |
| 124 | 189 destroy_mail_list(); |
| 105 | 190 handle = NULL; |
| 191 } | |
| 192 | |
| 193 char *name() { | |
| 5116 | 194 return _("Check Mail"); |
| 105 | 195 } |
| 196 | |
| 197 char *description() { | |
| 5116 | 198 return _("Check email every X seconds.\n"); |
| 105 | 199 } |
