Mercurial > pidgin.yaz
annotate plugins/chkmail.c @ 9824:94910b9520cf
[gaim-migrate @ 10695]
Really, someone needs to test this and tell me which of these files
need to include internal.h
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sun, 22 Aug 2004 17:33:36 +0000 |
parents | a09ffb82aef1 |
children | 61852117568f |
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 | |
105 | 21 #include "gaim.h" |
22 | |
114 | 23 char username[] = ""; |
24 char password[] = ""; | |
25 char mailhost[] = ""; | |
26 int mailport = 110; | |
127 | 27 int state = 0; |
114 | 28 |
105 | 29 static void *handle = NULL; |
124 | 30 extern GtkWidget *buddies; |
114 | 31 |
32 int lastnum = 0; | |
33 int orig = 0; | |
34 int mytimer; | |
35 | |
36 void update_mail(); | |
127 | 37 void check_mail(); |
114 | 38 |
39 int num_msgs() | |
40 { | |
6308 | 41 struct in_addr *sin; |
42 char recv[1024]; | |
43 char command[256]; | |
44 int fd; | |
45 int num = 0; | |
46 int step = 0; | |
47 int len; | |
114 | 48 |
6308 | 49 sin = (struct in_addr *)get_address(mailhost); |
50 fd = connect_address(sin->s_addr, mailport); | |
114 | 51 while ((len = read(fd, recv, 1023))>0) { |
52 recv[len] = 0; | |
6308 | 53 if (!strncmp(recv, "-ERR", strlen("-ERR"))) { |
54 step = 4; | |
55 break; | |
56 } else if (!strncmp(recv, "+OK", strlen("+OK"))) { | |
57 if (step == 3) { | |
58 if (sscanf(recv, "+OK %d %d\n", &num, &step) != 2) | |
59 break; | |
60 g_snprintf(command, sizeof(command), "QUIT\n"); | |
61 write(fd, command, strlen(command)); | |
114 | 62 close(fd); |
6308 | 63 return num; |
64 } | |
114 | 65 |
6308 | 66 if (step == 0) { |
67 g_snprintf(command, sizeof(command), "USER %s\n", username); | |
68 write(fd, command, strlen(command)); | |
69 step = 1; | |
70 } else if (step == 1) { | |
71 g_snprintf(command, sizeof(command), "PASS %s\n", password); | |
72 write(fd, command, strlen(command)); | |
73 step = 2; | |
74 } else if (step == 2) { | |
75 g_snprintf(command, sizeof(command), "STAT\n"); | |
76 write(fd, command, strlen(command)); | |
77 step = 3; | |
78 } | |
79 } | |
80 } | |
81 close(fd); | |
82 | |
114 | 83 return 0; |
84 } | |
105 | 85 |
124 | 86 void destroy_mail_list() |
87 { | |
6308 | 88 GList *list; |
89 GtkWidget *w; | |
124 | 90 |
6308 | 91 list = GTK_TREE(buddies)->children; |
92 while (list) { | |
93 w = (GtkWidget *)list->data; | |
94 if (!strcmp(GTK_LABEL(GTK_BIN(w)->child)->label, _("Mail Server"))) { | |
95 gtk_tree_remove_items(GTK_TREE(buddies), list); | |
126
b4cd83f1d0b8
[gaim-migrate @ 136]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
124
diff
changeset
|
96 list = GTK_TREE(buddies)->children; |
6308 | 97 if (!list) |
98 break; | |
99 } | |
100 list = list->next; | |
101 } | |
124 | 102 } |
103 | |
104 | |
105 void setup_mail_list() | |
106 { | |
107 GList *list; | |
108 GtkWidget *w; | |
109 GtkWidget *item; | |
110 GtkWidget *tree; | |
111 gchar *buf; | |
112 | |
113 list = GTK_TREE(buddies)->children; | |
114 | |
115 while (list) { | |
116 w = (GtkWidget *)list->data; | |
5116 | 117 if (!strcmp(GTK_LABEL(GTK_BIN(w)->child)->label, _("Mail Server"))) { |
124 | 118 gtk_tree_remove_items(GTK_TREE(buddies), list); |
126
b4cd83f1d0b8
[gaim-migrate @ 136]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
124
diff
changeset
|
119 list = GTK_TREE(buddies)->children; |
124 | 120 if (!list) |
121 break; | |
122 } | |
123 list = list->next; | |
124 } | |
125 | |
5116 | 126 item = gtk_tree_item_new_with_label(_("Mail Server")); |
124 | 127 tree = gtk_tree_new(); |
128 gtk_widget_show(item); | |
129 gtk_widget_show(tree); | |
130 gtk_tree_append(GTK_TREE(buddies), item); | |
131 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), tree); | |
132 gtk_tree_item_expand(GTK_TREE_ITEM(item)); | |
133 | |
134 buf = g_malloc(BUF_LONG); | |
135 | |
5116 | 136 g_snprintf(buf, BUF_LONG, _("%s (%d new/%d total)"), mailhost, lastnum - orig, lastnum); |
124 | 137 item = gtk_tree_item_new_with_label(buf); |
138 g_free(buf); | |
139 | |
140 gtk_tree_append(GTK_TREE(tree), item); | |
141 gtk_widget_show(item); | |
142 } | |
143 | |
105 | 144 void gaim_plugin_init(void *h) { |
145 handle = h; | |
146 | |
114 | 147 orig = num_msgs(); |
148 lastnum = orig; | |
149 | |
124 | 150 gaim_signal_connect(handle, event_blist_update, setup_mail_list, NULL); |
151 setup_mail_list(); | |
152 | |
4168 | 153 mytimer = g_timeout_add(30000, check_mail, NULL); |
127 | 154 } |
155 | |
156 void check_mail() { | |
157 pthread_t mail_thread; | |
158 pthread_attr_t attr; | |
159 | |
160 if (state == 0) { | |
161 state = 1; | |
162 pthread_attr_init(&attr); | |
163 pthread_create(&mail_thread, &attr, (void *)&update_mail, NULL); | |
164 } | |
105 | 165 } |
166 | |
114 | 167 void update_mail () { |
168 int newnum; | |
169 | |
4168 | 170 g_source_remove(mytimer); |
114 | 171 |
172 newnum = num_msgs(); | |
173 | |
6308 | 174 if ((newnum >= lastnum) && (newnum > 0)) { |
127 | 175 newnum = newnum - lastnum; |
114 | 176 } else { |
124 | 177 newnum = 0; |
114 | 178 } |
179 | |
180 if (newnum < lastnum) { | |
127 | 181 orig = lastnum; |
114 | 182 } |
183 | |
184 lastnum = newnum; | |
4168 | 185 mytimer = g_timeout_add(30000, check_mail, NULL); |
124 | 186 setup_mail_list(); |
127 | 187 state = 0; |
114 | 188 } |
189 | |
190 | |
105 | 191 void gaim_plugin_remove() { |
4168 | 192 g_source_remove(mytimer); |
127 | 193 while (state == 1) { } |
124 | 194 destroy_mail_list(); |
105 | 195 handle = NULL; |
196 } | |
197 | |
198 char *name() { | |
5116 | 199 return _("Check Mail"); |
105 | 200 } |
201 | |
202 char *description() { | |
5116 | 203 return _("Check email every X seconds.\n"); |
105 | 204 } |