view plugins/chkmail.c @ 5205:fefad67de2c7

[gaim-migrate @ 5573] I had a damn good commit message, but it was eaten. Let's try it again. Announcing, Gaim Plugin API version 2.0, or GPAPIV2.0 for short. There are lots'a cool thingies here. Okay now, this isn't as cool as the previous message, but: 1) There's now a single entry function for all plugin types. It returns a detailed information structure on the plugin. This removes a lot of the ugliness from old plugins. Oh yeah, libicq wasn't converted to this, so if you use it, well, you shouldn't have used it anyway, but now you can't! bwahahaha. Use AIM/ICQ. 2) There are now 3 types of plugins: Standard, Loader, and Protocol plugins. Standard plugins are, well, standard, compiled plugins. Loader plugins load other plugins. For example, the perl support is now a loader plugin. It loads perl scripts. In the future, we'll have Ruby and Python loader plugins. Protocol plugins are, well, protocol plugins... yeah... 3) Plugins have unique IDs, so they can be referred to or automatically updated from a plugin database in the future. Neat, huh? 4) Plugins will have dependency support in the future, and can be hidden, so if you have, say, a logging core plugin, it won't have to show up, but then you load the GTK+ logging plugin and it'll auto-load the core plugin. Core/UI split plugins! 5) There will eventually be custom plugin signals and RPC of some sort, for the core/ui split plugins. So, okay, back up .gaimrc. I'd like to thank my parents for their support, javabsp for helping convert a bunch of protocol plugins, and Etan for helping convert a bunch of standard plugins. Have fun. If you have any problems, please let me know, but you probably won't have anything major happen. You will have to convert your plugins, though, and I'm not guaranteeing that all perl scripts will still work. I'll end up changing the perl script API eventually, so I know they won't down the road. Don't worry, though. It'll be mass cool. faceprint wants me to just commit the damn code already. So, here we go!!! .. .. I need a massage. From a young, cute girl. Are there any young, cute girls in the audience? IM me plz k thx. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Fri, 25 Apr 2003 06:47:33 +0000
parents c081a81ee013
children a21cf07cd8e1
line wrap: on
line source

/* This is some funky code.  It is still being developed by Rob Flynn - rob@linuxpimps.com
 * I recommend not using this code right now. :)
*/

#define GAIM_PLUGINS

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include "gaim.h"

char username[] = "";
char password[] = "";
char mailhost[] = "";
int mailport = 110;
int state = 0;

static void *handle = NULL;
extern GtkWidget *buddies;

int lastnum = 0;
int orig = 0;
int mytimer;

void update_mail();
void check_mail();

int num_msgs()
{
        struct in_addr *sin;
        char recv[1024];
        char command[256];
        int fd;
        int num = 0;
        int step = 0;
        int len;

        sin = (struct in_addr *)get_address(mailhost);
        fd = connect_address(sin->s_addr, mailport);
	while ((len = read(fd, recv, 1023))>0) {
		recv[len] = 0;
                if (!strncmp(recv, "-ERR", strlen("-ERR"))) { step = 4; break; 
                } else if (!strncmp(recv, "+OK", strlen("+OK"))) {
                        if (step == 3) {
                                if (sscanf(recv, "+OK %d %d\n", &num, &step) != 2)
                                        break;
                                g_snprintf(command, sizeof(command), "QUIT\n");
                                write(fd, command, strlen(command));
				close(fd);
printf("DEBUG: Num is %d\n", num);
                                return num;
                        }

                        if (step == 0) {
                                g_snprintf(command, sizeof(command), "USER %s\n", username);
                                write(fd, command, strlen(command));
                                step = 1;
                        } else if (step == 1) {
                                g_snprintf(command, sizeof(command), "PASS %s\n", password);
                                write(fd, command, strlen(command));
                                step = 2;
                        } else if (step == 2) {
                                g_snprintf(command, sizeof(command), "STAT\n");
                                write(fd, command, strlen(command));
                                step = 3;
                        }
                }
        }
        close(fd);
	return 0;
}

void destroy_mail_list()
{
        GList *list;
        GtkWidget *w;

        list = GTK_TREE(buddies)->children;

        while (list) {
                w = (GtkWidget *)list->data;
                if (!strcmp(GTK_LABEL(GTK_BIN(w)->child)->label, _("Mail Server"))) {
                        gtk_tree_remove_items(GTK_TREE(buddies), list);
			list = GTK_TREE(buddies)->children;
                        if (!list)
                                break;
                }
                list = list->next;
        }
}


void setup_mail_list()
{
	GList *list;
	GtkWidget *w;
	GtkWidget *item;
	GtkWidget *tree;
	gchar *buf;

	list = GTK_TREE(buddies)->children;

	while (list) {
		w = (GtkWidget *)list->data;
		if (!strcmp(GTK_LABEL(GTK_BIN(w)->child)->label, _("Mail Server"))) {
			gtk_tree_remove_items(GTK_TREE(buddies), list);
			list = GTK_TREE(buddies)->children;
			if (!list)
				break;
		}
		list = list->next;
	}

	item = gtk_tree_item_new_with_label(_("Mail Server"));
	tree = gtk_tree_new();
	gtk_widget_show(item);
	gtk_widget_show(tree);
	gtk_tree_append(GTK_TREE(buddies), item);
	gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), tree);
	gtk_tree_item_expand(GTK_TREE_ITEM(item));

	buf = g_malloc(BUF_LONG);

	g_snprintf(buf, BUF_LONG, _("%s (%d new/%d total)"), mailhost, lastnum - orig, lastnum);
	item = gtk_tree_item_new_with_label(buf);
	g_free(buf);

	gtk_tree_append(GTK_TREE(tree), item);
	gtk_widget_show(item);
}
	
void gaim_plugin_init(void *h) {
	handle = h;

	orig = num_msgs();
	lastnum = orig;

	gaim_signal_connect(handle, event_blist_update, setup_mail_list, NULL);
	setup_mail_list();
	
	mytimer = g_timeout_add(30000, check_mail, NULL);
}

void check_mail() {
	pthread_t mail_thread;
	pthread_attr_t attr;

	printf("Looping in: State = %d\n", state);
	if (state == 0) {
		state = 1;
		printf("Before\n");
		pthread_attr_init(&attr);
		pthread_create(&mail_thread, &attr, (void *)&update_mail, NULL);
		printf("After\n");
	}
	printf("Bouncing out, state = %d\n", state);
}

void update_mail () {
	int newnum;

	printf("um\n");
	g_source_remove(mytimer);

	printf("nm1\n");
	newnum = num_msgs();

	printf("nm2\n");
	if ( (newnum >= lastnum) && (newnum > 0)) {
		newnum = newnum - lastnum;
	} else {
		newnum = 0;
	}

	if (newnum < lastnum) {
		orig = lastnum;
	}

	lastnum = newnum;
	mytimer = g_timeout_add(30000, check_mail, NULL);
	printf("sml1\n");
	setup_mail_list();
	printf("sml2\n");
	state = 0;
}


void gaim_plugin_remove() {
	g_source_remove(mytimer);
	while (state == 1) { }
	destroy_mail_list();
	handle = NULL;
}

char *name() {
	return _("Check Mail");
}

char *description() {
	return _("Check email every X seconds.\n");
}