2013-06-30 11:59:38 -04:00
|
|
|
|
2013-07-14 10:05:46 -04:00
|
|
|
#include <assert.h>
|
2013-06-30 11:59:38 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <strophe.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "muc.h"
|
|
|
|
#include "ui/ui.h"
|
|
|
|
#include "xmpp/connection.h"
|
|
|
|
#include "xmpp/stanza.h"
|
|
|
|
#include "xmpp/xmpp.h"
|
2013-07-14 16:58:02 -04:00
|
|
|
#include "xmpp/bookmark.h"
|
2013-06-30 11:59:38 -04:00
|
|
|
|
2013-07-14 10:05:46 -04:00
|
|
|
#define BOOKMARK_TIMEOUT 5000
|
|
|
|
/* TODO: replace with a preference */
|
|
|
|
#define BOOKMARK_AUTOJOIN_MAX 5
|
|
|
|
|
|
|
|
static int autojoin_count;
|
|
|
|
|
2013-07-14 16:58:02 -04:00
|
|
|
static Autocomplete bookmark_ac;
|
|
|
|
static GList *bookmark_list;
|
|
|
|
|
2013-06-30 11:59:38 -04:00
|
|
|
static int _bookmark_handle_result(xmpp_conn_t * const conn,
|
|
|
|
xmpp_stanza_t * const stanza, void * const userdata);
|
2013-07-14 10:05:46 -04:00
|
|
|
static int _bookmark_handle_delete(xmpp_conn_t * const conn,
|
|
|
|
void * const userdata);
|
2013-07-14 16:58:02 -04:00
|
|
|
static void _bookmark_item_destroy(gpointer item);
|
2013-06-30 11:59:38 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
bookmark_request(void)
|
|
|
|
{
|
2013-07-14 06:49:50 -04:00
|
|
|
char *id;
|
2013-07-14 10:05:46 -04:00
|
|
|
xmpp_conn_t *conn = connection_get_conn();
|
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
2013-07-14 06:49:50 -04:00
|
|
|
xmpp_stanza_t *iq;
|
2013-06-30 11:59:38 -04:00
|
|
|
|
2013-07-14 06:49:50 -04:00
|
|
|
id = get_unique_id();
|
|
|
|
if (!id) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-30 11:59:38 -04:00
|
|
|
|
2013-07-14 10:05:46 -04:00
|
|
|
autojoin_count = 0;
|
2013-07-14 16:58:02 -04:00
|
|
|
if (bookmark_ac != NULL) {
|
|
|
|
autocomplete_free(bookmark_ac);
|
|
|
|
}
|
|
|
|
bookmark_ac = autocomplete_new();
|
|
|
|
if (bookmark_list != NULL) {
|
|
|
|
g_list_free_full(bookmark_list, _bookmark_item_destroy);
|
|
|
|
bookmark_list = NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-14 10:05:46 -04:00
|
|
|
xmpp_timed_handler_add(conn, _bookmark_handle_delete, BOOKMARK_TIMEOUT, id);
|
|
|
|
xmpp_id_handler_add(conn, _bookmark_handle_result, id, id);
|
2013-06-30 11:59:38 -04:00
|
|
|
|
2013-07-14 06:49:50 -04:00
|
|
|
iq = stanza_create_storage_bookmarks(ctx);
|
|
|
|
xmpp_stanza_set_id(iq, id);
|
2013-06-30 11:59:38 -04:00
|
|
|
xmpp_send(conn, iq);
|
|
|
|
xmpp_stanza_release(iq);
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:58:02 -04:00
|
|
|
void
|
|
|
|
bookmark_add(const char *jid, const char *nick, gboolean autojoin)
|
|
|
|
{
|
|
|
|
/* TODO: send request */
|
|
|
|
/* TODO: manage bookmark_list */
|
|
|
|
|
|
|
|
/* this may be command for modifying */
|
|
|
|
autocomplete_remove(bookmark_ac, jid);
|
|
|
|
autocomplete_add(bookmark_ac, strdup(jid));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bookmark_remove(const char *jid, gboolean autojoin)
|
|
|
|
{
|
|
|
|
/* TODO: manage bookmark_list */
|
|
|
|
if (autojoin) {
|
|
|
|
/* TODO: just set autojoin=0 */
|
|
|
|
} else {
|
|
|
|
/* TODO: send request */
|
|
|
|
autocomplete_remove(bookmark_ac, jid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const GList *
|
|
|
|
bookmark_get_list(void)
|
|
|
|
{
|
|
|
|
return bookmark_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
bookmark_find(char *search_str)
|
|
|
|
{
|
|
|
|
return autocomplete_complete(bookmark_ac, search_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bookmark_autocomplete_reset(void)
|
|
|
|
{
|
|
|
|
if (bookmark_ac != NULL) {
|
|
|
|
autocomplete_reset(bookmark_ac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 11:59:38 -04:00
|
|
|
static int
|
|
|
|
_bookmark_handle_result(xmpp_conn_t * const conn,
|
|
|
|
xmpp_stanza_t * const stanza, void * const userdata)
|
|
|
|
{
|
2013-07-14 10:05:46 -04:00
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
|
|
|
char *id = (char *)userdata;
|
2013-06-30 11:59:38 -04:00
|
|
|
xmpp_stanza_t *ptr;
|
|
|
|
xmpp_stanza_t *nick;
|
|
|
|
char *name;
|
|
|
|
char *jid;
|
|
|
|
char *autojoin;
|
2013-07-14 16:58:02 -04:00
|
|
|
gboolean autojoin_val;
|
2013-06-30 11:59:38 -04:00
|
|
|
Jid *my_jid;
|
2013-07-14 16:58:02 -04:00
|
|
|
Bookmark *item;
|
2013-06-30 11:59:38 -04:00
|
|
|
|
2013-07-14 10:05:46 -04:00
|
|
|
xmpp_timed_handler_delete(conn, _bookmark_handle_delete);
|
|
|
|
g_free(id);
|
|
|
|
|
2013-06-30 11:59:38 -04:00
|
|
|
name = xmpp_stanza_get_name(stanza);
|
|
|
|
if (!name || strcmp(name, STANZA_NAME_IQ) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
|
|
|
|
if (!ptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ptr = xmpp_stanza_get_child_by_name(ptr, STANZA_NAME_STORAGE);
|
|
|
|
if (!ptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:58:02 -04:00
|
|
|
if (bookmark_ac == NULL) {
|
|
|
|
bookmark_ac = autocomplete_new();
|
|
|
|
}
|
2013-06-30 11:59:38 -04:00
|
|
|
my_jid = jid_create(jabber_get_fulljid());
|
|
|
|
|
|
|
|
ptr = xmpp_stanza_get_children(ptr);
|
|
|
|
while (ptr) {
|
|
|
|
name = xmpp_stanza_get_name(ptr);
|
2013-07-14 19:52:47 -04:00
|
|
|
if (!name || strcmp(name, STANZA_NAME_CONFERENCE) != 0) {
|
|
|
|
ptr = xmpp_stanza_get_next(ptr);
|
2013-06-30 11:59:38 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
jid = xmpp_stanza_get_attribute(ptr, STANZA_ATTR_JID);
|
|
|
|
if (!jid) {
|
2013-07-14 19:52:47 -04:00
|
|
|
ptr = xmpp_stanza_get_next(ptr);
|
2013-06-30 11:59:38 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug("Handle bookmark for %s", jid);
|
|
|
|
|
2013-07-14 16:58:02 -04:00
|
|
|
name = NULL;
|
|
|
|
nick = xmpp_stanza_get_child_by_name(ptr, "nick");
|
|
|
|
if (nick) {
|
|
|
|
char *tmp;
|
|
|
|
tmp = xmpp_stanza_get_text(nick);
|
|
|
|
if (tmp) {
|
|
|
|
name = strdup(tmp);
|
|
|
|
xmpp_free(ctx, tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 11:59:38 -04:00
|
|
|
autojoin = xmpp_stanza_get_attribute(ptr, "autojoin");
|
2013-07-14 19:52:47 -04:00
|
|
|
if (autojoin && (strcmp(autojoin, "1") == 0 || strcmp(autojoin, "true") == 0)) {
|
2013-07-14 16:58:02 -04:00
|
|
|
autojoin_val = TRUE;
|
|
|
|
} else {
|
|
|
|
autojoin_val = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
autocomplete_add(bookmark_ac, strdup(jid));
|
|
|
|
item = malloc(sizeof(*item));
|
|
|
|
item->jid = strdup(jid);
|
|
|
|
item->nick = name;
|
|
|
|
item->autojoin = autojoin_val;
|
|
|
|
bookmark_list = g_list_append(bookmark_list, item);
|
|
|
|
|
|
|
|
|
|
|
|
/* TODO: preference whether autojoin */
|
|
|
|
if (autojoin_val) {
|
|
|
|
if (autojoin_count < BOOKMARK_AUTOJOIN_MAX) {
|
|
|
|
Jid *room_jid;
|
|
|
|
|
|
|
|
++autojoin_count;
|
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
name = my_jid->localpart;
|
2013-06-30 11:59:38 -04:00
|
|
|
}
|
|
|
|
|
2013-07-14 16:58:02 -04:00
|
|
|
log_debug("Autojoin %s with nick=%s", jid, name);
|
|
|
|
room_jid = jid_create_from_bare_and_resource(jid, name);
|
|
|
|
if (!muc_room_is_active(room_jid)) {
|
|
|
|
presence_join_room(room_jid);
|
|
|
|
/* TODO: this should be removed after fixing #195 */
|
|
|
|
ui_room_join(room_jid);
|
2013-06-30 11:59:38 -04:00
|
|
|
}
|
2013-07-14 16:58:02 -04:00
|
|
|
jid_destroy(room_jid);
|
|
|
|
} else {
|
|
|
|
log_debug("Rejected autojoin %s (maximum has been reached)", jid);
|
2013-06-30 11:59:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = xmpp_stanza_get_next(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
jid_destroy(my_jid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-14 10:05:46 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2013-07-14 16:58:02 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
_bookmark_item_destroy(gpointer item)
|
|
|
|
{
|
|
|
|
Bookmark *p = (Bookmark *)item;
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(p->jid);
|
|
|
|
free(p->nick);
|
|
|
|
free(p);
|
|
|
|
}
|