switch over to use sessions
This commit is contained in:
@@ -31,20 +31,13 @@
|
||||
#include <dc/api.h>
|
||||
#include <dc/loop.h>
|
||||
#include <dc/account.h>
|
||||
#include <dc/session.h>
|
||||
|
||||
#define return_if_true(v,r) do { if (v) return r; } while(0)
|
||||
#define goto_if_true(v,l) do { if (v) goto l; } while(0)
|
||||
|
||||
struct ncdc_account_ {
|
||||
dc_account_t account;
|
||||
GPtrArray *friends;
|
||||
GPtrArray *guilds;
|
||||
};
|
||||
|
||||
typedef struct ncdc_account_ *ncdc_account_t;
|
||||
|
||||
extern GHashTable *accounts;
|
||||
extern dc_account_t current_account;
|
||||
extern GPtrArray *sessions;
|
||||
extern dc_session_t current_session;
|
||||
|
||||
extern dc_api_t api;
|
||||
extern dc_loop_t loop;
|
||||
@@ -55,6 +48,8 @@ extern void *mainwindow;
|
||||
|
||||
#define KEY_ESCAPE 27
|
||||
|
||||
bool is_logged_in(void);
|
||||
|
||||
wchar_t *util_readkey(int esc, WINDOW *win);
|
||||
|
||||
void exit_main(void);
|
||||
|
||||
@@ -4,15 +4,9 @@
|
||||
static bool
|
||||
ncdc_cmd_friends_list(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
{
|
||||
bool ret = false;
|
||||
size_t i = 0;
|
||||
char c = ' ';
|
||||
|
||||
ret = dc_api_get_friends(api, current_account);
|
||||
if (!ret) {
|
||||
LOG(n, L"friends: list: failed to fetch your friends");
|
||||
return false;
|
||||
}
|
||||
dc_account_t current_account = dc_session_me(current_session);
|
||||
|
||||
LOG(n, L"/FRIENDS list");
|
||||
for (i = 0; i < dc_account_friends_size(current_account); i++) {
|
||||
@@ -34,6 +28,7 @@ ncdc_cmd_friends_add(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
char *name = NULL;
|
||||
dc_account_t friend = NULL;
|
||||
bool ret = false;
|
||||
dc_account_t current_account = dc_session_me(current_session);
|
||||
|
||||
if (ac <= 1) {
|
||||
return false;
|
||||
@@ -71,6 +66,7 @@ ncdc_cmd_friends_remove(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
dc_account_t friend = NULL;
|
||||
bool ret = false;
|
||||
size_t i = 0;
|
||||
dc_account_t current_account = dc_session_me(current_session);
|
||||
|
||||
if (ac <= 1) {
|
||||
return false;
|
||||
@@ -114,6 +110,7 @@ ncdc_cmd_friends_accept(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
dc_account_t friend = NULL;
|
||||
bool ret = false;
|
||||
size_t i = 0;
|
||||
dc_account_t current_account = dc_session_me(current_session);
|
||||
|
||||
if (ac <= 1) {
|
||||
return false;
|
||||
@@ -164,8 +161,7 @@ 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)) {
|
||||
if (!is_logged_in()) {
|
||||
LOG(n, L"friends: not logged in");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -7,51 +7,54 @@ bool ncdc_cmd_login(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
char *arg = NULL;
|
||||
bool ret = false;
|
||||
dc_account_t acc = NULL;
|
||||
dc_gateway_t gw = NULL;
|
||||
dc_session_t s = NULL;
|
||||
uint32_t idx = 0;
|
||||
|
||||
goto_if_true(ac <= 1, cleanup);
|
||||
|
||||
arg = w_convert(av[1]);
|
||||
goto_if_true(arg == NULL, cleanup);
|
||||
|
||||
acc = g_hash_table_lookup(accounts, arg);
|
||||
acc = ncdc_config_account(config, arg);
|
||||
if (acc == NULL) {
|
||||
acc = ncdc_config_account(config, arg);
|
||||
if (acc == NULL) {
|
||||
LOG(n, L"login: %ls: no such account in configuration", av[1]);
|
||||
LOG(n, L"login: %ls: no such account in configuration", av[1]);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = g_ptr_array_find_with_equal_func(
|
||||
sessions, arg,
|
||||
(GEqualFunc)dc_session_equal_me_fullname,
|
||||
&idx
|
||||
);
|
||||
if (!ret) {
|
||||
s = dc_session_new(loop);
|
||||
if (s == NULL) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
g_hash_table_insert(accounts, strdup(arg), acc);
|
||||
g_ptr_array_add(sessions, s);
|
||||
} else {
|
||||
if (dc_account_has_token(acc)) {
|
||||
s = g_ptr_array_index(sessions, idx);
|
||||
if (dc_session_has_token(s)) {
|
||||
LOG(n, L"login: %ls: this account is already logged in", av[1]);
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
if (!dc_api_login(api, acc)) {
|
||||
if (!dc_session_login(s, acc)) {
|
||||
LOG(n, L"login: %ls: authentication failed; wrong password?", av[1]);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
gw = dc_gateway_new();
|
||||
if (gw == NULL) {
|
||||
LOG(n, L"login: %ls: failed to establish gateway", av[1]);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
dc_gateway_set_login(gw, acc);
|
||||
dc_loop_add_gateway(loop, gw);
|
||||
|
||||
dc_unref(current_account);
|
||||
current_account = dc_ref(acc);
|
||||
dc_unref(current_session);
|
||||
current_session = dc_ref(s);
|
||||
|
||||
LOG(n, L"login: %ls: authentication successful", av[1]);
|
||||
ret = true;
|
||||
|
||||
cleanup:
|
||||
|
||||
dc_unref(acc);
|
||||
free(arg);
|
||||
|
||||
return ret;
|
||||
|
||||
@@ -6,19 +6,19 @@ bool ncdc_cmd_logout(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
{
|
||||
bool ret = false;
|
||||
|
||||
goto_if_true(current_account == NULL ||
|
||||
!dc_account_has_token(current_account), error);
|
||||
goto_if_true(current_session == NULL ||
|
||||
!dc_session_has_token(current_session), error);
|
||||
|
||||
ret = dc_api_logout(api, current_account);
|
||||
ret = dc_session_logout(current_session);
|
||||
if (!ret) {
|
||||
LOG(n, L"logout: failed to log out the current account");
|
||||
goto error;
|
||||
}
|
||||
|
||||
g_hash_table_remove(accounts, dc_account_fullname(current_account));
|
||||
g_ptr_array_remove(sessions, current_session);
|
||||
|
||||
dc_unref(current_account);
|
||||
current_account = NULL;
|
||||
dc_unref(current_session);
|
||||
current_session = NULL;
|
||||
|
||||
LOG(n, L"logout: successfully logged out");
|
||||
|
||||
|
||||
@@ -200,9 +200,10 @@ static void ncdc_mainwindow_render_status(ncdc_mainwindow_t n)
|
||||
wcsftime(timestr, 99, L"[%H:%M]", t);
|
||||
fwprintf(f, L"%ls", timestr);
|
||||
|
||||
if (current_account == NULL) {
|
||||
if (!is_logged_in()) {
|
||||
fwprintf(f, L" [not logged in]");
|
||||
} else {
|
||||
dc_account_t current_account = dc_session_me(current_session);
|
||||
fwprintf(f, L" [%s]", dc_account_fullname(current_account));
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,10 @@ bool ncdc_cmd_msg(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
bool ret = false;
|
||||
dc_channel_t c = NULL;
|
||||
ncdc_textview_t v = NULL;
|
||||
dc_account_t current_account = NULL;
|
||||
size_t i = 0;
|
||||
|
||||
if (current_account == NULL || !dc_account_has_token(current_account)) {
|
||||
if (is_logged_in()) {
|
||||
LOG(n, L"msg: not logged in");
|
||||
return false;
|
||||
}
|
||||
@@ -22,6 +23,8 @@ bool ncdc_cmd_msg(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
target = w_convert(av[1]);
|
||||
goto_if_true(target == NULL, cleanup);
|
||||
|
||||
current_account = dc_session_me(current_session);
|
||||
|
||||
/* find out if the target is a friend we can contact
|
||||
*/
|
||||
dc_account_t f = dc_account_findfriend(current_account, target);
|
||||
|
||||
@@ -21,10 +21,10 @@ bool thread_done = false;
|
||||
static pthread_t event_thread;
|
||||
static struct event_base *base = NULL;
|
||||
|
||||
/* all the accounts we have logged into
|
||||
/* all the sessions we currently have around
|
||||
*/
|
||||
GHashTable *accounts = NULL;
|
||||
dc_account_t current_account = NULL;
|
||||
GPtrArray *sessions = NULL;
|
||||
dc_session_t current_session = NULL;
|
||||
|
||||
char *ncdc_private_dir = NULL;
|
||||
void *config = NULL;
|
||||
@@ -35,26 +35,17 @@ dc_loop_t loop = NULL;
|
||||
*/
|
||||
dc_api_t api = NULL;
|
||||
|
||||
static void cleanup_account(dc_account_t a)
|
||||
{
|
||||
if (dc_account_has_token(a)) {
|
||||
dc_api_logout(api, a);
|
||||
}
|
||||
|
||||
dc_unref(a);
|
||||
}
|
||||
|
||||
static void cleanup(void)
|
||||
{
|
||||
endwin();
|
||||
|
||||
if (accounts != NULL) {
|
||||
g_hash_table_unref(accounts);
|
||||
accounts = NULL;
|
||||
if (sessions != NULL) {
|
||||
g_ptr_array_unref(sessions);
|
||||
sessions = NULL;
|
||||
}
|
||||
|
||||
dc_unref(current_account);
|
||||
current_account = NULL;
|
||||
dc_unref(current_session);
|
||||
current_session = NULL;
|
||||
|
||||
thread_done = true;
|
||||
dc_loop_abort(loop);
|
||||
@@ -138,10 +129,8 @@ static bool init_everything(void)
|
||||
config = ncdc_config_new();
|
||||
return_if_true(config == NULL, false);
|
||||
|
||||
accounts = g_hash_table_new_full(g_str_hash, g_str_equal,
|
||||
g_free, (GDestroyNotify)cleanup_account
|
||||
);
|
||||
return_if_true(accounts == NULL, false);
|
||||
sessions = g_ptr_array_new_with_free_func((GDestroyNotify)dc_unref);
|
||||
return_if_true(sessions == NULL, false);
|
||||
|
||||
ret = pthread_create(&event_thread, NULL, looper, NULL);
|
||||
return_if_true(ret != 0, false);
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
#include <ncdc/ncdc.h>
|
||||
|
||||
bool is_logged_in(void)
|
||||
{
|
||||
return_if_true(current_session == NULL, false);
|
||||
return dc_session_has_token(current_session);
|
||||
}
|
||||
|
||||
wchar_t *util_readkey(int e, WINDOW *win)
|
||||
{
|
||||
wint_t esc[7] = {0};
|
||||
|
||||
Reference in New Issue
Block a user