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