pep.c
author Myhailo Danylenko <isbear@ukrpost.net>
Sun, 08 Nov 2009 23:11:53 +0200
changeset 4 b24eab8a6515
parent 2 d55fe04d7f35
child 6 f579d9637195
permissions -rw-r--r--
Cosmetics+install other docs

/*
 * 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"

static GSList *pep_xmlns_handlers = NULL;

static LmMessageHandler *pep_message_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 (!strcmp (handler->xmlns, xmlns))
			return handler;
	}

	return NULL;
}

static LmHandlerResult pep_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata)
{ // it is MESSAGE!
	LmMessageNode *m = lm_message_get_node (message);

	LmMessageNode *node = lm_message_node_get_child (m, "event");
	if (!node)
		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
	
	{
		const gchar *xmlns = lm_message_node_get_attribute (node, "xmlns");
		if (strcmp (xmlns, NS_PUBSUB_EVENT))
			return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
	}
	
	node = lm_message_node_get_child (node, "items");
	if (!node)
		return LM_HANDLER_RESULT_REMOVE_MESSAGE; // XXX

	{
		LmMessageNode *item;
		const gchar   *from  = lm_message_node_get_attribute (m, "from");
		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);
				}
			}
		}
	}

	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}

void pep_register_xmlns_handler (const gchar *xmlns, void (*handler) (const gchar *from, const gchar *node, LmMessageNode *n, const gchar *id, gpointer udata), gpointer udata, void (*data_destroy) (pep_handler_t *handler))
{
	pep_handler_t *h = g_new (pep_handler_t, 1);

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

	pep_xmlns_handlers = g_slist_append (pep_xmlns_handlers, h);
}

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

		if (handler->data_destroy)
			handler->data_destroy (handler);
		g_free (handler->xmlns);
		g_free (handler);
	}
}

static void pep_register_handlers (void)
{
	if (!pep_message_handler)
		pep_message_handler = lm_message_handler_new (pep_handler, NULL, NULL);
	else if (lconnection) // XXX
		lm_connection_unregister_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE);
	if (lconnection) // XXX
		lm_connection_register_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_FIRST);
}

static void pep_unregister_handlers (void)
{
	if (pep_message_handler)
		lm_connection_unregister_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE);
}

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

	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)
{
	// register hook handler
	hk_add_handler (pep_hh, HOOK_INTERNAL, NULL);

	pep_register_handlers ();

	return NULL;
}

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

	if (pep_message_handler)
		lm_message_handler_unref (pep_message_handler);
	pep_message_handler = NULL;

	// remove hook
	hk_del_handler (pep_hh, NULL);
}

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