From df14a46d4864eecbb0344eb05dcb929f72954871 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 12 Sep 2013 23:30:35 +0100 Subject: [PATCH] Allow users to set default muc service and nickname per account See #238 Conflicts: src/config/accounts.c --- src/command/command.c | 39 +++++++++++------- src/config/accounts.c | 96 ++++++++++++++++++++++++++++++++++++++++++- src/config/accounts.h | 4 ++ src/ui/console.c | 6 +++ 4 files changed, 129 insertions(+), 16 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index ef00fae8..711f94e9 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -314,8 +314,8 @@ static struct cmd_t command_defs[] = "--------------------------", "Join a chat room at the conference server.", "If nick is specified you will join with this nickname.", - "Otherwise the 'localpart' of your JID (before the @) will be used.", - "If no server is supplied, a default of 'conference.' will be used.", + "Otherwise the account preference 'muc.nick' will be used which is the localpart of your JID (before the @).", + "If no server is supplied, the account preference 'muc.service' is used, which is 'conference.' by default.", "If the room doesn't exist, and the server allows it, a new one will be created.", "", "Example : /join jdev@conference.jabber.org", @@ -364,8 +364,7 @@ static struct cmd_t command_defs[] = { "/rooms [conference-service]", "---------------------------", "List the chat rooms available at the specified conference service", - "If no argument is supplied, the domainpart of the current logged in JID is used,", - "with a prefix of 'conference'.", + "If no argument is supplied, the account preference 'muc.service' is used, which is 'conference.' by default.", "", "Example : /rooms conference.jabber.org", "Example : /rooms (if logged in as me@server.org, is equivalent to /rooms conference.server.org)", @@ -747,11 +746,15 @@ static struct cmd_t command_defs[] = "online|chat|away", "|xa|dnd : Priority for the specified presence.", "resource : The resource to be used.", + "muc : The default MUC chat service to use.", + "nick : The default nickname to use when joining chat rooms.", "", "Example : /account add work", " : /account set work jid myuser@mycompany.com", " : /account set work server talk.google.com", " : /account set work resource desktop", + " : /account set work muc chatservice.mycompany.com", + " : /account set work nick dennis", " : /account set work status dnd", " : /account set work dnd -1", " : /account set work online 10", @@ -1495,6 +1498,14 @@ _cmd_account(gchar **args, struct cmd_help_t help) accounts_set_resource(account_name, value); cons_show("Updated resource for account %s: %s", account_name, value); cons_show(""); + } else if (strcmp(property, "muc") == 0) { + accounts_set_muc_service(account_name, value); + cons_show("Updated muc service for account %s: %s", account_name, value); + cons_show(""); + } else if (strcmp(property, "nick") == 0) { + accounts_set_muc_nick(account_name, value); + cons_show("Updated muc nick for account %s: %s", account_name, value); + cons_show(""); } else if (strcmp(property, "status") == 0) { if (!valid_resource_presence_string(value) && (strcmp(value, "last") != 0)) { cons_show("Invalid status: %s", value); @@ -2735,11 +2746,12 @@ _cmd_join(gchar **args, struct cmd_help_t help) if (room_arg->localpart != NULL) { room = args[0]; - // server not supplied (room), guess conference. + // server not supplied (room), use account preference } else { + ProfAccount *account = accounts_get_account(jabber_get_account_name()); g_string_append(room_str, args[0]); - g_string_append(room_str, "@conference."); - g_string_append(room_str, my_jid->domainpart); + g_string_append(room_str, "@"); + g_string_append(room_str, account->muc_service); room = room_str->str; } @@ -2747,9 +2759,10 @@ _cmd_join(gchar **args, struct cmd_help_t help) if (num_args == 2) { nick = args[1]; - // use localpart for nick + // otherwise use account preference } else { - nick = my_jid->localpart; + ProfAccount *account = accounts_get_account(jabber_get_account_name()); + nick = account->muc_nick; } Jid *room_jid = jid_create_from_bare_and_resource(room, nick); @@ -2836,12 +2849,8 @@ _cmd_rooms(gchar **args, struct cmd_help_t help) } if (args[0] == NULL) { - Jid *jid = jid_create(jabber_get_fulljid()); - GString *conference_node = g_string_new("conference."); - g_string_append(conference_node, jid->domainpart); - jid_destroy(jid); - iq_room_list_request(conference_node->str); - g_string_free(conference_node, TRUE); + ProfAccount *account = accounts_get_account(jabber_get_account_name()); + iq_room_list_request(account->muc_service); } else { iq_room_list_request(args[0]); } diff --git a/src/config/accounts.c b/src/config/accounts.c index 3e3daec0..b903d6be 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -39,7 +39,15 @@ static GKeyFile *accounts; static Autocomplete all_ac; static Autocomplete enabled_ac; -static gchar *string_keys[] = {"jid", "server", "resource", "presence.last", "presence.login"}; +static gchar *string_keys[] = { + "jid", + "server", + "resource", + "presence.last", + "presence.login", + "muc.service", + "muc.nick" +}; static void _fix_legacy_accounts(const char * const account_name); static void _save_accounts(void); @@ -129,6 +137,19 @@ accounts_add(const char *account_name, const char *altdomain) if (altdomain != NULL) { g_key_file_set_string(accounts, account_name, "server", altdomain); } + + Jid *jidp = jid_create(barejid); + GString *muc_service = g_string_new("conference."); + g_string_append(muc_service, jidp->domainpart); + g_key_file_set_string(accounts, account_name, "muc.service", muc_service->str); + g_string_free(muc_service, TRUE); + if (jidp->localpart == NULL) { + g_key_file_set_string(accounts, account_name, "muc.nick", jidp->domainpart); + } else { + g_key_file_set_string(accounts, account_name, "muc.nick", jidp->localpart); + } + jid_destroy(jidp); + g_key_file_set_string(accounts, account_name, "presence.last", "online"); g_key_file_set_string(accounts, account_name, "presence.login", "online"); g_key_file_set_integer(accounts, account_name, "priority.online", 0); @@ -220,6 +241,27 @@ accounts_get_account(const char * const name) account->priority_xa = g_key_file_get_integer(accounts, name, "priority.xa", NULL); account->priority_dnd = g_key_file_get_integer(accounts, name, "priority.dnd", NULL); + gchar *muc_service = g_key_file_get_string(accounts, name, "muc.service", NULL); + if (muc_service == NULL) { + GString *g_muc_service = g_string_new("conference."); + Jid *jidp = jid_create(account->jid); + g_string_append(g_muc_service, jidp->domainpart); + account->muc_service = strdup(g_muc_service->str); + g_string_free(g_muc_service, TRUE); + jid_destroy(jidp); + } else { + account->muc_service = strdup(muc_service); + } + + gchar *muc_nick = g_key_file_get_string(accounts, name, "muc.nick", NULL); + if (muc_nick == NULL) { + Jid *jidp = jid_create(account->jid); + account->muc_nick = strdup(jidp->localpart); + jid_destroy(jidp); + } else { + account->muc_nick = strdup(muc_nick); + } + // get room history account->room_history = NULL; gsize history_size = 0; @@ -250,6 +292,8 @@ accounts_free_account(ProfAccount *account) free(account->server); free(account->last_presence); free(account->login_presence); + free(account->muc_service); + free(account->muc_nick); free(account); } } @@ -344,6 +388,17 @@ accounts_set_jid(const char * const account_name, const char * const value) if (jid->resourcepart != NULL) { g_key_file_set_string(accounts, account_name, "resource", jid->resourcepart); } + + GString *muc_service = g_string_new("conference."); + g_string_append(muc_service, jid->domainpart); + g_key_file_set_string(accounts, account_name, "muc.service", muc_service->str); + g_string_free(muc_service, TRUE); + if (jid->localpart == NULL) { + g_key_file_set_string(accounts, account_name, "muc.nick", jid->domainpart); + } else { + g_key_file_set_string(accounts, account_name, "muc.nick", jid->localpart); + } + _save_accounts(); } } @@ -367,6 +422,24 @@ accounts_set_resource(const char * const account_name, const char * const value) } } +void +accounts_set_muc_service(const char * const account_name, const char * const value) +{ + if (accounts_account_exists(account_name)) { + g_key_file_set_string(accounts, account_name, "muc.service", value); + _save_accounts(); + } +} + +void +accounts_set_muc_nick(const char * const account_name, const char * const value) +{ + if (accounts_account_exists(account_name)) { + g_key_file_set_string(accounts, account_name, "muc.nick", value); + _save_accounts(); + } +} + void accounts_set_priority_online(const char * const account_name, const gint value) { @@ -558,6 +631,27 @@ _fix_legacy_accounts(const char * const account_name) _save_accounts(); } + // acounts with no muc service or nick + if (!g_key_file_has_key(accounts, account_name, "muc.service", NULL)) { + gchar *account_jid = g_key_file_get_string(accounts, account_name, "jid", NULL); + Jid *jidp = jid_create(account_jid); + GString *muc_service = g_string_new("conference."); + g_string_append(muc_service, jidp->domainpart); + g_key_file_set_string(accounts, account_name, "muc.service", muc_service->str); + g_string_free(muc_service, TRUE); + jid_destroy(jidp); + } + if (!g_key_file_has_key(accounts, account_name, "muc.nick", NULL)) { + gchar *account_jid = g_key_file_get_string(accounts, account_name, "jid", NULL); + Jid *jidp = jid_create(account_jid); + if (jidp->localpart == NULL) { + g_key_file_set_string(accounts, account_name, "muc.nick", jidp->domainpart); + } else { + g_key_file_set_string(accounts, account_name, "muc.nick", jidp->localpart); + } + jid_destroy(jidp); + } + jid_destroy(jid); } diff --git a/src/config/accounts.h b/src/config/accounts.h index d74e8fe5..4c74a523 100644 --- a/src/config/accounts.h +++ b/src/config/accounts.h @@ -37,6 +37,8 @@ typedef struct prof_account_t { gint priority_away; gint priority_xa; gint priority_dnd; + gchar *muc_service; + gchar *muc_nick; gboolean enabled; GSList *room_history; } ProfAccount; @@ -60,6 +62,8 @@ gboolean accounts_account_exists(const char * const account_name); void accounts_set_jid(const char * const account_name, const char * const value); void accounts_set_server(const char * const account_name, const char * const value); void accounts_set_resource(const char * const account_name, const char * const value); +void accounts_set_muc_service(const char * const account_name, const char * const value); +void accounts_set_muc_nick(const char * const account_name, const char * const value); void accounts_set_last_presence(const char * const account_name, const char * const value); void accounts_set_login_presence(const char * const account_name, const char * const value); resource_presence_t accounts_get_login_presence(const char * const account_name); diff --git a/src/ui/console.c b/src/ui/console.c index 92671a90..fe15d73f 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -789,6 +789,12 @@ cons_show_account(ProfAccount *account) if (account->server != NULL) { cons_show ("server : %s", account->server); } + if (account->muc_service != NULL) { + cons_show ("muc service : %s", account->muc_service); + } + if (account->muc_nick != NULL) { + cons_show ("muc nick : %s", account->muc_nick); + } if (account->last_presence != NULL) { cons_show ("Last presence : %s", account->last_presence); }