about summary refs log tree commit diff stats
path: root/src/config/accounts.c
blob: b53f4fb0b837dfccaf1e7bf578eceb8b51bbb706 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
280pre { 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 */
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 088file.mu</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #aaaaaa; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; co
/*
 * accounts.c
 *
 * Copyright (C) 2012, 2013 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/>.
 *
 */

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

#include <glib.h>

#include "accounts.h"

#include "common.h"
#include "jid.h"
#include "log.h"
#include "tools/autocomplete.h"
#include "xmpp/xmpp.h"

static gchar *accounts_loc;
static GKeyFile *accounts;

static Autocomplete all_ac;
static Autocomplete enabled_ac;

static gchar *string_keys[] = {"jid", "server", "resource", "presence.last", "presence.login"};

static void _fix_legacy_accounts(const char * const account_name);
static void _save_accounts(void);
static gchar * _get_accounts_file(void);

void
accounts_load(void)
{
    log_info("Loading accounts");
    all_ac = autocomplete_new();
    enabled_ac = autocomplete_new();
    accounts_loc = _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 naccounts;
    gchar **account_names =
        g_key_file_get_groups(accounts, &naccounts);

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

        _fix_legacy_accounts(account_names[i]);
    }

    for (i = 0; i < naccounts; i++) {
        free(account_names[i]);
    }
    free(account_names);
}

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

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

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

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)
{
    // set account name 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;
        }
    }

    // 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 != NULL) {
            g_key_file_set_string(accounts, account_name, "server", altdomain);
        }
        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, strdup(account_name));
        autocomplete_add(enabled_ac, strdup(account_name));
    }

    jid_destroy(jid);
}

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 {
        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;
        }

        gchar *resource = g_key_file_get_string(accounts, name, "resource", NULL);
        if (resource != NULL) {
            account->resource = strdup(resource);
        } else {
            account->resource = NULL;
        }

        gchar *presence = g_key_file_get_string(accounts, name, "presence.last", NULL);
        if (presence == NULL || (!valid_resource_presence_string(presence))) {
            account->last_presence = strdup("online");
        } else {
            account->last_presence = strdup(presence);
        }

        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");
        } else if (!valid_resource_presence_string(presence)) {
            account->login_presence = strdup("online");
        } else {
            account->login_presence = strdup(presence);
        }

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

        return account;
    }
}

void
accounts_free_account(ProfAccount *account)
{
    if (account != NULL) {
        FREE_SET_NULL(account->name);
        FREE_SET_NULL(account->jid);
        FREE_SET_NULL(account->resource);
        FREE_SET_NULL(account->server);
        FREE_SET_NULL(account->last_presence);
        FREE_SET_NULL(account->login_presence);
        FREE_SET_NULL(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, strdup(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, strdup(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_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));

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

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

    autocomplete_remove(all_ac, strdup(account_name));
    autocomplete_add(all_ac, strdup(new_name));
    if (g_key_file_get_boolean(accounts, new_name, "enabled", NULL)) {
        autocomplete_remove(enabled_ac, strdup(account_name));
        autocomplete_add(enabled_ac, strdup(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 != 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);
            }
            _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_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_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)
{
    gchar *setting = g_key_file_get_string(accounts, account_name, "presence.last", NULL);
    if (setting == NULL || (strcmp(setting, "online") == 0)) {
        return RESOURCE_ONLINE;
    } else if (strcmp(setting, "chat") == 0) {
        return RESOURCE_CHAT;
    } else if (strcmp(setting, "away") == 0) {
        return RESOURCE_AWAY;
    } else if (strcmp(setting, "xa") == 0) {
        return RESOURCE_XA;
    } else if (strcmp(setting, "dnd") == 0) {
        return RESOURCE_DND;
    } else {
        log_warning("Error reading presence.last for account: '%s', value: '%s', defaulting to 'online'",
            account_name, setting);
        return RESOURCE_ONLINE;
    }
}

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

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

    jid_destroy(jid);
}

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

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;
}
quot;Comment"># real file system</span> <span id="L117" class="LineNr">117 </span> file:num <span class="Special">&lt;-</span> $open-file-for-writing filename <span id="L118" class="LineNr">118 </span> <span class="muControl">return-unless</span> file,<span class="Constant"> null/sink,</span> <span class="Constant">0/routine-id</span>,<span class="Constant"> true/error</span> <span id="L119" class="LineNr">119 </span> <span class="Delimiter">{</span> <span id="L120" class="LineNr">120 </span> <span class="muControl">break-if</span> file <span id="L121" class="LineNr">121 </span> msg:text <span class="Special">&lt;-</span> append <span class="Constant">[no such file: ]</span> filename <span id="L122" class="LineNr">122 </span> assert file, msg <span id="L123" class="LineNr">123 </span> <span class="Delimiter">}</span> <span id="L124" class="LineNr">124 </span> routine-id <span class="Special">&lt;-</span> <span class="muControl">start-running</span> <a href='088file.mu.html#L150'>transmit-to-file</a> file, <a href='075channel.mu.html#L36'>source</a> <span id="L125" class="LineNr">125 </span>] <span id="L126" class="LineNr">126 </span> <span id="L127" class="LineNr">127 </span><span class="muRecipe">def</span> <a href='088file.mu.html#L127'>dump</a> <a href='088file.mu.html#L11'>resources</a>:&amp;:<a href='088file.mu.html#L11'>resources</a>, filename:text, contents:text<span class="muRecipe"> -&gt; </span><a href='088file.mu.html#L11'>resources</a>:&amp;:<a href='088file.mu.html#L11'>resources</a>, error?:bool [ <span id="L128" class="LineNr">128 </span> <span class="Constant">local-scope</span> <span id="L129" class="LineNr">129 </span> <span class="Constant">load-inputs</span> <span id="L130" class="LineNr">130 </span> <span class="Comment"># todo: really create an empty file</span> <span id="L131" class="LineNr">131 </span> <span class="muControl">return-unless</span> contents, <a href='088file.mu.html#L11'>resources</a>,<span class="Constant"> false/no-error</span> <span id="L132" class="LineNr">132 </span> sink-file:&amp;:<a href='075channel.mu.html#L40'>sink</a>:char, write-routine:num, error?:bool <span class="Special">&lt;-</span> <a href='088file.mu.html#L105'>start-writing</a> <a href='088file.mu.html#L11'>resources</a>, filename <span id="L133" class="LineNr">133 </span> <span class="muControl">return-if</span> error? <span id="L134" class="LineNr">134 </span> i:num <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span> <span id="L135" class="LineNr">135 </span> len:num <span class="Special">&lt;-</span> length *contents <span id="L136" class="LineNr">136 </span> <span class="Delimiter">{</span> <span id="L137" class="LineNr">137 </span> done?:bool <span class="Special">&lt;-</span> greater-or-equal i, len <span id="L138" class="LineNr">138 </span> <span class="muControl">break-if</span> done? <span id="L139" class="LineNr">139 </span> c:char <span class="Special">&lt;-</span> index *contents, i <span id="L140" class="LineNr">140 </span> sink-file <span class="Special">&lt;-</span> <a href='075channel.mu.html#L60'>write</a> sink-file, c <span id="L141" class="LineNr">141 </span> i <span class="Special">&lt;-</span> add i,<span class="Constant"> 1</span> <span id="L142" class="LineNr">142 </span> <span class="muControl"> loop</span> <span id="L143" class="LineNr">143 </span> <span class="Delimiter">}</span> <span id="L144" class="LineNr">144 </span> close sink-file <span id="L145" class="LineNr">145 </span> <span class="Comment"># make sure to wait for the file to be actually written to disk</span> <span id="L146" class="LineNr">146 </span> <span class="Comment"># (Mu practices structured concurrency: <a href="http://250bpm.com/blog:71">http://250bpm.com/blog:71</a>)</span> <span id="L147" class="LineNr">147 </span> wait-for-routine write-routine <span id="L148" class="LineNr">148 </span>] <span id="L149" class="LineNr">149 </span> <span id="L150" class="LineNr">150 </span><span class="muRecipe">def</span> <a href='088file.mu.html#L150'>transmit-to-file</a> file:num, <a href='075channel.mu.html#L36'>source</a>:&amp;:<a href='075channel.mu.html#L36'>source</a>:char<span class="muRecipe"> -&gt; </span><a href='075channel.mu.html#L36'>source</a>:&amp;:<a href='075channel.mu.html#L36'>source</a>:char [ <span id="L151" class="LineNr">151 </span> <span class="Constant">local-scope</span> <span id="L152" class="LineNr">152 </span> <span class="Constant">load-inputs</span> <span id="L153" class="LineNr">153 </span> <span class="Delimiter">{</span> <span id="L154" class="LineNr">154 </span> c:char, done?:bool, <a href='075channel.mu.html#L36'>source</a> <span class="Special">&lt;-</span> read <a href='075channel.mu.html#L36'>source</a> <span id="L155" class="LineNr">155 </span> <span class="muControl">break-if</span> done? <span id="L156" class="LineNr">156 </span> $write-to-file file, c <span id="L157" class="LineNr">157 </span> <span class="muControl"> loop</span> <span id="L158" class="LineNr">158 </span> <span class="Delimiter">}</span> <span id="L159" class="LineNr">159 </span> file <span class="Special">&lt;-</span> $close-file file <span id="L160" class="LineNr">160 </span>] <span id="L161" class="LineNr">161 </span> <span id="L162" class="LineNr">162 </span><span class="muRecipe">def</span> <a href='088file.mu.html#L162'>transmit-to-fake-resource</a> <a href='088file.mu.html#L11'>resources</a>:&amp;:<a href='088file.mu.html#L11'>resources</a>, filename:text, <a href='075channel.mu.html#L36'>source</a>:&amp;:<a href='075channel.mu.html#L36'>source</a>:char<span class="muRecipe"> -&gt; </span><a href='088file.mu.html#L11'>resources</a>:&amp;:<a href='088file.mu.html#L11'>resources</a>, <a href='075channel.mu.html#L36'>source</a>:&amp;:<a href='075channel.mu.html#L36'>source</a>:char [ <span id="L163" class="LineNr">163 </span> <span class="Constant">local-scope</span> <span id="L164" class="LineNr">164 </span> <span class="Constant">load-inputs</span> <span id="L165" class="LineNr">165 </span> lock:location <span class="Special">&lt;-</span> get-location *resources, <span class="Constant">lock:offset</span> <span id="L166" class="LineNr">166 </span> wait-for-reset-then-set lock <span id="L167" class="LineNr">167 </span> <span class="Comment"># compute new file contents</span> <span id="L168" class="LineNr">168 </span> buf:&amp;:<a href='061text.mu.html#L120'>buffer</a>:char <span class="Special">&lt;-</span> <a href='061text.mu.html#L125'>new-buffer</a><span class="Constant"> 30</span> <span id="L169" class="LineNr">169 </span> <span class="Delimiter">{</span> <span id="L170" class="LineNr">170 </span> c:char, done?:bool, <a href='075channel.mu.html#L36'>source</a> <span class="Special">&lt;-</span> read <a href='075channel.mu.html#L36'>source</a> <span id="L171" class="LineNr">171 </span> <span class="muControl">break-if</span> done? <span id="L172" class="LineNr">172 </span> buf <span class="Special">&lt;-</span> append buf, c <span id="L173" class="LineNr">173 </span> <span class="muControl"> loop</span> <span id="L174" class="LineNr">174 </span> <span class="Delimiter">}</span> <span id="L175" class="LineNr">175 </span> contents:text <span class="Special">&lt;-</span> <a href='061text.mu.html#L338'>buffer-to-array</a> buf <span id="L176" class="LineNr">176 </span> new-resource:<a href='088file.mu.html#L16'>resource</a> <span class="Special">&lt;-</span> merge filename, contents <span id="L177" class="LineNr">177 </span> <span class="Comment"># write to resources</span> <span id="L178" class="LineNr">178 </span> curr-filename:text <span class="Special">&lt;-</span> copy<span class="Constant"> null</span> <span id="L179" class="LineNr">179 </span> data:&amp;:@:<a href='088file.mu.html#L16'>resource</a> <span class="Special">&lt;-</span> get *resources, <span class="Constant">data:offset</span> <span id="L180" class="LineNr">180 </span> <span class="Comment"># replace file contents if it already exists</span> <span id="L181" class="LineNr">181 </span> i:num <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span> <span id="L182" class="LineNr">182 </span> len:num <span class="Special">&lt;-</span> length *data <span id="L183" class="LineNr">183 </span> <span class="Delimiter">{</span> <span id="L184" class="LineNr">184 </span> done?:bool <span class="Special">&lt;-</span> greater-or-equal i, len <span id="L185" class="LineNr">185 </span> <span class="muControl">break-if</span> done? <span id="L186" class="LineNr">186 </span> tmp:<a href='088file.mu.html#L16'>resource</a> <span class="Special">&lt;-</span> index *data, i <span id="L187" class="LineNr">187 </span> curr-filename <span class="Special">&lt;-</span> get tmp, <span class="Constant">name:offset</span> <span id="L188" class="LineNr">188 </span> found?:bool <span class="Special">&lt;-</span> equal filename, curr-filename <span id="L189" class="LineNr">189 </span> <span class="Delimiter">{</span> <span id="L190" class="LineNr">190 </span> <span class="muControl">break-unless</span> found? <span id="L191" class="LineNr">191 </span> put-index *data, i, new-resource <span id="L192" class="LineNr">192 </span> <span class="muControl"> jump</span> <span class="Constant">+unlock-and-exit</span> <span id="L193" class="LineNr">193 </span> <span class="Delimiter">}</span> <span id="L194" class="LineNr">194 </span> i <span class="Special">&lt;-</span> add i,<span class="Constant"> 1</span> <span id="L195" class="LineNr">195 </span> <span class="muControl"> loop</span> <span id="L196" class="LineNr">196 </span> <span class="Delimiter">}</span> <span id="L197" class="LineNr">197 </span> <span class="Comment"># if file didn't already exist, make room for it</span> <span id="L198" class="LineNr">198 </span> new-len:num <span class="Special">&lt;-</span> add len,<span class="Constant"> 1</span> <span id="L199" class="LineNr">199 </span> new-data:&amp;:@:<a href='088file.mu.html#L16'>resource</a> <span class="Special">&lt;-</span> new <span class="Constant"><a href='088file.mu.html#L16'>resource</a>:type</span>, new-len <span id="L200" class="LineNr">200 </span> put *resources, <span class="Constant">data:offset</span>, new-data <span id="L201" class="LineNr">201 </span> <span class="Comment"># copy over old files</span> <span id="L202" class="LineNr">202 </span> i:num <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span> <span id="L203" class="LineNr">203 </span> <span class="Delimiter">{</span> <span id="L204" class="LineNr">204 </span> done?:bool <span class="Special">&lt;-</span> greater-or-equal i, len <span id="L205" class="LineNr">205 </span> <span class="muControl">break-if</span> done? <span id="L206" class="LineNr">206 </span> tmp:<a href='088file.mu.html#L16'>resource</a> <span class="Special">&lt;-</span> index *data, i <span id="L207" class="LineNr">207 </span> put-index *new-data, i, tmp <span id="L208" class="LineNr">208 </span> <span class="Delimiter">}</span> <span id="L209" class="LineNr">209 </span> <span class="Comment"># write new file</span> <span id="L210" class="LineNr">210 </span> put-index *new-data, len, new-resource <span id="L211" class="LineNr">211 </span><span class="Constant"> +unlock-and-exit</span> <span id="L212" class="LineNr">212 </span> reset lock <span id="L213" class="LineNr">213 </span>] </pre> </body> </html> <!-- vim: set foldmethod=manual : -->