Compare commits

...

6 Commits

9 changed files with 128 additions and 31 deletions

View File

@ -76,6 +76,8 @@ dc_account_t dc_session_account_fullname(dc_session_t s, char const *f);
*/
void dc_session_add_channel(dc_session_t s, dc_channel_t u);
dc_channel_t dc_session_channel_by_id(dc_session_t s, char const *snowflake);
/**
* Creates a new channel, or returns an existing channel if a channel with
* these recipients already exists.

View File

@ -83,7 +83,7 @@ static void dc_session_handle_ready(dc_session_t s, dc_event_t e)
json_t *user = NULL;
json_t *relationships = NULL;
json_t *presences = NULL;
size_t idx = 0;
size_t idx = 0, i = 0;
json_t *c = NULL;
json_t *channels = NULL;
json_t *guilds = NULL;
@ -143,6 +143,13 @@ static void dc_session_handle_ready(dc_session_t s, dc_event_t e)
dc_guild_t guild = dc_guild_from_json(c);
continue_if_true(guild == NULL);
dc_session_add_guild(s, guild);
/* add their channels to our own thing
*/
for (i = 0; i < dc_guild_channels(guild); i++) {
dc_channel_t chan = dc_guild_nth_channel(guild, i);
dc_session_add_channel(s, chan);
}
}
}
@ -346,6 +353,12 @@ dc_account_t dc_session_account_fullname(dc_session_t s, char const *f)
return NULL;
}
dc_channel_t dc_session_channel_by_id(dc_session_t s, char const *snowflake)
{
return_if_true(s == NULL || snowflake == NULL, NULL);
return (dc_channel_t)g_hash_table_lookup(s->channels, snowflake);
}
void dc_session_add_channel(dc_session_t s, dc_channel_t u)
{
return_if_true(s == NULL || u == NULL,);

View File

@ -16,6 +16,7 @@ void ncdc_treeitem_set_label(ncdc_treeitem_t i, wchar_t const *s);
void *ncdc_treeitem_tag(ncdc_treeitem_t i);
void ncdc_treeitem_set_tag(ncdc_treeitem_t i, void *t);
ncdc_treeitem_t ncdc_treeitem_parent(ncdc_treeitem_t i);
size_t ncdc_treeitem_size(ncdc_treeitem_t i);
void ncdc_treeitem_clear(ncdc_treeitem_t i);
@ -27,6 +28,7 @@ typedef struct ncdc_treeview_ *ncdc_treeview_t;
ncdc_treeview_t ncdc_treeview_new(void);
ncdc_treeitem_t ncdc_treeview_root(ncdc_treeview_t t);
ncdc_treeitem_t ncdc_treeview_current(ncdc_treeview_t t);
void ncdc_treeview_render(ncdc_treeview_t t, WINDOW *w, int lines, int cols);
/* move the cursor around, and collapse/expand items

View File

@ -136,9 +136,11 @@ bool ncdc_dispatch(ncdc_mainwindow_t n, wchar_t const *s)
*/
len = wcslen(s);
cmdlen = wcslen(it->name);
f = wcsdup(s);
return_if_true(f == NULL, false);
memmove(f, f+cmdlen, len-cmdlen);
memmove(f, f+cmdlen, (len-cmdlen) * sizeof(wchar_t));
f[len-cmdlen] = '\0';
item = calloc(1, sizeof(queue_item));

View File

@ -6,11 +6,12 @@ ncdc_cmd_join(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f)
{
char *guild = NULL;
char *channel = NULL;
char *id = NULL;
bool ret = true;
dc_guild_t g = NULL;
dc_channel_t c = NULL;
if (ac <= 2) {
if (ac <= 1) {
LOG(n, L"join: not enough arguments given");
return false;
}
@ -19,19 +20,29 @@ ncdc_cmd_join(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f)
return false;
}
guild = w_convert(av[1]);
channel = w_convert(av[2]);
if (ac == 3) {
guild = w_convert(av[1]);
channel = w_convert(av[2]);
g = dc_session_guild_by_name(current_session, guild);
if (g == NULL) {
LOG(n, L"join: no such guild: %s", guild);
goto cleanup;
}
g = dc_session_guild_by_name(current_session, guild);
if (g == NULL) {
LOG(n, L"join: no such guild: %s", guild);
goto cleanup;
}
c = dc_guild_channel_by_name(g, channel);
if (c == NULL) {
LOG(n, L"join: no such channel %s in guild %s", channel, guild);
goto cleanup;
c = dc_guild_channel_by_name(g, channel);
if (c == NULL) {
LOG(n, L"join: no such channel %s in guild %s", channel, guild);
goto cleanup;
}
} else if (ac == 2) {
id = w_convert(av[1]);
c = dc_session_channel_by_id(current_session, id);
if (c == NULL) {
LOG(n, L"join: no channel found with that snowflake: %s", id);
goto cleanup;
}
}
if (dc_channel_messages(c) == 0) {
@ -42,7 +53,9 @@ ncdc_cmd_join(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f)
c
);
if (!ret) {
LOG(n, L"join: failed to fetch messages for channel %s", channel);
LOG(n, L"join: failed to fetch messages for channel %s",
dc_channel_name(c)
);
goto cleanup;
}
}
@ -57,6 +70,7 @@ cleanup:
free(guild);
free(channel);
free(id);
return ret;
}

View File

@ -40,19 +40,16 @@ ncdc_keybinding_t keys_guilds[] = {
};
ncdc_keybinding_t keys_chat[] = {
/* ALT+KEY_RIGHT
*/
NCDC_BINDING(L"\x1B[1;3C", L"right-window", ncdc_mainwindow_rightview),
NCDC_BINDING(L"\x1B0C", L"right-window", ncdc_mainwindow_rightview),
/* ALT+KEY_LEFT
*/
NCDC_BINDING(L"\x1B[1;3D", L"left-window", ncdc_mainwindow_leftview),
NCDC_BINDING(L"\x1B0D", L"left-window", ncdc_mainwindow_leftview),
NCDC_BINDEND()
};
ncdc_keybinding_t keys_global[] = {
/* ALT+KEY_RIGHT
*/
NCDC_BINDING(L"\x1B[1;3C", L"right-window", ncdc_mainwindow_rightview),
/* ALT+KEY_LEFT
*/
NCDC_BINDING(L"\x1B[1;3D", L"left-window", ncdc_mainwindow_leftview),
/* F1
*/
NCDC_BINDING(L"\x1BOP", L"select-input", ncdc_mainwindow_switch_input),

View File

@ -126,11 +126,9 @@ ncdc_mainwindow_callback(ncdc_input_t i, wchar_t const *s,
if (s[0] == '/') {
ret = ncdc_dispatch(mainwin, s);
} else {
wchar_t *post = calloc(wcslen(s)+7, sizeof(wchar_t));
wcscat(post, L"/post ");
wcscat(post, s);
wchar_t *post = NULL;
aswprintf(&post, L"/post %ls", s);
ret = ncdc_dispatch(mainwin, post);
free(post);
}
@ -249,6 +247,34 @@ static void ncdc_mainwindow_render_status(ncdc_mainwindow_t n)
free(status);
}
static void ncdc_mainwindow_open_guildchat(ncdc_mainwindow_t n)
{
ncdc_treeitem_t cur = ncdc_treeview_current(n->guildview);
dc_channel_t channel = NULL;
wchar_t *cmd = NULL;
return_if_true(cur == NULL ||
/* not the root, thanks
*/
cur == ncdc_treeview_root(n->guildview) ||
/* not one root (again)
*/
ncdc_treeitem_parent(cur) == NULL ||
/* not a guild who are the first level after root
*/
ncdc_treeitem_parent(cur) == ncdc_treeview_root(n->guildview),
);
channel = ncdc_treeitem_tag(cur);
return_if_true(channel == NULL,);
aswprintf(&cmd, L"/join %s", dc_channel_id(channel));
return_if_true(cmd == NULL,);
ncdc_dispatch(n, cmd);
free(cmd);
}
void ncdc_mainwindow_update_guilds(ncdc_mainwindow_t n)
{
GHashTableIter iter;
@ -378,7 +404,7 @@ void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n)
{
if (key != NULL &&
(k = ncdc_find_keybinding(keys_chat, key, keylen)) != NULL) {
k->handler(n->guildview);
k->handler(n->chat);
}
} break;
@ -387,6 +413,8 @@ void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n)
if (key != NULL &&
(k = ncdc_find_keybinding(keys_guilds, key, keylen)) != NULL) {
k->handler(n->guildview);
} else if (i == '\r') {
ncdc_mainwindow_open_guildchat(n);
}
} break;
@ -472,6 +500,7 @@ ncdc_textview_t
ncdc_mainwindow_switch_or_add(ncdc_mainwindow_t n, dc_channel_t c)
{
ncdc_textview_t v = NULL;
wchar_t *name = NULL;
return_if_true(n == NULL || c == NULL, NULL);
return_if_true(!is_logged_in(), NULL);
@ -486,6 +515,32 @@ ncdc_mainwindow_switch_or_add(ncdc_mainwindow_t n, dc_channel_t c)
ncdc_textview_set_account(v, dc_session_me(current_session));
ncdc_textview_set_channel(v, c);
if (dc_channel_type(c) == CHANNEL_TYPE_GUILD_TEXT) {
aswprintf(&name, L"#%s", dc_channel_name(c));
} else if (dc_channel_type(c) == CHANNEL_TYPE_GUILD_VOICE) {
aswprintf(&name, L">%s", dc_channel_name(c));
} else if (dc_channel_is_dm(c)) {
size_t namelen = 0, i = 0;
FILE *f = open_wmemstream(&name, &namelen);
for (i = 0; i < dc_channel_recipients(c); i++) {
dc_account_t rec = dc_channel_nth_recipient(c, i);
if (dc_account_fullname(rec) != NULL) {
fwprintf(f, L"%s", dc_account_fullname(rec));
if (i < dc_channel_recipients(c)-1) {
fputwc('/', f);
}
}
}
fclose(f);
}
if (name != NULL) {
ncdc_textview_set_title(v, name);
free(name);
}
g_ptr_array_add(n->views, v);
ncdc_mainwindow_switch_view(n, v);
}

View File

@ -122,6 +122,12 @@ void ncdc_treeitem_set_tag(ncdc_treeitem_t i, void *t)
i->tag = t;
}
ncdc_treeitem_t ncdc_treeitem_parent(ncdc_treeitem_t i)
{
return_if_true(i == NULL, NULL);
return i->parent;
}
wchar_t const *ncdc_treeitem_label(ncdc_treeitem_t i)
{
return_if_true(i == NULL, NULL);
@ -220,6 +226,12 @@ void ncdc_treeview_render(ncdc_treeview_t t, WINDOW *w, int lines, int cols)
ncdc_treeitem_render(t->root, w, lines, cols, 0, 0);
}
ncdc_treeitem_t ncdc_treeview_current(ncdc_treeview_t t)
{
return_if_true(t == NULL, NULL);
return t->current;
}
ncdc_treeitem_t ncdc_treeview_root(ncdc_treeview_t t)
{
return_if_true(t == NULL, NULL);

View File

@ -20,8 +20,8 @@ wchar_t *util_readkey(int e)
switch (esc[1]) {
/* here it depends on the next character we read
*/
case L'[': len = 3; break;
/* O codes move the cursor, and have one additional bit set
case L'[': len = 4; break;
/* O codes move the cursor, and have one additional byte after
*/
case L'O': len = 1; break;
default: len = 5;