Refactored bookmarks to use option parser, allow bookmarking rooms with passwords

This commit is contained in:
James Booth
2014-05-10 00:50:43 +01:00
parent a519d25e4b
commit d2662a6f17
12 changed files with 243 additions and 288 deletions

View File

@@ -35,6 +35,7 @@
#include "xmpp/stanza.h"
#include "xmpp/xmpp.h"
#include "xmpp/bookmark.h"
#include "ui/ui.h"
#define BOOKMARK_TIMEOUT 5000
/* TODO: replace with a preference */
@@ -83,69 +84,128 @@ bookmark_request(void)
}
static gboolean
_bookmark_add(const char *jid, const char *nick, gboolean autojoin)
_bookmark_add(const char *jid, const char *nick, const char *password, const char *autojoin_str)
{
gboolean added = TRUE;
if (autocomplete_contains(bookmark_ac, jid)) {
added = FALSE;
}
/* this may be command for modifying */
Bookmark *item = malloc(sizeof(*item));
item->jid = strdup(jid);
if (nick != NULL) {
item->nick = strdup(nick);
return FALSE;
} else {
item->nick = NULL;
Bookmark *item = malloc(sizeof(*item));
item->jid = strdup(jid);
if (nick != NULL) {
item->nick = strdup(nick);
} else {
item->nick = NULL;
}
if (password != NULL) {
item->password = strdup(password);
} else {
item->password = NULL;
}
if (g_strcmp0(autojoin_str, "on") == 0) {
item->autojoin = TRUE;
} else {
item->autojoin = FALSE;
}
bookmark_list = g_list_append(bookmark_list, item);
autocomplete_add(bookmark_ac, jid);
_send_bookmarks();
return TRUE;
}
item->autojoin = autojoin;
GList *found = g_list_find_custom(bookmark_list, item, _match_bookmark_by_jid);
if (found != NULL) {
bookmark_list = g_list_remove_link(bookmark_list, found);
_bookmark_item_destroy(found->data);
g_list_free(found);
}
bookmark_list = g_list_append(bookmark_list, item);
autocomplete_remove(bookmark_ac, jid);
autocomplete_add(bookmark_ac, jid);
_send_bookmarks();
return added;
}
static gboolean
_bookmark_remove(const char *jid, gboolean autojoin)
_bookmark_update(const char *jid, const char *nick, const char *password, const char *autojoin_str)
{
Bookmark *item = malloc(sizeof(*item));
item->jid = strdup(jid);
item->nick = NULL;
item->autojoin = autojoin;
item->password = NULL;
item->autojoin = FALSE;
GList *found = g_list_find_custom(bookmark_list, item, _match_bookmark_by_jid);
_bookmark_item_destroy(item);
if (found == NULL) {
return FALSE;
} else {
Bookmark *bm = found->data;
if (nick != NULL) {
free(bm->nick);
bm->nick = strdup(nick);
}
if (password != NULL) {
free(bm->password);
bm->password = strdup(password);
}
if (autojoin_str != NULL) {
if (g_strcmp0(autojoin_str, "on") == 0) {
bm->autojoin = TRUE;
} else if (g_strcmp0(autojoin_str, "off") == 0) {
bm->autojoin = FALSE;
}
}
_send_bookmarks();
return TRUE;
}
}
static gboolean
_bookmark_join(const char *jid)
{
Bookmark *item = malloc(sizeof(*item));
item->jid = strdup(jid);
item->nick = NULL;
item->password = NULL;
item->autojoin = FALSE;
GList *found = g_list_find_custom(bookmark_list, item, _match_bookmark_by_jid);
_bookmark_item_destroy(item);
if (found == NULL) {
return FALSE;
} else {
char *account_name = jabber_get_account_name();
ProfAccount *account = accounts_get_account(account_name);
Bookmark *item = found->data;
if (!muc_room_is_active(item->jid)) {
char *nick = item->nick;
if (nick == NULL) {
nick = account->muc_nick;
}
presence_join_room(item->jid, nick, item->password);
muc_join_room(item->jid, nick, item->password, FALSE);
account_free(account);
} else if (muc_get_roster_received(item->jid)) {
ui_room_join(item->jid, TRUE);
}
return TRUE;
}
}
static gboolean
_bookmark_remove(const char *jid)
{
Bookmark *item = malloc(sizeof(*item));
item->jid = strdup(jid);
item->nick = NULL;
item->password = NULL;
item->autojoin = FALSE;
GList *found = g_list_find_custom(bookmark_list, item, _match_bookmark_by_jid);
_bookmark_item_destroy(item);
gboolean removed = found != NULL;
if (removed) {
// set autojoin FALSE
if (autojoin) {
Bookmark *bookmark = found->data;
bookmark->autojoin = FALSE;
// remove bookmark
} else {
bookmark_list = g_list_remove_link(bookmark_list, found);
_bookmark_item_destroy(found->data);
g_list_free(found);
autocomplete_remove(bookmark_ac, jid);
}
bookmark_list = g_list_remove_link(bookmark_list, found);
_bookmark_item_destroy(found->data);
g_list_free(found);
autocomplete_remove(bookmark_ac, jid);
_send_bookmarks();
return TRUE;
} else {
return FALSE;
}
return removed;
}
static const GList *
@@ -176,9 +236,11 @@ _bookmark_handle_result(xmpp_conn_t * const conn,
char *id = (char *)userdata;
xmpp_stanza_t *ptr;
xmpp_stanza_t *nick;
xmpp_stanza_t *password_st;
char *name;
char *jid;
char *autojoin;
char *password;
gboolean autojoin_val;
Jid *my_jid;
Bookmark *item;
@@ -231,6 +293,17 @@ _bookmark_handle_result(xmpp_conn_t * const conn,
}
}
password = NULL;
password_st = xmpp_stanza_get_child_by_name(ptr, "password");
if (password_st) {
char *tmp;
tmp = xmpp_stanza_get_text(password_st);
if (tmp) {
password = strdup(tmp);
xmpp_free(ctx, tmp);
}
}
autojoin = xmpp_stanza_get_attribute(ptr, "autojoin");
if (autojoin && (strcmp(autojoin, "1") == 0 || strcmp(autojoin, "true") == 0)) {
autojoin_val = TRUE;
@@ -242,6 +315,7 @@ _bookmark_handle_result(xmpp_conn_t * const conn,
item = malloc(sizeof(*item));
item->jid = strdup(jid);
item->nick = name;
item->password = password;
item->autojoin = autojoin_val;
bookmark_list = g_list_append(bookmark_list, item);
@@ -259,8 +333,8 @@ _bookmark_handle_result(xmpp_conn_t * const conn,
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->barejid)) {
presence_join_room(jid, name, NULL);
muc_join_room(jid, name, NULL, TRUE);
presence_join_room(jid, name, password);
muc_join_room(jid, name, password, TRUE);
}
jid_destroy(room_jid);
} else {
@@ -303,6 +377,7 @@ _bookmark_item_destroy(gpointer item)
free(p->jid);
free(p->nick);
free(p->password);
free(p);
}
@@ -364,6 +439,18 @@ _send_bookmarks(void)
xmpp_stanza_release(nick_st);
}
if (bookmark->password != NULL) {
xmpp_stanza_t *password_st = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(password_st, STANZA_NAME_PASSWORD);
xmpp_stanza_t *password_text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(password_text, bookmark->password);
xmpp_stanza_add_child(password_st, password_text);
xmpp_stanza_add_child(conference, password_st);
xmpp_stanza_release(password_text);
xmpp_stanza_release(password_st);
}
xmpp_stanza_add_child(storage, conference);
xmpp_stanza_release(conference);
@@ -383,7 +470,9 @@ void
bookmark_init_module(void)
{
bookmark_add = _bookmark_add;
bookmark_update = _bookmark_update;
bookmark_remove = _bookmark_remove;
bookmark_join = _bookmark_join;
bookmark_get_list = _bookmark_get_list;
bookmark_find = _bookmark_find;
bookmark_autocomplete_reset = _bookmark_autocomplete_reset;

View File

@@ -28,6 +28,7 @@
struct bookmark_t {
char *jid;
char *nick;
char *password;
gboolean autojoin;
};

View File

@@ -59,6 +59,7 @@
#define STANZA_NAME_FIELD "field"
#define STANZA_NAME_STORAGE "storage"
#define STANZA_NAME_NICK "nick"
#define STANZA_NAME_PASSWORD "password"
#define STANZA_NAME_CONFERENCE "conference"
#define STANZA_NAME_VALUE "value"

View File

@@ -132,8 +132,10 @@ void (*iq_set_autoping)(int seconds);
Capabilities* (*caps_get)(const char * const caps_str);
void (*caps_close)(void);
gboolean (*bookmark_add)(const char *jid, const char *nick, gboolean autojoin);
gboolean (*bookmark_remove)(const char *jid, gboolean autojoin);
gboolean (*bookmark_add)(const char *jid, const char *nick, const char *password, const char *autojoin_str);
gboolean (*bookmark_update)(const char *jid, const char *nick, const char *password, const char *autojoin_str);
gboolean (*bookmark_remove)(const char *jid);
gboolean (*bookmark_join)(const char *jid);
const GList * (*bookmark_get_list)(void);
char * (*bookmark_find)(char *search_str);
void (*bookmark_autocomplete_reset)(void);