Make nonces unique

Send every message with a unique nonce saving and saving it in a
hashtable making sure we don't echo back our own messages.

Commit is based on a patch contributed by Trecourt Nicolas, thank you!

Fixes: #182
This commit is contained in:
Artem Savkov 2018-11-30 20:32:14 +01:00
parent d020155446
commit dc4e7a3c51
5 changed files with 37 additions and 26 deletions

View File

@ -615,7 +615,7 @@ static gboolean discord_prepare_message(struct im_connection *ic,
time_t tstamp = use_tstamp ? parse_iso_8601(json_o_str(minfo, "timestamp")) : 0;
// Don't echo self messages that we sent in this session
if (is_self && nonce != NULL && g_strcmp0(nonce, dd->nonce) == 0) {
if (is_self && nonce != NULL && g_hash_table_remove(dd->sent_message_ids, nonce)) {
g_free(author);
g_free(msg);
return FALSE;

View File

@ -527,8 +527,15 @@ void discord_http_send_msg(struct im_connection *ic, const char *id,
emsg = nmsg;
}
gchar *nonce;
guchar nonce_bytes[16];
random_bytes(nonce_bytes, sizeof(nonce_bytes));
nonce = g_base64_encode(nonce_bytes, sizeof(nonce_bytes));
g_hash_table_insert(dd->sent_message_ids, nonce,
GUINT_TO_POINTER((guint)time(NULL)));
g_string_printf(content, "{\"content\":\"%s\", \"nonce\":\"%s\"}",
emsg, dd->nonce);
emsg, nonce);
g_free(emsg);
g_string_printf(request, "POST /api/channels/%s/messages HTTP/1.1\r\n"
"Host: %s\r\n"

View File

@ -113,6 +113,7 @@ static void free_pending_ev(gpointer *ev)
void free_discord_data(discord_data *dd)
{
g_hash_table_destroy(dd->sent_message_ids);
g_slist_free_full(dd->pending_events, (GDestroyNotify)free_pending_ev);
g_slist_free_full(dd->pending_reqs, (GDestroyNotify)free_pending_req);
g_slist_free_full(dd->pchannels, (GDestroyNotify)free_channel_info);

View File

@ -133,6 +133,8 @@ static void discord_login(account_t *acc)
struct im_connection *ic = imcb_new(acc);
discord_data *dd = g_new0(discord_data, 1);
dd->sent_message_ids = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, NULL);
dd->keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
ic->proto_data = dd;

View File

@ -54,30 +54,31 @@ typedef struct _gw_data {
} gw_data;
typedef struct _discord_data {
char *token;
char *id;
char *session_id;
char *uname;
char *nonce;
gw_data *gateway;
GSList *servers;
GSList *pchannels;
gint main_loop_id;
GString *ws_buf;
ws_state state;
gint keepalive_interval;
gint keepalive_loop_id;
gint heartbeat_timeout_id;
gint status_timeout_id;
void *ssl;
int sslfd;
gint inpa;
gint wsid;
guint64 seq;
guint pending_sync;
GSList *pending_reqs;
GSList *pending_events;
gboolean reconnecting;
char *token;
char *id;
char *session_id;
char *uname;
char *nonce;
gw_data *gateway;
GSList *servers;
GSList *pchannels;
gint main_loop_id;
GString *ws_buf;
ws_state state;
gint keepalive_interval;
gint keepalive_loop_id;
gint heartbeat_timeout_id;
gint status_timeout_id;
void *ssl;
int sslfd;
gint inpa;
gint wsid;
guint64 seq;
guint pending_sync;
GSList *pending_reqs;
GSList *pending_events;
gboolean reconnecting;
GHashTable *sent_message_ids;
} discord_data;
typedef struct _server_info {