Merge branch '8-handle-incoming-images' into 'master'

Handle incoming images

See merge request lupine/purple-plugin-delta!14
This commit is contained in:
2019-04-22 00:15:58 +00:00

View File

@@ -1,11 +1,13 @@
#include <connection.h>
#include <debug.h>
#include <eventloop.h>
#include <imgstore.h>
#include <util.h>
#include <string.h>
#include <pthread.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "delta-connection.h"
#include "libdelta.h"
@@ -401,11 +403,72 @@ delta_send_im(PurpleConnection *pc, const char *who, const char *message, Purple
uint32_t contact_id = dc_create_contact(mailbox, NULL, who);
uint32_t chat_id = dc_create_chat_by_contact_id(mailbox, contact_id);
char *unescaped_message = purple_unescape_html(message);
g_assert(unescaped_message != NULL);
dc_send_text_msg(mailbox, chat_id, unescaped_message);
g_free(unescaped_message);
GData *attrs;
const char *msg_ptr, *start, *end;
msg_ptr = message;
// Send each image included in the message.
while (purple_markup_find_tag("img", msg_ptr, &start, &end, &attrs) == TRUE) {
char *id_str = g_datalist_id_get_data(&attrs, g_quark_from_string("id"));
purple_debug_info(PLUGIN_ID, "In a loop, got %s\n", id_str);
msg_ptr = end + 1;
if (id_str == NULL || strlen(id_str) == 0) {
g_datalist_clear(&attrs);
continue;
}
int id = atoi(id_str);
g_datalist_clear(&attrs);
if (id <= 0) {
continue;
}
GError *err = NULL;
char *tempdir = g_dir_make_tmp(NULL, &err);
if (err != NULL) {
purple_debug_info(PLUGIN_ID, "Couldn't get a temporary dir for image %d: %s", id, err->message);
g_free(err);
continue;
}
PurpleStoredImage *img = purple_imgstore_find_by_id(id);
const char *filename = purple_imgstore_get_filename(img);
const char *extension = purple_imgstore_get_extension(img);
gconstpointer data = purple_imgstore_get_data(img);
char *path = g_strdup_printf("%s/%s", tempdir, filename);
g_file_set_contents(path, data, purple_imgstore_get_size(img), &err);
if (err != NULL) {
purple_debug_info(PLUGIN_ID, "failed to write %s to temporary file: %s\n", filename, err->message);
g_free(err);
goto next;
}
purple_debug_info(PLUGIN_ID, "Sending image %s from imgstore: %d\n", filename, id);
dc_msg_t *img_msg = dc_msg_new(mailbox, DC_MSG_IMAGE);
dc_msg_set_file(img_msg, path, extension);
dc_send_msg(mailbox, chat_id, img_msg);
dc_msg_unref(img_msg);
next:
remove(path);
remove(tempdir);
g_free(path);
}
// Send any text left
char *stripped_message = purple_markup_strip_html(message);
g_assert(stripped_message != NULL);
if (strlen(stripped_message) > 0) {
dc_send_text_msg(mailbox, chat_id, stripped_message);
}
g_free(stripped_message);
return 1; // success; echo the message to the chat window
}
@@ -420,7 +483,7 @@ delta_recv_im(DeltaConnectionData *conn, uint32_t msg_id)
g_assert(pc != NULL);
dc_msg_t* msg = dc_get_msg(mailbox, msg_id);
int viewtype = dc_msg_get_viewtype(msg);
time_t timestamp = dc_msg_get_timestamp(msg);
char *text = dc_msg_get_text(msg);
uint32_t contact_id = dc_msg_get_from_id(msg);
@@ -432,8 +495,49 @@ delta_recv_im(DeltaConnectionData *conn, uint32_t msg_id)
}
char *who = dc_contact_get_addr(contact);
int flags = PURPLE_MESSAGE_RECV;
serv_got_im(pc, who, text, PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_RAW, timestamp);
switch(viewtype) {
case DC_MSG_GIF:
case DC_MSG_IMAGE:
flags = flags | PURPLE_MESSAGE_IMAGES;
break;
case DC_MSG_TEXT:
flags = flags | PURPLE_MESSAGE_RAW;
break;
case DC_MSG_VIDEO: // Pidgin only supports these as files for download
case DC_MSG_FILE:
case DC_MSG_AUDIO: // Sound to play
case DC_MSG_VOICE:
break;
default:
purple_debug_info(PLUGIN_ID, "Message %d: unknown message type: %d\n", msg_id, viewtype);
}
int image_id = 0;
if ((flags & PURPLE_MESSAGE_IMAGES) > 0) {
char *filename = dc_msg_get_file(msg);
gchar *data;
gsize length;
GError *err = NULL;
g_file_get_contents(filename, &data, &length, &err);
if (err != NULL) {
purple_debug_info(PLUGIN_ID, "Failed to read image %s: %s\n", filename, err->message);
g_error_free(err);
goto out;
}
image_id = purple_imgstore_add_with_id(data, length, filename);
text = g_strdup_printf("<img id='%d'>", image_id);
}
serv_got_im(pc, who, text, flags, timestamp);
if (image_id > 0) {
purple_imgstore_unref_by_id(image_id);
}
dc_markseen_msgs(mailbox, &msg_id, 1);
g_free(who);