2015-12-05 17:28:53 +00:00
|
|
|
/*
|
2016-01-18 14:50:21 +00:00
|
|
|
* Copyright 2015-2016 Artem Savkov <artem.savkov@gmail.com>
|
2015-12-05 17:28:53 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2017-12-09 16:42:46 +00:00
|
|
|
#include "config.h"
|
2015-12-05 17:28:53 +00:00
|
|
|
#include "discord.h"
|
|
|
|
#include "discord-http.h"
|
|
|
|
#include "discord-util.h"
|
|
|
|
#include "discord-websockets.h"
|
2017-08-25 07:55:54 +00:00
|
|
|
#include "help.h"
|
|
|
|
|
|
|
|
#define HELPFILE_NAME "discord-help.txt"
|
|
|
|
|
|
|
|
static void discord_help_init()
|
|
|
|
{
|
|
|
|
/* Figure out where our help file is by looking at the global helpfile. */
|
|
|
|
gchar *dir = g_path_get_dirname (global.helpfile);
|
|
|
|
if (strcmp(dir, ".") == 0) {
|
|
|
|
log_message(LOGLVL_WARNING, "Error finding the directory of helpfile %s.", global.helpfile);
|
|
|
|
g_free(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gchar *df = g_strjoin("/", dir, HELPFILE_NAME, NULL);
|
|
|
|
g_free(dir);
|
|
|
|
|
|
|
|
/* Load help from our own help file. */
|
|
|
|
help_t *dh;
|
|
|
|
help_init(&dh, df);
|
|
|
|
if(dh == NULL) {
|
2017-08-31 19:33:37 +00:00
|
|
|
log_message(LOGLVL_WARNING, "Error opening helpfile: %s.", df);
|
|
|
|
g_free(df);
|
2017-08-25 07:55:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-31 19:33:37 +00:00
|
|
|
g_free(df);
|
2017-08-25 07:55:54 +00:00
|
|
|
|
|
|
|
/* Link the last entry of global.help with first entry of our help. */
|
|
|
|
help_t *h, *l = NULL;
|
|
|
|
for (h = global.help; h; h = h->next) {
|
|
|
|
l = h;
|
|
|
|
}
|
|
|
|
if (l) {
|
|
|
|
l->next = dh;
|
|
|
|
} else {
|
|
|
|
/* No global help but ours? */
|
|
|
|
global.help = dh;
|
|
|
|
}
|
|
|
|
}
|
2015-12-05 17:28:53 +00:00
|
|
|
|
2016-05-26 07:34:49 +00:00
|
|
|
#ifdef BITLBEE_ABI_VERSION_CODE
|
|
|
|
struct plugin_info *init_plugin_info(void)
|
|
|
|
{
|
|
|
|
static struct plugin_info info = {
|
|
|
|
BITLBEE_ABI_VERSION_CODE,
|
2016-05-28 07:00:32 +00:00
|
|
|
"bitlbee-discord",
|
2017-12-09 16:42:46 +00:00
|
|
|
PACKAGE_VERSION,
|
2016-05-26 07:34:49 +00:00
|
|
|
"Bitlbee plugin for discordapp.com",
|
|
|
|
"Artem Savkov <artem.savkov@gmail.com>",
|
|
|
|
"https://github.com/sm00th/bitlbee-discord"
|
|
|
|
};
|
|
|
|
|
|
|
|
return &info;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-12-05 17:28:53 +00:00
|
|
|
static void discord_init(account_t *acc)
|
|
|
|
{
|
|
|
|
set_t *s;
|
|
|
|
|
|
|
|
s = set_add(&acc->set, "host", DISCORD_HOST, NULL, acc);
|
|
|
|
s->flags |= ACC_SET_OFFLINE_ONLY;
|
2015-12-12 11:41:32 +00:00
|
|
|
|
|
|
|
s = set_add(&acc->set, "voice_status_notify", "off", set_eval_bool, acc);
|
2016-01-01 21:48:15 +00:00
|
|
|
s = set_add(&acc->set, "send_acks", "on", set_eval_bool, acc);
|
2015-12-27 16:43:04 +00:00
|
|
|
s = set_add(&acc->set, "edit_prefix", "EDIT: ", NULL, acc);
|
2015-12-27 17:41:48 +00:00
|
|
|
s = set_add(&acc->set, "urlinfo_handle", "urlinfo", NULL, acc);
|
2016-01-11 22:50:49 +00:00
|
|
|
s = set_add(&acc->set, "mention_suffix", ":", NULL, acc);
|
2016-01-15 15:26:18 +00:00
|
|
|
s = set_add(&acc->set, "mention_ignorecase", "off", set_eval_bool, acc);
|
2016-01-18 14:50:21 +00:00
|
|
|
s = set_add(&acc->set, "incoming_me_translation", "on", set_eval_bool, acc);
|
2017-02-14 14:07:06 +00:00
|
|
|
s = set_add(&acc->set, "fetch_pinned", "off", set_eval_bool, acc);
|
2018-06-03 20:47:33 +00:00
|
|
|
s = set_add(&acc->set, "always_afk", "off", set_eval_bool, acc);
|
2018-07-25 14:34:09 +00:00
|
|
|
s = set_add(&acc->set, "emoji_urls", "on", set_eval_bool, acc);
|
2018-01-27 17:09:33 +00:00
|
|
|
|
2018-01-26 18:57:49 +00:00
|
|
|
s = set_add(&acc->set, "auto_join", "off", set_eval_bool, acc);
|
2018-01-27 17:09:33 +00:00
|
|
|
s->flags |= ACC_SET_OFFLINE_ONLY;
|
|
|
|
|
2018-01-26 18:57:49 +00:00
|
|
|
s = set_add(&acc->set, "auto_join_exclude", "", NULL, acc);
|
2018-01-27 17:09:33 +00:00
|
|
|
s->flags |= ACC_SET_OFFLINE_ONLY;
|
2015-12-28 10:52:21 +00:00
|
|
|
|
|
|
|
s = set_add(&acc->set, "max_backlog", "50", set_eval_int, acc);
|
|
|
|
s->flags |= ACC_SET_OFFLINE_ONLY;
|
2016-04-10 20:50:36 +00:00
|
|
|
|
2016-05-05 12:39:29 +00:00
|
|
|
s = set_add(&acc->set, "never_offline", "off", set_eval_bool, acc);
|
|
|
|
s->flags |= ACC_SET_OFFLINE_ONLY;
|
|
|
|
|
2017-02-07 09:45:59 +00:00
|
|
|
s = set_add(&acc->set, "server_prefix_len", "3", set_eval_int, acc);
|
2016-05-26 15:36:57 +00:00
|
|
|
s->flags |= ACC_SET_OFFLINE_ONLY;
|
|
|
|
|
2016-08-08 07:42:59 +00:00
|
|
|
s = set_add(&acc->set, "token_cache", NULL, NULL, acc);
|
|
|
|
s->flags |= SET_HIDDEN | SET_NULL_OK;
|
|
|
|
|
2017-09-24 17:44:20 +00:00
|
|
|
s = set_add(&acc->set, "friendship_mode", "on", set_eval_bool, acc);
|
|
|
|
s->flags |= ACC_SET_OFFLINE_ONLY;
|
|
|
|
|
2020-06-21 07:52:08 +00:00
|
|
|
s = set_add(&acc->set, "verbose", "off", set_eval_bool, acc);
|
|
|
|
|
2016-04-10 20:50:36 +00:00
|
|
|
acc->flags |= ACC_FLAG_AWAY_MESSAGE;
|
|
|
|
acc->flags |= ACC_FLAG_STATUS_MESSAGE;
|
2017-08-25 07:55:54 +00:00
|
|
|
|
|
|
|
discord_help_init();
|
2015-12-05 17:28:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 20:10:24 +00:00
|
|
|
static void discord_do_login(struct im_connection *ic)
|
|
|
|
{
|
|
|
|
if (set_getstr(&ic->acc->set,"token_cache")) {
|
|
|
|
discord_http_get_gateway(ic, set_getstr(&ic->acc->set,"token_cache"));
|
|
|
|
} else {
|
|
|
|
discord_http_login(ic->acc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 17:28:53 +00:00
|
|
|
static void discord_login(account_t *acc)
|
|
|
|
{
|
|
|
|
struct im_connection *ic = imcb_new(acc);
|
|
|
|
|
|
|
|
discord_data *dd = g_new0(discord_data, 1);
|
2018-11-30 19:32:14 +00:00
|
|
|
dd->sent_message_ids = g_hash_table_new_full(g_str_hash, g_str_equal,
|
|
|
|
g_free, NULL);
|
2015-12-05 17:28:53 +00:00
|
|
|
dd->keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
|
|
|
|
ic->proto_data = dd;
|
|
|
|
|
2018-02-14 20:10:24 +00:00
|
|
|
discord_do_login(ic);
|
2015-12-05 17:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void discord_logout(struct im_connection *ic)
|
|
|
|
{
|
|
|
|
discord_data *dd = ic->proto_data;
|
|
|
|
|
|
|
|
discord_ws_cleanup(dd);
|
|
|
|
|
|
|
|
free_discord_data(dd);
|
2016-07-28 08:06:33 +00:00
|
|
|
g_slist_free(ic->chatlist);
|
2015-12-05 17:28:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 07:51:23 +00:00
|
|
|
void discord_soft_reconnect(struct im_connection *ic)
|
2018-02-14 20:10:24 +00:00
|
|
|
{
|
|
|
|
discord_data *dd = ic->proto_data;
|
|
|
|
|
2020-06-21 07:52:08 +00:00
|
|
|
if (set_getbool(&ic->acc->set, "verbose")) {
|
|
|
|
imcb_log(ic, "Performing soft-reconnect");
|
|
|
|
}
|
2018-02-14 20:10:24 +00:00
|
|
|
discord_ws_cleanup(dd);
|
|
|
|
dd->reconnecting = TRUE;
|
|
|
|
discord_do_login(ic);
|
|
|
|
}
|
|
|
|
|
2015-12-05 17:28:53 +00:00
|
|
|
static void discord_chat_msg(struct groupchat *gc, char *msg, int flags)
|
|
|
|
{
|
|
|
|
channel_info *cinfo = gc->data;
|
|
|
|
|
2015-12-08 22:07:47 +00:00
|
|
|
discord_http_send_msg(gc->ic, cinfo->id, msg);
|
2015-12-05 17:28:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 08:06:33 +00:00
|
|
|
static void discord_chat_list(struct im_connection *ic, const char *server)
|
|
|
|
{
|
2016-10-16 08:18:54 +00:00
|
|
|
imcb_chat_list_finish(ic);
|
2016-07-28 08:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct groupchat *discord_chat_join(struct im_connection *ic,
|
|
|
|
const char *room,
|
|
|
|
const char *nick,
|
|
|
|
const char *password,
|
|
|
|
set_t **sets)
|
2018-01-26 18:57:49 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
return discord_chat_do_join(ic, room, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct groupchat *discord_chat_do_join(struct im_connection *ic,
|
|
|
|
const char *room,
|
|
|
|
gboolean is_auto_join)
|
2016-07-28 08:06:33 +00:00
|
|
|
{
|
|
|
|
discord_data *dd = ic->proto_data;
|
|
|
|
struct groupchat *gc = NULL;
|
|
|
|
server_info *sinfo = NULL;
|
|
|
|
channel_info *cinfo = get_channel(dd, room, NULL, SEARCH_FNAME);
|
|
|
|
|
2017-11-27 16:16:32 +00:00
|
|
|
if (cinfo != NULL && cinfo->type == CHANNEL_TEXT) {
|
|
|
|
sinfo = cinfo->to.channel.sinfo;
|
|
|
|
gc = imcb_chat_new(ic, cinfo->to.channel.name);
|
2020-02-06 12:32:26 +00:00
|
|
|
discord_ws_sync_channel(dd, sinfo->id, cinfo->id, 0);
|
2018-01-26 18:57:49 +00:00
|
|
|
|
|
|
|
if (is_auto_join) {
|
|
|
|
imcb_chat_name_hint(gc, room);
|
|
|
|
}
|
|
|
|
|
2017-11-27 16:16:32 +00:00
|
|
|
if (cinfo->to.channel.bci->topic != NULL) {
|
|
|
|
imcb_chat_topic(gc, "root", cinfo->to.channel.bci->topic, 0);
|
|
|
|
}
|
2016-07-28 08:06:33 +00:00
|
|
|
|
2017-11-27 16:16:32 +00:00
|
|
|
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);
|
2016-07-28 08:06:33 +00:00
|
|
|
|
2017-11-27 16:16:32 +00:00
|
|
|
cinfo->to.channel.gc = gc;
|
|
|
|
} else if (cinfo != NULL && cinfo->type == CHANNEL_GROUP_PRIVATE) {
|
|
|
|
gc = imcb_chat_new(ic, cinfo->to.group.name);
|
|
|
|
|
2020-02-07 20:59:25 +00:00
|
|
|
discord_ws_sync_private_group(dd, cinfo->id);
|
2018-01-26 18:57:49 +00:00
|
|
|
if (is_auto_join) {
|
|
|
|
imcb_chat_name_hint(gc, room);
|
|
|
|
}
|
|
|
|
|
2017-11-27 16:16:32 +00:00
|
|
|
for (GSList *ul = cinfo->to.group.users; ul; ul = g_slist_next(ul)) {
|
|
|
|
user_info *uinfo = ul->data;
|
2016-07-28 08:06:33 +00:00
|
|
|
imcb_chat_add_buddy(gc, uinfo->user->handle);
|
|
|
|
}
|
2017-11-27 16:16:32 +00:00
|
|
|
imcb_chat_add_buddy(gc, dd->uname);
|
2016-07-28 08:06:33 +00:00
|
|
|
|
2017-11-27 16:16:32 +00:00
|
|
|
cinfo->to.group.gc = gc;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-07-28 08:06:33 +00:00
|
|
|
gc->data = cinfo;
|
|
|
|
|
2017-02-14 14:07:06 +00:00
|
|
|
if (set_getbool(&ic->acc->set, "fetch_pinned")) {
|
|
|
|
discord_http_get_pinned(ic, cinfo->id);
|
|
|
|
}
|
|
|
|
|
2017-01-19 08:59:39 +00:00
|
|
|
if (set_getint(&ic->acc->set, "max_backlog") > 0 &&
|
|
|
|
cinfo->last_msg > cinfo->last_read) {
|
2016-10-05 12:01:16 +00:00
|
|
|
discord_http_get_backlog(ic, cinfo->id);
|
|
|
|
}
|
|
|
|
|
2016-07-28 08:06:33 +00:00
|
|
|
return gc;
|
|
|
|
}
|
|
|
|
|
2018-04-27 06:11:58 +00:00
|
|
|
static void discord_chat_leave(struct groupchat *gc)
|
|
|
|
{
|
|
|
|
channel_info *cinfo = gc->data;
|
|
|
|
imcb_chat_free(cinfo->to.channel.gc);
|
|
|
|
cinfo->to.channel.gc = NULL;
|
|
|
|
}
|
|
|
|
|
2015-12-05 17:28:53 +00:00
|
|
|
static int discord_buddy_msg(struct im_connection *ic, char *to, char *msg,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
discord_data *dd = ic->proto_data;
|
|
|
|
|
2016-07-28 08:40:58 +00:00
|
|
|
if (g_strcmp0(to, DISCORD_MFA_HANDLE) == 0) {
|
|
|
|
discord_http_mfa_auth(ic, msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-05 17:28:53 +00:00
|
|
|
for (GSList *cl = dd->pchannels; cl; cl = g_slist_next(cl)) {
|
|
|
|
channel_info *cinfo = cl->data;
|
2015-12-08 22:07:47 +00:00
|
|
|
if (cinfo->type == CHANNEL_PRIVATE &&
|
|
|
|
g_strcmp0(cinfo->to.handle.name, to) == 0) {
|
2015-12-05 17:28:53 +00:00
|
|
|
discord_http_send_msg(ic, cinfo->id, msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 21:13:41 +00:00
|
|
|
// If we are here we didn't find an appropriate channel, create it
|
|
|
|
discord_http_create_and_send_msg(ic, to, msg);
|
|
|
|
|
|
|
|
return 0;
|
2015-12-05 17:28:53 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 20:05:08 +00:00
|
|
|
gboolean discord_is_self(struct im_connection *ic, const char *who)
|
2015-12-05 17:28:53 +00:00
|
|
|
{
|
|
|
|
discord_data *dd = ic->proto_data;
|
|
|
|
return !g_strcmp0(dd->uname, who);
|
|
|
|
}
|
|
|
|
|
2016-04-10 20:50:36 +00:00
|
|
|
static GList *discord_away_states(struct im_connection *ic)
|
|
|
|
{
|
|
|
|
static GList *m = NULL;
|
|
|
|
|
2018-06-03 20:19:46 +00:00
|
|
|
m = g_list_prepend(m, "invisible");
|
|
|
|
m = g_list_prepend(m, "dnd");
|
|
|
|
m = g_list_prepend(m, "online");
|
2018-06-24 08:55:17 +00:00
|
|
|
m = g_list_prepend(m, "idle");
|
2016-04-10 20:50:36 +00:00
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void discord_set_away(struct im_connection *ic, char *state,
|
|
|
|
char *message)
|
|
|
|
{
|
2018-06-03 20:47:33 +00:00
|
|
|
discord_ws_set_status(ic, state, message);
|
2016-04-10 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 17:28:53 +00:00
|
|
|
G_MODULE_EXPORT void init_plugin(void)
|
|
|
|
{
|
|
|
|
struct prpl *dpp;
|
|
|
|
|
|
|
|
static const struct prpl pp = {
|
|
|
|
.name = "discord",
|
|
|
|
.init = discord_init,
|
|
|
|
.login = discord_login,
|
|
|
|
.logout = discord_logout,
|
|
|
|
.chat_msg = discord_chat_msg,
|
2016-07-28 08:06:33 +00:00
|
|
|
.chat_list = discord_chat_list,
|
|
|
|
.chat_join = discord_chat_join,
|
2018-04-27 06:11:58 +00:00
|
|
|
.chat_leave = discord_chat_leave,
|
2015-12-05 17:28:53 +00:00
|
|
|
.buddy_msg = discord_buddy_msg,
|
|
|
|
.handle_cmp = g_strcmp0,
|
2016-04-10 20:50:36 +00:00
|
|
|
.handle_is_self = discord_is_self,
|
|
|
|
.away_states = discord_away_states,
|
|
|
|
.set_away = discord_set_away
|
2015-12-05 17:28:53 +00:00
|
|
|
};
|
|
|
|
dpp = g_memdup(&pp, sizeof pp);
|
|
|
|
register_protocol(dpp);
|
|
|
|
}
|