summary refs log tree commit diff stats
path: root/compiler/parser.nim
Commit message (Collapse)AuthorAgeFilesLines
* interpret `tuple` as a class and `tuple[]` as the empty tupleMax Zerzouri2015-03-061-0/+3
| | | | | | | | When the indentation syntax is allowed it is always interpreted as a tuple: type Unit = tuple
* fixes #2123Araq2015-02-141-0/+1
|
* fixes #2073; language spec change: arrow like operators are not right ↵Araq2015-02-081-2/+2
| | | | associative anymore
* nimsuggest improvementsAraq2015-01-301-1/+4
|
* nimsuggest: first versionAraq2015-01-271-6/+11
|
* 'discard' can be used instead of 'nil' for empty case object branchesAraq2015-01-121-3/+3
|
* Happy new year!Guillaume Gelin2015-01-061-1/+1
|
* fixes #1120Araq2014-12-251-2/+6
|
* fixes #1742Araq2014-12-241-1/+1
|
* fixes a small bug concerning semicolons for top level statementsAraq2014-12-231-2/+4
|
* implemented arrow like operatorsAraq2014-12-081-23/+22
|
* implements 'defer'Araq2014-12-041-4/+6
|
* fixes #1473Araq2014-11-121-4/+5
|
* Nimrod renamed to NimAraq2014-08-281-2/+2
|
* renamefestAraq2014-08-231-1/+1
|
* fixes #1161Araq2014-07-011-9/+12
|
* fix tokenizing bugflaviut2014-06-081-8/+13
|
* fix overlooked grammer commentflaviut2014-06-081-0/+1
|
* updated grammer, fixed oversightflaviut2014-06-041-1/+1
|
* fix bug with unexpected tkInvalid,tkEof,tkCommentflaviut2014-06-041-0/+2
|
* Clean up codeflaviut2014-06-031-12/+7
|
* Fix accents in enumsflaviut2014-06-031-3/+5
|
* Generalize accent parsingflaviut2014-06-031-20/+9
|
* Document parser structures and procedures.Clay Sweetser2014-05-251-20/+39
|
* new concurrency model: first steps; shared is not a keyword anymoreAraq2014-04-141-2/+1
|
* implemented 'R ptr T' syntaxAraq2014-04-131-1/+1
|
* fixes #992Araq2014-03-261-0/+2
|
* implements ``distinct with/without X, Y``Zahary Karadjov2014-03-201-16/+28
| | | | | | This still doesn't work quite right, because some common operations like array indexing lay completely outside the scope/symbol lookup system - they are not even magics.
* allow multi-line expressions using the dot operatorZahary Karadjov2014-03-161-6/+10
| | | | | | | | | | | | this enables parsing of expressions like: ``` Nimrod s.items .map(proc (x): auto = x * 2) .filter do (x) -> auto: return x % 2 == 0 .group_by((x) => x.length) ```
* grammar fix: allow free-standing expressions such as type(foo) is barZahary Karadjov2014-03-161-4/+19
|
* implements strongSpaces parsing modeAraq2014-03-071-20/+45
|
* the delegator pragma becomes a set of dot operatorsZahary Karadjov2014-02-151-1/+1
|
* fixes regression: constant fac4 didn't workAraq2014-02-061-8/+15
|
* cleaned up command expressionsAraq2014-02-021-6/+5
|
* only 1 argument allowed for command expressionsAraq2014-02-021-6/+10
|
* fixes a minor parser bugAraq2014-01-251-2/+1
|
* typeClasses now documented in grammar.txtAraq2014-01-231-0/+4
|
* next steps for closure iteratorsAraq2014-01-221-2/+2
|
* parser support anon iteratorsAraq2014-01-201-2/+3
|
* updated grammar.txtAraq2014-01-201-2/+2
|
* command syntax is allowed in expressionsAraq2014-01-201-44/+74
|
* 'nil' as a statement is deprecated, use an empty 'discard' insteadAraq2014-01-191-1/+1
|
* Merge branch 'upstream' into develZahary Karadjov2013-12-291-9/+17
|\ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Conflicts: compiler/ccgutils.nim compiler/msgs.nim compiler/sem.nim compiler/semexprs.nim compiler/seminst.nim compiler/semmagic.nim compiler/semstmts.nim compiler/semtypes.nim compiler/semtypinst.nim compiler/sigmatch.nim compiler/types.nim compiler/vmgen.nim lib/core/macros.nim lib/system.nim tests/reject/tenummix.nim web/news.txt
| * static params: expr[T] is now static[T]Zahary Karadjov2013-12-191-9/+17
| | | | | | | | | | | | | | This introduces tyStatic and successfully bootstraps and handles few simple test cases. Static params within macros are no longer treated as PNimrodNodes - they are now equivalent to constants of the designated type.
* | case consistency: cs:partial bootstraps on windowsAraq2013-12-291-7/+7
| |
* | case consistency: next stepsAraq2013-12-291-10/+10
| |
* | case consistency part 4Araq2013-12-271-7/+7
| |
* | case consistency part 1Araq2013-12-271-16/+16
|/
* support for multiple test variables and var qualifiers in user-defined type ↵Zahary Karadjov2013-11-131-1/+14
| | | | classes
* fixed another semicolon related parsing bugAraq2013-10-021-4/+3
|
nti/profani-tty/blame/src/accounts.c?id=8304b30b5206c6cedcda44f046b24037eaa364dc'>^
a8d80ef5 ^
731dabd7 ^
67cfe50e ^

b48d0e90 ^




731dabd7 ^
6826a66f ^
a8d80ef5 ^

6826a66f ^
324a85a2 ^
a8d80ef5 ^
324a85a2 ^





6826a66f ^

ce6f2276 ^







e6749d66 ^
ce6f2276 ^







e6749d66 ^
ce6f2276 ^
e6749d66 ^
ce6f2276 ^





e6749d66 ^
43841ee9 ^





ce6f2276 ^
e6749d66 ^
e9225687 ^
e6749d66 ^









e9225687 ^
e6749d66 ^




b397a8c5 ^





ce6f2276 ^







43841ee9 ^



e6749d66 ^

43841ee9 ^
ce6f2276 ^


c80383e2 ^





527e739a ^
c80383e2 ^











527e739a ^
c80383e2 ^



















b48d0e90 ^










8c03bb97 ^






43841ee9 ^

c80383e2 ^


527e739a ^

5013cce8 ^
527e739a ^

5013cce8 ^
c80383e2 ^



8415f57b ^









972ca34d ^








8415f57b ^











f6176b32 ^








a9c629dd ^
8a69fffd ^











































b397a8c5 ^

8a69fffd ^
b397a8c5 ^







8a69fffd ^


b397a8c5 ^
e9225687 ^
8a69fffd ^
b397a8c5 ^



e9225687 ^
b397a8c5 ^

e9225687 ^
b397a8c5 ^

e9225687 ^
b397a8c5 ^

e9225687 ^
b397a8c5 ^

b397a8c5 ^
e9225687 ^
b397a8c5 ^

2cea2639 ^
1cea320a ^

8a69fffd ^
b397a8c5 ^
8a69fffd ^


a9c629dd ^
















e9225687 ^
e6749d66 ^



e9225687 ^
e6749d66 ^
e9225687 ^
e6749d66 ^
e9225687 ^
e6749d66 ^
e9225687 ^
e6749d66 ^
e9225687 ^
e6749d66 ^


e9225687 ^
e6749d66 ^


e9225687 ^
06abe13f ^
9aa6a39c ^
9aa6a39c ^
e6749d66 ^
e9225687 ^
e6749d66 ^
e9225687 ^
e6749d66 ^
e9225687 ^
e6749d66 ^
e9225687 ^
e6749d66 ^
e9225687 ^
e6749d66 ^


9aa6a39c ^

e9225687 ^
9aa6a39c ^


6826a66f ^
b9361542 ^




























6826a66f ^





1247d1c7 ^













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


             
                                                           





















                                                                       

                     
 
                   
                
                
                               
                      



                           

                               
 

                                                                                               
                                                                  
                                 
                                        




                                 

                                    
                                        





                                                                               


                                                    

            
                                     
                                                           

                                                                                  
         
 
                                               

     

                                     
     
                        




                    

                                  



                              

                                   
                                                     



                               
 
                                                 


    
                               
 
                               




                                   
                                   


    
                                                             
 
                                    
                                       
                                       
                                        

                               


                                         

     
                        


                                                                        
                                                                            
                                
                                                                               
         

                                                                                  




                                                                             
 
                         

                                                           
     
 
                     





                                                 

 







                                                           
 







                                                                        
 
                                                                                   
 





                                                                              
 





                                                                                  
 
                                                                                       
                                                                              









                                                                                 
                                                               




                                                       





                                                                                                   







                                           



                                         

                                               
                               


     





                                                                
                                                   











                                                                 
                                                      



















                                                                             










                                                                                 






                                                                                          

     


                                                          

                                                      
                                                                      

                                                              
     



                









                                                                           








                                                                                             











                                                                              








                                                                                
    











































                                                                                 

                                                                            
 







                                                          


    
                                                                        
                                      
 



                          
                               

                                                                                             
                             

                                                                                           
                             

                                                                                           
                           

                                                                                         
                
                                                                                          

                  
 

                                                                     
 
                  


    
















                                                                                      
                   



                                                                                          
                               
                                              
                             
                                              
                             
                                            
                           
                                             
                            


                                                                                                         
                               


     
                   
                                                            
 
                                                                                           
                                                              
                               
                                              
                             
                                              
                             
                                            
                           
                                             
                            


                                                        

                                                                                                          
                               


     
           




























                                                                            





                                                                             













                                                    
/*
 * 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;
}