full friends list support, add remove, and accept

This commit is contained in:
2019-07-04 19:25:38 +02:00
parent efce6a6543
commit 7f5ceff688
11 changed files with 380 additions and 165 deletions

View File

@@ -19,6 +19,11 @@ bool ncdc_dispatch_deinit(void);
bool ncdc_dispatch(ncdc_mainwindow_t n, wchar_t const *s);
/* find a given command in a list of commands, helpful if your command has
* sub commands. for example usage see the friends command
*/
ncdc_commands_t *ncdc_find_cmd(ncdc_commands_t *cmds, wchar_t const *name);
bool ncdc_cmd_friends(ncdc_mainwindow_t n, size_t ac, wchar_t **av);
bool ncdc_cmd_login(ncdc_mainwindow_t n, size_t ac, wchar_t **av);
bool ncdc_cmd_quit(ncdc_mainwindow_t n, size_t ac, wchar_t **av);

View File

@@ -89,26 +89,29 @@ bool ncdc_dispatch_deinit(void)
return true;
}
ncdc_commands_t *ncdc_find_cmd(ncdc_commands_t *cmds, wchar_t const *name)
{
ncdc_commands_t *it = NULL;
for (it = cmds; it->name != NULL; it++) {
if (wcscmp(it->name, name) == 0) {
return it;
}
}
return NULL;
}
bool ncdc_dispatch(ncdc_mainwindow_t n, wchar_t const *s)
{
wchar_t **tokens = NULL;
size_t i = 0, tokenlen = 0;
ncdc_commands_t *it = NULL;
queue_item *item = NULL;
tokens = w_tokenise(s);
return_if_true(tokens == NULL, false);
tokenlen = wcslen(tokens[0]);
for (i = 0; cmds[i].name != NULL; i++) {
if (wcsncmp(cmds[i].name, tokens[0], tokenlen) == 0) {
it = cmds+i;
break;
}
}
if (it == NULL) {
if ((it = ncdc_find_cmd(cmds, tokens[0])) == NULL) {
/* no such command
*/
LOG(n, L"error: no such command \"%ls\"", tokens[0]);

View File

@@ -6,7 +6,7 @@ ncdc_cmd_friends_list(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
{
bool ret = false;
size_t i = 0;
wchar_t c = ' ';
char c = ' ';
ret = dc_api_get_friends(api, current_account);
if (!ret) {
@@ -21,7 +21,7 @@ ncdc_cmd_friends_list(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
case FRIEND_STATE_PENDING: c = 'P'; break;
default: c = ' '; break;
}
LOG(n, L"%lc %s", c, dc_account_full_username(acc));
LOG(n, L" %c %s", c, dc_account_fullname(acc));
}
LOG(n, L"End of /FRIENDS list");
@@ -42,7 +42,7 @@ ncdc_cmd_friends_add(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
name = w_convert(av[1]);
return_if_true(name == NULL, false);
friend = dc_account_from_fullid(name);
friend = dc_account_from_fullname(name);
if (friend == NULL) {
LOG(n, L"friends: add: invalid username given, use the full ID");
goto cleanup;
@@ -53,7 +53,7 @@ ncdc_cmd_friends_add(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
goto cleanup;
}
LOG(n, L"friends: add: request for friendship sent");
LOG(n, L"friends: add: request for friendship sent to %s", name);
ret = true;
cleanup:
@@ -64,9 +64,105 @@ cleanup:
return ret;
}
static bool
ncdc_cmd_friends_remove(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
{
char *name = NULL;
dc_account_t friend = NULL;
bool ret = false;
size_t i = 0;
if (ac <= 1) {
return false;
}
name = w_convert(av[1]);
return_if_true(name == NULL, false);
for (i = 0; i < dc_account_friends_size(current_account); i++) {
dc_account_t cur = dc_account_nthfriend(current_account, i);
if (strcmp(dc_account_fullname(cur), name) == 0) {
friend = cur;
break;
}
}
if (friend == NULL) {
LOG(n, L"friends: remove: no such friend in friend's list");
goto cleanup;
}
if (!dc_api_remove_friend(api, current_account, friend)) {
LOG(n, L"friends: remove: failed to remove friend");
goto cleanup;
}
LOG(n, L"friends: remove: friend %s removed", name);
ret = true;
cleanup:
free(name);
return ret;
}
static bool
ncdc_cmd_friends_accept(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
{
char *name = NULL;
dc_account_t friend = NULL;
bool ret = false;
size_t i = 0;
if (ac <= 1) {
return false;
}
name = w_convert(av[1]);
return_if_true(name == NULL, false);
for (i = 0; i < dc_account_friends_size(current_account); i++) {
dc_account_t cur = dc_account_nthfriend(current_account, i);
if (strcmp(dc_account_fullname(cur), name) == 0 &&
dc_account_friend_state(cur) == FRIEND_STATE_PENDING) {
friend = cur;
break;
}
}
if (friend == NULL) {
LOG(n, L"friends: accept: no such pending friend on the list");
goto cleanup;
}
if (!dc_api_accept_friend(api, current_account, friend)) {
LOG(n, L"friends: accept: failed to accept friend");
goto cleanup;
}
LOG(n, L"friends: accept: friend %s accepted", name);
ret = true;
cleanup:
free(name);
return ret;
}
static ncdc_commands_t subcmds[] = {
{ L"accept", ncdc_cmd_friends_accept },
{ L"add", ncdc_cmd_friends_add },
{ L"list", ncdc_cmd_friends_list },
{ L"remove", ncdc_cmd_friends_remove },
{ NULL, NULL }
};
bool ncdc_cmd_friends(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
{
wchar_t *subcmd = NULL;
ncdc_commands_t *it = NULL;
if (current_account == NULL ||
!dc_account_has_token(current_account)) {
@@ -83,13 +179,10 @@ bool ncdc_cmd_friends(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
--ac;
++av;
if (wcscmp(subcmd, L"list") == 0) {
return ncdc_cmd_friends_list(n, ac, av);
} else if (wcscmp(subcmd, L"add") == 0) {
return ncdc_cmd_friends_add(n, ac, av);
} else {
if ((it = ncdc_find_cmd(subcmds, subcmd)) == NULL) {
LOG(n, L"friends: no such subcommand \"%ls\"", subcmd);
return false;
}
return true;
return it->handler(n, ac, av);
}