Skip to content

Instantly share code, notes, and snippets.

@sm00th
Created January 2, 2016 11:48
Show Gist options
  • Save sm00th/eab69cebf3400f2491b1 to your computer and use it in GitHub Desktop.
Save sm00th/eab69cebf3400f2491b1 to your computer and use it in GitHub Desktop.
bitlbee-discord patch enabling selfmessages while failing to distinguish between messages sent from other sources and self.
diff --git a/src/discord-handlers.c b/src/discord-handlers.c
index d76ff65..07fa0b0 100644
--- a/src/discord-handlers.c
+++ b/src/discord-handlers.c
@@ -302,12 +302,18 @@ static void discord_handle_server(struct im_connection *ic, json_value *sinfo,
}
static void discord_post_message(channel_info *cinfo, const gchar *author,
- gchar *msg)
+ gchar *msg, gboolean is_self)
{
+ guint32 flags = 0;
+
+ if (is_self) {
+ flags |= 0x00080000;
+ }
+
if (cinfo->type == CHANNEL_PRIVATE) {
- imcb_buddy_msg(cinfo->to.handle.ic, author, msg, 0, 0);
+ imcb_buddy_msg(cinfo->to.handle.ic, author, msg, flags, 0);
} else if (cinfo->type == CHANNEL_TEXT) {
- imcb_chat_msg(cinfo->to.channel.gc, author, msg, 0, 0);
+ imcb_chat_msg(cinfo->to.channel.gc, author, msg, flags, 0);
}
}
@@ -315,6 +321,7 @@ static void discord_prepare_message(struct im_connection *ic,
json_value *minfo,
channel_info *cinfo, gboolean is_edit)
{
+ discord_data *dd = ic->proto_data;
gchar *msg = json_o_strdup(minfo, "content");
if (is_edit == TRUE) {
@@ -324,11 +331,22 @@ static void discord_prepare_message(struct im_connection *ic,
msg = newmsg;
}
- if (cinfo->type == CHANNEL_PRIVATE) {
- if (!g_strcmp0(json_o_str(json_o_get(minfo, "author"), "username"),
- cinfo->to.handle.name)) {
+ const char *aname = json_o_str(json_o_get(minfo, "author"), "username");
+ gboolean is_self = discord_is_self(ic, aname);
- discord_post_message(cinfo, cinfo->to.handle.name, msg);
+ if (is_self) {
+ const gchar *msgid = json_o_str(minfo, "id");
+ GSList *ptr = g_slist_find_custom(dd->sentmsgs, msgid, (GCompareFunc)g_strcmp0);
+ if (ptr != NULL) {
+ dd->sentmsgs = g_slist_remove(dd->sentmsgs, ptr);
+ g_free(ptr->data);
+ return;
+ }
+ }
+
+ if (cinfo->type == CHANNEL_PRIVATE) {
+ if (!g_strcmp0(aname, cinfo->to.handle.name)) {
+ discord_post_message(cinfo, cinfo->to.handle.name, msg, is_self);
}
} else if (cinfo->type == CHANNEL_TEXT) {
json_value *mentions = json_o_get(minfo, "mentions");
@@ -350,8 +368,7 @@ static void discord_prepare_message(struct im_connection *ic,
}
}
- discord_post_message(cinfo, json_o_str(json_o_get(minfo, "author"),
- "username"), msg);
+ discord_post_message(cinfo, aname, msg, is_self);
}
g_free(msg);
}
@@ -399,7 +416,7 @@ void discord_handle_message(struct im_connection *ic, json_value *minfo,
const char *title = json_o_str(embeds->u.array.values[eidx], "title");
if (title != NULL) {
msg = g_strconcat("title: ", title, NULL);
- discord_post_message(cinfo, author, msg);
+ discord_post_message(cinfo, author, msg, FALSE);
g_free(msg);
}
@@ -407,7 +424,7 @@ void discord_handle_message(struct im_connection *ic, json_value *minfo,
"description");
if (description != NULL) {
msg = g_strconcat("description: ", description, NULL);
- discord_post_message(cinfo, author, msg);
+ discord_post_message(cinfo, author, msg, FALSE);
g_free(msg);
}
}
diff --git a/src/discord-http.c b/src/discord-http.c
index bbe0ad6..9b989c6 100644
--- a/src/discord-http.c
+++ b/src/discord-http.c
@@ -127,8 +127,17 @@ static void discord_http_noop_cb(struct http_request *req)
static void discord_http_send_msg_cb(struct http_request *req)
{
struct im_connection *ic = req->data;
+ discord_data *dd = ic->proto_data;
+
if (req->status_code != 200) {
imcb_error(ic, "Failed to send message (%d).", req->status_code);
+ } else {
+ json_value *minfo = json_parse(req->reply_body, req->body_size);
+ if (!minfo || minfo->type != json_object) {
+ return;
+ }
+ gchar *msgid = json_o_strdup(minfo, "id");
+ dd->sentmsgs = g_slist_prepend(dd->sentmsgs, msgid);
}
}
diff --git a/src/discord-util.c b/src/discord-util.c
index 04f646c..7f0c123 100644
--- a/src/discord-util.c
+++ b/src/discord-util.c
@@ -50,6 +50,7 @@ void free_server_info(server_info *sinfo)
void free_discord_data(discord_data *dd)
{
+ g_slist_free_full(dd->sentmsgs, g_free);
g_slist_free_full(dd->pchannels, (GDestroyNotify)free_channel_info);
g_slist_free_full(dd->servers, (GDestroyNotify)free_server_info);
diff --git a/src/discord.c b/src/discord.c
index b668515..53fed07 100644
--- a/src/discord.c
+++ b/src/discord.c
@@ -79,7 +79,7 @@ static int discord_buddy_msg(struct im_connection *ic, char *to, char *msg,
return 1;
}
-static gboolean discord_is_self(struct im_connection *ic, const char *who)
+gboolean discord_is_self(struct im_connection *ic, const char *who)
{
discord_data *dd = ic->proto_data;
return !g_strcmp0(dd->uname, who);
diff --git a/src/discord.h b/src/discord.h
index 5d671a0..15fd107 100644
--- a/src/discord.h
+++ b/src/discord.h
@@ -50,6 +50,7 @@ typedef struct _discord_data {
ws_state state;
gint keepalive_interval;
gint keepalive_loop_id;
+ GSList *sentmsgs;
} discord_data;
typedef struct _server_info {
@@ -83,4 +84,6 @@ typedef struct _user_info {
bee_user_t *user;
} user_info;
+gboolean discord_is_self(struct im_connection *ic, const char *who);
+
#endif //__DISCORD_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment