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

Tidy ping handlers

This commit is contained in:
James Booth 2015-12-31 00:11:58 +00:00
parent a0c9b0cb70
commit 2847e39659

View File

@ -126,18 +126,20 @@ iq_add_handlers(void)
void void
iq_set_autoping(const int seconds) iq_set_autoping(const int seconds)
{ {
xmpp_conn_t * const conn = connection_get_conn(); if (jabber_get_connection_status() != JABBER_CONNECTED) {
xmpp_ctx_t * const ctx = connection_get_ctx(); return;
}
if (jabber_get_connection_status() == JABBER_CONNECTED) { xmpp_conn_t * const conn = connection_get_conn();
xmpp_timed_handler_delete(conn, _autoping_timed_handler); xmpp_timed_handler_delete(conn, _autoping_timed_handler);
if (seconds != 0) { if (seconds == 0) {
return;
}
int millis = seconds * 1000; int millis = seconds * 1000;
xmpp_timed_handler_add(conn, _autoping_timed_handler, millis, xmpp_ctx_t * const ctx = connection_get_ctx();
ctx); xmpp_timed_handler_add(conn, _autoping_timed_handler, millis, ctx);
}
}
} }
void void
@ -522,42 +524,45 @@ _error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
} }
static int static int
_pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, _auto_pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata)
void *const userdata)
{ {
char *id = xmpp_stanza_get_id(stanza); char *id = xmpp_stanza_get_id(stanza);
char *type = xmpp_stanza_get_type(stanza); if (id == NULL) {
if (id) {
log_debug("IQ pong handler fired, id: %s.", id);
} else {
log_debug("IQ pong handler fired."); log_debug("IQ pong handler fired.");
return 0;
}
log_debug("IQ pong handler fired, id: %s.", id);
char *type = xmpp_stanza_get_type(stanza);
if (type == NULL) {
return 0;
}
if (g_strcmp0(type, STANZA_TYPE_ERROR) != 0) {
return 0;
} }
if (id && type) {
// show warning if error // show warning if error
if (strcmp(type, STANZA_TYPE_ERROR) == 0) {
char *error_msg = stanza_get_error_message(stanza); char *error_msg = stanza_get_error_message(stanza);
log_warning("Server ping (id=%s) responded with error: %s", id, error_msg); log_warning("Server ping (id=%s) responded with error: %s", id, error_msg);
free(error_msg); free(error_msg);
// turn off autoping if error type is 'cancel' // turn off autoping if error type is 'cancel'
xmpp_stanza_t *error = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR); xmpp_stanza_t *error = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR);
if (error) { if (error == NULL) {
return 0;
}
char *errtype = xmpp_stanza_get_type(error); char *errtype = xmpp_stanza_get_type(error);
if (errtype) { if (errtype == NULL) {
if (strcmp(errtype, "cancel") == 0) { return 0;
}
if (g_strcmp0(errtype, "cancel") == 0) {
log_warning("Server ping (id=%s) error type 'cancel', disabling autoping.", id); log_warning("Server ping (id=%s) error type 'cancel', disabling autoping.", id);
prefs_set_autoping(0); prefs_set_autoping(0);
cons_show_error("Server ping not supported, autoping disabled."); cons_show_error("Server ping not supported, autoping disabled.");
xmpp_timed_handler_delete(conn, _autoping_timed_handler); xmpp_timed_handler_delete(conn, _autoping_timed_handler);
} }
}
}
}
}
// remove this handler
return 0; return 0;
} }
@ -841,20 +846,19 @@ _manual_pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
static int static int
_autoping_timed_handler(xmpp_conn_t *const conn, void *const userdata) _autoping_timed_handler(xmpp_conn_t *const conn, void *const userdata)
{ {
if (jabber_get_connection_status() != JABBER_CONNECTED) {
return 1;
}
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
if (jabber_get_connection_status() == JABBER_CONNECTED) {
xmpp_stanza_t *iq = stanza_create_ping_iq(ctx, NULL); xmpp_stanza_t *iq = stanza_create_ping_iq(ctx, NULL);
char *id = xmpp_stanza_get_id(iq); char *id = xmpp_stanza_get_id(iq);
// add pong handler // add pong handler
xmpp_id_handler_add(conn, _pong_handler, id, ctx); xmpp_id_handler_add(conn, _auto_pong_handler, id, ctx);
xmpp_send(conn, iq); xmpp_send(conn, iq);
xmpp_stanza_release(iq); xmpp_stanza_release(iq);
}
return 1; return 1;
} }