--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pep.c Mon Nov 02 05:00:21 2009 +0200
@@ -0,0 +1,179 @@
+/*
+ * disco.c -- Service discovery requests
+ *
+ * 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 <strings.h>
+
+#include "xmpp.h"
+#include "xmpp_defines.h"
+#include "hooks.h"
+#include "pep.h"
+
+static GSList *pep_xmlns_handlers = NULL;
+
+static LmMessageHandler *pep_message_handler = NULL;
+
+static GSList *pep_reply_handlers = 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 *event = lm_message_node_get_child (m, "event");
+ if (!event)
+ return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+
+ const gchar *xmlns = lm_message_node_get_attribute (event, "xmlns");
+ if (strcmp (xmlns, NS_PUBSUB_EVENT))
+ return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+
+ LmMessageNode *items = lm_message_node_get_child (event, "items");
+ if (!items)
+ return LM_HANDLER_RESULT_REMOVE_MESSAGE; // XXX
+
+ const gchar *from = lm_message_node_get_attribute (m, "from");
+ const gchar *node = lm_message_node_get_attribute (items, "node");
+
+ LmMessageNode *item;
+ for (item = items->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, node, 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);
+ 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)
+{
+ GSList *hel;
+
+ if (lconnection) { // XXX more checks?
+ // let's hope, that after invalidation, lm will remove and free unreffed by us handler
+ for (hel = pep_reply_handlers; hel; hel = hel->next) {
+ LmMessageHandler *handler = (LmMessageHandler *) hel->data;
+ lm_message_handler_invalidate (handler);
+ }
+ }
+
+ g_slist_free (pep_reply_handlers);
+ pep_reply_handlers = NULL;
+
+ if (pep_message_handler)
+ lm_connection_unregister_handler (lconnection, pep_message_handler);
+}
+
+// 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, "name")) {
+ 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)
+{
+ pep_reply_handlers = NULL;
+
+ // 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: */