Check online status
authorMyhailo Danylenko <isbear@ukrpost.net>
Wed, 20 Jan 2010 19:39:43 +0200
changeset 14 9a4d0f04c7d3
parent 13 6ed6279de87f
child 15 06f8bb3de689
Check online status * Delay publication until connected * Not send requests if not online * Unregister reply handlers
CMakeLists.txt
avatar.c
config.h
config.h.in
--- a/CMakeLists.txt	Sun Jan 17 12:04:48 2010 +0200
+++ b/CMakeLists.txt	Wed Jan 20 19:39:43 2010 +0200
@@ -29,6 +29,15 @@
 set(AALIB_LIBRARY_DIRS "/usr/lib" CACHE FILEPATH "Path, where AAlib is located")
 set(AALIB_INCLUDE_DIRS "/usr/include" CACHE FILEPATH "Path to AAlib includes")
 set(AALIB_LIBRARIES "-laa" CACHE TEXT "Libraries, necessary to link with AAlib")
+find_program(GREP_EXECUTABLE grep DOC "Grep binary to detect functions in headers")
+if(GREP_EXECUTABLE)
+	execute_process(COMMAND ${GREP_EXECUTABLE} xmpp_is_online ${MCABBER_INCLUDE_DIR}/mcabber/xmpp.h OUTPUT_VARIABLE HAVE_XMPP_IS_ONLINE ERROR_QUIET)
+endif()
+include(CheckSymbolExists)
+set(CMAKE_REQUIRED_INCLUDES ${LM_INCLUDE_DIRS})
+set(CMAKE_REQUIRED_LIBRARIES ${LM_LIBRARIES})
+set(CMAKE_REQUIRED_FLAGS ${LM_LDFLAGS} ${LM_CFLAGS})
+check_symbol_exists(lm_connection_unregister_reply_handler loudmouth/loudmouth.h HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER)
 link_directories(${GLIB_LIBRARY_DIRS}
 				 ${GMODULE_LIBRARY_DIRS}
 				 ${LM_LIBRARY_DIRS}
@@ -39,6 +48,7 @@
 add_library(avatar MODULE avatar.c) 
 
 ## Compiler setup
+configure_file(config.h.in config.h)
 include_directories(SYSTEM ${GLIB_INCLUDE_DIRS} 
                     ${GMODULE_INCLUDE_DIRS} 
                     ${LM_INCLUDE_DIRS}
--- a/avatar.c	Sun Jan 17 12:04:48 2010 +0200
+++ b/avatar.c	Wed Jan 20 19:39:43 2010 +0200
@@ -45,6 +45,8 @@
 
 #include <mcabber/pep.h>
 
+#include "config.h"
+
 #define NS_AVATAR_DATA            ( "urn:xmpp:avatar:data"            )
 #define NS_AVATAR_METADATA        ( "urn:xmpp:avatar:metadata"        )
 #define NS_AVATAR_METADATA_NOTIFY ( "urn:xmpp:avatar:metadata+notify" )
@@ -55,6 +57,10 @@
 
 static LmMessageHandler *avatar_metadata_reply_handler = NULL;
 
+static gboolean  publish_delayed = FALSE;
+static gsize     publish_len     = 0;
+static gchar    *publish_data    = NULL;
+
 // predeclarations
 
 static LmHandlerResult avatar_retrieve_data_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata);
@@ -562,10 +568,17 @@
 		struct stat buf;
 
 		if (stat (file, &buf) == -1) {
-			gchar         *bjid    = jidtodisp (jid);
+			gchar         *bjid;
 			LmMessage     *request;
 			LmMessageNode *node;
 
+			if (!xmpp_is_online ()) {
+				scr_LogPrint (LPRINT_NORMAL, "avatar: You are not online, request not sent.");
+				return FALSE;
+			}
+
+			bjid = jidtodisp (jid);
+
 			scr_WriteIncomingMessage (bjid, "No avatar for this buddy yet, sending request.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8
 
 			// create data request
@@ -783,6 +796,18 @@
 	LmMessage        *request;
 	LmMessageNode    *node;
 
+	if (!xmpp_is_online ()) {
+		scr_LogPrint (LPRINT_DEBUG, "avatar: Not connected, delaying publish.");
+
+		g_free (publish_data);
+
+		publish_data    = g_strndup (data, len);
+		publish_len     = len;
+		publish_delayed = TRUE;
+
+		return;
+	}
+
 	request = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET);
 	node = lm_message_get_node (request);
 
@@ -1032,16 +1057,40 @@
 	for (hel = reply_handlers; hel; hel = hel->next) {
 		LmMessageHandler *handler = (LmMessageHandler *) hel->data;
 		lm_message_handler_invalidate (handler);
-		// it is already unreffed
+#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
+		if (lconnection)
+			lm_connection_unregister_reply_handler (lconnection, handler);
+#endif
 	}
 
 	g_slist_free (reply_handlers);
 	reply_handlers = NULL;
+
+#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLERS
+	if (lconnection)
+		lm_connection_unregister_reply_handler (lconnection, avatar_metadata_reply_handler);
+#endif
 }
 
 // release handlers before reconnect
 static void avatar_hh (guint32 hid, hk_arg_t *args, gpointer userdata)
 {
+#ifdef HOOK_PRE_DISCONNECT
+	if (hid == HOOK_PRE_DISCONNECT)
+		avatar_free_reply_handlers ();
+	else if (hid == HOOK_POST_CONNECT && publish_delayed) {
+		char *tmp_data = publish_data;
+
+		scr_LogPrint (LPRINT_DEBUG, "avatar: Publishing delayed data.");
+
+		publish_data    = NULL;
+		publish_delayed = FALSE;
+
+		avatar_publish (publish_data, publish_len);
+
+		g_free (tmp_data);
+	}
+#else
 	hk_arg_t *arg;
 
 	for (arg = args; arg->name; arg++) {
@@ -1050,6 +1099,7 @@
 			return;
 		}
 	}
+#endif
 }
 
 const gchar *g_module_check_init (GModule *module)
@@ -1058,8 +1108,12 @@
 	avatar_metadata_reply_handler = lm_message_handler_new (avatar_publish_metadata_reply_handler, NULL, NULL);
 
 	cmd_add ("avatar", "", COMPL_FILENAME, 0, do_avatar, NULL);
-	
+
+#ifdef HOOK_PRE_DISCONNECT
+	hk_add_handler (avatar_hh, HOOK_PRE_DISCONNECT | HOOK_POST_CONNECT, NULL);
+#else
 	hk_add_handler (avatar_hh, HOOK_INTERNAL, NULL);
+#endif
 
 	xmpp_add_feature (NS_AVATAR_METADATA);
 	xmpp_add_feature (NS_AVATAR_METADATA_NOTIFY);
@@ -1084,6 +1138,8 @@
 		lm_message_handler_invalidate (avatar_metadata_reply_handler);
 		lm_message_handler_unref (avatar_metadata_reply_handler);
 	}
+
+	g_free (publish_data);
 }
 
 /* vim: se ts=4 sw=4: */
--- a/config.h	Sun Jan 17 12:04:48 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-
-#ifndef LOCAL_CONFIG_H
-#define LOCAL_CONFIG_H
-
-#define MODULES_ENABLE 1
-
-# endif
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.h.in	Wed Jan 20 19:39:43 2010 +0200
@@ -0,0 +1,20 @@
+
+#ifndef LOCAL_CONFIG_H
+#define LOCAL_CONFIG_H
+
+#cmakedefine HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
+
+#cmakedefine HAVE_XMPP_IS_ONLINE
+
+#ifndef HAVE_XMPP_IS_ONLINE
+inline gboolean xmpp_is_online (void)
+{
+	if (lconnection && lm_connection_is_authenticated (lconnection))
+		return TRUE;
+	else
+		return FALSE;
+}
+#endif
+
+#endif
+