]> www.infradead.org Git - pidgin-chime.git/commitdiff
Fix attachment messages when the user sends it from other clients
authorGuilherme Melo <guilmelo@amazon.com>
Sat, 25 Nov 2017 16:22:06 +0000 (18:22 +0200)
committerGuilherme Melo <guilmelo@amazon.com>
Thu, 7 Dec 2017 12:04:28 +0000 (14:04 +0200)
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

attachments.c
chat.c
chime.h
conversations.c

index 6745c5cfa8252146e096033ca65d4594e169b50d..aafb5a4ddae4c5d1efcbe500fdb08544e604e2a5 100755 (executable)
 
 #include <errno.h>
 #include <glib/gi18n.h>
+#include <debug.h>
 #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("<br><img id=\"%u\">", 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 6973c143b5aec491e7ca594f773d79935b432f21..a4aa504f6b2242b7d4bd013e3f717229a6ce9121 100644 (file)
--- 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 59584b460e5288e5b14e9ddc5e329456e0f7cddd..48971a4bb9e887bbba09eeac9c253ca03c084858 100644 (file)
--- 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;
index 28944e84ec832372689729d7f87cfb21c90c355a..9414a6a79bad483db4aa36e9ca11b496bdbf5731 100644 (file)
@@ -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);