Skip to content

Commit

Permalink
Move id from DeliveryReceipt to ProfBuffEntry struct
Browse files Browse the repository at this point in the history
XEP-0184: Message Delivery Receipts, *requires* the id attribute.
Generally this is not the case.
For this reason the id was only present in the DeliveryReceipt struct
since it was only used for XEP-0184.

For #660 XEP-0313 MAM
and #805 XEP-0308 Last Message Correction
we will also need the id.

So in preparation for further work let's move the id to the general
ProfBuffEntry.

We will need to adapt code so that we actually always write the ID if we
receive one.
  • Loading branch information
jubalh committed Nov 1, 2019
1 parent d5212d8 commit bc282ef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
13 changes: 6 additions & 7 deletions src/ui/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ buffer_free(ProfBuff buffer)

void
buffer_append(ProfBuff buffer, const char show_char, int pad_indent, GDateTime *time,
int flags, theme_item_t theme_item, const char *const from, const char *const message, DeliveryReceipt *receipt)
int flags, theme_item_t theme_item, const char *const from, const char *const message, DeliveryReceipt *receipt, const char *const id)
{
ProfBuffEntry *e = malloc(sizeof(struct prof_buff_entry_t));
e->show_char = show_char;
Expand All @@ -91,6 +91,7 @@ buffer_append(ProfBuff buffer, const char show_char, int pad_indent, GDateTime *
e->from = from ? strdup(from) : NULL;
e->message = strdup(message);
e->receipt = receipt;
e->id = strdup(id);

if (g_slist_length(buffer->entries) == BUFF_SIZE) {
_free_entry(buffer->entries->data);
Expand All @@ -106,7 +107,7 @@ buffer_mark_received(ProfBuff buffer, const char *const id)
GSList *entries = buffer->entries;
while (entries) {
ProfBuffEntry *entry = entries->data;
if (entry->receipt && g_strcmp0(entry->receipt->id, id) == 0) {
if (entry->receipt && g_strcmp0(entry->id, id) == 0) {
if (!entry->receipt->received) {
entry->receipt->received = TRUE;
return TRUE;
Expand All @@ -131,7 +132,7 @@ buffer_get_entry_by_id(ProfBuff buffer, const char *const id)
GSList *entries = buffer->entries;
while (entries) {
ProfBuffEntry *entry = entries->data;
if (entry->receipt && g_strcmp0(entry->receipt->id, id) == 0) {
if (g_strcmp0(entry->id, id) == 0) {
return entry;
}
entries = g_slist_next(entries);
Expand All @@ -145,10 +146,8 @@ _free_entry(ProfBuffEntry *entry)
{
free(entry->message);
free(entry->from);
free(entry->id);
free(entry->receipt);
g_date_time_unref(entry->time);
if (entry->receipt) {
free(entry->receipt->id);
free(entry->receipt);
}
free(entry);
}
7 changes: 4 additions & 3 deletions src/ui/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "config/theme.h"

typedef struct delivery_receipt_t {
char *id;
gboolean received;
} DeliveryReceipt;

Expand All @@ -54,14 +53,16 @@ typedef struct prof_buff_entry_t {
char *from;
char *message;
DeliveryReceipt *receipt;
// message id, in case we have it
char *id;
} ProfBuffEntry;

typedef struct prof_buff_t *ProfBuff;

ProfBuff buffer_create();
void buffer_free(ProfBuff buffer);
void buffer_append(ProfBuff buffer, const char show_char, int pad_indent, GDateTime *time, int flags, theme_item_t theme_item,
const char *const from, const char *const message, DeliveryReceipt *receipt);
void buffer_append(ProfBuff buffer, const char show_char, int pad_indent, GDateTime *time,
int flags, theme_item_t theme_item, const char *const from, const char *const message, DeliveryReceipt *receipt, const char *const id);
int buffer_size(ProfBuff buffer);
ProfBuffEntry* buffer_get_entry(ProfBuff buffer, int entry);
ProfBuffEntry* buffer_get_entry_by_id(ProfBuff buffer, const char *const id);
Expand Down
30 changes: 14 additions & 16 deletions src/ui/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ win_println_them_message(ProfWin *window, char ch, int flags, const char *const
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, ch, 0, timestamp, flags | NO_ME, THEME_TEXT_THEM, them, fmt_msg->str, NULL);
buffer_append(window->layout->buffer, ch, 0, timestamp, flags | NO_ME, THEME_TEXT_THEM, them, fmt_msg->str, NULL, NULL);

_win_print(window, ch, 0, timestamp, flags | NO_ME, THEME_TEXT_THEM, them, fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1122,7 +1122,7 @@ win_println_me_message(ProfWin *window, char ch, const char *const me, const cha
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, ch, 0, timestamp, 0, THEME_TEXT_ME, me, fmt_msg->str, NULL);
buffer_append(window->layout->buffer, ch, 0, timestamp, 0, THEME_TEXT_ME, me, fmt_msg->str, NULL, NULL);

_win_print(window, ch, 0, timestamp, 0, THEME_TEXT_ME, me, fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1142,7 +1142,7 @@ win_print_outgoing(ProfWin *window, const char ch, const char *const message, ..
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, ch, 0, timestamp, 0, THEME_TEXT_ME, "me", fmt_msg->str, NULL);
buffer_append(window->layout->buffer, ch, 0, timestamp, 0, THEME_TEXT_ME, "me", fmt_msg->str, NULL, NULL);

_win_print(window, ch, 0, timestamp, 0, THEME_TEXT_ME, "me", fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1162,7 +1162,7 @@ win_print_history(ProfWin *window, GDateTime *timestamp, const char *const messa
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, '-', 0, timestamp, 0, THEME_TEXT_HISTORY, "", fmt_msg->str, NULL);
buffer_append(window->layout->buffer, '-', 0, timestamp, 0, THEME_TEXT_HISTORY, "", fmt_msg->str, NULL, NULL);
_win_print(window, '-', 0, timestamp, 0, THEME_TEXT_HISTORY, "", fmt_msg->str, NULL);

inp_nonblocking(TRUE);
Expand All @@ -1182,7 +1182,7 @@ win_print(ProfWin *window, theme_item_t theme_item, const char ch, const char *c
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, ch, 0, timestamp, NO_EOL, theme_item, "", fmt_msg->str, NULL);
buffer_append(window->layout->buffer, ch, 0, timestamp, NO_EOL, theme_item, "", fmt_msg->str, NULL, NULL);

_win_print(window, ch, 0, timestamp, NO_EOL, theme_item, "", fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1202,7 +1202,7 @@ win_println(ProfWin *window, theme_item_t theme_item, const char ch, const char
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, ch, 0, timestamp, 0, theme_item, "", fmt_msg->str, NULL);
buffer_append(window->layout->buffer, ch, 0, timestamp, 0, theme_item, "", fmt_msg->str, NULL, NULL);

_win_print(window, ch, 0, timestamp, 0, theme_item, "", fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1222,7 +1222,7 @@ win_println_indent(ProfWin *window, int pad, const char *const message, ...)
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, '-', pad, timestamp, 0, THEME_DEFAULT, "", fmt_msg->str, NULL);
buffer_append(window->layout->buffer, '-', pad, timestamp, 0, THEME_DEFAULT, "", fmt_msg->str, NULL, NULL);

_win_print(window, '-', pad, timestamp, 0, THEME_DEFAULT, "", fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1242,7 +1242,7 @@ win_append(ProfWin *window, theme_item_t theme_item, const char *const message,
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, '-', 0, timestamp, NO_DATE | NO_EOL, theme_item, "", fmt_msg->str, NULL);
buffer_append(window->layout->buffer, '-', 0, timestamp, NO_DATE | NO_EOL, theme_item, "", fmt_msg->str, NULL, NULL);

_win_print(window, '-', 0, timestamp, NO_DATE | NO_EOL, theme_item, "", fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1262,7 +1262,7 @@ win_appendln(ProfWin *window, theme_item_t theme_item, const char *const message
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, '-', 0, timestamp, NO_DATE, theme_item, "", fmt_msg->str, NULL);
buffer_append(window->layout->buffer, '-', 0, timestamp, NO_DATE, theme_item, "", fmt_msg->str, NULL, NULL);

_win_print(window, '-', 0, timestamp, NO_DATE, theme_item, "", fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1282,7 +1282,7 @@ win_append_highlight(ProfWin *window, theme_item_t theme_item, const char *const
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, '-', 0, timestamp, NO_DATE | NO_ME | NO_EOL, theme_item, "", fmt_msg->str, NULL);
buffer_append(window->layout->buffer, '-', 0, timestamp, NO_DATE | NO_ME | NO_EOL, theme_item, "", fmt_msg->str, NULL, NULL);

_win_print(window, '-', 0, timestamp, NO_DATE | NO_ME | NO_EOL, theme_item, "", fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1302,7 +1302,7 @@ win_appendln_highlight(ProfWin *window, theme_item_t theme_item, const char *con
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, '-', 0, timestamp, NO_DATE | NO_ME, theme_item, "", fmt_msg->str, NULL);
buffer_append(window->layout->buffer, '-', 0, timestamp, NO_DATE | NO_ME, theme_item, "", fmt_msg->str, NULL, NULL);

_win_print(window, '-', 0, timestamp, NO_DATE | NO_ME, theme_item, "", fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand All @@ -1319,16 +1319,14 @@ win_print_http_upload(ProfWin *window, const char *const message, char *url)
}

void
win_print_with_receipt(ProfWin *window, const char show_char, const char *const from, const char *const message,
char *id)
win_print_with_receipt(ProfWin *window, const char show_char, const char *const from, const char *const message, char *id)
{
GDateTime *time = g_date_time_new_now_local();

DeliveryReceipt *receipt = malloc(sizeof(struct delivery_receipt_t));
receipt->id = strdup(id);
receipt->received = FALSE;

buffer_append(window->layout->buffer, show_char, 0, time, 0, THEME_TEXT_ME, from, message, receipt);
buffer_append(window->layout->buffer, show_char, 0, time, 0, THEME_TEXT_ME, from, message, receipt, id);
_win_print(window, show_char, 0, time, 0, THEME_TEXT_ME, from, message, receipt);
// TODO: cross-reference.. this should be replaced by a real event-based system
inp_nonblocking(TRUE);
Expand Down Expand Up @@ -1376,7 +1374,7 @@ _win_printf(ProfWin *window, const char show_char, int pad_indent, GDateTime *ti
GString *fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, message, arg);

buffer_append(window->layout->buffer, show_char, pad_indent, timestamp, flags, theme_item, from, fmt_msg->str, NULL);
buffer_append(window->layout->buffer, show_char, pad_indent, timestamp, flags, theme_item, from, fmt_msg->str, NULL, NULL);

_win_print(window, show_char, pad_indent, timestamp, flags, theme_item, from, fmt_msg->str, NULL);
inp_nonblocking(TRUE);
Expand Down

0 comments on commit bc282ef

Please sign in to comment.