1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Refactored connecting, and reconnecting

The user either logs in with an account, in which case its name, and the
password is kept for reconnects. Or the user can login with a jid (and
server) which does not currently exist as a local account.  On success
an account is created, with the jid as the name.
This commit is contained in:
James Booth 2013-01-27 20:23:42 +00:00
parent b936154299
commit 972ca34d4b
5 changed files with 119 additions and 80 deletions

View File

@ -270,10 +270,16 @@ accounts_account_exists(const char * const account_name)
void void
accounts_set_jid(const char * const account_name, const char * const value) accounts_set_jid(const char * const account_name, const char * const value)
{ {
Jid *jid = jid_create(value);
if (jid != NULL) {
if (accounts_account_exists(account_name)) { if (accounts_account_exists(account_name)) {
g_key_file_set_string(accounts, account_name, "jid", value); g_key_file_set_string(accounts, account_name, "jid", jid->barejid);
if (jid->resourcepart != NULL) {
g_key_file_set_string(accounts, account_name, "resource", jid->resourcepart);
}
_save_accounts(); _save_accounts();
} }
}
} }
void void

View File

@ -1017,13 +1017,11 @@ _cmd_connect(gchar **args, struct cmd_help_t help)
jid = strdup(account->jid); jid = strdup(account->jid);
} }
cons_show("Connecting with account %s as %s", account->name, jid); cons_show("Connecting with account %s as %s", account->name, jid);
log_debug("Connecting with account %s as %s", account->name, jid);
conn_status = jabber_connect_with_account(account, passwd); conn_status = jabber_connect_with_account(account, passwd);
} else { } else {
jid = strdup(lower); jid = strdup(lower);
cons_show("Connecting as %s", jid); cons_show("Connecting as %s", jid);
log_debug("Connecting as %s", jid); conn_status = jabber_connect_with_details(jid, passwd, altdomain);
conn_status = jabber_connect(jid, passwd, altdomain);
} }
if (conn_status == JABBER_DISCONNECTED) { if (conn_status == JABBER_DISCONNECTED) {

View File

@ -53,11 +53,16 @@ static GHashTable *sub_requests;
// for auto reconnect // for auto reconnect
static struct { static struct {
char *account; char *name;
char *passwd;
} saved_account;
static struct {
char *name;
char *jid; char *jid;
char *passwd; char *passwd;
char *altdomain; char *altdomain;
} saved_user; } saved_details;
static GTimer *reconnect_timer; static GTimer *reconnect_timer;
@ -68,6 +73,10 @@ static void _xmpp_file_logger(void * const userdata,
const char * const msg); const char * const msg);
static xmpp_log_t * _xmpp_get_file_logger(); static xmpp_log_t * _xmpp_get_file_logger();
static jabber_conn_status_t _jabber_connect(const char * const fulljid,
const char * const passwd, const char * const altdomain);
static void _jabber_reconnect(void);
static void _jabber_roster_request(void); static void _jabber_roster_request(void);
// XMPP event handlers // XMPP event handlers
@ -105,61 +114,30 @@ jabber_restart(void)
FREE_SET_NULL(jabber_conn.status); FREE_SET_NULL(jabber_conn.status);
} }
jabber_conn_status_t static jabber_conn_status_t
jabber_connect_with_account(ProfAccount *account, const char * const passwd) _jabber_connect(const char * const fulljid, const char * const passwd,
const char * const altdomain)
{ {
saved_user.account = strdup(account->name); Jid *jid = jid_create(fulljid);
if (saved_user.jid == NULL) { if (jid == NULL) {
saved_user.jid = create_fulljid(account->jid, account->resource); log_error("Malformed JID not able to connect: %s", fulljid);
} jabber_conn.conn_status = JABBER_DISCONNECTED;
if (saved_user.passwd == NULL) { return jabber_conn.conn_status;
saved_user.passwd = strdup(passwd); } else if (jid->fulljid == NULL) {
} log_error("Full JID required to connect, received: %s", fulljid);
if (saved_user.altdomain == NULL) { jabber_conn.conn_status = JABBER_DISCONNECTED;
if (account->server != NULL) { return jabber_conn.conn_status;
saved_user.altdomain = strdup(account->server);
}
} }
log_info("Connecting with account: %s", account->name); jid_destroy(jid);
return jabber_connect(account->jid, passwd, account->server);
}
jabber_conn_status_t log_info("Connecting as %s", fulljid);
jabber_connect(const char * const jid,
const char * const passwd, const char * const altdomain)
{
Jid *jidp = jid_create(jid);
if (saved_user.jid == NULL) {
if (jidp->resourcepart == NULL) {
jid_destroy(jidp);
jidp = jid_create_from_bare_and_resource(jid, "profanity");
saved_user.jid = strdup(jidp->fulljid);
} else {
saved_user.jid = strdup(jid);
}
}
jid_destroy(jidp);
if (saved_user.passwd == NULL) {
saved_user.passwd = strdup(passwd);
}
if (saved_user.altdomain == NULL) {
if (altdomain != NULL) {
saved_user.altdomain = strdup(altdomain);
}
}
log_info("Connecting as %s", saved_user.jid);
xmpp_initialize(); xmpp_initialize();
jabber_conn.log = _xmpp_get_file_logger(); jabber_conn.log = _xmpp_get_file_logger();
jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log); jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log);
jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx); jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx);
xmpp_conn_set_jid(jabber_conn.conn, fulljid);
xmpp_conn_set_jid(jabber_conn.conn, saved_user.jid);
xmpp_conn_set_pass(jabber_conn.conn, passwd); xmpp_conn_set_pass(jabber_conn.conn, passwd);
if (jabber_conn.tls_disabled) if (jabber_conn.tls_disabled)
@ -176,6 +154,64 @@ jabber_connect(const char * const jid,
return jabber_conn.conn_status; return jabber_conn.conn_status;
} }
jabber_conn_status_t
jabber_connect_with_account(ProfAccount *account, const char * const passwd)
{
saved_account.name = strdup(account->name);
saved_account.passwd = strdup(passwd);
log_info("Connecting using account: %s", account->name);
char *fulljid = create_fulljid(account->jid, account->resource);
jabber_conn_status_t result = _jabber_connect(fulljid, passwd, account->server);
free(fulljid);
return result;
}
jabber_conn_status_t
jabber_connect_with_details(const char * const jid,
const char * const passwd, const char * const altdomain)
{
saved_details.name = strdup(jid);
saved_details.passwd = strdup(passwd);
if (altdomain != NULL) {
saved_details.altdomain = strdup(altdomain);
} else {
saved_details.altdomain = NULL;
}
Jid *jidp = jid_create(jid);
if (jidp->resourcepart == NULL) {
jid_destroy(jidp);
jidp = jid_create_from_bare_and_resource(jid, "profanity");
saved_details.jid = strdup(jidp->fulljid);
} else {
saved_details.jid = strdup(jid);
}
jid_destroy(jidp);
log_info("Connecting without account, JID: %s", saved_details.jid);
return _jabber_connect(saved_details.jid, passwd, saved_details.altdomain);
}
static void
_jabber_reconnect(void)
{
// reconnect with account.
ProfAccount *account = accounts_get_account(saved_account.name);
if (account == NULL) {
log_error("Unable to reconnect, account no longer exists: %s", saved_account.name);
} else {
char *fulljid = create_fulljid(account->jid, account->resource);
log_debug("Attempting reconnect with account %s", account->name);
_jabber_connect(fulljid, saved_account.passwd, account->server);
free(fulljid);
g_timer_start(reconnect_timer);
}
}
void void
jabber_disconnect(void) jabber_disconnect(void)
{ {
@ -206,9 +242,7 @@ jabber_process_events(void)
if ((jabber_conn.conn_status == JABBER_DISCONNECTED) && if ((jabber_conn.conn_status == JABBER_DISCONNECTED) &&
(reconnect_timer != NULL)) { (reconnect_timer != NULL)) {
if (g_timer_elapsed(reconnect_timer, NULL) > prefs_get_reconnect()) { if (g_timer_elapsed(reconnect_timer, NULL) > prefs_get_reconnect()) {
log_debug("Attempting reconnect as %s", saved_user.jid); _jabber_reconnect();
jabber_connect(saved_user.jid, saved_user.passwd, saved_user.altdomain);
g_timer_start(reconnect_timer);
} }
} }
} }
@ -514,10 +548,12 @@ jabber_get_status(void)
void void
jabber_free_resources(void) jabber_free_resources(void)
{ {
FREE_SET_NULL(saved_user.jid); FREE_SET_NULL(saved_details.name);
FREE_SET_NULL(saved_user.passwd); FREE_SET_NULL(saved_details.jid);
FREE_SET_NULL(saved_user.account); FREE_SET_NULL(saved_details.passwd);
FREE_SET_NULL(saved_user.altdomain); FREE_SET_NULL(saved_details.altdomain);
FREE_SET_NULL(saved_account.name);
FREE_SET_NULL(saved_account.passwd);
chat_sessions_clear(); chat_sessions_clear();
if (sub_requests != NULL) if (sub_requests != NULL)
g_hash_table_remove_all(sub_requests); g_hash_table_remove_all(sub_requests);
@ -748,11 +784,24 @@ _connection_handler(xmpp_conn_t * const conn,
// login success // login success
if (status == XMPP_CONN_CONNECT) { if (status == XMPP_CONN_CONNECT) {
if (saved_user.account != NULL) {
prof_handle_login_account_success(saved_user.account); // logged in with account
if (saved_account.name != NULL) {
prof_handle_login_account_success(saved_account.name);
// logged in without account, use details to create new account
} else { } else {
const char *jid = xmpp_conn_get_jid(conn); accounts_add(saved_details.name, saved_details.altdomain);
prof_handle_login_success(jid, saved_user.altdomain); accounts_set_jid(saved_details.name, saved_details.jid);
prof_handle_login_account_success(saved_details.name);
saved_account.name = strdup(saved_details.name);
saved_account.passwd = strdup(saved_details.passwd);
FREE_SET_NULL(saved_details.name);
FREE_SET_NULL(saved_details.jid);
FREE_SET_NULL(saved_details.passwd);
FREE_SET_NULL(saved_details.altdomain);
} }
chat_sessions_init(); chat_sessions_init();

View File

@ -56,7 +56,7 @@ typedef enum {
#define JABBER_PRIORITY_MAX 127 #define JABBER_PRIORITY_MAX 127
void jabber_init(const int disable_tls); void jabber_init(const int disable_tls);
jabber_conn_status_t jabber_connect(const char * const jid, jabber_conn_status_t jabber_connect_with_details(const char * const jid,
const char * const passwd, const char * const altdomain); const char * const passwd, const char * const altdomain);
jabber_conn_status_t jabber_connect_with_account(ProfAccount *account, jabber_conn_status_t jabber_connect_with_account(ProfAccount *account,
const char * const passwd); const char * const passwd);

View File

@ -213,20 +213,6 @@ prof_handle_login_account_success(char *account_name)
accounts_free_account(account); accounts_free_account(account);
} }
void
prof_handle_login_success(const char *jid, const char *altdomain)
{
const char *msg = "logged in successfully.";
cons_show("%s %s", jid, msg);
title_bar_set_status(PRESENCE_ONLINE);
log_info("%s %s", jid, msg);
win_current_page_off();
status_bar_print_message(jid);
status_bar_refresh();
accounts_add(jid, altdomain);
}
void void
prof_handle_gone(const char * const from) prof_handle_gone(const char * const from)
{ {