|
|
|
@@ -12,7 +12,9 @@
|
|
|
|
|
#include "delta-connection.h"
|
|
|
|
|
#include "libdelta.h"
|
|
|
|
|
|
|
|
|
|
void delta_recv_im(DeltaConnectionData *conn, uint32_t msg_id);
|
|
|
|
|
#define IMEX_RECEIVED_MESSAGE "Setup message received. To apply it, reply with:\nIMEX: %d nnnn-nnnn-nnnn-nnnn-nnnn-nnnn-nnnn-nnnn-nnnn\nNo whitespace in the setup-code!"
|
|
|
|
|
|
|
|
|
|
void delta_recv_im(DeltaConnectionData *conn, dc_msg_t *msg);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_transpose_config(dc_context_t *mailbox, PurpleAccount *acct)
|
|
|
|
@@ -30,6 +32,8 @@ _transpose_config(dc_context_t *mailbox, PurpleAccount *acct)
|
|
|
|
|
const char *smtp_pass = purple_account_get_string(acct, PLUGIN_ACCOUNT_OPT_SMTP_PASS, NULL);
|
|
|
|
|
const char *smtp_port = purple_account_get_string(acct, PLUGIN_ACCOUNT_OPT_SMTP_SERVER_PORT, NULL);
|
|
|
|
|
|
|
|
|
|
gboolean bcc_self = purple_account_get_bool(acct, PLUGIN_ACCOUNT_OPT_BCC_SELF, FALSE);
|
|
|
|
|
|
|
|
|
|
dc_set_config(mailbox, PLUGIN_ACCOUNT_OPT_ADDR, addr);
|
|
|
|
|
dc_set_config(mailbox, PLUGIN_ACCOUNT_OPT_DISPLAY_NAME, display);
|
|
|
|
|
|
|
|
|
@@ -42,6 +46,12 @@ _transpose_config(dc_context_t *mailbox, PurpleAccount *acct)
|
|
|
|
|
dc_set_config(mailbox, PLUGIN_ACCOUNT_OPT_SMTP_USER, smtp_user);
|
|
|
|
|
dc_set_config(mailbox, PLUGIN_ACCOUNT_OPT_SMTP_PASS, smtp_pass);
|
|
|
|
|
dc_set_config(mailbox, PLUGIN_ACCOUNT_OPT_SMTP_SERVER_PORT, smtp_port);
|
|
|
|
|
|
|
|
|
|
if (bcc_self) {
|
|
|
|
|
dc_set_config(mailbox, PLUGIN_ACCOUNT_OPT_BCC_SELF, "1");
|
|
|
|
|
} else {
|
|
|
|
|
dc_set_config(mailbox, PLUGIN_ACCOUNT_OPT_BCC_SELF, "0");
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
@@ -49,9 +59,10 @@ typedef struct {
|
|
|
|
|
|
|
|
|
|
// Used by delta_process_incoming_message
|
|
|
|
|
uint32_t msg_id;
|
|
|
|
|
gboolean msg_changed;
|
|
|
|
|
|
|
|
|
|
// Used by delta_process_connection_state
|
|
|
|
|
int connection_state;
|
|
|
|
|
// int connection_state;
|
|
|
|
|
} ProcessRequest;
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
@@ -61,13 +72,14 @@ delta_process_incoming_message(void *data)
|
|
|
|
|
g_assert(pr != NULL);
|
|
|
|
|
g_assert(pr->conn != NULL);
|
|
|
|
|
|
|
|
|
|
delta_recv_im(pr->conn, pr->msg_id);
|
|
|
|
|
|
|
|
|
|
dc_msg_t *msg = dc_get_msg(pr->conn->mailbox, pr->msg_id);
|
|
|
|
|
delta_recv_im(pr->conn, msg);
|
|
|
|
|
dc_msg_unref(msg);
|
|
|
|
|
g_free(data);
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
gboolean
|
|
|
|
|
delta_process_connection_state(void *data)
|
|
|
|
|
{
|
|
|
|
@@ -83,33 +95,38 @@ delta_process_connection_state(void *data)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (pr->connection_state == MAX_DELTA_CONFIGURE) {
|
|
|
|
|
purple_connection_set_state(pr->conn->pc, PURPLE_CONNECTED);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_free(data);
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
delta_process_fresh_messages(void *data)
|
|
|
|
|
{
|
|
|
|
|
ProcessRequest *pr = (ProcessRequest *)data;
|
|
|
|
|
g_assert(pr != NULL);
|
|
|
|
|
g_assert(pr->conn != NULL);
|
|
|
|
|
g_assert(pr->conn->mailbox != NULL);
|
|
|
|
|
|
|
|
|
|
dc_context_t *mailbox = pr->conn->mailbox;
|
|
|
|
|
g_assert(mailbox != NULL);
|
|
|
|
|
|
|
|
|
|
// Spot any messages received while offline
|
|
|
|
|
dc_array_t *fresh_msgs = dc_get_fresh_msgs(pr->conn->mailbox);
|
|
|
|
|
dc_array_t *fresh_msgs = dc_get_fresh_msgs(mailbox);
|
|
|
|
|
size_t fresh_count = dc_array_get_cnt(fresh_msgs);
|
|
|
|
|
|
|
|
|
|
purple_debug_info(PLUGIN_ID, "fresh_count: %zu\n", fresh_count);
|
|
|
|
|
|
|
|
|
|
for(size_t i = 0; i < fresh_count; i++) {
|
|
|
|
|
delta_recv_im(pr->conn, dc_array_get_id(fresh_msgs, i));
|
|
|
|
|
uint32_t msg_id = dc_array_get_id(fresh_msgs, i);
|
|
|
|
|
dc_msg_t *msg = dc_get_msg(mailbox, msg_id);
|
|
|
|
|
|
|
|
|
|
delta_recv_im(pr->conn, msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_free(fresh_msgs);
|
|
|
|
|
dc_array_unref(fresh_msgs);
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
@@ -141,8 +158,6 @@ delta_event_handler(void *context)
|
|
|
|
|
dc_event_emitter_t* emitter = dc_get_event_emitter(mailbox);
|
|
|
|
|
dc_event_t* event;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: do we still need runthreads?
|
|
|
|
|
while (conn->runthreads && (event = dc_get_next_event(emitter)) != NULL) {
|
|
|
|
|
ProcessRequest *pr = NULL;
|
|
|
|
@@ -168,8 +183,7 @@ delta_event_handler(void *context)
|
|
|
|
|
dc_str_unref(warn);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case DC_EVENT_ERROR:
|
|
|
|
|
case DC_EVENT_ERROR_NETWORK: {
|
|
|
|
|
case DC_EVENT_ERROR: {
|
|
|
|
|
int errcode = dc_event_get_data1_int(event);
|
|
|
|
|
char *err = dc_event_get_data2_str(event);
|
|
|
|
|
purple_debug_info(PLUGIN_ID, "ERROR from Delta: %d: %s\n", errcode, err);
|
|
|
|
@@ -177,10 +191,25 @@ delta_event_handler(void *context)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DC_EVENT_MSGS_CHANGED:
|
|
|
|
|
case DC_EVENT_MSGS_CHANGED: {
|
|
|
|
|
// This event may be issued for a single message, in which case the
|
|
|
|
|
// message ID is in data2 and we should treat it as an incoming msg
|
|
|
|
|
// FIXME: this leads to duplicate messages when it's an outgoing
|
|
|
|
|
// message we just sent
|
|
|
|
|
uint32_t msg_id = dc_event_get_data2_int(event);
|
|
|
|
|
|
|
|
|
|
pr = delta_build_process_request(conn);
|
|
|
|
|
purple_timeout_add(0, delta_process_fresh_messages, pr);
|
|
|
|
|
if (msg_id) {
|
|
|
|
|
// FIXME: for now, only display IMEX setup messages to avoid duplicates
|
|
|
|
|
pr->msg_id = msg_id;
|
|
|
|
|
pr->msg_changed = TRUE;
|
|
|
|
|
|
|
|
|
|
purple_timeout_add(0, delta_process_incoming_message, pr);
|
|
|
|
|
} else {
|
|
|
|
|
purple_timeout_add(0, delta_process_fresh_messages, pr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DC_EVENT_INCOMING_MSG:
|
|
|
|
|
// data1 is chat_id, which we don't seem to need yet.
|
|
|
|
@@ -210,9 +239,10 @@ delta_event_handler(void *context)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case DC_EVENT_CONFIGURE_PROGRESS:
|
|
|
|
|
pr = delta_build_process_request(conn);
|
|
|
|
|
pr->connection_state = dc_event_get_data1_int(event);
|
|
|
|
|
purple_timeout_add(0, delta_process_connection_state, pr);
|
|
|
|
|
//pr = delta_build_process_request(conn);
|
|
|
|
|
//pr->connection_state = dc_event_get_data1_int(event);
|
|
|
|
|
//purple_timeout_add(0, delta_process_connection_state, pr);
|
|
|
|
|
purple_debug_info(PLUGIN_ID, "Configure progress: %d\n", dc_event_get_data1_int(event));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
@@ -294,9 +324,30 @@ delta_connection_start_login(PurpleConnection *pc)
|
|
|
|
|
dc_start_io(mailbox);
|
|
|
|
|
dc_maybe_network(mailbox);
|
|
|
|
|
|
|
|
|
|
purple_connection_set_state(pc, PURPLE_CONNECTED);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean delta_try_process_imex(dc_context_t *mailbox, char *text) {
|
|
|
|
|
if (!g_str_has_prefix(text, "IMEX: ")) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gchar **parts = g_strsplit(text, " ", 3);
|
|
|
|
|
if (g_strv_length(parts) != 3) {
|
|
|
|
|
g_strfreev(parts);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int msg_id = atoi(parts[1]);
|
|
|
|
|
gboolean success = dc_continue_key_transfer(mailbox, msg_id, parts[2]);
|
|
|
|
|
|
|
|
|
|
g_strfreev(parts);
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
delta_send_im(PurpleConnection *pc, const char *who, const char *message, PurpleMessageFlags flags)
|
|
|
|
|
{
|
|
|
|
@@ -373,15 +424,17 @@ next:
|
|
|
|
|
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);
|
|
|
|
|
if (!delta_try_process_imex(mailbox, stripped_message)) {
|
|
|
|
|
dc_send_text_msg(mailbox, chat_id, stripped_message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g_free(stripped_message);
|
|
|
|
|
|
|
|
|
|
return 1; // success; echo the message to the chat window
|
|
|
|
|
return 0; // success; don't echo the message to the chat window since we display it anyway
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
delta_recv_im(DeltaConnectionData *conn, uint32_t msg_id)
|
|
|
|
|
delta_recv_im(DeltaConnectionData *conn, dc_msg_t *msg)
|
|
|
|
|
{
|
|
|
|
|
dc_context_t *mailbox = conn->mailbox;
|
|
|
|
|
g_assert(mailbox != NULL);
|
|
|
|
@@ -389,24 +442,69 @@ delta_recv_im(DeltaConnectionData *conn, uint32_t msg_id)
|
|
|
|
|
PurpleConnection *pc = conn->pc;
|
|
|
|
|
g_assert(pc != NULL);
|
|
|
|
|
|
|
|
|
|
dc_msg_t* msg = dc_get_msg(mailbox, msg_id);
|
|
|
|
|
uint32_t msg_id = dc_msg_get_id(msg);
|
|
|
|
|
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);
|
|
|
|
|
uint32_t chat_id = dc_msg_get_chat_id(msg);
|
|
|
|
|
dc_contact_t *from = dc_get_contact(mailbox, dc_msg_get_from_id(msg));
|
|
|
|
|
dc_chat_t *chat = dc_get_chat(mailbox, chat_id);
|
|
|
|
|
dc_array_t *contacts = dc_get_chat_contacts(mailbox, chat_id);
|
|
|
|
|
int num_contacts = dc_array_get_cnt(contacts);
|
|
|
|
|
|
|
|
|
|
dc_contact_t *contact = dc_get_contact(mailbox, contact_id);
|
|
|
|
|
if (contact == NULL) {
|
|
|
|
|
purple_debug_info(PLUGIN_ID, "Receiving IM: unknown contact: %d\n", contact_id);
|
|
|
|
|
if (chat == NULL) {
|
|
|
|
|
purple_debug_info(PLUGIN_ID, "Receiving IM: unknown chat: %d\n", chat_id);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *who = dc_contact_get_addr(contact);
|
|
|
|
|
int flags = PURPLE_MESSAGE_RECV;
|
|
|
|
|
if (dc_chat_get_type(chat) == DC_CHAT_TYPE_GROUP) {
|
|
|
|
|
purple_debug_info(PLUGIN_ID, "Receiving IM: group chat with ID %d! Not yet supported\n", chat_id);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (num_contacts != 1) {
|
|
|
|
|
purple_debug_info(PLUGIN_ID, "Receiving IM: 1-1 chat %d with %d contacts instead of 1!\n", chat_id, num_contacts);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: using dc_array_get_contact_id fails here, complaining that it's not an array of locations
|
|
|
|
|
dc_contact_t *contact = dc_get_contact(mailbox, dc_array_get_id(contacts, 0));
|
|
|
|
|
char *who = NULL;
|
|
|
|
|
|
|
|
|
|
// In the current architecture, delta_send_im and delta_recv_im must agree
|
|
|
|
|
// on the value for 'who'. Using the email address is an easy cheat for this
|
|
|
|
|
// but gets shaky in the long term.
|
|
|
|
|
if (contact != NULL) {
|
|
|
|
|
who = dc_contact_get_addr(contact);
|
|
|
|
|
} else {
|
|
|
|
|
who = dc_chat_get_name(chat);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int flags = 0;
|
|
|
|
|
int state = dc_msg_get_state(msg);
|
|
|
|
|
|
|
|
|
|
if (state == DC_STATE_IN_FRESH || state == DC_STATE_IN_NOTICED || state == DC_STATE_IN_SEEN) {
|
|
|
|
|
flags |= PURPLE_MESSAGE_RECV;
|
|
|
|
|
} else {
|
|
|
|
|
flags |= PURPLE_MESSAGE_SEND;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: as a massive hack, convert IMEX setup messages into a text message
|
|
|
|
|
// prompting the user how to trigger the IMEX filter in outgoing messages.
|
|
|
|
|
if (dc_msg_is_setupmessage(msg)) {
|
|
|
|
|
purple_debug_info(PLUGIN_ID, "Receiving IMEX: ID=%d\n", msg_id);
|
|
|
|
|
viewtype = DC_MSG_TEXT;
|
|
|
|
|
dc_str_unref(text);
|
|
|
|
|
text = g_strndup("", 1024);
|
|
|
|
|
g_assert(text != NULL);
|
|
|
|
|
|
|
|
|
|
g_snprintf(text, 1024, IMEX_RECEIVED_MESSAGE, msg_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(viewtype) {
|
|
|
|
|
case DC_MSG_GIF:
|
|
|
|
|
case DC_MSG_IMAGE:
|
|
|
|
|
case DC_MSG_STICKER:
|
|
|
|
|
flags = flags | PURPLE_MESSAGE_IMAGES;
|
|
|
|
|
break;
|
|
|
|
|
case DC_MSG_TEXT:
|
|
|
|
@@ -437,18 +535,28 @@ delta_recv_im(DeltaConnectionData *conn, uint32_t msg_id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
image_id = purple_imgstore_add_with_id(data, length, filename);
|
|
|
|
|
text = g_strdup_printf("<img id='%d'>", image_id);
|
|
|
|
|
text = g_strdup_printf("<img id='%d'><br/>%s", image_id, text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serv_got_im(pc, who, text, flags, timestamp);
|
|
|
|
|
char *name = dc_contact_get_display_name(from);
|
|
|
|
|
int msglen = strlen(name) + 3 + strlen(text);
|
|
|
|
|
|
|
|
|
|
char *msgtext = malloc(msglen);
|
|
|
|
|
g_snprintf(msgtext, msglen, "%s: %s", name, text);
|
|
|
|
|
|
|
|
|
|
serv_got_im(pc, who, msgtext, flags, timestamp);
|
|
|
|
|
|
|
|
|
|
if (image_id > 0) {
|
|
|
|
|
purple_imgstore_unref_by_id(image_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dc_markseen_msgs(mailbox, &msg_id, 1);
|
|
|
|
|
g_free(who);
|
|
|
|
|
g_free(msgtext);
|
|
|
|
|
dc_str_unref(who);
|
|
|
|
|
dc_str_unref(name);
|
|
|
|
|
dc_contact_unref(contact);
|
|
|
|
|
out:
|
|
|
|
|
g_free(text);
|
|
|
|
|
dc_msg_unref(msg);
|
|
|
|
|
dc_str_unref(text);
|
|
|
|
|
dc_chat_unref(chat);
|
|
|
|
|
dc_array_unref(contacts);
|
|
|
|
|
}
|
|
|
|
|