Skip to content

Commit

Permalink
Merge pull request sm00th#116 from arcnmx/group-dm
Browse files Browse the repository at this point in the history
Group DMs
  • Loading branch information
sm00th committed Dec 9, 2017
2 parents da7ac86 + a11c4f4 commit 0e04b56
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 33 deletions.
73 changes: 64 additions & 9 deletions src/discord-handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,53 @@ void discord_handle_channel(struct im_connection *ic, json_value *cinfo,
}
case CHANNEL_GROUP_PRIVATE:
{
// TODO: Implement group pms
gchar *fullname = g_strdup(id);

bee_chat_info_t *bci = g_new0(bee_chat_info_t, 1);
while (get_channel(dd, fullname, NULL, SEARCH_FNAME) != NULL) {
gchar *tmpname = fullname;
fullname = g_strconcat(tmpname, "_", NULL);
g_free(tmpname);
}
bci->title = g_strdup(fullname);
if (topic != NULL && strlen(topic) > 0) {
bci->topic = g_strdup(topic);
} else {
bci->topic = g_strdup_printf("Group DM: %s", name);
}

ic->chatlist = g_slist_prepend(ic->chatlist, bci);

g_free(fullname);

channel_info *ci = g_new0(channel_info, 1);
ci->type = ctype;
ci->to.group.name = g_strdup(name);
ci->to.group.bci = bci;
ci->to.group.ic = ic;
ci->id = g_strdup(id);
if (lmid != NULL) {
ci->last_msg = g_ascii_strtoull(lmid, NULL, 10);
}

json_value *rcplist = json_o_get(cinfo, "recipients");
if (rcplist != NULL && rcplist->type == json_array) {
for (int ridx = 0; ridx < rcplist->u.array.length; ridx++) {
json_value *rcp = rcplist->u.array.values[ridx];

discord_handle_user(ic, rcp, GLOBAL_SERVER_ID, ACTION_CREATE);

user_info *ui = get_user(dd, json_o_str(rcp, "id"), GLOBAL_SERVER_ID, SEARCH_ID);

ci->to.group.users = g_slist_prepend(ci->to.group.users, ui);
}

dd->pchannels = g_slist_prepend(dd->pchannels, ci);
} else {
g_print("Failed to get recepients for private channel.\n");
free_channel_info(ci);
}

break;
}
case CHANNEL_VOICE:
Expand All @@ -350,14 +396,16 @@ void discord_handle_channel(struct im_connection *ic, json_value *cinfo,

if (action == ACTION_DELETE) {
GSList **clist;
if (cdata->type == CHANNEL_PRIVATE) {
if (cdata->type == CHANNEL_PRIVATE || cdata->type == CHANNEL_GROUP_PRIVATE) {
clist = &dd->pchannels;
} else {
clist = &sinfo->channels;
}

if (cdata->type == CHANNEL_TEXT) {
ic->chatlist = g_slist_remove(ic->chatlist, cdata->to.channel.bci);
} else if (cdata->type == CHANNEL_GROUP_PRIVATE) {
ic->chatlist = g_slist_remove(ic->chatlist, cdata->to.group.bci);
}

*clist = g_slist_remove(*clist, cdata);
Expand All @@ -374,12 +422,12 @@ void discord_handle_channel(struct im_connection *ic, json_value *cinfo,

static void discord_add_global_server(struct im_connection *ic) {
discord_data *dd = ic->proto_data;
server_info *sinfo = g_new0(server_info, 1);
server_info *sinfo = g_new0(server_info, 1);

sinfo->name = g_strdup("_global");
sinfo->id = g_strdup(GLOBAL_SERVER_ID);
sinfo->ic = ic;
dd->servers = g_slist_prepend(dd->servers, sinfo);
sinfo->name = g_strdup("_global");
sinfo->id = g_strdup(GLOBAL_SERVER_ID);
sinfo->ic = ic;
dd->servers = g_slist_prepend(dd->servers, sinfo);
}

static void discord_handle_server(struct im_connection *ic, json_value *sinfo,
Expand Down Expand Up @@ -469,6 +517,9 @@ static gboolean discord_post_message(channel_info *cinfo, const gchar *author,
if (cinfo->type == CHANNEL_PRIVATE) {
imcb_buddy_msg(cinfo->to.handle.ic, author, msg, flags, 0);
return TRUE;
} else if (cinfo->type == CHANNEL_GROUP_PRIVATE && cinfo->to.group.gc != NULL) {
imcb_chat_msg(cinfo->to.group.gc, author, msg, flags, 0);
return TRUE;
} else if (cinfo->type == CHANNEL_TEXT && cinfo->to.channel.gc != NULL) {
imcb_chat_msg(cinfo->to.channel.gc, author, msg, flags, 0);
return TRUE;
Expand All @@ -489,6 +540,10 @@ static gboolean discord_replace_channel(const GMatchInfo *match,
gchar *r = g_strdup_printf("#%s", cinfo->to.channel.name);
result = g_string_append(result, r);
g_free(r);
} else if (cinfo != NULL && cinfo->type == CHANNEL_GROUP_PRIVATE) {
gchar *r = g_strdup_printf("#%s", cinfo->to.group.name);
result = g_string_append(result, r);
g_free(r);
} else {
result = g_string_append(result, mstring);
}
Expand Down Expand Up @@ -562,7 +617,7 @@ static gboolean discord_prepare_message(struct im_connection *ic,

if (cinfo->type == CHANNEL_PRIVATE) {
posted = discord_post_message(cinfo, cinfo->to.handle.name, msg, is_self);
} else if (cinfo->type == CHANNEL_TEXT) {
} else if (cinfo->type == CHANNEL_TEXT || cinfo->type == CHANNEL_GROUP_PRIVATE) {
json_value *mentions = json_o_get(minfo, "mentions");
if (mentions != NULL && mentions->type == json_array) {
for (int midx = 0; midx < mentions->u.array.length; midx++) {
Expand Down Expand Up @@ -661,7 +716,7 @@ void discord_handle_message(struct im_connection *ic, json_value *minfo,

if (cinfo->type == CHANNEL_PRIVATE) {
author = cinfo->to.handle.name;
} else if (cinfo->type == CHANNEL_TEXT) {
} else if (cinfo->type == CHANNEL_TEXT || cinfo->type == CHANNEL_GROUP_PRIVATE) {
author = set_getstr(&ic->acc->set, "urlinfo_handle");
}

Expand Down
39 changes: 29 additions & 10 deletions src/discord-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,29 @@ void free_channel_info(channel_info *cinfo)
cinfo->id = NULL;

g_slist_free_full(cinfo->pinned, (GDestroyNotify)g_free);
if (cinfo->type != CHANNEL_TEXT) {
g_free(cinfo->to.handle.name);
} else {
if (cinfo->to.channel.gc != NULL) {
imcb_chat_free(cinfo->to.channel.gc);
}
g_free(cinfo->to.channel.name);
g_free(cinfo->to.channel.bci->title);
g_free(cinfo->to.channel.bci->topic);
g_free(cinfo->to.channel.bci);
switch (cinfo->type) {
case CHANNEL_TEXT:
if (cinfo->to.channel.gc != NULL) {
imcb_chat_free(cinfo->to.channel.gc);
}
g_free(cinfo->to.channel.name);
g_free(cinfo->to.channel.bci->title);
g_free(cinfo->to.channel.bci->topic);
g_free(cinfo->to.channel.bci);
break;
case CHANNEL_GROUP_PRIVATE:
if (cinfo->to.group.gc != NULL) {
imcb_chat_free(cinfo->to.group.gc);
}
g_free(cinfo->to.group.name);
g_free(cinfo->to.group.bci->title);
g_free(cinfo->to.group.bci->topic);
g_free(cinfo->to.group.bci);
g_slist_free(cinfo->to.group.users);
break;
default:
g_free(cinfo->to.handle.name);
break;
}

g_free(cinfo);
Expand Down Expand Up @@ -118,6 +131,8 @@ static gint cmp_chan_name(const channel_info *cinfo, const char *cname)
gchar *ciname = NULL;
if (cinfo->type == CHANNEL_TEXT) {
ciname = cinfo->to.channel.name;
} else if (cinfo->type == CHANNEL_GROUP_PRIVATE) {
ciname = cinfo->to.group.name;
} else {
ciname = cinfo->to.handle.name;
}
Expand All @@ -130,6 +145,8 @@ static gint cmp_chan_fname(const channel_info *cinfo, const char *cname)
gchar *ciname = NULL;
if (cinfo->type == CHANNEL_TEXT) {
ciname = cinfo->to.channel.bci->title;
} else if (cinfo->type == CHANNEL_GROUP_PRIVATE) {
ciname = cinfo->to.group.bci->title;
}

return g_strcmp0(ciname, cname);
Expand All @@ -141,6 +158,8 @@ static gint cmp_chan_name_ignorecase(const channel_info *cinfo,
gchar *cfn1 = NULL;
if (cinfo->type == CHANNEL_TEXT) {
cfn1 = g_utf8_casefold(cinfo->to.channel.name, -1);
} else if (cinfo->type == CHANNEL_GROUP_PRIVATE) {
cfn1 = g_utf8_casefold(cinfo->to.group.name, -1);
} else {
cfn1 = g_utf8_casefold(cinfo->to.handle.name, -1);
}
Expand Down
38 changes: 24 additions & 14 deletions src/discord.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,35 @@ static struct groupchat *discord_chat_join(struct im_connection *ic,
server_info *sinfo = NULL;
channel_info *cinfo = get_channel(dd, room, NULL, SEARCH_FNAME);

if (cinfo == NULL || cinfo->type != CHANNEL_TEXT) {
return NULL;
}
if (cinfo != NULL && cinfo->type == CHANNEL_TEXT) {
sinfo = cinfo->to.channel.sinfo;
gc = imcb_chat_new(ic, cinfo->to.channel.name);
if (cinfo->to.channel.bci->topic != NULL) {
imcb_chat_topic(gc, "root", cinfo->to.channel.bci->topic, 0);
}

sinfo = cinfo->to.channel.sinfo;
gc = imcb_chat_new(ic, cinfo->to.channel.name);
if (cinfo->to.channel.bci->topic != NULL) {
imcb_chat_topic(gc, "root", cinfo->to.channel.bci->topic, 0);
}
for (GSList *ul = sinfo->users; ul; ul = g_slist_next(ul)) {
user_info *uinfo = ul->data;
if (uinfo->flags & BEE_USER_ONLINE) {
imcb_chat_add_buddy(gc, uinfo->user->handle);
}
}
imcb_chat_add_buddy(gc, dd->uname);

for (GSList *ul = sinfo->users; ul; ul = g_slist_next(ul)) {
user_info *uinfo = ul->data;
if (uinfo->flags & BEE_USER_ONLINE) {
cinfo->to.channel.gc = gc;
} else if (cinfo != NULL && cinfo->type == CHANNEL_GROUP_PRIVATE) {
gc = imcb_chat_new(ic, cinfo->to.group.name);

for (GSList *ul = cinfo->to.group.users; ul; ul = g_slist_next(ul)) {
user_info *uinfo = ul->data;
imcb_chat_add_buddy(gc, uinfo->user->handle);
}
}
imcb_chat_add_buddy(gc, dd->uname);
imcb_chat_add_buddy(gc, dd->uname);

cinfo->to.channel.gc = gc;
cinfo->to.group.gc = gc;
} else {
return NULL;
}
gc->data = cinfo;

if (set_getbool(&ic->acc->set, "fetch_pinned")) {
Expand Down
7 changes: 7 additions & 0 deletions src/discord.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ typedef struct _channel_info {
char *name;
struct im_connection *ic;
} handle;
struct {
struct groupchat *gc;
char *name;
bee_chat_info_t *bci;
GSList *users;
struct im_connection *ic;
} group;
} to;
channel_type type;
GSList *pinned;
Expand Down

0 comments on commit 0e04b56

Please sign in to comment.