avatar.c
changeset 14 9a4d0f04c7d3
parent 11 2f9ffa1c24c5
child 15 06f8bb3de689
equal deleted inserted replaced
13:6ed6279de87f 14:9a4d0f04c7d3
    43 #include <mcabber/hooks.h>
    43 #include <mcabber/hooks.h>
    44 #include <mcabber/settings.h>
    44 #include <mcabber/settings.h>
    45 
    45 
    46 #include <mcabber/pep.h>
    46 #include <mcabber/pep.h>
    47 
    47 
       
    48 #include "config.h"
       
    49 
    48 #define NS_AVATAR_DATA            ( "urn:xmpp:avatar:data"            )
    50 #define NS_AVATAR_DATA            ( "urn:xmpp:avatar:data"            )
    49 #define NS_AVATAR_METADATA        ( "urn:xmpp:avatar:metadata"        )
    51 #define NS_AVATAR_METADATA        ( "urn:xmpp:avatar:metadata"        )
    50 #define NS_AVATAR_METADATA_NOTIFY ( "urn:xmpp:avatar:metadata+notify" )
    52 #define NS_AVATAR_METADATA_NOTIFY ( "urn:xmpp:avatar:metadata+notify" )
    51 
    53 
    52 static guint avatar_cid = 0;
    54 static guint avatar_cid = 0;
    53 
    55 
    54 static GSList *reply_handlers = NULL;
    56 static GSList *reply_handlers = NULL;
    55 
    57 
    56 static LmMessageHandler *avatar_metadata_reply_handler = NULL;
    58 static LmMessageHandler *avatar_metadata_reply_handler = NULL;
       
    59 
       
    60 static gboolean  publish_delayed = FALSE;
       
    61 static gsize     publish_len     = 0;
       
    62 static gchar    *publish_data    = NULL;
    57 
    63 
    58 // predeclarations
    64 // predeclarations
    59 
    65 
    60 static LmHandlerResult avatar_retrieve_data_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata);
    66 static LmHandlerResult avatar_retrieve_data_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata);
    61 
    67 
   560 
   566 
   561 	{ // check if file exists to not trigger unnecessary error messages
   567 	{ // check if file exists to not trigger unnecessary error messages
   562 		struct stat buf;
   568 		struct stat buf;
   563 
   569 
   564 		if (stat (file, &buf) == -1) {
   570 		if (stat (file, &buf) == -1) {
   565 			gchar         *bjid    = jidtodisp (jid);
   571 			gchar         *bjid;
   566 			LmMessage     *request;
   572 			LmMessage     *request;
   567 			LmMessageNode *node;
   573 			LmMessageNode *node;
       
   574 
       
   575 			if (!xmpp_is_online ()) {
       
   576 				scr_LogPrint (LPRINT_NORMAL, "avatar: You are not online, request not sent.");
       
   577 				return FALSE;
       
   578 			}
       
   579 
       
   580 			bjid = jidtodisp (jid);
   568 
   581 
   569 			scr_WriteIncomingMessage (bjid, "No avatar for this buddy yet, sending request.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8
   582 			scr_WriteIncomingMessage (bjid, "No avatar for this buddy yet, sending request.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8
   570 
   583 
   571 			// create data request
   584 			// create data request
   572 			request = lm_message_new_with_sub_type (bjid, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
   585 			request = lm_message_new_with_sub_type (bjid, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
   780 	LmMessage        *datarequest;
   793 	LmMessage        *datarequest;
   781 	LmMessageHandler *handler;
   794 	LmMessageHandler *handler;
   782 	gchar            *id;
   795 	gchar            *id;
   783 	LmMessage        *request;
   796 	LmMessage        *request;
   784 	LmMessageNode    *node;
   797 	LmMessageNode    *node;
       
   798 
       
   799 	if (!xmpp_is_online ()) {
       
   800 		scr_LogPrint (LPRINT_DEBUG, "avatar: Not connected, delaying publish.");
       
   801 
       
   802 		g_free (publish_data);
       
   803 
       
   804 		publish_data    = g_strndup (data, len);
       
   805 		publish_len     = len;
       
   806 		publish_delayed = TRUE;
       
   807 
       
   808 		return;
       
   809 	}
   785 
   810 
   786 	request = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET);
   811 	request = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET);
   787 	node = lm_message_get_node (request);
   812 	node = lm_message_get_node (request);
   788 
   813 
   789 	if (len) {
   814 	if (len) {
  1030 	GSList *hel;
  1055 	GSList *hel;
  1031 
  1056 
  1032 	for (hel = reply_handlers; hel; hel = hel->next) {
  1057 	for (hel = reply_handlers; hel; hel = hel->next) {
  1033 		LmMessageHandler *handler = (LmMessageHandler *) hel->data;
  1058 		LmMessageHandler *handler = (LmMessageHandler *) hel->data;
  1034 		lm_message_handler_invalidate (handler);
  1059 		lm_message_handler_invalidate (handler);
  1035 		// it is already unreffed
  1060 #ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
       
  1061 		if (lconnection)
       
  1062 			lm_connection_unregister_reply_handler (lconnection, handler);
       
  1063 #endif
  1036 	}
  1064 	}
  1037 
  1065 
  1038 	g_slist_free (reply_handlers);
  1066 	g_slist_free (reply_handlers);
  1039 	reply_handlers = NULL;
  1067 	reply_handlers = NULL;
       
  1068 
       
  1069 #ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLERS
       
  1070 	if (lconnection)
       
  1071 		lm_connection_unregister_reply_handler (lconnection, avatar_metadata_reply_handler);
       
  1072 #endif
  1040 }
  1073 }
  1041 
  1074 
  1042 // release handlers before reconnect
  1075 // release handlers before reconnect
  1043 static void avatar_hh (guint32 hid, hk_arg_t *args, gpointer userdata)
  1076 static void avatar_hh (guint32 hid, hk_arg_t *args, gpointer userdata)
  1044 {
  1077 {
       
  1078 #ifdef HOOK_PRE_DISCONNECT
       
  1079 	if (hid == HOOK_PRE_DISCONNECT)
       
  1080 		avatar_free_reply_handlers ();
       
  1081 	else if (hid == HOOK_POST_CONNECT && publish_delayed) {
       
  1082 		char *tmp_data = publish_data;
       
  1083 
       
  1084 		scr_LogPrint (LPRINT_DEBUG, "avatar: Publishing delayed data.");
       
  1085 
       
  1086 		publish_data    = NULL;
       
  1087 		publish_delayed = FALSE;
       
  1088 
       
  1089 		avatar_publish (publish_data, publish_len);
       
  1090 
       
  1091 		g_free (tmp_data);
       
  1092 	}
       
  1093 #else
  1045 	hk_arg_t *arg;
  1094 	hk_arg_t *arg;
  1046 
  1095 
  1047 	for (arg = args; arg->name; arg++) {
  1096 	for (arg = args; arg->name; arg++) {
  1048 		if (!strcmp (arg->name, "hook") && !strcmp (arg->value, "hook-pre-disconnect")) {
  1097 		if (!strcmp (arg->name, "hook") && !strcmp (arg->value, "hook-pre-disconnect")) {
  1049 			avatar_free_reply_handlers ();
  1098 			avatar_free_reply_handlers ();
  1050 			return;
  1099 			return;
  1051 		}
  1100 		}
  1052 	}
  1101 	}
       
  1102 #endif
  1053 }
  1103 }
  1054 
  1104 
  1055 const gchar *g_module_check_init (GModule *module)
  1105 const gchar *g_module_check_init (GModule *module)
  1056 {
  1106 {
  1057 	pep_register_xmlns_handler (NS_AVATAR_METADATA, avatar_handler, NULL, NULL);
  1107 	pep_register_xmlns_handler (NS_AVATAR_METADATA, avatar_handler, NULL, NULL);
  1058 	avatar_metadata_reply_handler = lm_message_handler_new (avatar_publish_metadata_reply_handler, NULL, NULL);
  1108 	avatar_metadata_reply_handler = lm_message_handler_new (avatar_publish_metadata_reply_handler, NULL, NULL);
  1059 
  1109 
  1060 	cmd_add ("avatar", "", COMPL_FILENAME, 0, do_avatar, NULL);
  1110 	cmd_add ("avatar", "", COMPL_FILENAME, 0, do_avatar, NULL);
  1061 	
  1111 
       
  1112 #ifdef HOOK_PRE_DISCONNECT
       
  1113 	hk_add_handler (avatar_hh, HOOK_PRE_DISCONNECT | HOOK_POST_CONNECT, NULL);
       
  1114 #else
  1062 	hk_add_handler (avatar_hh, HOOK_INTERNAL, NULL);
  1115 	hk_add_handler (avatar_hh, HOOK_INTERNAL, NULL);
       
  1116 #endif
  1063 
  1117 
  1064 	xmpp_add_feature (NS_AVATAR_METADATA);
  1118 	xmpp_add_feature (NS_AVATAR_METADATA);
  1065 	xmpp_add_feature (NS_AVATAR_METADATA_NOTIFY);
  1119 	xmpp_add_feature (NS_AVATAR_METADATA_NOTIFY);
  1066 
  1120 
  1067 	return NULL;
  1121 	return NULL;
  1082 
  1136 
  1083 	if (avatar_metadata_reply_handler) {
  1137 	if (avatar_metadata_reply_handler) {
  1084 		lm_message_handler_invalidate (avatar_metadata_reply_handler);
  1138 		lm_message_handler_invalidate (avatar_metadata_reply_handler);
  1085 		lm_message_handler_unref (avatar_metadata_reply_handler);
  1139 		lm_message_handler_unref (avatar_metadata_reply_handler);
  1086 	}
  1140 	}
       
  1141 
       
  1142 	g_free (publish_data);
  1087 }
  1143 }
  1088 
  1144 
  1089 /* vim: se ts=4 sw=4: */
  1145 /* vim: se ts=4 sw=4: */