Use GDestroyNotify and unregister on unload
* Use GDestroyNotify for data freeing
* Really unregister handlers on unload
* Prevent possible passing NULL to strcmp
* Privatize pep_handler_t
--- 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);
--- a/pep.h Wed Nov 11 17:05:46 2009 +0200
+++ b/pep.h Sat Dec 26 04:18:28 2009 +0200
@@ -1,46 +1,37 @@
-/*
- * disco.c -- Service discovery requests
+
+/* Copyright 2009 Myhailo Danylenko
*
- * Copyrigth (C) 2009 Myhailo Danylenko <isbear@ukrpost.net>
+ * Common pep event listener
*
- * This program is free software; you can redistribute it and/or modify
+ * This file is part of mcabber-pep
+ *
+ * mcabber-pep 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.
+ * 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.
+ * 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
- */
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef MCABBER_PEP_H
#define MCABBER_PEP_H
#include <glib.h>
-#include <gmodule.h>
#include <loudmouth/loudmouth.h>
#define NS_PUBSUB_EVENT ( "http:/" "/jabber.org/protocol/pubsub#event" )
#define NS_PUBSUB ( "http:/" "/jabber.org/protocol/pubsub" )
-typedef struct pep_handler_struct pep_handler_t;
+typedef void (*pep_xmlns_handler_t) (const gchar *from, const gchar *node, LmMessageNode *n, const gchar *id, gpointer userdata);
-struct pep_handler_struct {
- gchar *xmlns;
- gpointer data;
- void (*handler) (const gchar *from, const gchar *node, LmMessageNode *n, const gchar *id, gpointer userdata);
- void (*data_destroy) (pep_handler_t *handler);
-};
-
-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);
void pep_unregister_xmlns_handler (const gchar *xmlns);
#endif
-/* vim: se ts=4: */
+/* vim: se ts=4 sw=4: */