diff --git a/libdc/include/dc/session.h b/libdc/include/dc/session.h index 479ee8c..1978955 100644 --- a/libdc/include/dc/session.h +++ b/libdc/include/dc/session.h @@ -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. diff --git a/libdc/src/session.c b/libdc/src/session.c index ad57418..391b655 100644 --- a/libdc/src/session.c +++ b/libdc/src/session.c @@ -346,6 +346,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,); diff --git a/ncdc/src/join.c b/ncdc/src/join.c index 089080a..001fdf4 100644 --- a/ncdc/src/join.c +++ b/ncdc/src/join.c @@ -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 == 2) { + 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 == 1) { + 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; }