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-01-26 17:19:20 -05:00
|
|
|
#include "autocomplete.h"
|
2013-01-26 17:25:00 -05:00
|
|
|
#include "common.h"
|
2012-12-01 20:29:42 -05:00
|
|
|
#include "files.h"
|
2013-01-26 17:19:20 -05:00
|
|
|
#include "jid.h"
|
2012-12-01 20:29:42 -05:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
static void _save_accounts(void);
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_load(void)
|
|
|
|
{
|
|
|
|
log_info("Loading accounts");
|
2013-01-24 20:11:49 -05:00
|
|
|
all_ac = autocomplete_new();
|
|
|
|
enabled_ac = autocomplete_new();
|
2012-12-01 20:29:42 -05:00
|
|
|
accounts_loc = files_get_accounts_file();
|
|
|
|
|
|
|
|
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
|
|
|
|
gsize njids;
|
|
|
|
gchar **jids =
|
|
|
|
g_key_file_get_groups(accounts, &njids);
|
|
|
|
|
|
|
|
gsize i;
|
|
|
|
for (i = 0; i < njids; i++) {
|
2012-12-09 19:53:57 -05:00
|
|
|
if (g_key_file_get_boolean(accounts, jids[i], "enabled", NULL)) {
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_add(enabled_ac, strdup(jids[i]));
|
2012-12-09 19:53:57 -05:00
|
|
|
}
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_add(all_ac, strdup(jids[i]));
|
2012-12-01 20:29:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < njids; i++) {
|
|
|
|
free(jids[i]);
|
|
|
|
}
|
|
|
|
free(jids);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-26 17:19:20 -05:00
|
|
|
accounts_add_login(const char *account_name, const char *altdomain)
|
2012-12-01 20:29:42 -05:00
|
|
|
{
|
2013-01-26 17:19:20 -05:00
|
|
|
const char *barejid = account_name;
|
|
|
|
const char *resource = NULL;
|
|
|
|
|
|
|
|
Jid *jid = jid_create(account_name);
|
|
|
|
|
|
|
|
if (jid != NULL) {
|
|
|
|
barejid = jid->barejid;
|
|
|
|
resource = jid->resourcepart;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (resource != NULL) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "resource", resource);
|
|
|
|
} else {
|
|
|
|
g_key_file_set_string(accounts, account_name, "resource", "profanity");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-12-01 20:29:42 -05:00
|
|
|
_save_accounts();
|
2013-01-26 17:19:20 -05:00
|
|
|
autocomplete_add(all_ac, strdup(account_name));
|
|
|
|
autocomplete_add(enabled_ac, strdup(account_name));
|
2012-12-09 15:18:38 -05:00
|
|
|
|
|
|
|
// already exists, update old style accounts
|
|
|
|
} else {
|
2013-01-26 17:19:20 -05:00
|
|
|
g_key_file_set_string(accounts, account_name, "jid", barejid);
|
|
|
|
if (resource != NULL) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "resource", resource);
|
|
|
|
} else {
|
|
|
|
g_key_file_set_string(accounts, account_name, "resource", "profanity");
|
|
|
|
}
|
2012-12-09 15:18:38 -05:00
|
|
|
_save_accounts();
|
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);
|
|
|
|
gchar *jid = g_key_file_get_string(accounts, name, "jid", NULL);
|
|
|
|
if (jid != NULL) {
|
|
|
|
account->jid = strdup(jid);
|
|
|
|
} else {
|
|
|
|
account->jid = strdup(name);
|
|
|
|
g_key_file_set_string(accounts, name, "jid", name);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
account->enabled = g_key_file_get_boolean(accounts, name, "enabled", NULL);
|
|
|
|
gchar *server = g_key_file_get_string(accounts, name, "server", NULL);
|
|
|
|
if (server != NULL) {
|
|
|
|
account->server = strdup(server);
|
|
|
|
} else {
|
|
|
|
account->server = NULL;
|
|
|
|
}
|
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);
|
|
|
|
} else {
|
|
|
|
account->resource = NULL;
|
|
|
|
}
|
2012-12-09 17:14:38 -05:00
|
|
|
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
accounts_free_account(ProfAccount *account)
|
|
|
|
{
|
|
|
|
if (account != NULL) {
|
2013-01-26 17:25:00 -05:00
|
|
|
FREE_SET_NULL(account->name);
|
|
|
|
FREE_SET_NULL(account->jid);
|
|
|
|
FREE_SET_NULL(account->resource);
|
|
|
|
FREE_SET_NULL(account->server);
|
|
|
|
FREE_SET_NULL(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-01-24 20:11:49 -05:00
|
|
|
autocomplete_add(enabled_ac, strdup(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-01-24 20:11:49 -05:00
|
|
|
autocomplete_remove(enabled_ac, strdup(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));
|
|
|
|
|
|
|
|
char *jid = g_key_file_get_string(accounts, account_name, "jid", NULL);
|
|
|
|
if (jid != NULL) {
|
|
|
|
g_key_file_set_string(accounts, new_name, "jid", jid);
|
|
|
|
free(jid);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *server = g_key_file_get_string(accounts, account_name, "server", NULL);
|
|
|
|
if (server != NULL) {
|
|
|
|
g_key_file_set_string(accounts, new_name, "server", server);
|
|
|
|
free(server);
|
|
|
|
}
|
|
|
|
|
2013-01-26 17:25:00 -05:00
|
|
|
char *resource = g_key_file_get_string(accounts, account_name, "resource", NULL);
|
|
|
|
if (resource != NULL) {
|
|
|
|
g_key_file_set_string(accounts, new_name, "resource", resource);
|
|
|
|
free(resource);
|
|
|
|
}
|
|
|
|
|
2012-12-09 19:08:03 -05:00
|
|
|
g_key_file_remove_group(accounts, account_name, NULL);
|
|
|
|
_save_accounts();
|
|
|
|
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_remove(all_ac, strdup(account_name));
|
|
|
|
autocomplete_add(all_ac, strdup(new_name));
|
2012-12-09 19:53:57 -05:00
|
|
|
if (g_key_file_get_boolean(accounts, new_name, "enabled", NULL)) {
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_remove(enabled_ac, strdup(account_name));
|
|
|
|
autocomplete_add(enabled_ac, strdup(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)
|
|
|
|
{
|
|
|
|
if (accounts_account_exists(account_name)) {
|
|
|
|
g_key_file_set_string(accounts, account_name, "jid", value);
|
|
|
|
_save_accounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-01 20:29:42 -05:00
|
|
|
static void
|
|
|
|
_save_accounts(void)
|
|
|
|
{
|
|
|
|
gsize g_data_size;
|
|
|
|
char *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL);
|
|
|
|
g_file_set_contents(accounts_loc, g_accounts_data, g_data_size, NULL);
|
|
|
|
}
|