diff options
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | src/command/command.c | 126 | ||||
-rw-r--r-- | src/common.c | 9 | ||||
-rw-r--r-- | src/ui/console.c | 29 | ||||
-rw-r--r-- | src/ui/ui.h | 1 | ||||
-rw-r--r-- | src/xmpp/bookmark.c | 245 | ||||
-rw-r--r-- | src/xmpp/bookmark.h | 22 | ||||
-rw-r--r-- | src/xmpp/connection.c | 2 | ||||
-rw-r--r-- | src/xmpp/stanza.c | 57 | ||||
-rw-r--r-- | src/xmpp/stanza.h | 6 |
10 files changed, 493 insertions, 6 deletions
diff --git a/Makefile.am b/Makefile.am index f7c3f825..fff66a12 100644 --- a/Makefile.am +++ b/Makefile.am @@ -10,6 +10,7 @@ profanity_SOURCES = \ src/xmpp/stanza.h src/xmpp/message.h src/xmpp/iq.h src/xmpp/presence.h \ src/xmpp/capabilities.h src/xmpp/connection.h \ src/xmpp/roster.c src/xmpp/roster.h \ + src/xmpp/bookmark.c src/xmpp/bookmark.h \ src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \ src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \ src/ui/console.c src/ui/notifier.c src/ui/notifier.h \ @@ -36,6 +37,7 @@ tests_testsuite_SOURCES = \ src/xmpp/stanza.h src/xmpp/message.h src/xmpp/iq.h src/xmpp/presence.h \ src/xmpp/capabilities.h src/xmpp/connection.h \ src/xmpp/roster.c src/xmpp/roster.h \ + src/xmpp/bookmark.c src/xmpp/bookmark.h \ src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \ src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \ src/ui/console.c src/ui/notifier.c src/ui/notifier.h \ diff --git a/src/command/command.c b/src/command/command.c index 2e31605d..6cf60ce7 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -44,6 +44,7 @@ #include "tools/tinyurl.h" #include "ui/ui.h" #include "xmpp/xmpp.h" +#include "xmpp/bookmark.h" /* * Command structure @@ -83,6 +84,7 @@ static char * _account_autocomplete(char *input, int *size); static char * _who_autocomplete(char *input, int *size); static char * _roster_autocomplete(char *input, int *size); static char * _group_autocomplete(char *input, int *size); +static char * _bookmark_autocomplete(char *input, int *size); static int _strtoi(char *str, int *saveptr, int min, int max); @@ -128,6 +130,7 @@ static gboolean _cmd_priority(gchar **args, struct cmd_help_t help); static gboolean _cmd_quit(gchar **args, struct cmd_help_t help); static gboolean _cmd_reconnect(gchar **args, struct cmd_help_t help); static gboolean _cmd_rooms(gchar **args, struct cmd_help_t help); +static gboolean _cmd_bookmark(gchar **args, struct cmd_help_t help); static gboolean _cmd_roster(gchar **args, struct cmd_help_t help); static gboolean _cmd_software(gchar **args, struct cmd_help_t help); static gboolean _cmd_splash(gchar **args, struct cmd_help_t help); @@ -367,6 +370,15 @@ static struct cmd_t command_defs[] = "Example : /rooms (if logged in as me@server.org, is equivalent to /rooms conference.server.org)", NULL } } }, + { "/bookmark", + _cmd_bookmark, parse_args, 0, 4, NULL, + { "/bookmark [add|list|remove] [room@server] [autojoin on|off] [nick nickname]", + "Manage bookmarks.", + { "/bookmark [add|list|remove] [room@server] [autojoin on|off] [nick nickname]", + "---------------------------------------------------------------------------", + "Manage bookmarks.", + NULL } } }, + { "/disco", _cmd_disco, parse_args, 1, 2, NULL, { "/disco command entity", "Service discovery.", @@ -853,6 +865,7 @@ static Autocomplete close_ac; static Autocomplete wins_ac; static Autocomplete roster_ac; static Autocomplete group_ac; +static Autocomplete bookmark_ac; /* * Initialise command autocompleter and history @@ -978,6 +991,11 @@ cmd_init(void) autocomplete_add(who_ac, strdup("unavailable")); autocomplete_add(who_ac, strdup("any")); + bookmark_ac = autocomplete_new(); + autocomplete_add(bookmark_ac, strdup("add")); + autocomplete_add(bookmark_ac, strdup("list")); + autocomplete_add(bookmark_ac, strdup("remove")); + cmd_history_init(); } @@ -1003,6 +1021,7 @@ cmd_close(void) autocomplete_free(wins_ac); autocomplete_free(roster_ac); autocomplete_free(group_ac); + autocomplete_free(bookmark_ac); } // Command autocompletion functions @@ -1072,6 +1091,8 @@ cmd_reset_autocomplete() autocomplete_reset(wins_ac); autocomplete_reset(roster_ac); autocomplete_reset(group_ac); + autocomplete_reset(bookmark_ac); + bookmark_autocomplete_reset(); } // Command execution @@ -1255,6 +1276,13 @@ _cmd_complete_parameters(char *input, int *size) } } + result = autocomplete_param_with_func(input, size, "/join", bookmark_find); + if (result != NULL) { + inp_replace_input(input, result, size); + g_free(result); + return; + } + result = autocomplete_param_with_func(input, size, "/connect", accounts_find_enabled); if (result != NULL) { inp_replace_input(input, result, size); @@ -1276,7 +1304,8 @@ _cmd_complete_parameters(char *input, int *size) autocompleter acs[] = { _who_autocomplete, _sub_autocomplete, _notify_autocomplete, _autoaway_autocomplete, _titlebar_autocomplete, _theme_autocomplete, - _account_autocomplete, _roster_autocomplete, _group_autocomplete }; + _account_autocomplete, _roster_autocomplete, _group_autocomplete, + _bookmark_autocomplete }; for (i = 0; i < ARRAY_SIZE(acs); i++) { result = acs[i](input, size); @@ -2799,6 +2828,73 @@ _cmd_rooms(gchar **args, struct cmd_help_t help) } static gboolean +_cmd_bookmark(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + gchar *cmd = args[0]; + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currenlty connect."); + return TRUE; + } + + /* TODO: /bookmark list room@server */ + + if (cmd == NULL || strcmp(cmd, "list") == 0) { + cons_show_bookmarks(bookmark_get_list()); + } else { + gboolean autojoin = FALSE; + gboolean jid_release = FALSE; + gchar *jid = NULL; + gchar *nick = NULL; + int idx = 1; + + while (args[idx] != NULL) { + gchar *opt = args[idx]; + + if (strcmp(opt, "autojoin") == 0) { + autojoin = TRUE; + } else if (jid == NULL) { + jid = opt; + } else if (nick == NULL) { + nick = opt; + } else { + cons_show("Usage: %s", help.usage); + } + + ++idx; + } + + if (jid == NULL) { + win_type_t win_type = ui_current_win_type(); + + if (win_type == WIN_MUC) { + jid = ui_current_recipient(); + jid_release = TRUE; + nick = muc_get_room_nick(jid); + } else { + cons_show("Usage: %s", help.usage); + return TRUE; + } + } + + if (strcmp(cmd, "add") == 0) { + bookmark_add(jid, nick, autojoin); + } else if (strcmp(cmd, "remove") == 0) { + bookmark_remove(jid, autojoin); + } else { + cons_show("Usage: %s", help.usage); + } + + if (jid_release) { + free(jid); + } + } + + return TRUE; +} + +static gboolean _cmd_disco(gchar **args, struct cmd_help_t help) { jabber_conn_status_t conn_status = jabber_get_connection_status(); @@ -3589,6 +3685,34 @@ _group_autocomplete(char *input, int *size) } static char * +_bookmark_autocomplete(char *input, int *size) +{ + char *result = NULL; + + if (strcmp(input, "/bookmark add ") == 0) { + GString *str = g_string_new(input); + + str = g_string_append(str, "autojoin"); + result = str->str; + g_string_free(str, FALSE); + return result; + } + + result = autocomplete_param_with_func(input, size, "/bookmark list", bookmark_find); + if (result != NULL) { + return result; + } + result = autocomplete_param_with_func(input, size, "/bookmark remove", bookmark_find); + if (result != NULL) { + return result; + } + + result = autocomplete_param_with_ac(input, size, "/bookmark", bookmark_ac); + + return result; +} + +static char * _notify_autocomplete(char *input, int *size) { int i = 0; diff --git a/src/common.c b/src/common.c index 2d1ac3d7..e2ff0171 100644 --- a/src/common.c +++ b/src/common.c @@ -37,9 +37,6 @@ // and page size is at least 4KB #define READ_BUF_SIZE 4088 -// for generating ids -static int unique_id = 0; - struct curl_data_t { char *buffer; @@ -400,10 +397,12 @@ xdg_get_data_home(void) char * get_unique_id(void) { + static unsigned long unique_id; char *result = NULL; - unique_id++; GString *result_str = g_string_new(""); - g_string_printf(result_str, "prof%d", unique_id); + + unique_id++; + g_string_printf(result_str, "prof%lu", unique_id); result = result_str->str; g_string_free(result_str, FALSE); diff --git a/src/ui/console.c b/src/ui/console.c index ee5ad221..50d2b649 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -37,6 +37,7 @@ #include "ui/window.h" #include "ui/ui.h" #include "xmpp/xmpp.h" +#include "xmpp/bookmark.h" #define CONS_WIN_TITLE "_cons" @@ -644,6 +645,34 @@ cons_show_room_list(GSList *rooms, const char * const conference_node) } void +cons_show_bookmarks(const GList *list) +{ + Bookmark *item; + + cons_show(""); + cons_show("Bookmarks:"); + + /* TODO: show status (connected or not) and window number */ + while (list != NULL) { + item = list->data; + + win_print_time(console, '-'); + wprintw(console->win, " %s", item->jid); + if (item->nick != NULL) { + wprintw(console->win, "/%s", item->nick); + } + if (item->autojoin) { + wprintw(console->win, " (autojoin)"); + } + wprintw(console->win, "\n"); + list = g_list_next(list); + } + + ui_console_dirty(); + cons_alert(); +} + +void cons_show_disco_info(const char *jid, GSList *identities, GSList *features) { if (((identities != NULL) && (g_slist_length(identities) > 0)) || diff --git a/src/ui/ui.h b/src/ui/ui.h index d8ac22fa..f0601753 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -184,6 +184,7 @@ void cons_show_software_version(const char * const jid, const char * const version, const char * const os); void cons_show_account_list(gchar **accounts); void cons_show_room_list(GSList *room, const char * const conference_node); +void cons_show_bookmarks(const GList *list); void cons_show_disco_items(GSList *items, const char * const jid); void cons_show_disco_info(const char *from, GSList *identities, GSList *features); void cons_show_room_invite(const char * const invitor, const char * const room, diff --git a/src/xmpp/bookmark.c b/src/xmpp/bookmark.c new file mode 100644 index 00000000..92e6d7e8 --- /dev/null +++ b/src/xmpp/bookmark.c @@ -0,0 +1,245 @@ + +#include <assert.h> +#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" +#include "xmpp/bookmark.h" + +#define BOOKMARK_TIMEOUT 5000 +/* TODO: replace with a preference */ +#define BOOKMARK_AUTOJOIN_MAX 5 + +static int autojoin_count; + +static Autocomplete bookmark_ac; +static GList *bookmark_list; + +static int _bookmark_handle_result(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, void * const userdata); +static int _bookmark_handle_delete(xmpp_conn_t * const conn, + void * const userdata); +static void _bookmark_item_destroy(gpointer item); + +void +bookmark_request(void) +{ + char *id; + xmpp_conn_t *conn = connection_get_conn(); + xmpp_ctx_t *ctx = connection_get_ctx(); + xmpp_stanza_t *iq; + + id = get_unique_id(); + if (!id) { + return; + } + + autojoin_count = 0; + 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; + } + + 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); + xmpp_stanza_set_id(iq, id); + xmpp_send(conn, iq); + xmpp_stanza_release(iq); +} + +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); + } +} + +static int +_bookmark_handle_result(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, void * const userdata) +{ + xmpp_ctx_t *ctx = connection_get_ctx(); + char *id = (char *)userdata; + xmpp_stanza_t *ptr; + xmpp_stanza_t *nick; + char *name; + char *jid; + char *autojoin; + gboolean autojoin_val; + Jid *my_jid; + Bookmark *item; + + xmpp_timed_handler_delete(conn, _bookmark_handle_delete); + g_free(id); + + 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; + } + + if (bookmark_ac == NULL) { + bookmark_ac = autocomplete_new(); + } + my_jid = jid_create(jabber_get_fulljid()); + + ptr = xmpp_stanza_get_children(ptr); + while (ptr) { + name = xmpp_stanza_get_name(ptr); + if (!name || strcmp(name, STANZA_NAME_CONFERENCE) != 0) { + ptr = xmpp_stanza_get_next(ptr); + continue; + } + jid = xmpp_stanza_get_attribute(ptr, STANZA_ATTR_JID); + if (!jid) { + ptr = xmpp_stanza_get_next(ptr); + continue; + } + + log_debug("Handle bookmark for %s", jid); + + 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); + } + } + + autojoin = xmpp_stanza_get_attribute(ptr, "autojoin"); + if (autojoin && (strcmp(autojoin, "1") == 0 || strcmp(autojoin, "true") == 0)) { + 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; + } + + 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); + } + jid_destroy(room_jid); + } else { + log_debug("Rejected autojoin %s (maximum has been reached)", jid); + } + } + + ptr = xmpp_stanza_get_next(ptr); + } + + jid_destroy(my_jid); + + 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; +} + +static void +_bookmark_item_destroy(gpointer item) +{ + Bookmark *p = (Bookmark *)item; + + if (p == NULL) { + return; + } + + free(p->jid); + free(p->nick); + free(p); +} diff --git a/src/xmpp/bookmark.h b/src/xmpp/bookmark.h new file mode 100644 index 00000000..e15b6eab --- /dev/null +++ b/src/xmpp/bookmark.h @@ -0,0 +1,22 @@ + +#ifndef BOOKMARK_H +#define BOOKMARK_H + +#include <glib.h> + +struct bookmark_t { + char *jid; + char *nick; + gboolean autojoin; +}; + +typedef struct bookmark_t Bookmark; + +void bookmark_request(void); +void bookmark_add(const char *jid, const char *nick, gboolean autojoin); +void bookmark_remove(const char *jid, gboolean autojoin); +const GList *bookmark_get_list(void); +char *bookmark_find(char *search_str); +void bookmark_autocomplete_reset(void); + +#endif diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 85ddfa45..5040aa91 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -33,6 +33,7 @@ #include "log.h" #include "muc.h" #include "profanity.h" +#include "xmpp/bookmark.h" #include "xmpp/capabilities.h" #include "xmpp/connection.h" #include "xmpp/iq.h" @@ -494,6 +495,7 @@ _connection_handler(xmpp_conn_t * const conn, } roster_request(); + bookmark_request(); jabber_conn.conn_status = JABBER_CONNECTED; if (prefs_get_reconnect() != 0) { diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 2e2f9858..4d7d1f9d 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -33,6 +33,63 @@ static int _field_compare(FormField *f1, FormField *f2); +#if 0 +xmpp_stanza_t * +stanza_create_storage_bookmarks(xmpp_ctx_t *ctx) +{ + xmpp_stanza_t *iq, *pubsub, *items; + + /* TODO: check pointers for NULL */ + iq = xmpp_stanza_new(ctx); + pubsub = xmpp_stanza_new(ctx); + items = xmpp_stanza_new(ctx); + + xmpp_stanza_set_name(iq, STANZA_NAME_IQ); + xmpp_stanza_set_type(iq, STANZA_TYPE_GET); + + xmpp_stanza_set_name(pubsub, STANZA_NAME_PUBSUB); + xmpp_stanza_set_ns(pubsub, STANZA_NS_PUBSUB); + + xmpp_stanza_set_name(items, STANZA_NAME_ITEMS); + xmpp_stanza_set_attribute(items, "node", "storage:bookmarks"); + + xmpp_stanza_add_child(pubsub, items); + xmpp_stanza_add_child(iq, pubsub); + xmpp_stanza_release(items); + xmpp_stanza_release(pubsub); + + return iq; +} +#endif + +xmpp_stanza_t * +stanza_create_storage_bookmarks(xmpp_ctx_t *ctx) +{ + xmpp_stanza_t *iq, *query, *storage; + + /* TODO: check pointers for NULL */ + iq = xmpp_stanza_new(ctx); + query = xmpp_stanza_new(ctx); + storage = xmpp_stanza_new(ctx); + + xmpp_stanza_set_name(iq, STANZA_NAME_IQ); + xmpp_stanza_set_type(iq, STANZA_TYPE_GET); + xmpp_stanza_set_ns(iq, "jabber:client"); + + xmpp_stanza_set_name(query, STANZA_NAME_QUERY); + xmpp_stanza_set_ns(query, "jabber:iq:private"); + + xmpp_stanza_set_name(storage, STANZA_NAME_STORAGE); + xmpp_stanza_set_ns(storage, "storage:bookmarks"); + + xmpp_stanza_add_child(query, storage); + xmpp_stanza_add_child(iq, query); + xmpp_stanza_release(storage); + xmpp_stanza_release(query); + + return iq; +} + xmpp_stanza_t * stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient, const char * const state) diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index ad777f24..108b0806 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -52,6 +52,9 @@ #define STANZA_NAME_INVITE "invite" #define STANZA_NAME_REASON "reason" #define STANZA_NAME_GROUP "group" +#define STANZA_NAME_PUBSUB "pubsub" +#define STANZA_NAME_STORAGE "storage" +#define STANZA_NAME_CONFERENCE "conference" #define STANZA_TYPE_CHAT "chat" #define STANZA_TYPE_GROUPCHAT "groupchat" @@ -100,6 +103,7 @@ #define STANZA_NS_VERSION "jabber:iq:version" #define STANZA_NS_CONFERENCE "jabber:x:conference" #define STANZA_NS_CAPTCHA "urn:xmpp:captcha" +#define STANZA_NS_PUBSUB "http://jabber.org/protocol/pubsub" #define STANZA_DATAFORM_SOFTWARE "urn:xmpp:dataforms:softwareinfo" @@ -113,6 +117,8 @@ typedef struct data_form_t { GSList *fields; } DataForm; +xmpp_stanza_t* stanza_create_storage_bookmarks(xmpp_ctx_t *ctx); + xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient, const char * const state); |