2012-12-01 20:29:42 -05:00
|
|
|
/*
|
|
|
|
* accounts.c
|
|
|
|
*
|
2013-01-10 21:05:29 -05:00
|
|
|
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
|
2012-12-01 20:29:42 -05:00
|
|
|
*
|
|
|
|
* This file is part of Profanity.
|
|
|
|
*
|
|
|
|
* Profanity is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Profanity is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
2012-12-09 17:14:38 -05:00
|
|
|
|
|
|
|
#include "accounts.h"
|
2013-02-02 15:55:58 -05:00
|
|
|
|
2013-01-26 17:25:00 -05:00
|
|
|
#include "common.h"
|
2013-01-26 17:19:20 -05:00
|
|
|
#include "jid.h"
|
2012-12-01 20:29:42 -05:00
|
|
|
#include "log.h"
|
2013-02-02 16:43:59 -05:00
|
|
|
#include "tools/autocomplete.h"
|
2013-02-02 14:47:41 -05:00
|
|
|
#include "xmpp/xmpp.h"
|
2012-12-01 20:29:42 -05:00
|
|
|
|
|
|
|
static gchar *accounts_loc;
|
|
|
|
static GKeyFile *accounts;
|
|
|
|
|
2013-01-24 20:11:49 -05:00
|
|
|
static Autocomplete all_ac;
|
|
|
|
static Autocomplete enabled_ac;
|
2012-12-01 20:29:42 -05:00
|
|
|
|
2013-09-12 18:30:35 -04:00
|
|
|
static gchar *string_keys[] = {
|
|
|
|
"jid",
|
|
|
|
"server",
|
|
|
|
"resource",
|
|
|
|
"presence.last",
|
|
|
|
"presence.login",
|
|
|
|
"muc.service",
|
|
|
|
"muc.nick"
|
|
|
|
};
|
2013-01-30 20:37:42 -05:00
|
|
|
|
2013-01-27 13:47:33 -05:00
|
|
|
static void _fix_legacy_accounts(const char * const account_name);
|
2012-12-01 20:29:42 -05:00
|
|
|
static void _save_accounts(void);
|
2013-02-02 17:20:19 -05:00
|
|
|
static gchar * _get_accounts_file(void);
|
2012-12-01 20:29:42 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
accounts_load(void)
|
|
|
|
{
|
|
|
|
log_info("Loading accounts");
|
2013-01-24 20:11:49 -05:00
|
|
|
all_ac = autocomplete_new();
|
|
|
|
enabled_ac = autocomplete_new();
|
2013-02-02 17:20:19 -05:00
|
|
|
accounts_loc = _get_accounts_file();
|
2012-12-01 20:29:42 -05:00
|
|
|
|
|
|
|
accounts = g_key_file_new();
|
|
|
|
g_key_file_load_from_file(accounts, accounts_loc, G_KEY_FILE_KEEP_COMMENTS,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
// create the logins searchable list for autocompletion
|
2013-01-27 12:59:37 -05:00
|
|
|
gsize naccounts;
|
|
|
|
gchar **account_names =
|
|
|
|
g_key_file_get_groups(accounts, &naccounts);
|
2012-12-01 20:29:42 -05:00
|
|
|
|
|
|
|
gsize i;
|
2013-01-27 12:59:37 -05:00
|
|
|
for (i = 0; i < naccounts; i++) {
|
2013-08-25 20:29:50 -04:00
|
|
|
autocomplete_add(all_ac, account_names[i]);
|
2013-01-27 12:59:37 -05:00
|
|
|
if (g_key_file_get_boolean(accounts, account_names[i], "enabled", NULL)) {
|
2013-08-25 20:29:50 -04:00
|
|
|
autocomplete_add(enabled_ac, account_names[i]);
|
2012-12-09 19:53:57 -05:00
|
|
|
}
|
2013-01-26 19:02:28 -05:00
|
|
|
|
2013-01-27 13:47:33 -05:00
|
|
|
_fix_legacy_accounts(account_names[i]);
|
2012-12-01 20:29:42 -05:00
|
|
|
}
|
|
|
|
|
2013-08-25 07:54:34 -04:00
|
|
|
g_strfreev(account_names);
|
2012-12-01 20:29:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_close(void)
|
|
|
|
{
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_free(all_ac);
|
|
|
|
autocomplete_free(enabled_ac);
|
2012-12-01 20:29:42 -05:00
|
|
|
g_key_file_free(accounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2012-12-09 19:53:57 -05:00
|
|
|
accounts_find_enabled(char *prefix)
|
|
|
|
{
|
2013-01-24 20:11:49 -05:00
|
|
|
return autocomplete_complete(enabled_ac, prefix);
|
2012-12-09 19:53:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
accounts_find_all(char *prefix)
|
2012-12-01 20:29:42 -05:00
|
|
|
{
|
2013-01-24 20:11:49 -05:00
|
|
|
return autocomplete_complete(all_ac, prefix);
|
2012-12-01 20:29:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-09 19:53:57 -05:00
|
|
|
accounts_reset_all_search(void)
|
2012-12-01 20:29:42 -05:00
|
|
|
{
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_reset(all_ac);
|
2012-12-09 19:53:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_reset_enabled_search(void)
|
|
|
|
{
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_reset(enabled_ac);
|
2012-12-01 20:29:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-27 13:10:30 -05:00
|
|
|
accounts_add(const char *account_name, const char *altdomain)
|
2012-12-01 20:29:42 -05:00
|
|
|
{
|
2013-01-27 13:47:33 -05:00
|
|
|
// set account name and resource
|
2013-01-26 17:19:20 -05:00
|
|
|
const char *barejid = account_name;
|
2013-01-27 13:10:30 -05:00
|
|
|
const char *resource = "profanity";
|
2013-01-26 17:19:20 -05:00
|
|
|
Jid *jid = jid_create(account_name);
|
|
|
|
if (jid != NULL) {
|
|
|
|
barejid = jid->barejid;
|
2013-01-27 13:47:33 -05:00
|
|
|
if (jid->resourcepart != NULL) {
|
|
|
|
resource = jid->resourcepart;
|
|
|
|
}
|
2013-01-26 17:19:20 -05:00
|
|
|
}
|
|
|
|
|
2012-12-09 15:18:38 -05:00
|
|
|
// doesn't yet exist
|
2013-01-26 17:19:20 -05:00
|
|
|
if (!g_key_file_has_group(accounts, account_name)) {
|
|
|
|
g_key_file_set_boolean(accounts, account_name, "enabled", TRUE);
|
|
|
|
g_key_file_set_string(accounts, account_name, "jid", barejid);
|
2013-01-27 13:10:30 -05:00
|
|
|
g_key_file_set_string(accounts, account_name, "resource", resource);
|
2012-12-06 19:10:00 -05:00
|
|
|
if (altdomain != NULL) {
|
2013-01-26 17:19:20 -05:00
|
|
|
g_key_file_set_string(accounts, account_name, "server", altdomain);
|
2012-12-06 19:10:00 -05:00
|
|
|
}
|
2013-09-12 18:30:35 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-01-30 20:18:05 -05:00
|
|
|
g_key_file_set_string(accounts, account_name, "presence.last", "online");
|
|
|
|
g_key_file_set_string(accounts, account_name, "presence.login", "online");
|
2013-01-30 21:02:16 -05:00
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.online", 0);
|
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.chat", 0);
|
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.away", 0);
|
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.xa", 0);
|
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.dnd", 0);
|
2012-12-06 19:10:00 -05:00
|
|
|
|
2012-12-01 20:29:42 -05:00
|
|
|
_save_accounts();
|
2013-08-25 20:29:50 -04:00
|
|
|
autocomplete_add(all_ac, account_name);
|
|
|
|
autocomplete_add(enabled_ac, account_name);
|
2012-12-01 20:29:42 -05:00
|
|
|
}
|
2012-12-09 15:18:38 -05:00
|
|
|
|
2013-01-26 17:19:20 -05:00
|
|
|
jid_destroy(jid);
|
2012-12-09 15:18:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
gchar**
|
|
|
|
accounts_get_list(void)
|
|
|
|
{
|
|
|
|
return g_key_file_get_groups(accounts, NULL);
|
2012-12-01 20:29:42 -05:00
|
|
|
}
|
|
|
|
|
2012-12-09 17:14:38 -05:00
|
|
|
ProfAccount*
|
|
|
|
accounts_get_account(const char * const name)
|
|
|
|
{
|
|
|
|
if (!g_key_file_has_group(accounts, name)) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
ProfAccount *account = malloc(sizeof(ProfAccount));
|
|
|
|
account->name = strdup(name);
|
2013-01-30 19:01:38 -05:00
|
|
|
|
2012-12-09 17:14:38 -05:00
|
|
|
gchar *jid = g_key_file_get_string(accounts, name, "jid", NULL);
|
|
|
|
if (jid != NULL) {
|
|
|
|
account->jid = strdup(jid);
|
2013-08-25 07:54:34 -04:00
|
|
|
g_free(jid);
|
2012-12-09 17:14:38 -05:00
|
|
|
} else {
|
|
|
|
account->jid = strdup(name);
|
|
|
|
g_key_file_set_string(accounts, name, "jid", name);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
2013-01-30 19:01:38 -05:00
|
|
|
|
2012-12-09 17:14:38 -05:00
|
|
|
account->enabled = g_key_file_get_boolean(accounts, name, "enabled", NULL);
|
2013-01-30 19:01:38 -05:00
|
|
|
|
2012-12-09 17:14:38 -05:00
|
|
|
gchar *server = g_key_file_get_string(accounts, name, "server", NULL);
|
|
|
|
if (server != NULL) {
|
|
|
|
account->server = strdup(server);
|
2013-08-25 07:54:34 -04:00
|
|
|
g_free(server);
|
2012-12-09 17:14:38 -05:00
|
|
|
} else {
|
|
|
|
account->server = NULL;
|
|
|
|
}
|
2013-01-30 19:01:38 -05:00
|
|
|
|
2013-01-26 17:25:00 -05:00
|
|
|
gchar *resource = g_key_file_get_string(accounts, name, "resource", NULL);
|
|
|
|
if (resource != NULL) {
|
|
|
|
account->resource = strdup(resource);
|
2013-08-25 07:54:34 -04:00
|
|
|
g_free(resource);
|
2013-01-26 17:25:00 -05:00
|
|
|
} else {
|
|
|
|
account->resource = NULL;
|
|
|
|
}
|
2012-12-09 17:14:38 -05:00
|
|
|
|
2013-01-30 19:01:38 -05:00
|
|
|
gchar *presence = g_key_file_get_string(accounts, name, "presence.last", NULL);
|
2013-02-10 12:13:19 -05:00
|
|
|
if (presence == NULL || (!valid_resource_presence_string(presence))) {
|
2013-01-30 19:01:38 -05:00
|
|
|
account->last_presence = strdup("online");
|
|
|
|
} else {
|
|
|
|
account->last_presence = strdup(presence);
|
|
|
|
}
|
|
|
|
|
2013-08-25 07:54:34 -04:00
|
|
|
if (presence != NULL) {
|
|
|
|
g_free(presence);
|
|
|
|
}
|
|
|
|
|
2013-01-30 19:01:38 -05:00
|
|
|
presence = g_key_file_get_string(accounts, name, "presence.login", NULL);
|
|
|
|
if (presence == NULL) {
|
|
|
|
account->login_presence = strdup("online");
|
|
|
|
} else if (strcmp(presence, "last") == 0) {
|
|
|
|
account->login_presence = strdup("last");
|
2013-02-10 12:13:19 -05:00
|
|
|
} else if (!valid_resource_presence_string(presence)) {
|
2013-01-30 19:01:38 -05:00
|
|
|
account->login_presence = strdup("online");
|
|
|
|
} else {
|
|
|
|
account->login_presence = strdup(presence);
|
|
|
|
}
|
|
|
|
|
2013-08-25 07:54:34 -04:00
|
|
|
if (presence != NULL) {
|
|
|
|
g_free(presence);
|
|
|
|
}
|
|
|
|
|
2013-01-31 17:48:21 -05:00
|
|
|
account->priority_online = g_key_file_get_integer(accounts, name, "priority.online", NULL);
|
|
|
|
account->priority_chat = g_key_file_get_integer(accounts, name, "priority.chat", NULL);
|
|
|
|
account->priority_away = g_key_file_get_integer(accounts, name, "priority.away", NULL);
|
|
|
|
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);
|
|
|
|
|
2013-09-12 18:30:35 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-05-23 17:29:41 -04:00
|
|
|
// get room history
|
|
|
|
account->room_history = NULL;
|
|
|
|
gsize history_size = 0;
|
|
|
|
gchar **room_history_values = g_key_file_get_string_list(accounts, name,
|
|
|
|
"rooms.history", &history_size, NULL);
|
|
|
|
|
|
|
|
if (room_history_values != NULL) {
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < history_size; i++) {
|
|
|
|
account->room_history = g_slist_append(account->room_history,
|
|
|
|
strdup(room_history_values[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(room_history_values);
|
|
|
|
}
|
|
|
|
|
2012-12-09 17:14:38 -05:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_free_account(ProfAccount *account)
|
|
|
|
{
|
|
|
|
if (account != NULL) {
|
2013-08-03 07:27:07 -04:00
|
|
|
free(account->name);
|
|
|
|
free(account->jid);
|
|
|
|
free(account->resource);
|
|
|
|
free(account->server);
|
|
|
|
free(account->last_presence);
|
|
|
|
free(account->login_presence);
|
2013-09-12 18:30:35 -04:00
|
|
|
free(account->muc_service);
|
|
|
|
free(account->muc_nick);
|
2013-08-03 07:27:07 -04:00
|
|
|
free(account);
|
2012-12-09 17:14:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 19:08:03 -05:00
|
|
|
gboolean
|
|
|
|
accounts_enable(const char * const name)
|
|
|
|
{
|
|
|
|
if (g_key_file_has_group(accounts, name)) {
|
|
|
|
g_key_file_set_boolean(accounts, name, "enabled", TRUE);
|
|
|
|
_save_accounts();
|
2013-08-25 20:29:50 -04:00
|
|
|
autocomplete_add(enabled_ac, name);
|
2012-12-09 19:08:03 -05:00
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
accounts_disable(const char * const name)
|
|
|
|
{
|
|
|
|
if (g_key_file_has_group(accounts, name)) {
|
|
|
|
g_key_file_set_boolean(accounts, name, "enabled", FALSE);
|
|
|
|
_save_accounts();
|
2013-08-25 20:29:50 -04:00
|
|
|
autocomplete_remove(enabled_ac, name);
|
2012-12-09 19:08:03 -05:00
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
accounts_rename(const char * const account_name, const char * const new_name)
|
|
|
|
{
|
|
|
|
if (g_key_file_has_group(accounts, new_name)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_key_file_has_group(accounts, account_name)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_key_file_set_boolean(accounts, new_name, "enabled",
|
|
|
|
g_key_file_get_boolean(accounts, account_name, "enabled", NULL));
|
|
|
|
|
2013-01-30 21:02:16 -05:00
|
|
|
g_key_file_set_integer(accounts, new_name, "priority.online",
|
|
|
|
g_key_file_get_boolean(accounts, account_name, "priority.online", NULL));
|
|
|
|
g_key_file_set_integer(accounts, new_name, "priority.chat",
|
|
|
|
g_key_file_get_boolean(accounts, account_name, "priority.chat", NULL));
|
|
|
|
g_key_file_set_integer(accounts, new_name, "priority.away",
|
|
|
|
g_key_file_get_boolean(accounts, account_name, "priority.away", NULL));
|
|
|
|
g_key_file_set_integer(accounts, new_name, "priority.xa",
|
|
|
|
g_key_file_get_boolean(accounts, account_name, "priority.xa", NULL));
|
|
|
|
g_key_file_set_integer(accounts, new_name, "priority.dnd",
|
|
|
|
g_key_file_get_boolean(accounts, account_name, "priority.dnd", NULL));
|
|
|
|
|
2013-01-30 20:37:42 -05:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(string_keys); i++) {
|
|
|
|
char *value = g_key_file_get_string(accounts, account_name, string_keys[i], NULL);
|
|
|
|
if (value != NULL) {
|
|
|
|
g_key_file_set_string(accounts, new_name, string_keys[i], value);
|
2013-08-25 07:54:34 -04:00
|
|
|
g_free(value);
|
2013-01-30 20:37:42 -05:00
|
|
|
}
|
2013-01-26 17:25:00 -05:00
|
|
|
}
|
|
|
|
|
2012-12-09 19:08:03 -05:00
|
|
|
g_key_file_remove_group(accounts, account_name, NULL);
|
|
|
|
_save_accounts();
|
|
|
|
|
2013-08-25 20:29:50 -04:00
|
|
|
autocomplete_remove(all_ac, account_name);
|
|
|
|
autocomplete_add(all_ac, new_name);
|
2012-12-09 19:53:57 -05:00
|
|
|
if (g_key_file_get_boolean(accounts, new_name, "enabled", NULL)) {
|
2013-08-25 20:29:50 -04:00
|
|
|
autocomplete_remove(enabled_ac, account_name);
|
|
|
|
autocomplete_add(enabled_ac, new_name);
|
2012-12-09 19:53:57 -05:00
|
|
|
}
|
2012-12-09 19:08:03 -05:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-12-09 19:23:55 -05:00
|
|
|
gboolean
|
|
|
|
accounts_account_exists(const char * const account_name)
|
|
|
|
{
|
|
|
|
return g_key_file_has_group(accounts, account_name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_set_jid(const char * const account_name, const char * const value)
|
|
|
|
{
|
2013-01-27 15:23:42 -05:00
|
|
|
Jid *jid = jid_create(value);
|
|
|
|
if (jid != NULL) {
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "jid", jid->barejid);
|
|
|
|
if (jid->resourcepart != NULL) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "resource", jid->resourcepart);
|
|
|
|
}
|
2013-09-12 18:30:35 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-01-27 15:23:42 -05:00
|
|
|
_save_accounts();
|
|
|
|
}
|
2012-12-09 19:23:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_set_server(const char * const account_name, const char * const value)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "server", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-27 15:34:56 -05:00
|
|
|
void
|
|
|
|
accounts_set_resource(const char * const account_name, const char * const value)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "resource", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 18:30:35 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 20:50:37 -05:00
|
|
|
void
|
|
|
|
accounts_set_priority_online(const char * const account_name, const gint value)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.online", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_set_priority_chat(const char * const account_name, const gint value)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.chat", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_set_priority_away(const char * const account_name, const gint value)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.away", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_set_priority_xa(const char * const account_name, const gint value)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.xa", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_set_priority_dnd(const char * const account_name, const gint value)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_integer(accounts, account_name, "priority.dnd", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 17:48:21 -05:00
|
|
|
void
|
|
|
|
accounts_set_priority_all(const char * const account_name, const gint value)
|
2013-01-30 20:50:37 -05:00
|
|
|
{
|
2013-01-31 17:48:21 -05:00
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
accounts_set_priority_online(account_name, value);
|
|
|
|
accounts_set_priority_chat(account_name, value);
|
|
|
|
accounts_set_priority_away(account_name, value);
|
|
|
|
accounts_set_priority_xa(account_name, value);
|
|
|
|
accounts_set_priority_dnd(account_name, value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
2013-01-30 20:50:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
2013-01-31 17:48:21 -05:00
|
|
|
accounts_get_priority_for_presence_type(const char * const account_name,
|
2013-02-10 12:13:19 -05:00
|
|
|
resource_presence_t presence_type)
|
2013-01-30 20:50:37 -05:00
|
|
|
{
|
2013-01-31 17:48:21 -05:00
|
|
|
gint result;
|
|
|
|
|
|
|
|
switch (presence_type)
|
|
|
|
{
|
2013-02-10 12:13:19 -05:00
|
|
|
case (RESOURCE_ONLINE):
|
2013-01-31 17:48:21 -05:00
|
|
|
result = g_key_file_get_integer(accounts, account_name, "priority.online", NULL);
|
|
|
|
break;
|
2013-02-10 12:13:19 -05:00
|
|
|
case (RESOURCE_CHAT):
|
2013-01-31 17:48:21 -05:00
|
|
|
result = g_key_file_get_integer(accounts, account_name, "priority.chat", NULL);
|
|
|
|
break;
|
2013-02-10 12:13:19 -05:00
|
|
|
case (RESOURCE_AWAY):
|
2013-01-31 17:48:21 -05:00
|
|
|
result = g_key_file_get_integer(accounts, account_name, "priority.away", NULL);
|
|
|
|
break;
|
2013-02-10 12:13:19 -05:00
|
|
|
case (RESOURCE_XA):
|
2013-01-31 17:48:21 -05:00
|
|
|
result = g_key_file_get_integer(accounts, account_name, "priority.xa", NULL);
|
|
|
|
break;
|
|
|
|
default:
|
2013-02-10 12:13:19 -05:00
|
|
|
result = g_key_file_get_integer(accounts, account_name, "priority.dnd", NULL);
|
2013-01-31 17:48:21 -05:00
|
|
|
break;
|
|
|
|
}
|
2013-02-03 18:46:20 -05:00
|
|
|
|
2013-02-03 15:09:56 -05:00
|
|
|
if (result < JABBER_PRIORITY_MIN || result > JABBER_PRIORITY_MAX)
|
|
|
|
result = 0;
|
2013-01-30 20:50:37 -05:00
|
|
|
|
2013-01-31 17:48:21 -05:00
|
|
|
return result;
|
2013-01-30 20:50:37 -05:00
|
|
|
}
|
|
|
|
|
2013-01-27 17:27:30 -05:00
|
|
|
void
|
|
|
|
accounts_set_last_presence(const char * const account_name, const char * const value)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "presence.last", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_set_login_presence(const char * const account_name, const char * const value)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "presence.login", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-10 12:13:19 -05:00
|
|
|
resource_presence_t
|
2013-01-30 19:01:38 -05:00
|
|
|
accounts_get_last_presence(const char * const account_name)
|
|
|
|
{
|
2013-08-25 07:54:34 -04:00
|
|
|
resource_presence_t result;
|
2013-01-30 19:01:38 -05:00
|
|
|
gchar *setting = g_key_file_get_string(accounts, account_name, "presence.last", NULL);
|
2013-08-25 07:54:34 -04:00
|
|
|
|
2013-01-30 19:01:38 -05:00
|
|
|
if (setting == NULL || (strcmp(setting, "online") == 0)) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_ONLINE;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else if (strcmp(setting, "chat") == 0) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_CHAT;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else if (strcmp(setting, "away") == 0) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_AWAY;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else if (strcmp(setting, "xa") == 0) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_XA;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else if (strcmp(setting, "dnd") == 0) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_DND;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else {
|
|
|
|
log_warning("Error reading presence.last for account: '%s', value: '%s', defaulting to 'online'",
|
|
|
|
account_name, setting);
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_ONLINE;
|
2013-01-30 19:01:38 -05:00
|
|
|
}
|
2013-08-25 07:54:34 -04:00
|
|
|
|
|
|
|
if (setting != NULL) {
|
|
|
|
g_free(setting);
|
|
|
|
}
|
|
|
|
return result;
|
2013-01-30 19:01:38 -05:00
|
|
|
}
|
|
|
|
|
2013-02-10 12:13:19 -05:00
|
|
|
resource_presence_t
|
2013-01-30 19:40:27 -05:00
|
|
|
accounts_get_login_presence(const char * const account_name)
|
2013-01-30 17:59:09 -05:00
|
|
|
{
|
2013-08-25 07:54:34 -04:00
|
|
|
resource_presence_t result;
|
2013-01-30 17:59:09 -05:00
|
|
|
gchar *setting = g_key_file_get_string(accounts, account_name, "presence.login", NULL);
|
2013-08-25 07:54:34 -04:00
|
|
|
|
2013-01-30 19:01:38 -05:00
|
|
|
if (setting == NULL || (strcmp(setting, "online") == 0)) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_ONLINE;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else if (strcmp(setting, "chat") == 0) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_CHAT;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else if (strcmp(setting, "away") == 0) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_AWAY;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else if (strcmp(setting, "xa") == 0) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_XA;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else if (strcmp(setting, "dnd") == 0) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_DND;
|
2013-01-30 19:01:38 -05:00
|
|
|
} else if (strcmp(setting, "last") == 0) {
|
2013-08-25 07:54:34 -04:00
|
|
|
result = accounts_get_last_presence(account_name);
|
2013-01-30 19:01:38 -05:00
|
|
|
} else {
|
2013-01-30 17:59:09 -05:00
|
|
|
log_warning("Error reading presence.login for account: '%s', value: '%s', defaulting to 'online'",
|
|
|
|
account_name, setting);
|
2013-08-25 07:54:34 -04:00
|
|
|
result = RESOURCE_ONLINE;
|
2013-01-30 17:59:09 -05:00
|
|
|
}
|
2013-08-25 07:54:34 -04:00
|
|
|
|
|
|
|
if (setting != NULL) {
|
|
|
|
g_free(setting);
|
|
|
|
}
|
|
|
|
return result;
|
2013-01-30 17:59:09 -05:00
|
|
|
}
|
|
|
|
|
2013-01-27 13:47:33 -05:00
|
|
|
static void
|
|
|
|
_fix_legacy_accounts(const char * const account_name)
|
|
|
|
{
|
|
|
|
// set barejid and resource
|
|
|
|
const char *barejid = account_name;
|
|
|
|
const char *resource = "profanity";
|
|
|
|
Jid *jid = jid_create(account_name);
|
|
|
|
if (jid != NULL) {
|
|
|
|
barejid = jid->barejid;
|
|
|
|
if (jid->resourcepart != NULL) {
|
|
|
|
resource = jid->resourcepart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// accounts with no jid property
|
|
|
|
if (!g_key_file_has_key(accounts, account_name, "jid", NULL)) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "jid", barejid);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
|
|
|
|
// accounts with no resource, property
|
|
|
|
if (!g_key_file_has_key(accounts, account_name, "resource", NULL)) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "resource", resource);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
|
2013-09-12 18:30:35 -04:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2013-01-27 13:47:33 -05:00
|
|
|
jid_destroy(jid);
|
|
|
|
}
|
|
|
|
|
2012-12-01 20:29:42 -05:00
|
|
|
static void
|
|
|
|
_save_accounts(void)
|
|
|
|
{
|
|
|
|
gsize g_data_size;
|
2013-08-25 07:54:34 -04:00
|
|
|
gchar *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL);
|
2012-12-01 20:29:42 -05:00
|
|
|
g_file_set_contents(accounts_loc, g_accounts_data, g_data_size, NULL);
|
2013-08-25 07:54:34 -04:00
|
|
|
g_free(g_accounts_data);
|
2012-12-01 20:29:42 -05:00
|
|
|
}
|
2013-02-02 17:20:19 -05:00
|
|
|
|
|
|
|
static gchar *
|
|
|
|
_get_accounts_file(void)
|
|
|
|
{
|
|
|
|
gchar *xdg_data = xdg_get_data_home();
|
|
|
|
GString *logfile = g_string_new(xdg_data);
|
|
|
|
g_string_append(logfile, "/profanity/accounts");
|
|
|
|
gchar *result = strdup(logfile->str);
|
|
|
|
g_free(xdg_data);
|
|
|
|
g_string_free(logfile, TRUE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|