about summary refs log tree commit diff stats
path: root/html/apps/ex7.subx.html
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-10-16 22:46:39 -0700
committerKartik Agaram <vc@akkartik.com>2020-10-16 22:46:39 -0700
commit2dd300eb208813300f5605b2ff0efe50df2ccc8c (patch)
tree5176fea8b96e74d953051f72752b037307827a78 /html/apps/ex7.subx.html
parentb72c5a60ff466bd743c19e66dc7173c3143ff509 (diff)
downloadmu-2dd300eb208813300f5605b2ff0efe50df2ccc8c.tar.gz
7045
Diffstat (limited to 'html/apps/ex7.subx.html')
0 files changed, 0 insertions, 0 deletions
href='#n123'>123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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
<!DOCTYPE html>
<html dir="ltr" lang="en">
    <head>
        <meta charset='utf-8'>
        <title>Postgresql</title>
    </head>
    <body>

        <a href="index.html">Tools Index</a>

        <h1>Postgresql</h1>


        <h2 id="install">1. Install Postgresql</h2>

        <p>Install postgresql;</p>

        <pre>
        $ prt-get depinst postgresql
        </pre>

        <pre>
        # mkdir /srv/pgsql/
        # touch /var/log/postgresql
        # chown postgres:postgres /srv/pgsql /var/log/postgresql
        # sudo -u postgres initdb -D /srv/pgsql/data
        </pre>

        <h2 id="config">2. Configure Server</h2>

        <h3>2.1. Init script</h3>

        <p>Change <a href="conf/etc/rc.d/postgresql">/etc/rc.d/postgresql</a>;</p>

        <pre>
        #
        # /etc/rc.d/postgresql: start, stop or restart PostgreSQL server postmaster
        #

        PG_DATA=/srv/pgsql/data

        case "$1" in
            start|stop|status|restart|reload)
                sudo -u postgres pg_ctl -D "$PG_DATA" -l /var/log/postgresql "$1"
                ;;
            *)
                echo "usage: $0 start|stop|restart|reload|status"
                ;;
        esac

        # End of file
        </pre>

        <h3>2.2. Certificates</h3>

        <pre>
        $ sudo openssl genrsa -des3 -out /etc/ssl/keys/pg.key 2048
        Password:
        Generating RSA private key, 2048 bit long modulus
        ..............................+++
        ............+++
        e is 65537 (0x10001)
        Enter pass phrase for /etc/ssl/keys/pg.key:
        Verifying - Enter pass phrase for /etc/ssl/keys/pg.key:
        </pre>

        <p>Create ceritificate signing request. For "Common Name"
        provide domain name or ip address, leave challange password
        and optional company name blank;</p>

        <pre>
        $ sudo openssl req -x509 -in server.req -text -key /etc/ssl/keys/pg.key -out /etc/ssl/certs/pg.crt

        Enter pass phrase for /etc/ssl/keys/pg.key:
        You are about to be asked to enter information that will be incorporated
        into your certificate request.
        What you are about to enter is what is called a Distinguished Name or a DN.
        There are quite a few fields but you can leave some blank
        For some fields there will be a default value,
        If you enter '.', the field will be left blank.
        -----
        Country Name (2 letter code) [AU]:PT
        State or Province Name (full name) [Some-State]:
        Locality Name (eg, city) []:
        Organization Name (eg, company) [Internet Widgits Pty Ltd]:
        Organizational Unit Name (eg, section) []:
        Common Name (e.g. server FQDN or YOUR name) []:machine.example.org
        Email Address []:

        Please enter the following 'extra' attributes
        to be sent with your certificate request
        A challenge password []:
        An optional company name []:
        $
        </pre>

        <p>Having password is a good idea, but requires it every
        time pg is restarted. To remove;</p>

        <pre>
        $ sudo cp /etc/ssl/keys/pg.key /etc/ssl/keys/pg.key.pass
        $ sudo openssl rsa \
            -in /etc/ssl/keys/pg.key.pass \
            -out /etc/ssl/keys/pg.key
        </pre>

        <pre>
        Enter pass phrase for /etc/ssl/keys/pg.key.pass:
        writing RSA key
        </pre>

        <pre>
        $ sudo chown postgres:postgres /etc/ssl/keys/pg.key*
        $ sudo chmod 0600 /etc/ssl/keys/pg.key*
        $ sudo chmod 644 /etc/ssl/certs/pg.cert
        </pre>

        <h3>2.3. Super user password</h3>

        <p>Create password for super user;</p>

        <pre>
        # su postgres
        $ psql -U postgres
        </pre>

        <h3>2.4. Configure postgresql.conf</h3>

        <p>Edit <a href="conf/srv/pgsql/data/postgresql.conf">/srv/pgsql/data/postgresql.conf</a>;</p>

        <pre>
        # - Security and Authentication -

        #authentication_timeout = 1min          # 1s-600s
        ssl = on                                # (change requires restart)
        #ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
                                                # (change requires restart)
        #ssl_prefer_server_ciphers = on         # (change requires restart)
        #ssl_ecdh_curve = 'prime256v1'          # (change requires restart)
        ssl_cert_file = '/etc/ssl/certs/pg.crt' # (change requires restart)
        ssl_key_file = '/etc/ssl/keys/pg.key'   # (change requires restart)
        #ssl_ca_file = ''                       # (change requires restart)
        #ssl_crl_file = ''                      # (change requires restart)
        password_encryption = scram-sha-256
        </pre>

        <h3>2.5. Configure pg_hba.conf</h3>

        <p>Edit
        <a href="conf/srv/pgsql/data/pg_hba.conf">/srv/pgsql/data/pg_hba.conf</a>;
        </p>

        <pre>
        # TYPE  DATABASE        USER            ADDRESS                 METHOD
        local   postgres        all             trust
        host    postgres        all             127.0.0.1/32            trust
        host    all             all             127.0.0.1/32            scram-sha-256
        host all all 0.0.0.0/0 reject
        </pre>

        <p>Start server and alter postgres password</p>

        <pre>
        # /etc/rc.d/postgresql start
        </pre>

        <pre>
        postgres=# alter user postgres with password 'new_password';
        </pre>

        <h3 id="syslog">2.6. Configure syslog-ng</h3>

        <p><a href="syslog-ng.html">Configure Syslog-ng</a>, check <a href="http://michael.otacoo.com/postgresql-2/postgres-settings-simple-syslog-configuration-with-syslog-ng/">Michael at otacoo</a> article. Example;</p>

        <p>Edit /pgsql/data/<a href="../conf/srv/pgsql/data/postgresql.conf">postgresql.conf</a>;</p>

        <pre>
        log_destination = 'syslog' # Can specify multiple destinations
        syslog_facility='LOCAL0'
        syslog_ident='postgres'
        log_connections = on
        log_disconnections = on
        log_duration = on
        </pre>

        <p>Create /etc/logrotate.d/postgres;</p>

        <pre>
        /var/log/pgsql {
            weekly
            compress
            delaycompress
            rotate 10
            notifempty
            create 660 postgres postgres
        }
        </pre>


        <pre>
        destination postgres { file("/var/log/pgsql"); };
        filter f_postgres { facility(local0); };
        log { source(s_log); filter(f_postgres); destination(postgres); };
        </pre>


        <h2 id="createuser">3. Create User</
/*
 * accounts.c
 *
 * Copyright (C) 2012 - 2015 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 <http://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 "config/account.h"
#include "jid.h"
#include "log.h"
#include "tools/autocomplete.h"
#include "xmpp/xmpp.h"

static gchar *accounts_loc;
">all_ac; static Autocomplete enabled_ac; // used to rename account (copies properties to new account) static gchar *string_keys[] = { "jid", "server", "port", "resource", "password", "eval_password", "presence.last", "presence.login", "muc.service", "muc.nick", "otr.policy" }; static void _fix_legacy_accounts(const char * const account_name); static void _save_accounts(void); static gchar * _get_accounts_file(void); static void _remove_from_list(GKeyFile *accounts, const char * const account_name, const char * const key, const char * const contact_jid); void accounts_load(void) { log_info("Loading accounts"); all_ac = autocomplete_new(); enabled_ac = autocomplete_new(); accounts_loc = _get_accounts_file(); 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]); } _fix_legacy_accounts(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) { // 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); } 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); } 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); 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); 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; } g_key_file_set_boolean(accounts, new_name, "enabled", g_key_file_get_boolean(accounts, account_name, "enabled", NULL)); g_key_file_set_integer(accounts, new_name, "priority.online", g_key_file_get_integer(accounts, account_name, "priority.online", NULL)); g_key_file_set_integer(accounts, new_name, "priority.chat", g_key_file_get_integer(accounts, account_name, "priority.chat", NULL)); g_key_file_set_integer(accounts, new_name, "priority.away", g_key_file_get_integer(accounts, account_name, "priority.away", NULL)); g_key_file_set_integer(accounts, new_name, "priority.xa", g_key_file_get_integer(accounts, account_name, "priority.xa", NULL)); g_key_file_set_integer(accounts, new_name, "priority.dnd", g_key_file_get_integer(accounts, account_name, "priority.dnd", NULL)); // copy other string properties 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(); } } } 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_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_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); gsize length; gchar **list = g_key_file_get_string_list(accounts, account_name, key->str, &length, NULL); GList *glist = NULL; // list found if (list) { int i = 0; for (i = 0; i < length; i++) { // item already in list, exit function if (strcmp(list[i], contact_jid) == 0) { g_list_free_full(glist, g_free); g_strfreev(list); return; } // add item to our g_list glist = g_list_append(glist, strdup(list[i])); } // item not found, add to our g_list glist = g_list_append(glist, strdup(contact_jid)); // create the new list entry const gchar* new_list[g_list_length(glist)+1]; GList *curr = glist; i = 0; while (curr) { new_list[i++] = strdup(curr->data); curr = g_list_next(curr); } new_list[i] = NULL; g_key_file_set_string_list(accounts, account_name, key->str, new_list, g_list_length(glist)); // list not found } else { const gchar* new_list[2]; new_list[0] = strdup(contact_jid); new_list[1] = NULL; g_key_file_set_string_list(accounts, account_name, key->str, new_list, 1); } g_strfreev(list); g_list_free_full(glist, g_free); g_string_free(key, TRUE); // check for and remove from other lists if (strcmp(policy, "manual") == 0) { _remove_from_list(accounts, account_name, "otr.opportunistic", contact_jid); _remove_from_list(accounts, account_name, "otr.always", contact_jid); } if (strcmp(policy, "opportunistic") == 0) { _remove_from_list(accounts, account_name, "otr.manual", contact_jid); _remove_from_list(accounts, account_name, "otr.always", contact_jid); } if (strcmp(policy, "always") == 0) { _remove_from_list(accounts, account_name, "otr.opportunistic", contact_jid); _remove_from_list(accounts, account_name, "otr.manual", contact_jid); } _save_accounts(); } } static void _remove_from_list(GKeyFile *accounts, const char * const account_name, const char * const key, const char * const contact_jid) { gsize length; gchar **list = g_key_file_get_string_list(accounts, account_name, key, &length, NULL); if (list) { int i = 0; GList *glist = NULL; gboolean deleted = FALSE; for (i = 0; i < length; i++) { // item found, mark as deleted if (strcmp(list[i], contact_jid) == 0) { deleted = TRUE; } else { // add item to our g_list glist = g_list_append(glist, strdup(list[i])); } } if (deleted) { if (g_list_length(glist) == 0) { g_key_file_remove_key(accounts, account_name, key, NULL); } else { // create the new list entry const gchar* new_list[g_list_length(glist)+1]; GList *curr = glist; i = 0; while (curr) { new_list[i++] = strdup(curr->data); curr = g_list_next(curr); } new_list[i] = NULL; g_key_file_set_string_list(accounts, account_name, key, new_list, g_list_length(glist)); } } g_list_free_full(glist, g_free); } g_strfreev(list); } 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_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_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; } 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 _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) { barejid = jid->barejid; if (jid->resourcepart) { 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(); } // accounts 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); } static void _save_accounts(void) { gsize g_data_size; gchar *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL); gchar *xdg_data = xdg_get_data_home(); GString *base_str = g_string_new(xdg_data); g_string_append(base_str, "/profanity/"); gchar *true_loc = get_file_or_linked(accounts_loc, base_str->str); g_file_set_contents(true_loc, g_accounts_data, g_data_size, NULL); g_chmod(accounts_loc, S_IRUSR | S_IWUSR); g_free(xdg_data); free(true_loc); g_free(g_accounts_data); g_string_free(base_str, TRUE); } 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; }