pep.c
author Myhailo Danylenko <isbear@ukrpost.net>
Thu, 14 Jan 2010 16:08:11 +0200
changeset 9 c80824a5a042
parent 8 b98346c5040d
child 10 a7575953642d
permissions -rw-r--r--
Fix function name

/*
 * pep.c                -- Common pep routines
 *
 * Copyrigth (C) 2009      Myhailo Danylenko <isbear@ukrpost.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <glib.h>
#include <gmodule.h>
#include <loudmouth/loudmouth.h>
#include <string.h>

#include "xmpp.h"
#include "hooks.h"

#include "pep.h"

typedef struct pep_handler_struct pep_handler_t;

struct pep_handler_struct {
	gchar               *xmlns;
	gpointer             data;
	pep_xmlns_handler_t  handler;
	GDestroyNotify       destroy_notify;
};

static GSList *pep_xmlns_handlers = NULL;

static LmMessageHandler *pep_message_handler = NULL;
static LmMessageHandler *pep_iq_handler      = NULL;

static pep_handler_t *pep_find_handler (const gchar *xmlns)
{
	GSList *hel;

	for (hel = pep_xmlns_handlers; hel; hel = hel->next) {
		pep_handler_t *handler = (pep_handler_t *) hel->data;

		if (!g_strcmp0 (handler->xmlns, xmlns))
			return handler;
	}

	return NULL;
}

static void pep_handler_free (pep_handler_t *handler)
{
	if (handler -> destroy_notify)
		handler -> destroy_notify (handler -> data);
	g_free (handler -> xmlns);
	g_free (handler);
	return;
}

static LmHandlerResult pep_handler (const gchar *from, LmMessageNode *mnode)
{
	LmMessageNode *node = lm_message_node_get_child (mnode, "items");

	if (node) {
		LmMessageNode *item;
		const gchar   *inode = lm_message_node_get_attribute (node, "node");

		for (item = node->children; item; item = item->next) {
			const gchar   *id = lm_message_node_get_attribute (item, "id");
			LmMessageNode *n;

			for (n = item->children; n; n = n->next) {
				const gchar *xmlns = lm_message_node_get_attribute (n, "xmlns");

				if (xmlns) {
					pep_handler_t *handler = pep_find_handler (xmlns);

					if (handler)
						handler->handler (from, inode, n, id, handler->data);
				}
			}
		}
	} // XXX else

	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}

static LmHandlerResult pep_iq_pubsub_handler (LmMessageHandler *handler, LmConnection *connectio, LmMessage *message, gpointer udata)
{
	LmMessageNode *node = lm_message_get_node (message);
	const gchar   *from = lm_message_node_get_attribute (node, "from");
	
	node = lm_message_node_get_child (node, "pubsub");
	if (node && !g_strcmp0 (lm_message_node_get_attribute (node, "xmlns"), NS_PUBSUB)) {
		return pep_handler (from, node);
	}
	
	return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}

static LmHandlerResult pep_message_event_handler (LmMessageHandler *handler, LmConnection *connectio, LmMessage *message, gpointer udata)
{
	LmMessageNode *node = lm_message_get_node (message);
	const gchar   *from = lm_message_node_get_attribute (node, "from");
	
	node = lm_message_node_get_child (node, "event");
	if (node && !g_strcmp0 (lm_message_node_get_attribute (node, "xmlns"), NS_PUBSUB_EVENT)) {
		return pep_handler (from, node);
	}
	
	return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}

void pep_register_xmlns_handler (const gchar *xmlns, pep_xmlns_handler_t handler, gpointer udata, GDestroyNotify notify)
{
	pep_handler_t *h = g_new (pep_handler_t, 1);

	h->xmlns          = g_strdup (xmlns);
	h->handler        = handler;
	h->data           = udata;
	h->destroy_notify = notify;

	pep_xmlns_handlers = g_slist_append (pep_xmlns_handlers, h);
	return;
}

void pep_unregister_xmlns_handler (const gchar *xmlns)
{
	pep_handler_t *handler = pep_find_handler (xmlns);
	if (handler) {
		pep_handler_free (handler);
		pep_xmlns_handlers = g_slist_remove (pep_xmlns_handlers, handler);
	}
}

static void pep_register_handlers (void)
{
	if (lconnection) { // XXX
		lm_connection_register_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_FIRST);
		lm_connection_register_message_handler (lconnection, pep_iq_handler, LM_MESSAGE_TYPE_IQ, LM_HANDLER_PRIORITY_FIRST);
	}
}

static void pep_unregister_handlers (void)
{
	if (lconnection) { // XXX
		lm_connection_unregister_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE);
		lm_connection_unregister_message_handler (lconnection, pep_iq_handler, LM_MESSAGE_TYPE_IQ);
	}
}

// release handlers before reconnect
static void pep_hh (guint32 hid, hk_arg_t *args, gpointer userdata)
{
	hk_arg_t *arg;

	for (arg = args; arg->name; ++arg) {
		if (!strcmp (arg->name, "hook")) {
			if (!strcmp (arg->value, "hook-pre-disconnect"))
				pep_unregister_handlers ();
			else if (!strcmp (arg->value, "hook-post-connect"))
				pep_register_handlers ();
			return;
		}
	}
}

const gchar *g_module_check_init (GModule *module)
{
	// create handlers
	pep_message_handler = lm_message_handler_new (pep_message_event_handler, NULL, NULL);
	pep_iq_handler      = lm_message_handler_new (pep_iq_pubsub_handler,     NULL, NULL);

	// register hook handler
	hk_add_handler (pep_hh, HOOK_INTERNAL, NULL);

	// register handlers to connection
	pep_register_handlers ();

	return NULL;
}

void g_module_unload (GModule *module)
{
	// release handlers
	pep_unregister_handlers ();

	// remove hook
	hk_del_handler (pep_hh, NULL);

	{ // unregister xmlns handlers
		GSList *hel;

		for (hel = pep_xmlns_handlers; hel; hel = hel -> next) {
			pep_handler_t *handler = hel -> data;
			pep_handler_free (handler);
		}

		g_slist_free (pep_xmlns_handlers);
	}

	// destroy handlers (invalidate it just to be sure, though this should not happen :)
	lm_message_handler_invalidate (pep_message_handler);
	lm_message_handler_invalidate (pep_iq_handler);
	lm_message_handler_unref (pep_message_handler);
	lm_message_handler_unref (pep_iq_handler);
}

/* vim: se ts=4 sw=4: */