mirror of
https://github.com/profanity-im/profanity.git
synced 2025-01-03 14:57:42 -05:00
Merge pull request #132 from pasis/next
Some code refactoring in jabber.c
This commit is contained in:
commit
f519ae78d8
151
src/jabber.c
151
src/jabber.c
@ -48,14 +48,18 @@ static struct _jabber_conn_t {
|
||||
int priority;
|
||||
} jabber_conn;
|
||||
|
||||
// for auto reconnect
|
||||
static char *saved_account;
|
||||
static char *saved_user;
|
||||
static char *saved_password;
|
||||
static char *saved_altdomain;
|
||||
static GTimer *reconnect_timer;
|
||||
static GHashTable *sub_requests;
|
||||
|
||||
// for auto reconnect
|
||||
static struct {
|
||||
char *account;
|
||||
char *jid;
|
||||
char *passwd;
|
||||
char *altdomain;
|
||||
} saved_user;
|
||||
|
||||
static GTimer *reconnect_timer;
|
||||
|
||||
static log_level_t _get_log_level(xmpp_log_level_t xmpp_level);
|
||||
static xmpp_log_level_t _get_xmpp_log_level();
|
||||
static void _xmpp_file_logger(void * const userdata,
|
||||
@ -85,10 +89,11 @@ static int _presence_handler(xmpp_conn_t * const conn,
|
||||
static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata);
|
||||
|
||||
#define FREE_SET_NULL(resource) \
|
||||
{\
|
||||
if (resource != NULL) \
|
||||
{ \
|
||||
if (resource != NULL) { \
|
||||
free(resource); \
|
||||
resource = NULL; \
|
||||
resource = NULL; \
|
||||
} \
|
||||
}
|
||||
|
||||
void
|
||||
@ -107,81 +112,47 @@ jabber_restart(void)
|
||||
{
|
||||
jabber_conn.conn_status = JABBER_STARTED;
|
||||
jabber_conn.presence = PRESENCE_OFFLINE;
|
||||
if (jabber_conn.status != NULL)
|
||||
free(jabber_conn.status);
|
||||
jabber_conn.status = NULL;
|
||||
FREE_SET_NULL(jabber_conn.status);
|
||||
}
|
||||
|
||||
jabber_conn_status_t
|
||||
jabber_connect_with_account(ProfAccount *account, const char * const passwd)
|
||||
{
|
||||
saved_account = strdup(account->name);
|
||||
if (saved_user == NULL) {
|
||||
saved_user = strdup(account->jid);
|
||||
}
|
||||
if (saved_password == NULL) {
|
||||
saved_password = strdup(passwd);
|
||||
}
|
||||
if (saved_altdomain == NULL) {
|
||||
if (account->server != NULL) {
|
||||
saved_altdomain = strdup(account->server);
|
||||
}
|
||||
}
|
||||
FREE_SET_NULL(saved_user.account);
|
||||
|
||||
log_info("Connecting with account:%s, jid: %s", account->name, account->jid);
|
||||
xmpp_initialize();
|
||||
if (account->name == NULL)
|
||||
return JABBER_UNDEFINED;
|
||||
|
||||
jabber_conn.log = _xmpp_get_file_logger();
|
||||
jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log);
|
||||
jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx);
|
||||
|
||||
xmpp_conn_set_jid(jabber_conn.conn, account->jid);
|
||||
xmpp_conn_set_pass(jabber_conn.conn, passwd);
|
||||
|
||||
if (jabber_conn.tls_disabled)
|
||||
xmpp_conn_disable_tls(jabber_conn.conn);
|
||||
|
||||
int connect_status = xmpp_connect_client(jabber_conn.conn, account->server, 0,
|
||||
_connection_handler, jabber_conn.ctx);
|
||||
|
||||
if (connect_status == 0)
|
||||
jabber_conn.conn_status = JABBER_CONNECTING;
|
||||
else
|
||||
jabber_conn.conn_status = JABBER_DISCONNECTED;
|
||||
|
||||
return jabber_conn.conn_status;
|
||||
saved_user.account = strdup(account->name);
|
||||
log_info("Connecting with account: %s", account->name);
|
||||
return jabber_connect(account->jid, passwd, account->server);
|
||||
}
|
||||
|
||||
jabber_conn_status_t
|
||||
jabber_connect(const char * const user,
|
||||
jabber_connect(const char * const jid,
|
||||
const char * const passwd, const char * const altdomain)
|
||||
{
|
||||
if (saved_account != NULL) {
|
||||
free(saved_account);
|
||||
saved_account = NULL;
|
||||
}
|
||||
FREE_SET_NULL(saved_user.jid);
|
||||
FREE_SET_NULL(saved_user.passwd);
|
||||
FREE_SET_NULL(saved_user.altdomain);
|
||||
|
||||
if (saved_user == NULL) {
|
||||
saved_user = strdup(user);
|
||||
}
|
||||
if (saved_password == NULL) {
|
||||
saved_password = strdup(passwd);
|
||||
}
|
||||
if (saved_altdomain == NULL) {
|
||||
if (altdomain != NULL) {
|
||||
saved_altdomain = strdup(altdomain);
|
||||
}
|
||||
}
|
||||
if (jid == NULL || passwd == NULL)
|
||||
return JABBER_UNDEFINED;
|
||||
|
||||
log_info("Connecting as %s", saved_user);
|
||||
saved_user.jid = strdup(jid);
|
||||
saved_user.passwd = strdup(passwd);
|
||||
if (altdomain != NULL)
|
||||
saved_user.altdomain = strdup(altdomain);
|
||||
|
||||
log_info("Connecting as %s", jid);
|
||||
xmpp_initialize();
|
||||
|
||||
jabber_conn.log = _xmpp_get_file_logger();
|
||||
jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log);
|
||||
jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx);
|
||||
|
||||
xmpp_conn_set_jid(jabber_conn.conn, saved_user);
|
||||
xmpp_conn_set_pass(jabber_conn.conn, saved_password);
|
||||
xmpp_conn_set_jid(jabber_conn.conn, jid);
|
||||
xmpp_conn_set_pass(jabber_conn.conn, passwd);
|
||||
|
||||
if (jabber_conn.tls_disabled)
|
||||
xmpp_conn_disable_tls(jabber_conn.conn);
|
||||
@ -227,8 +198,8 @@ jabber_process_events(void)
|
||||
if ((jabber_conn.conn_status == JABBER_DISCONNECTED) &&
|
||||
(reconnect_timer != NULL)) {
|
||||
if (g_timer_elapsed(reconnect_timer, NULL) > prefs_get_reconnect()) {
|
||||
log_debug("Attempting reconnect as %s", saved_user);
|
||||
jabber_connect(saved_user, saved_password, saved_altdomain);
|
||||
log_debug("Attempting reconnect as %s", saved_user.jid);
|
||||
jabber_connect(saved_user.jid, saved_user.passwd, saved_user.altdomain);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -329,8 +300,10 @@ jabber_subscription(const char * const jid, jabber_subscr_t action)
|
||||
type = STANZA_TYPE_SUBSCRIBED;
|
||||
else if (action == PRESENCE_UNSUBSCRIBED)
|
||||
type = STANZA_TYPE_UNSUBSCRIBED;
|
||||
else // unknown action
|
||||
else { // unknown action
|
||||
free(jid_cpy);
|
||||
return;
|
||||
}
|
||||
|
||||
presence = xmpp_stanza_new(jabber_conn.ctx);
|
||||
xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE);
|
||||
@ -396,7 +369,7 @@ jabber_update_presence(jabber_presence_t status, const char * const msg,
|
||||
return;
|
||||
|
||||
pri = prefs_get_priority();
|
||||
if (pri < -128 || pri > 127)
|
||||
if (pri < JABBER_PRIORITY_MIN || pri > JABBER_PRIORITY_MAX)
|
||||
pri = 0;
|
||||
|
||||
jabber_conn.presence = status;
|
||||
@ -474,9 +447,7 @@ jabber_update_presence(jabber_presence_t status, const char * const msg,
|
||||
void
|
||||
jabber_set_autoping(int seconds)
|
||||
{
|
||||
if (jabber_conn.conn_status != JABBER_CONNECTED) {
|
||||
return;
|
||||
} else {
|
||||
if (jabber_conn.conn_status == JABBER_CONNECTED) {
|
||||
xmpp_timed_handler_delete(jabber_conn.conn, _ping_timed_handler);
|
||||
|
||||
if (seconds != 0) {
|
||||
@ -523,10 +494,10 @@ jabber_get_status(void)
|
||||
void
|
||||
jabber_free_resources(void)
|
||||
{
|
||||
FREE_SET_NULL(saved_user);
|
||||
FREE_SET_NULL(saved_password);
|
||||
FREE_SET_NULL(saved_account);
|
||||
FREE_SET_NULL(saved_altdomain);
|
||||
FREE_SET_NULL(saved_user.jid);
|
||||
FREE_SET_NULL(saved_user.passwd);
|
||||
FREE_SET_NULL(saved_user.account);
|
||||
FREE_SET_NULL(saved_user.altdomain);
|
||||
chat_sessions_clear();
|
||||
if (sub_requests != NULL)
|
||||
g_hash_table_remove_all(sub_requests);
|
||||
@ -570,7 +541,7 @@ _groupchat_message_handler(xmpp_stanza_t * const stanza)
|
||||
char *room = NULL;
|
||||
char *nick = NULL;
|
||||
char *message = NULL;
|
||||
gchar *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||
char *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||
|
||||
// handle room broadcasts
|
||||
if (jid_is_room(room_jid)) {
|
||||
@ -603,17 +574,14 @@ _groupchat_message_handler(xmpp_stanza_t * const stanza)
|
||||
// room jid not of form room/nick
|
||||
if (!parse_room_jid(room_jid, &room, &nick)) {
|
||||
log_error("Could not parse room jid: %s", room_jid);
|
||||
g_free(room);
|
||||
g_free(nick);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// room not active in profanity
|
||||
if (!muc_room_is_active(room_jid)) {
|
||||
log_error("Message recieved for inactive groupchat: %s", room_jid);
|
||||
g_free(room);
|
||||
g_free(nick);
|
||||
free(room);
|
||||
free(nick);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -633,6 +601,9 @@ _groupchat_message_handler(xmpp_stanza_t * const stanza)
|
||||
}
|
||||
}
|
||||
|
||||
free(room);
|
||||
free(nick);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -741,7 +712,7 @@ _chat_message_handler(xmpp_stanza_t * const stanza)
|
||||
}
|
||||
}
|
||||
|
||||
g_free(jid);
|
||||
free(jid);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -755,11 +726,11 @@ _connection_handler(xmpp_conn_t * const conn,
|
||||
|
||||
// login success
|
||||
if (status == XMPP_CONN_CONNECT) {
|
||||
if (saved_account != NULL) {
|
||||
prof_handle_login_account_success(saved_account);
|
||||
if (saved_user.account != NULL) {
|
||||
prof_handle_login_account_success(saved_user.account);
|
||||
} else {
|
||||
const char *jid = xmpp_conn_get_jid(conn);
|
||||
prof_handle_login_success(jid, saved_altdomain);
|
||||
prof_handle_login_success(jid, saved_user.altdomain);
|
||||
}
|
||||
|
||||
chat_sessions_init();
|
||||
@ -792,7 +763,7 @@ _connection_handler(xmpp_conn_t * const conn,
|
||||
if (prefs_get_reconnect() != 0) {
|
||||
assert(reconnect_timer == NULL);
|
||||
reconnect_timer = g_timer_new();
|
||||
// TODO: free resources but leave saved_* untouched
|
||||
// TODO: free resources but leave saved_user untouched
|
||||
} else {
|
||||
jabber_free_resources();
|
||||
}
|
||||
@ -806,7 +777,7 @@ _connection_handler(xmpp_conn_t * const conn,
|
||||
if (prefs_get_reconnect() != 0) {
|
||||
g_timer_start(reconnect_timer);
|
||||
}
|
||||
// TODO: free resources but leave saved_* untouched
|
||||
// TODO: free resources but leave saved_user untouched
|
||||
}
|
||||
}
|
||||
|
||||
@ -980,9 +951,6 @@ _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
|
||||
|
||||
if (!parse_room_jid(jid, &room, &nick)) {
|
||||
log_error("Could not parse room jid: %s", room);
|
||||
g_free(room);
|
||||
g_free(nick);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -1058,6 +1026,9 @@ _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
|
||||
}
|
||||
}
|
||||
|
||||
free(room);
|
||||
free(nick);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include "accounts.h"
|
||||
|
||||
typedef enum {
|
||||
JABBER_UNDEFINED,
|
||||
JABBER_STARTED,
|
||||
JABBER_CONNECTING,
|
||||
JABBER_CONNECTED,
|
||||
@ -48,8 +49,11 @@ typedef enum {
|
||||
PRESENCE_UNSUBSCRIBED
|
||||
} jabber_subscr_t;
|
||||
|
||||
#define JABBER_PRIORITY_MIN -128
|
||||
#define JABBER_PRIORITY_MAX 127
|
||||
|
||||
void jabber_init(const int disable_tls);
|
||||
jabber_conn_status_t jabber_connect(const char * const user,
|
||||
jabber_conn_status_t jabber_connect(const char * const jid,
|
||||
const char * const passwd, const char * const altdomain);
|
||||
jabber_conn_status_t jabber_connect_with_account(ProfAccount *account,
|
||||
const char * const passwd);
|
||||
|
Loading…
Reference in New Issue
Block a user