From: Guilherme Melo Date: Sat, 25 Nov 2017 16:22:06 +0000 (+0200) Subject: Fix attachment messages when the user sends it from other clients X-Git-Tag: v0.9~87^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=488e7e64f4c05f574eaec87a3e1e3e5cbca78e8f;p=pidgin-chime.git Fix attachment messages when the user sends it from other clients On an IM conversation between "A" and "B", if "A" sent an attachment from other client (e.g. Web), "A" would see the message on pidgin as being sent by "B". Also "A" would receive a notification from pidgin for its own attachments. Fix #10 https://github.com/awslabs/PRIVATE-purple-chime/issues/10 --- diff --git a/attachments.c b/attachments.c index 6745c5c..aafb5a4 100755 --- a/attachments.c +++ b/attachments.c @@ -17,19 +17,46 @@ #include #include +#include #include "chime.h" // According to http://docs.aws.amazon.com/chime/latest/ug/chime-ug.pdf this is the maximum allowed size for attachments. // (The default limit for purple_util_fetch_url() is 512 kB.) #define ATTACHMENT_MAX_SIZE (50*1000*1000) +/* + * Writes to the IM conversation handling the case where the user sent message + * from other client. + */ +static void write_conversation_message(const char *from, const char *im_email, + PurpleConnection *conn, const gchar *msg, PurpleMessageFlags flags, time_t when) +{ + if (!strcmp(from, im_email)) { + serv_got_im(conn, im_email, msg, flags | PURPLE_MESSAGE_RECV, when); + } else { + PurpleAccount *account = conn->account; + PurpleConversation *pconv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, + im_email, account); + if (!pconv) { + pconv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, im_email); + if (!pconv) { + purple_debug_error("chime", "NO CONV FOR %s\n", im_email); + return; + } + } + /* Just inject a message from ourselves, avoiding any notifications */ + purple_conversation_write(pconv, NULL, msg, flags | PURPLE_MESSAGE_SEND, when); + } +} + static void img_message(AttachmentContext *ctx, int image_id) { + PurpleMessageFlags flags = PURPLE_MESSAGE_IMAGES; gchar *msg = g_strdup_printf("
", image_id); if (ctx->chat_id != -1) { - serv_got_chat_in(ctx->conn, ctx->chat_id, ctx->from, PURPLE_MESSAGE_IMAGES, msg, ctx->when); + serv_got_chat_in(ctx->conn, ctx->chat_id, ctx->from, flags, msg, ctx->when); } else { - serv_got_im(ctx->conn, ctx->from, msg, PURPLE_MESSAGE_IMAGES, ctx->when); + write_conversation_message(ctx->from, ctx->im_email, ctx->conn, msg, flags, ctx->when); } g_free(msg); } @@ -40,7 +67,7 @@ static void sys_message(AttachmentContext *ctx, const gchar *msg, PurpleMessageF if (ctx->chat_id != -1) { serv_got_chat_in(ctx->conn, ctx->chat_id, "", flags, msg, time(NULL)); } else { - serv_got_im(ctx->conn, ctx->from, msg, flags, time(NULL)); + write_conversation_message(ctx->from, ctx->im_email, ctx->conn, msg, flags, time(NULL)); } } diff --git a/chat.c b/chat.c index 6973c14..a4aa504 100644 --- a/chat.c +++ b/chat.c @@ -148,6 +148,7 @@ static void do_chat_deliver_msg(ChimeConnection *cxn, struct chime_msgs *msgs, ctx->conn = conn; ctx->chat_id = id; ctx->from = from; + ctx->im_email = ""; ctx->when = msg_time; /* The attachment and context structs will be owned by the code doing the download and will be disposed of at the end. */ download_attachment(cxn, att, ctx); diff --git a/chime.h b/chime.h index 59584b4..48971a4 100644 --- a/chime.h +++ b/chime.h @@ -144,7 +144,8 @@ typedef struct _ChimeAttachment { typedef struct _AttachmentContext { PurpleConnection *conn; - const char *from; + const char *from; /* Sender email. May be the own user in case he/she sends from another client. */ + const char *im_email; /* Email identifying the IM conversation. May be the the same as `from` */ time_t when; int chat_id; /* -1 for IM */ } AttachmentContext; diff --git a/conversations.c b/conversations.c index 28944e8..9414a6a 100644 --- a/conversations.c +++ b/conversations.c @@ -50,6 +50,14 @@ static gboolean do_conv_deliver_msg(ChimeConnection *cxn, struct chime_im *im, flags |= PURPLE_MESSAGE_SYSTEM; const gchar *email = chime_contact_get_email(im->peer); + const gchar *from = _("Unknown sender"); + if (!strcmp(sender, chime_connection_get_profile_id(cxn))) { + from = chime_connection_get_email(cxn); + } else { + ChimeContact *who = chime_connection_contact_by_id(cxn, sender); + if (who) + from = chime_contact_get_email(who); + } gchar *escaped = g_markup_escape_text(message, -1); ChimeAttachment *att = extract_attachment(record); @@ -57,7 +65,8 @@ static gboolean do_conv_deliver_msg(ChimeConnection *cxn, struct chime_im *im, AttachmentContext *ctx = g_new(AttachmentContext, 1); ctx->conn = im->m.conn; ctx->chat_id = -1; - ctx->from = email; + ctx->from = from; + ctx->im_email = email; ctx->when = msg_time; /* The attachment and context structs will be owned by the code doing the download and will be disposed of at the end. */ download_attachment(cxn, att, ctx);