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

improved autojoin logic

Delete id handler after timeout (5sec) if response isn't received
Auto join maximum 5 conferences
This commit is contained in:
Dmitry Podgorny 2013-07-14 17:05:46 +03:00
parent ccbbd16d5f
commit 6e23584575

View File

@ -1,4 +1,5 @@
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -12,15 +13,23 @@
#include "xmpp/stanza.h" #include "xmpp/stanza.h"
#include "xmpp/xmpp.h" #include "xmpp/xmpp.h"
#define BOOKMARK_TIMEOUT 5000
/* TODO: replace with a preference */
#define BOOKMARK_AUTOJOIN_MAX 5
static int autojoin_count;
static int _bookmark_handle_result(xmpp_conn_t * const conn, static int _bookmark_handle_result(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata); xmpp_stanza_t * const stanza, void * const userdata);
static int _bookmark_handle_delete(xmpp_conn_t * const conn,
void * const userdata);
void void
bookmark_request(void) bookmark_request(void)
{ {
char *id; char *id;
xmpp_conn_t * const conn = connection_get_conn(); xmpp_conn_t *conn = connection_get_conn();
xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_ctx_t *ctx = connection_get_ctx();
xmpp_stanza_t *iq; xmpp_stanza_t *iq;
id = get_unique_id(); id = get_unique_id();
@ -28,22 +37,22 @@ bookmark_request(void)
return; return;
} }
/* TODO: timed handler to remove this id_handler */ autojoin_count = 0;
xmpp_id_handler_add(conn, _bookmark_handle_result, id, ctx); xmpp_timed_handler_add(conn, _bookmark_handle_delete, BOOKMARK_TIMEOUT, id);
xmpp_id_handler_add(conn, _bookmark_handle_result, id, id);
iq = stanza_create_storage_bookmarks(ctx); iq = stanza_create_storage_bookmarks(ctx);
xmpp_stanza_set_id(iq, id); xmpp_stanza_set_id(iq, id);
xmpp_send(conn, iq); xmpp_send(conn, iq);
xmpp_stanza_release(iq); xmpp_stanza_release(iq);
g_free(id);
} }
static int static int
_bookmark_handle_result(xmpp_conn_t * const conn, _bookmark_handle_result(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata) xmpp_stanza_t * const stanza, void * const userdata)
{ {
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; xmpp_ctx_t *ctx = connection_get_ctx();
char *id = (char *)userdata;
xmpp_stanza_t *ptr; xmpp_stanza_t *ptr;
xmpp_stanza_t *nick; xmpp_stanza_t *nick;
char *name; char *name;
@ -51,6 +60,9 @@ _bookmark_handle_result(xmpp_conn_t * const conn,
char *autojoin; char *autojoin;
Jid *my_jid; Jid *my_jid;
xmpp_timed_handler_delete(conn, _bookmark_handle_delete);
g_free(id);
name = xmpp_stanza_get_name(stanza); name = xmpp_stanza_get_name(stanza);
if (!name || strcmp(name, STANZA_NAME_IQ) != 0) { if (!name || strcmp(name, STANZA_NAME_IQ) != 0) {
return 0; return 0;
@ -96,15 +108,21 @@ _bookmark_handle_result(xmpp_conn_t * const conn,
} }
if (name) { if (name) {
/* TODO: autojoin maximum (e.g. 5) rooms */ if (autojoin_count < BOOKMARK_AUTOJOIN_MAX) {
Jid *room_jid;
log_debug("Autojoin %s with nick=%s", jid, name); log_debug("Autojoin %s with nick=%s", jid, name);
Jid *room_jid = jid_create_from_bare_and_resource(jid, name); ++autojoin_count;
room_jid = jid_create_from_bare_and_resource(jid, name);
if (!muc_room_is_active(room_jid)) { if (!muc_room_is_active(room_jid)) {
presence_join_room(room_jid); presence_join_room(room_jid);
/* XXX: this should be removed after fixing #195 */ /* TODO: this should be removed after fixing #195 */
ui_room_join(room_jid); ui_room_join(room_jid);
} }
jid_destroy(room_jid); jid_destroy(room_jid);
} else {
log_debug("Rejected autojoin %s (maximum has been reached)", jid);
}
free(name); free(name);
} }
} }
@ -118,3 +136,19 @@ _bookmark_handle_result(xmpp_conn_t * const conn,
return 0; return 0;
} }
static int
_bookmark_handle_delete(xmpp_conn_t * const conn,
void * const userdata)
{
char *id = (char *)userdata;
assert(id != NULL);
log_debug("Timeout for handler with id=%s", id);
xmpp_id_handler_delete(conn, _bookmark_handle_result, id);
g_free(id);
return 0;
}