/cpw/tomkiewicz/gg11: 74dff2310289: Gadu-Gadu: reimplementation ...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Wed Sep 12 11:44:40 EDT 2012


Changeset: 74dff23102891883a6b3b30a54dfcc5f199c8ab2
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-09-12 17:44 +0200
Branch:	 default
URL: http://hg.pidgin.im/cpw/tomkiewicz/gg11/rev/74dff2310289

Description:

Gadu-Gadu: reimplementation of sending messages

diffstat:

 libpurple/protocols/gg/gg.c           |  130 +---------------------------------
 libpurple/protocols/gg/message-prpl.c |   71 ++++++++++++++++++
 libpurple/protocols/gg/message-prpl.h |    3 +
 3 files changed, 77 insertions(+), 127 deletions(-)

diffs (257 lines):

diff --git a/libpurple/protocols/gg/gg.c b/libpurple/protocols/gg/gg.c
--- a/libpurple/protocols/gg/gg.c
+++ b/libpurple/protocols/gg/gg.c
@@ -610,6 +610,8 @@ static void ggp_login(PurpleAccount *acc
 	if (!ggp_deprecated_setup_proxy(gc))
 		return;
 
+	purple_connection_set_flags(gc, PURPLE_CONNECTION_HTML | PURPLE_CONNECTION_NO_URLDESC);
+
 	glp = g_new0(struct gg_login_params, 1);
 	info = g_new0(GGPInfo, 1);
 
@@ -745,132 +747,6 @@ static void ggp_close(PurpleConnection *
 	purple_debug_info("gg", "Connection closed.\n");
 }
 
-static int ggp_send_im(PurpleConnection *gc, const char *who, const char *msg,
-		       PurpleMessageFlags flags)
-{
-	GGPInfo *info = purple_connection_get_protocol_data(gc);
-	char *tmp, *plain;
-	int ret = 1;
-	unsigned char format[1024];
-	unsigned int format_length = sizeof(struct gg_msg_richtext);
-	gint pos = 0;
-	GData *attribs;
-	const char *start, *end = NULL, *last;
-	ggp_buddy_data *buddy_data = ggp_buddy_get_data(
-		purple_find_buddy(purple_connection_get_account(gc), who));
-
-	if (msg == NULL || *msg == '\0') {
-		return 0;
-	}
-
-	if (buddy_data->blocked)
-		return -1;
-
-	last = msg;
-
-	/* Check if the message is richtext */
-	/* TODO: Check formatting, too */
-	if(purple_markup_find_tag("img", last, &start, &end, &attribs)) {
-
-		GString *string_buffer = g_string_new(NULL);
-		struct gg_msg_richtext fmt;
-
-		do
-		{
-			const char *id = g_datalist_get_data(&attribs, "id");
-			struct gg_msg_richtext_format actformat;
-			struct gg_msg_richtext_image actimage;
-			ggp_image_prepare_result prepare_result;
-
-			/* Add text before the image */
-			if(start - last)
-			{
-				pos = pos + g_utf8_strlen(last, start - last);
-				g_string_append_len(string_buffer, last,
-					start - last);
-			}
-			last = end + 1;
-			
-			if (id == NULL)
-			{
-				g_datalist_clear(&attribs);
-				continue;
-			}
-
-			/* add the image itself */
-			prepare_result = ggp_image_prepare(
-				gc, atoi(id), who, &actimage);
-			if (prepare_result == GGP_IMAGE_PREPARE_OK)
-			{
-				actformat.font = GG_FONT_IMAGE;
-				actformat.position = pos;
-
-				memcpy(format + format_length, &actformat,
-					sizeof(actformat));
-				format_length += sizeof(actformat);
-				memcpy(format + format_length, &actimage,
-					sizeof(actimage));
-				format_length += sizeof(actimage);
-			}
-			else if (prepare_result == GGP_IMAGE_PREPARE_TOO_BIG)
-			{
-				PurpleConversation *conv =
-					purple_find_conversation_with_account(
-						PURPLE_CONV_TYPE_IM, who,
-						purple_connection_get_account(gc));
-				purple_conversation_write(conv, "",
-					_("Image is too large, please try "
-					"smaller one."), PURPLE_MESSAGE_ERROR,
-					time(NULL));
-			}
-			
-			g_datalist_clear(&attribs);
-		} while (purple_markup_find_tag("img", last, &start, &end,
-			&attribs));
-
-		/* Add text after the images */
-		if(last && *last) {
-			pos = pos + g_utf8_strlen(last, -1);
-			g_string_append(string_buffer, last);
-		}
-
-		fmt.flag = 2;
-		fmt.length = format_length - sizeof(fmt);
-		memcpy(format, &fmt, sizeof(fmt));
-
-		purple_debug_info("gg", "ggp_send_im: richtext msg = %s\n", string_buffer->str);
-		plain = purple_unescape_html(string_buffer->str);
-		g_string_free(string_buffer, TRUE);
-	} else {
-		purple_debug_info("gg", "ggp_send_im: msg = %s\n", msg);
-		plain = purple_unescape_html(msg);
-	}
-
-	tmp = g_strdup(plain);
-
-	if (tmp && (format_length - sizeof(struct gg_msg_richtext))) {
-		if(gg_send_message_richtext(info->session, GG_CLASS_CHAT, ggp_str_to_uin(who), (unsigned char *)tmp, format, format_length) < 0) {
-			ret = -1;
-		} else {
-			ret = 1;
-		}
-	} else if (NULL == tmp || *tmp == 0) {
-		ret = 0;
-	} else if (strlen(tmp) > GG_MSG_MAXSIZE) {
-		ret = -E2BIG;
-	} else if (gg_send_message(info->session, GG_CLASS_CHAT,
-				ggp_str_to_uin(who), (unsigned char *)tmp) < 0) {
-		ret = -1;
-	} else {
-		ret = 1;
-	}
-
-	g_free(plain);
-	g_free(tmp);
-
-	return ret;
-}
-
 static unsigned int ggp_send_typing(PurpleConnection *gc, const char *name, PurpleTypingState state)
 {
 	GGPInfo *info = purple_connection_get_protocol_data(gc);
@@ -1028,7 +904,7 @@ static PurplePluginProtocolInfo prpl_inf
 	ggp_chat_info_defaults,		/* chat_info_defaults */
 	ggp_login,			/* login */
 	ggp_close,			/* close */
-	ggp_send_im,			/* send_im */
+	ggp_message_send_im,		/* send_im */
 	NULL,				/* set_info */
 	ggp_send_typing,		/* send_typing */
 	ggp_pubdir_get_info_prpl,	/* get_info */
diff --git a/libpurple/protocols/gg/message-prpl.c b/libpurple/protocols/gg/message-prpl.c
--- a/libpurple/protocols/gg/message-prpl.c
+++ b/libpurple/protocols/gg/message-prpl.c
@@ -2,6 +2,7 @@
 
 #include <debug.h>
 
+#include "gg.h"
 #include "chat.h"
 #include "utils.h"
 
@@ -31,6 +32,7 @@ static void ggp_message_got_display(Purp
 	ggp_message_got_data *msg);
 static void ggp_message_format_from_gg(ggp_message_got_data *msg,
 	const gchar *text);
+static gchar * ggp_message_format_to_gg(const gchar *text);
 
 /**************/
 
@@ -138,3 +140,72 @@ static void ggp_message_format_from_gg(g
 
 	msg->text = text_new;
 }
+
+static gchar * ggp_message_format_to_gg(const gchar *text)
+{
+	gchar *text_new, *tmp;
+	GRegex *regex;
+
+	/* TODO: do it via xml parser*/
+
+	text_new = purple_strreplace(text, "<hr>", "<br>---<br>");
+
+	tmp = text_new;
+	text_new = purple_strreplace(text_new, "</a> ", " ");
+	g_free(tmp);
+
+	tmp = text_new;
+	text_new = purple_strreplace(text_new, "</a>", "");
+	g_free(tmp);
+	
+	tmp = text_new;
+	text_new = purple_strreplace(text_new, " <a ", " <a ");
+	g_free(tmp);
+	
+	regex = g_regex_new("<a href=[^>]+>", G_REGEX_CASELESS /*| G_REGEX_OPTIMIZE */, 0, NULL);
+	tmp = text_new;
+	text_new = g_regex_replace_literal(regex, text_new, -1, 0, "", 0, NULL);
+	g_free(tmp);
+	g_regex_unref(regex);
+	
+	/* TODO: sent images */
+	return text_new;
+}
+
+/* sending */
+
+int ggp_message_send_im(PurpleConnection *gc, const char *who,
+	const char *message, PurpleMessageFlags flags)
+{
+	GGPInfo *info = purple_connection_get_protocol_data(gc);
+	ggp_buddy_data *buddy_data;
+	gchar *gg_msg;
+	gboolean succ;
+
+	/* TODO: return -ENOTCONN, if not connected */
+
+	if (message == NULL || message[0] == '\0')
+		return 0;
+
+	buddy_data = ggp_buddy_get_data(purple_find_buddy(
+		purple_connection_get_account(gc), who));
+
+	if (buddy_data->blocked)
+		return -1;
+
+	gg_msg = ggp_message_format_to_gg(message);
+
+	/* TODO: splitting messages */
+	if (strlen(gg_msg) > GG_MSG_MAXSIZE)
+	{
+		g_free(gg_msg);
+		return -E2BIG;
+	}
+
+	succ = (gg_send_message_html(info->session, GG_CLASS_CHAT,
+		ggp_str_to_uin(who), (unsigned char *)gg_msg) >= 0);
+
+	g_free(gg_msg);
+
+	return succ ? 1 : -1;
+}
diff --git a/libpurple/protocols/gg/message-prpl.h b/libpurple/protocols/gg/message-prpl.h
--- a/libpurple/protocols/gg/message-prpl.h
+++ b/libpurple/protocols/gg/message-prpl.h
@@ -8,4 +8,7 @@ void ggp_message_got(PurpleConnection *g
 void ggp_message_got_multilogon(PurpleConnection *gc,
 	const struct gg_event_msg *ev);
 
+int ggp_message_send_im(PurpleConnection *gc, const char *who,
+	const char *message, PurpleMessageFlags flags);
+
 #endif /* _GGP_MESSAGE_PRPL_H */



More information about the Commits mailing list