Send images in messages
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "delta-connection.h"
|
||||
@@ -402,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
|
||||
}
|
||||
|
Reference in New Issue
Block a user