Use persistant reply handlers
authorMyhailo Danylenko <isbear@ukrpost.net>
Wed, 11 Nov 2009 15:14:34 +0200
changeset 12 4d2ac929e61a
parent 11 18de00544c46
child 13 339e8bf46039
Use persistant reply handlers
CMakeLists.txt
TODO
disco.c
--- a/CMakeLists.txt	Wed Nov 11 15:14:09 2009 +0200
+++ b/CMakeLists.txt	Wed Nov 11 15:14:34 2009 +0200
@@ -63,7 +63,7 @@
 
 ## Installation
 install(TARGETS disco DESTINATION lib/mcabber) 
-install(FILES disco.rc DESTINATION share/doc/${CPACK_PACKAGE_NAME})
+install(FILES disco.rc COPYING TODO README DESTINATION share/doc/${CPACK_PACKAGE_NAME})
 install(DIRECTORY help DESTINATION share/mcabber)
 
 ## The End ## vim: se ts=4: ##
--- a/TODO	Wed Nov 11 15:14:09 2009 +0200
+++ b/TODO	Wed Nov 11 15:14:34 2009 +0200
@@ -1,4 +1,3 @@
 
 Split large messages to avoid splitting in the middle of line?
-Check handlers
 
--- a/disco.c	Wed Nov 11 15:14:09 2009 +0200
+++ b/disco.c	Wed Nov 11 15:14:34 2009 +0200
@@ -22,7 +22,7 @@
 #include <glib.h>
 #include <gmodule.h>
 #include <loudmouth/loudmouth.h>
-#include <strings.h>
+#include <string.h>
 
 #include "commands.h"
 #include "logprint.h"
@@ -32,17 +32,13 @@
 #include "xmpp_defines.h"
 #include "screen.h"
 #include "hbuf.h"
-#include "hooks.h"
-
-static GSList *reply_handlers = NULL;
-static guint   disco_cid      = 0;
 
-static LmHandlerResult disco_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata)
+static guint             disco_cid                 = 0;
+static LmMessageHandler *disco_info_reply_handler  = NULL;
+static LmMessageHandler *disco_items_reply_handler = NULL;
+
+static LmHandlerResult disco_handler (LmMessage *message, gboolean info_request)
 {
-	int info_request = (int) udata;
-
-	reply_handlers = g_slist_remove (reply_handlers, handler);
-
 	switch (lm_message_get_sub_type (message)) {
 	case LM_MESSAGE_SUB_TYPE_RESULT:
 
@@ -163,6 +159,16 @@
 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
 }
 
+static LmHandlerResult disco_info_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata)
+{
+		return disco_handler (message, TRUE);
+}
+
+static LmHandlerResult disco_items_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata)
+{
+		return disco_handler (message, FALSE);
+}
+
 static void do_disco (char *arg)
 {
 	char **args = split_arg (arg, 3, 0);
@@ -197,10 +203,6 @@
 		}
 			// XXX send to all resources/current resource?
 
-		handler = lm_message_handler_new (disco_handler, (gpointer) info, NULL);
-
-		reply_handlers = g_slist_append (reply_handlers, handler);
-
 		{ // create message
 			LmMessageNode *node;
 
@@ -212,47 +214,22 @@
 				lm_message_node_set_attribute (node, "node", dnode);
 		}
 
-		lm_connection_send_with_reply (lconnection, request, handler, NULL);
+		lm_connection_send_with_reply (lconnection, request, info ? disco_info_reply_handler : disco_items_reply_handler, NULL);
 
-		lm_message_handler_unref (handler);
 		lm_message_unref (request);
-		g_free (dnode);
+		if (dnode)
+			g_free (dnode);
 		g_free (to);
 	}
 
 	free_arg_lst (args);
 }
 
-static void disco_free_reply_handlers (void)
-{
-	GSList *hel;
-
-	// let's hope, that after invalidation, lm will remove and free unreffed by us handler
-	for (hel = reply_handlers; hel; hel = hel->next) {
-		LmMessageHandler *handler = (LmMessageHandler *) hel->data;
-		lm_message_handler_invalidate (handler);
-	}
-
-	g_slist_free (reply_handlers);
-	reply_handlers = NULL;
-}
-
-// release handlers before reconnect
-static void disco_hh (guint32 hid, hk_arg_t *args, gpointer userdata)
-{
-	hk_arg_t *arg;
-
-	for (arg = args; arg->name; arg++) {
-		if (!strcmp (arg->name, "hook") && !strcmp (arg->value, "hook-pre-disconnect")) {
-			disco_free_reply_handlers ();
-			return;
-		}
-	}
-}
-
 const gchar *g_module_check_init(GModule *module)
 {
-	reply_handlers = NULL;
+	// create handlers
+	disco_info_reply_handler  = lm_message_handler_new (disco_info_handler,  NULL, NULL);
+	disco_items_reply_handler = lm_message_handler_new (disco_items_handler, NULL, NULL);
 
 	// completion
 	disco_cid = compl_new_category ();
@@ -264,26 +241,28 @@
 	// command
 	cmd_add ("disco", "", disco_cid, COMPL_JID, do_disco, NULL);
 
-	// register hook handler
-	hk_add_handler (disco_hh, HOOK_INTERNAL, NULL);
-
 	return NULL;
 }
 
 void g_module_unload(GModule *module)
 {
-	// release handlers
-	disco_free_reply_handlers ();
-
-	// remove hook handler
-	hk_del_handler (disco_hh, NULL);
-
 	// command
 	cmd_del ("disco");
 
 	// completion
 	if (disco_cid)
 		compl_del_category (disco_cid);
+	
+	// unregister handlers
+	if (disco_info_reply_handler) {
+		lm_message_handler_invalidate (disco_info_reply_handler);
+		lm_message_handler_unref (disco_info_reply_handler);
+	}
+
+	if (disco_items_reply_handler) {
+		lm_message_handler_invalidate (disco_items_reply_handler);
+		lm_message_handler_unref (disco_items_reply_handler);
+	}
 }
 
-/* vim: se ts=4: */
+/* vim: se ts=4 sw=4: */