[gaim-migrate @ 4676]
[pidgin-git.git] / plugins / chkmail.c
blob3d05b7d27458e4aee423f7c12217d76e55b3c874
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 */
5 #define GAIM_PLUGINS
7 #include <stdio.h>
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>
16 #include <pthread.h>
17 #include "gaim.h"
19 char username[] = "";
20 char password[] = "";
21 char mailhost[] = "";
22 int mailport = 110;
23 int state = 0;
25 static void *handle = NULL;
26 extern GtkWidget *buddies;
28 int lastnum = 0;
29 int orig = 0;
30 int mytimer;
32 void update_mail();
33 void check_mail();
35 int num_msgs()
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;
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);
57 printf("DEBUG: Num is %d\n", num);
58 return num;
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;
76 close(fd);
77 return 0;
80 void destroy_mail_list()
82 GList *list;
83 GtkWidget *w;
85 list = GTK_TREE(buddies)->children;
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);
91 list = GTK_TREE(buddies)->children;
92 if (!list)
93 break;
95 list = list->next;
100 void setup_mail_list()
102 GList *list;
103 GtkWidget *w;
104 GtkWidget *item;
105 GtkWidget *tree;
106 gchar *buf;
108 list = GTK_TREE(buddies)->children;
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);
114 list = GTK_TREE(buddies)->children;
115 if (!list)
116 break;
118 list = list->next;
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));
129 buf = g_malloc(BUF_LONG);
131 g_snprintf(buf, BUF_LONG, "%s (%d new/%d total)", mailhost, lastnum - orig, lastnum);
132 item = gtk_tree_item_new_with_label(buf);
133 g_free(buf);
135 gtk_tree_append(GTK_TREE(tree), item);
136 gtk_widget_show(item);
139 void gaim_plugin_init(void *h) {
140 handle = h;
142 orig = num_msgs();
143 lastnum = orig;
145 gaim_signal_connect(handle, event_blist_update, setup_mail_list, NULL);
146 setup_mail_list();
148 mytimer = g_timeout_add(30000, check_mail, NULL);
151 void check_mail() {
152 pthread_t mail_thread;
153 pthread_attr_t attr;
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");
163 printf("Bouncing out, state = %d\n", state);
166 void update_mail () {
167 int newnum;
169 printf("um\n");
170 g_source_remove(mytimer);
172 printf("nm1\n");
173 newnum = num_msgs();
175 printf("nm2\n");
176 if ( (newnum >= lastnum) && (newnum > 0)) {
177 newnum = newnum - lastnum;
178 } else {
179 newnum = 0;
182 if (newnum < lastnum) {
183 orig = lastnum;
186 lastnum = newnum;
187 mytimer = g_timeout_add(30000, check_mail, NULL);
188 printf("sml1\n");
189 setup_mail_list();
190 printf("sml2\n");
191 state = 0;
195 void gaim_plugin_remove() {
196 g_source_remove(mytimer);
197 while (state == 1) { }
198 destroy_mail_list();
199 handle = NULL;
202 char *name() {
203 return "Check Mail";
206 char *description() {
207 return "Check email every X seconds.\n";