about summary refs log tree commit diff stats
path: root/src/config/accounts.c
blob: 96168b5be85d1684470131eeba781ddec9e3a4ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */

'n188' href='#n188'>188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
/*
 * accounts.c
 *
 * Copyright (C) 2012 - 2016 James Booth <boothj5@gmail.com>
 *
 * 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 <https://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#include <stdlib.h>
#include <string.h>

#include <glib.h>
#include <glib/gstdio.h>

#include "accounts.h"

#include "common.h"
#include "log.h"
#include "config/files.h"
#include "config/account.h"
#include "config/conflists.h"
#include "tools/autocomplete.h"
#include "xmpp/xmpp.h"
#include "xmpp/jid.h"

static char *accounts_loc;
static GKeyFile *accounts;

static Autocomplete all_ac;
static Autocomplete enabled_ac;

static void _save_accounts(void);

void
accounts_load(void)
{
    log_info("Loading accounts");
    all_ac = autocomplete_new();
    enabled_ac = autocomplete_new();
    accounts_loc = files_get_data_path(FILE_ACCOUNTS);

    if (g_file_test(accounts_loc, G_FILE_TEST_EXISTS)) {
        g_chmod(accounts_loc, S_IRUSR | S_IWUSR);
    }

    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 naccounts;
    gchar **account_names = g_key_file_get_groups(accounts, &naccounts);

    gsize i;
    for (i = 0; i < naccounts; i++) {
        autocomplete_add(all_ac, account_names[i]);
        if (g_key_file_get_boolean(accounts, account_names[i], "enabled", NULL)) {
            autocomplete_add(enabled_ac, account_names[i]);
        }
    }

    g_strfreev(account_names);
}

void
accounts_close(void)
{
    autocomplete_free(all_ac);
    autocomplete_free(enabled_ac);
    g_key_file_free(accounts);
}

char*
accounts_find_enabled(const char *const prefix)
{
    return autocomplete_complete(enabled_ac, prefix, TRUE);
}

char*
accounts_find_all(const char *const prefix)
{
    return autocomplete_complete(all_ac, prefix, TRUE);
}

void
accounts_reset_all_search(void)
{
    autocomplete_reset(all_ac);
}

void
accounts_reset_enabled_search(void)
{
    autocomplete_reset(enabled_ac);
}

void
accounts_add(const char *account_name, const char *altdomain, const int port, const char *const tls_policy)
{
    // set account name and resource
    const char *barejid = account_name;
    const char *resource = "profanity";
    Jid *jid = jid_create(account_name);
    if (jid) {
        barejid = jid->barejid;
        if (jid->resourcepart) {
            resource = jid->resourcepart;
        }
    }

    // doesn't yet exist
    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);
        g_key_file_set_string(accounts, account_name, "resource", resource);
        if (altdomain) {
            g_key_file_set_string(accounts, account_name, "server", altdomain);
        }
        if (port != 0) {
            g_key_file_set_integer(accounts, account_name, "port", port);
        }
        if (tls_policy) {
            g_key_file_set_string(accounts, account_name, "tls.policy", tls_policy);
        }

        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);
        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);

        _save_accounts();
        autocomplete_add(all_ac, account_name);
        autocomplete_add(enabled_ac, account_name);
    }

    jid_destroy(jid);
}

int
accounts_remove(const char *account_name)
{
    int r = g_key_file_remove_group(accounts, account_name, NULL);
    _save_accounts();
    autocomplete_remove(all_ac, account_name);
    autocomplete_remove(enabled_ac, account_name);
    return r;
}

gchar**
accounts_get_list(void)
{
    return g_key_file_get_groups(accounts, NULL);
}

ProfAccount*
accounts_get_account(const char *const name)
{
    if (!g_key_file_has_group(accounts, name)) {
        return NULL;
    } else {
        gchar *jid = g_key_file_get_string(accounts, name, "jid", NULL);

        // fix accounts that have no jid property by setting to name
        if (jid == NULL) {
            g_key_file_set_string(accounts, name, "jid", name);
            _save_accounts();
        }

        gchar *password = g_key_file_get_string(accounts, name, "password", NULL);
        gchar *eval_password = g_key_file_get_string(accounts, name, "eval_password", NULL);
        gboolean enabled = g_key_file_get_boolean(accounts, name, "enabled", NULL);

        gchar *server = g_key_file_get_string(accounts, name, "server", NULL);
        gchar *resource = g_key_file_get_string(accounts, name, "resource", NULL);
        int port = g_key_file_get_integer(accounts, name, "port", NULL);

        gchar *last_presence = g_key_file_get_string(accounts, name, "presence.last", NULL);
        gchar *login_presence = g_key_file_get_string(accounts, name, "presence.login", NULL);

        int priority_online = g_key_file_get_integer(accounts, name, "priority.online", NULL);
        int priority_chat = g_key_file_get_integer(accounts, name, "priority.chat", NULL);
        int priority_away = g_key_file_get_integer(accounts, name, "priority.away", NULL);
        int priority_xa = g_key_file_get_integer(accounts, name, "priority.xa", NULL);
        int 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);
        gchar *muc_nick = g_key_file_get_string(accounts, name, "muc.nick", NULL);

        gchar *otr_policy = NULL;
        if (g_key_file_has_key(accounts, name, "otr.policy", NULL)) {
            otr_policy = g_key_file_get_string(accounts, name, "otr.policy", NULL);
        }

        gsize length;
        GList *otr_manual = NULL;
        gchar **manual = g_key_file_get_string_list(accounts, name, "otr.manual", &length, NULL);
        if (manual) {
            int i = 0;
            for (i = 0; i < length; i++) {
                otr_manual = g_list_append(otr_manual, strdup(manual[i]));
            }
            g_strfreev(manual);
        }

        GList *otr_opportunistic = NULL;
        gchar **opportunistic = g_key_file_get_string_list(accounts, name, "otr.opportunistic", &length, NULL);
        if (opportunistic) {
            int i = 0;
            for (i = 0; i < length; i++) {
                otr_opportunistic = g_list_append(otr_opportunistic, strdup(opportunistic[i]));
            }
            g_strfreev(opportunistic);
        }

        GList *otr_always = NULL;
        gchar **always = g_key_file_get_string_list(accounts, name, "otr.always", &length, NULL);
        if (always) {
            int i = 0;
            for (i = 0; i < length; i++) {
                otr_always = g_list_append(otr_always, strdup(always[i]));
            }
            g_strfreev(always);
        }

        gchar *pgp_keyid = NULL;
        if (g_key_file_has_key(accounts, name, "pgp.keyid", NULL)) {
            pgp_keyid = g_key_file_get_string(accounts, name, "pgp.keyid", NULL);
        }

        gchar *startscript = NULL;
        if (g_key_file_has_key(accounts, name, "script.start", NULL)) {
            startscript = g_key_file_get_string(accounts, name, "script.start", NULL);
        }

        gchar *theme = NULL;
        if (g_key_file_has_key(accounts, name, "theme", NULL)) {
            theme = g_key_file_get_string(accounts, name, "theme", NULL);
        }

        gchar *tls_policy = g_key_file_get_string(accounts, name, "tls.policy", NULL);
        if (tls_policy && ((g_strcmp0(tls_policy, "force") != 0) &&
                (g_strcmp0(tls_policy, "allow") != 0) &&
                (g_strcmp0(tls_policy, "disable") != 0))) {
            g_free(tls_policy);
            tls_policy = NULL;
        }

        ProfAccount *new_account = account_new(name, jid, password, eval_password, enabled,
            server, port, resource, last_presence, login_presence,
            priority_online, priority_chat, priority_away, priority_xa,
            priority_dnd, muc_service, muc_nick, otr_policy, otr_manual,
            otr_opportunistic, otr_always, pgp_keyid, startscript, theme, tls_policy);

        g_free(jid);
        g_free(password);
        g_free(eval_password);
        g_free(server);
        g_free(resource);
        g_free(last_presence);
        g_free(login_presence);
        g_free(muc_service);
        g_free(muc_nick);
        g_free(otr_policy);
        g_free(pgp_keyid);
        g_free(startscript);
        g_free(theme);
        g_free(tls_policy);

        return new_account;
    }
}

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();
        autocomplete_add(enabled_ac, name);
        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();
        autocomplete_remove(enabled_ac, name);
        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;
    }

    // treat all properties as strings for copy
    gchar *string_keys[] = {
        "enabled",
        "jid",
        "server",
        "port",
        "resource",
        "password",
        "eval_password",
        "presence.last",
        "presence.laststatus",
        "presence.login",
        "priority.online",
        "priority.chat",
        "priority.away",
        "priority.xa",
        "priority.dnd",
        "muc.service",
        "muc.nick",
        "otr.policy",
        "otr.manual",
        "otr.opportunistic",
        "otr.always",
        "pgp.keyid",
        "last.activity",
        "script.start",
        "tls.policy"
    };

    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) {
            g_key_file_set_string(accounts, new_name, string_keys[i], value);
            g_free(value);
        }
    }

    g_key_file_remove_group(accounts, account_name, NULL);
    _save_accounts();

    autocomplete_remove(all_ac, account_name);
    autocomplete_add(all_ac, new_name);
    if (g_key_file_get_boolean(accounts, new_name, "enabled", NULL)) {
        autocomplete_remove(enabled_ac, account_name);
        autocomplete_add(enabled_ac, new_name);
    }

    return TRUE;
}

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)
{
    Jid *jid = jid_create(value);
    if (jid) {
        if (accounts_account_exists(account_name)) {
            g_key_file_set_string(accounts, account_name, "jid", jid->barejid);
            if (jid->resourcepart) {
                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();
        }

        jid_destroy(jid);
    }
}

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();
    }
}

void
accounts_set_port(const char *const account_name, const int value)
{
    if (value != 0) {
        g_key_file_set_integer(accounts, account_name, "port", value);
        _save_accounts();
    }
}

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();
    }
}

void
accounts_set_password(const char *const account_name, const char *const value)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_set_string(accounts, account_name, "password", value);
        _save_accounts();
    }
}

void
accounts_set_eval_password(const char *const account_name, const char *const value)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_set_string(accounts, account_name, "eval_password", value);
        _save_accounts();
    }
}

void
accounts_set_pgp_keyid(const char *const account_name, const char *const value)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_set_string(accounts, account_name, "pgp.keyid", value);
        _save_accounts();
    }
}

void
accounts_set_script_start(const char *const account_name, const char *const value)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_set_string(accounts, account_name, "script.start", value);
        _save_accounts();
    }
}

void
accounts_set_theme(const char *const account_name, const char *const value)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_set_string(accounts, account_name, "theme", value);
        _save_accounts();
    }
}

void
accounts_clear_password(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_remove_key(accounts, account_name, "password", NULL);
        _save_accounts();
    }
}

void
accounts_clear_eval_password(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_remove_key(accounts, account_name, "eval_password", NULL);
        _save_accounts();
    }
}

void
accounts_clear_server(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_remove_key(accounts, account_name, "server", NULL);
        _save_accounts();
    }
}

void
accounts_clear_port(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_remove_key(accounts, account_name, "port", NULL);
        _save_accounts();
    }
}

void
accounts_clear_pgp_keyid(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_remove_key(accounts, account_name, "pgp.keyid", NULL);
        _save_accounts();
    }
}

void
accounts_clear_script_start(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_remove_key(accounts, account_name, "script.start", NULL);
        _save_accounts();
    }
}

void
accounts_clear_theme(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_remove_key(accounts, account_name, "theme", NULL);
        _save_accounts();
    }
}

void
accounts_clear_otr(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_remove_key(accounts, account_name, "otr.policy", NULL);
        _save_accounts();
    }
}

void
accounts_add_otr_policy(const char *const account_name, const char *const contact_jid, const char *const policy)
{
    if (accounts_account_exists(account_name)) {
        GString *key = g_string_new("otr.");
        g_string_append(key, policy);
        conf_string_list_add(accounts, account_name, key->str, contact_jid);
        g_string_free(key, TRUE);

        // check for and remove from other lists
        if (strcmp(policy, "manual") == 0) {
            conf_string_list_remove(accounts, account_name, "otr.opportunistic", contact_jid);
            conf_string_list_remove(accounts, account_name, "otr.always", contact_jid);
        }
        if (strcmp(policy, "opportunistic") == 0) {
            conf_string_list_remove(accounts, account_name, "otr.manual", contact_jid);
            conf_string_list_remove(accounts, account_name, "otr.always", contact_jid);
        }
        if (strcmp(policy, "always") == 0) {
            conf_string_list_remove(accounts, account_name, "otr.opportunistic", contact_jid);
            conf_string_list_remove(accounts, account_name, "otr.manual", contact_jid);
        }

        _save_accounts();
    }
}

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_otr_policy(const char *const account_name, const char *const value)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_set_string(accounts, account_name, "otr.policy", value);
        _save_accounts();
    }
}

void
accounts_set_tls_policy(const char *const account_name, const char *const value)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_set_string(accounts, account_name, "tls.policy", value);
        _save_accounts();
    }
}

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();
    }
}

void
accounts_set_priority_all(const char *const account_name, const gint value)
{
    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();
    }
}

gint
accounts_get_priority_for_presence_type(const char *const account_name,
    resource_presence_t presence_type)
{
    gint result;

    switch (presence_type)
    {
        case (RESOURCE_ONLINE):
            result = g_key_file_get_integer(accounts, account_name, "priority.online", NULL);
            break;
        case (RESOURCE_CHAT):
            result = g_key_file_get_integer(accounts, account_name, "priority.chat", NULL);
            break;
        case (RESOURCE_AWAY):
            result = g_key_file_get_integer(accounts, account_name, "priority.away", NULL);
            break;
        case (RESOURCE_XA):
            result = g_key_file_get_integer(accounts, account_name, "priority.xa", NULL);
            break;
        default:
            result = g_key_file_get_integer(accounts, account_name, "priority.dnd", NULL);
            break;
    }

    if (result < JABBER_PRIORITY_MIN || result > JABBER_PRIORITY_MAX)
        result = 0;

    return result;
}

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_last_status(const char *const account_name, const char *const value)
{
    if (accounts_account_exists(account_name)) {
        if (value) {
            g_key_file_set_string(accounts, account_name, "presence.laststatus", value);
        } else {
            g_key_file_remove_key(accounts, account_name, "presence.laststatus", NULL);
        }
        _save_accounts();
    }
}

void
accounts_set_last_activity(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        GDateTime *nowdt = g_date_time_new_now_utc();
        GTimeVal nowtv;
        gboolean res = g_date_time_to_timeval(nowdt, &nowtv);
        g_date_time_unref(nowdt);

        if (res) {
            char *timestr = g_time_val_to_iso8601(&nowtv);
            g_key_file_set_string(accounts, account_name, "last.activity", timestr);
            free(timestr);
            _save_accounts();
        }
    }
}

char*
accounts_get_last_activity(const char *const account_name)
{
    if (accounts_account_exists(account_name)) {
        return g_key_file_get_string(accounts, account_name, "last.activity", NULL);
    } else {
        return NULL;
    }
}

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();
    }
}

resource_presence_t
accounts_get_last_presence(const char *const account_name)
{
    resource_presence_t result;
    gchar *setting = g_key_file_get_string(accounts, account_name, "presence.last", NULL);

    if (setting == NULL || (strcmp(setting, "online") == 0)) {
        result = RESOURCE_ONLINE;
    } else if (strcmp(setting, "chat") == 0) {
        result = RESOURCE_CHAT;
    } else if (strcmp(setting, "away") == 0) {
        result = RESOURCE_AWAY;
    } else if (strcmp(setting, "xa") == 0) {
        result = RESOURCE_XA;
    } else if (strcmp(setting, "dnd") == 0) {
        result = RESOURCE_DND;
    } else {
        log_warning("Error reading presence.last for account: '%s', value: '%s', defaulting to 'online'",
            account_name, setting);
        result = RESOURCE_ONLINE;
    }

    if (setting) {
        g_free(setting);
    }
    return result;
}

char*
accounts_get_last_status(const char *const account_name)
{
    return g_key_file_get_string(accounts, account_name, "presence.laststatus", NULL);
}

resource_presence_t
accounts_get_login_presence(const char *const account_name)
{
    resource_presence_t result;
    gchar *setting = g_key_file_get_string(accounts, account_name, "presence.login", NULL);

    if (setting == NULL || (strcmp(setting, "online") == 0)) {
        result = RESOURCE_ONLINE;
    } else if (strcmp(setting, "chat") == 0) {
        result = RESOURCE_CHAT;
    } else if (strcmp(setting, "away") == 0) {
        result = RESOURCE_AWAY;
    } else if (strcmp(setting, "xa") == 0) {
        result = RESOURCE_XA;
    } else if (strcmp(setting, "dnd") == 0) {
        result = RESOURCE_DND;
    } else if (strcmp(setting, "last") == 0) {
        result = accounts_get_last_presence(account_name);
    } else {
        log_warning("Error reading presence.login for account: '%s', value: '%s', defaulting to 'online'",
            account_name, setting);
        result = RESOURCE_ONLINE;
    }

    if (setting) {
        g_free(setting);
    }
    return result;
}

static void
_save_accounts(void)
{
    gsize g_data_size;
    gchar *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL);

    gchar *base = g_path_get_basename(accounts_loc);
    gchar *true_loc = get_file_or_linked(accounts_loc, base);
    g_file_set_contents(true_loc, g_accounts_data, g_data_size, NULL);
    g_chmod(accounts_loc, S_IRUSR | S_IWUSR);

    g_free(base);
    free(true_loc);
    g_free(g_accounts_data);
}