diff -r 4b96709ac242 -r b98346c5040d pep.c --- a/pep.c Wed Nov 11 17:05:46 2009 +0200 +++ b/pep.c Sat Dec 26 04:18:28 2009 +0200 @@ -26,8 +26,18 @@ #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; @@ -40,13 +50,22 @@ for (hel = pep_xmlns_handlers; hel; hel = hel->next) { pep_handler_t *handler = (pep_handler_t *) hel->data; - if (!strcmp (handler->xmlns, xmlns)) + if (!g_strcmp0 (handler->xmlns, xmlns)) return handler; } return NULL; } +static void pep_free_handler (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"); @@ -81,11 +100,8 @@ const gchar *from = lm_message_node_get_attribute (node, "from"); node = lm_message_node_get_child (node, "pubsub"); - if (node) { - const gchar *xmlns = lm_message_node_get_attribute (node, "xmlns"); - - if (!strcmp (xmlns, NS_PUBSUB)) - return pep_handler (from, node); + 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; @@ -97,38 +113,32 @@ const gchar *from = lm_message_node_get_attribute (node, "from"); node = lm_message_node_get_child (node, "event"); - if (node) { - const gchar *xmlns = lm_message_node_get_attribute (node, "xmlns"); - - if (!strcmp (xmlns, NS_PUBSUB_EVENT)) - return pep_handler (from, node); + 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, void (*handler) (const gchar *from, const gchar *node, LmMessageNode *n, const gchar *id, gpointer udata), gpointer udata, void (*data_destroy) (pep_handler_t *handler)) +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->data_destroy = data_destroy; + 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); - - if (handler->data_destroy) - handler->data_destroy (handler); - g_free (handler->xmlns); - g_free (handler); } } @@ -151,7 +161,7 @@ // release handlers before reconnect static void pep_hh (guint32 hid, hk_arg_t *args, gpointer userdata) { - hk_arg_t *arg = args; + hk_arg_t *arg; for (arg = args; arg->name; ++arg) { if (!strcmp (arg->name, "hook")) { @@ -187,6 +197,17 @@ // 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);