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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
|
/*
* preferences.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 "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "common.h"
#include "log.h"
#include "preferences.h"
#include "tools/autocomplete.h"
#include "config/conflists.h"
// preference groups refer to the sections in .profrc, for example [ui]
#define PREF_GROUP_LOGGING "logging"
#define PREF_GROUP_CHATSTATES "chatstates"
#define PREF_GROUP_UI "ui"
#define PREF_GROUP_NOTIFICATIONS "notifications"
#define PREF_GROUP_PRESENCE "presence"
#define PREF_GROUP_CONNECTION "connection"
#define PREF_GROUP_ALIAS "alias"
#define PREF_GROUP_OTR "otr"
#define PREF_GROUP_PGP "pgp"
#define INPBLOCK_DEFAULT 1000
static gchar *prefs_loc;
static GKeyFile *prefs;
gint log_maxsize = 0;
static Autocomplete boolean_choice_ac;
static Autocomplete room_trigger_ac;
static void _save_prefs(void);
static gchar* _get_preferences_file(void);
static const char* _get_group(preference_t pref);
static const char* _get_key(preference_t pref);
static gboolean _get_default_boolean(preference_t pref);
static char* _get_default_string(preference_t pref);
void
prefs_load(void)
{
GError *err;
prefs_loc = _get_preferences_file();
if (g_file_test(prefs_loc, G_FILE_TEST_EXISTS)) {
g_chmod(prefs_loc, S_IRUSR | S_IWUSR);
}
prefs = g_key_file_new();
g_key_file_load_from_file(prefs, prefs_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
err = NULL;
log_maxsize = g_key_file_get_integer(prefs, PREF_GROUP_LOGGING, "maxsize", &err);
if (err) {
log_maxsize = 0;
g_error_free(err);
}
// move pre 0.5.0 autoaway.time to autoaway.awaytime
if (g_key_file_has_key(prefs, PREF_GROUP_PRESENCE, "autoaway.time", NULL)) {
gint time = g_key_file_get_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.time", NULL);
g_key_file_set_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.awaytime", time);
g_key_file_remove_key(prefs, PREF_GROUP_PRESENCE, "autoaway.time", NULL);
}
// move pre 0.5.0 autoaway.message to autoaway.awaymessage
if (g_key_file_has_key(prefs, PREF_GROUP_PRESENCE, "autoaway.message", NULL)) {
char *message = g_key_file_get_string(prefs, PREF_GROUP_PRESENCE, "autoaway.message", NULL);
g_key_file_set_string(prefs, PREF_GROUP_PRESENCE, "autoaway.awaymessage", message);
g_key_file_remove_key(prefs, PREF_GROUP_PRESENCE, "autoaway.message", NULL);
prefs_free_string(message);
}
// migrate pre 0.5.0 time settings
if (g_key_file_has_key(prefs, PREF_GROUP_UI, "time", NULL)) {
char *time = g_key_file_get_string(prefs, PREF_GROUP_UI, "time", NULL);
char *val = NULL;
if (time) {
val = time;
} else {
val = "off";
}
g_key_file_set_string(prefs, PREF_GROUP_UI, "time.console", val);
g_key_file_set_string(prefs, PREF_GROUP_UI, "time.chat", val);
g_key_file_set_string(prefs, PREF_GROUP_UI, "time.muc", val);
g_key_file_set_string(prefs, PREF_GROUP_UI, "time.mucconfig", val);
g_key_file_set_string(prefs, PREF_GROUP_UI, "time.private", val);
g_key_file_set_string(prefs, PREF_GROUP_UI, "time.xmlconsole", val);
g_key_file_remove_key(prefs, PREF_GROUP_UI, "time", NULL);
prefs_free_string(time);
}
// move pre 0.5.0 notify settings
if (g_key_file_has_key(prefs, PREF_GROUP_NOTIFICATIONS, "room", NULL)) {
char *value = g_key_file_get_string(prefs, PREF_GROUP_NOTIFICATIONS, "room", NULL);
if (g_strcmp0(value, "on") == 0) {
g_key_file_set_boolean(prefs, PREF_GROUP_NOTIFICATIONS, "room", TRUE);
} else if (g_strcmp0(value, "off") == 0) {
g_key_file_set_boolean(prefs, PREF_GROUP_NOTIFICATIONS, "room", FALSE);
} else if (g_strcmp0(value, "mention") == 0) {
g_key_file_set_boolean(prefs, PREF_GROUP_NOTIFICATIONS, "room", FALSE);
g_key_file_set_boolean(prefs, PREF_GROUP_NOTIFICATIONS, "room.mention", TRUE);
}
}
_save_prefs();
boolean_choice_ac = autocomplete_new();
autocomplete_add(boolean_choice_ac, "on");
autocomplete_add(boolean_choice_ac, "off");
room_trigger_ac = autocomplete_new();
gsize len = 0;
gchar **triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL);
int i = 0;
for (i = 0; i < len; i++) {
autocomplete_add(room_trigger_ac, triggers[i]);
}
g_strfreev(triggers);
}
void
prefs_close(void)
{
autocomplete_free(boolean_choice_ac);
autocomplete_free(room_trigger_ac);
g_key_file_free(prefs);
prefs = NULL;
}
char*
prefs_autocomplete_boolean_choice(const char *const prefix)
{
return autocomplete_complete(boolean_choice_ac, prefix, TRUE);
}
void
prefs_reset_boolean_choice(void)
{
autocomplete_reset(boolean_choice_ac);
}
char*
prefs_autocomplete_room_trigger(const char *const prefix)
{
return autocomplete_complete(room_trigger_ac, prefix, TRUE);
}
void
prefs_reset_room_trigger_ac(void)
{
autocomplete_reset(room_trigger_ac);
}
gboolean
prefs_do_chat_notify(gboolean current_win, const char *const message)
{
gboolean notify_current = prefs_get_boolean(PREF_NOTIFY_CHAT_CURRENT);
gboolean notify_window = FALSE;
if (!current_win || (current_win && notify_current) ) {
notify_window = TRUE;
}
if (!notify_window) {
return FALSE;
}
gboolean notify_message = prefs_get_boolean(PREF_NOTIFY_CHAT);
if (notify_message) {
return TRUE;
}
return FALSE;
}
GList*
prefs_message_get_triggers(const char *const message)
{
GList *result = NULL;
char *message_lower = g_utf8_strdown(message, -1);
gsize len = 0;
gchar **triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL);
int i;
for (i = 0; i < len; i++) {
char *trigger_lower = g_utf8_strdown(triggers[i], -1);
if (g_strrstr(message_lower, trigger_lower)) {
result = g_list_append(result, strdup(triggers[i]));
}
g_free(trigger_lower);
}
g_strfreev(triggers);
g_free(message_lower);
return result;
}
gboolean
prefs_do_room_notify(gboolean current_win, const char *const roomjid, const char *const nick, const char *const message,
gboolean mention, gboolean trigger_found)
{
gboolean notify_current = prefs_get_boolean(PREF_NOTIFY_ROOM_CURRENT);
gboolean notify_window = FALSE;
if (!current_win || (current_win && notify_current) ) {
notify_window = TRUE;
}
if (!notify_window) {
return FALSE;
}
gboolean notify_room = FALSE;
if (g_key_file_has_key(prefs, roomjid, "notify", NULL)) {
notify_room = g_key_file_get_boolean(prefs, roomjid, "notify", NULL);
} else {
notify_room = prefs_get_boolean(PREF_NOTIFY_ROOM);
}
if (notify_room) {
return TRUE;
}
gboolean notify_mention = FALSE;
if (g_key_file_has_key(prefs, roomjid, "notify.mention", NULL)) {
notify_mention = g_key_file_get_boolean(prefs, roomjid, "notify.mention", NULL);
} else {
notify_mention = prefs_get_boolean(PREF_NOTIFY_ROOM_MENTION);
}
if (notify_mention && mention) {
return TRUE;
}
gboolean notify_trigger = FALSE;
if (g_key_file_has_key(prefs, roomjid, "notify.trigger", NULL)) {
notify_trigger = g_key_file_get_boolean(prefs, roomjid, "notify.trigger", NULL);
} else {
notify_trigger = prefs_get_boolean(PREF_NOTIFY_ROOM_TRIGGER);
}
if (notify_trigger && trigger_found) {
return TRUE;
}
return FALSE;
}
void
prefs_set_room_notify(const char *const roomjid, gboolean value)
{
g_key_file_set_boolean(prefs, roomjid, "notify", value);
_save_prefs();
}
void
prefs_set_room_notify_mention(const char *const roomjid, gboolean value)
{
g_key_file_set_boolean(prefs, roomjid, "notify.mention", value);
_save_prefs();
}
void
prefs_set_room_notify_trigger(const char *const roomjid, gboolean value)
{
g_key_file_set_boolean(prefs, roomjid, "notify.trigger", value);
_save_prefs();
}
gboolean
prefs_has_room_notify(const char *const roomjid)
{
return g_key_file_has_key(prefs, roomjid, "notify", NULL);
}
gboolean
prefs_has_room_notify_mention(const char *const roomjid)
{
return g_key_file_has_key(prefs, roomjid, "notify.mention", NULL);
}
gboolean
prefs_has_room_notify_trigger(const char *const roomjid)
{
return g_key_file_has_key(prefs, roomjid, "notify.trigger", NULL);
}
gboolean
prefs_get_room_notify(const char *const roomjid)
{
return g_key_file_get_boolean(prefs, roomjid, "notify", NULL);
}
gboolean
prefs_get_room_notify_mention(const char *const roomjid)
{
return g_key_file_get_boolean(prefs, roomjid, "notify.mention", NULL);
}
gboolean
prefs_get_room_notify_trigger(const char *const roomjid)
{
return g_key_file_get_boolean(prefs, roomjid, "notify.trigger", NULL);
}
gboolean
prefs_reset_room_notify(const char *const roomjid)
{
if (g_key_file_has_group(prefs, roomjid)) {
g_key_file_remove_group(prefs, roomjid, NULL);
_save_prefs();
return TRUE;
}
return FALSE;
}
gboolean
prefs_get_boolean(preference_t pref)
{
const char *group = _get_group(pref);
const char *key = _get_key(pref);
gboolean def = _get_default_boolean(pref);
if (!g_key_file_has_key(prefs, group, key, NULL)) {
return def;
}
return g_key_file_get_boolean(prefs, group, key, NULL);
}
void
prefs_set_boolean(preference_t pref, gboolean value)
{
const char *group = _get_group(pref);
const char *key = _get_key(pref);
g_key_file_set_boolean(prefs, group, key, value);
_save_prefs();
}
char*
prefs_get_string(preference_t pref)
{
const char *group = _get_group(pref);
const char *key = _get_key(pref);
char *def = _get_default_string(pref);
char *result = g_key_file_get_string(prefs, group, key, NULL);
if (result == NULL) {
if (def) {
return g_strdup(def);
} else {
return NULL;
}
} else {
return result;
}
}
void
prefs_free_string(char *pref)
{
if (pref) {
g_free(pref);
}
}
void
prefs_set_string(preference_t pref, char *value)
{
const char *group = _get_group(pref);
const char *key = _get_key(pref);
if (value == NULL) {
g_key_file_remove_key(prefs, group, key, NULL);
} else {
g_key_file_set_string(prefs, group, key, value);
}
_save_prefs();
}
gint
prefs_get_gone(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_CHATSTATES, "gone", NULL);
}
void
prefs_set_gone(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CHATSTATES, "gone", value);
_save_prefs();
}
gint
prefs_get_notify_remind(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_NOTIFICATIONS, "remind", NULL);
}
void
prefs_set_notify_remind(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_NOTIFICATIONS, "remind", value);
_save_prefs();
}
gint
prefs_get_max_log_size(void)
{
if (log_maxsize < PREFS_MIN_LOG_SIZE)
return PREFS_MAX_LOG_SIZE;
else
return log_maxsize;
}
void
prefs_set_max_log_size(gint value)
{
log_maxsize = value;
g_key_file_set_integer(prefs, PREF_GROUP_LOGGING, "maxsize", value);
_save_prefs();
}
gint
prefs_get_inpblock(void)
{
int val = g_key_file_get_integer(prefs, PREF_GROUP_UI, "inpblock", NULL);
if (val == 0) {
return INPBLOCK_DEFAULT;
} else {
return val;
}
}
void
prefs_set_inpblock(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_UI, "inpblock", value);
_save_prefs();
}
gint
prefs_get_reconnect(void)
{
if (!g_key_file_has_key(prefs, PREF_GROUP_CONNECTION, "reconnect", NULL)) {
return 30;
} else {
return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "reconnect", NULL);
}
}
void
prefs_set_reconnect(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "reconnect", value);
_save_prefs();
}
gint
prefs_get_autoping(void)
{
if (!g_key_file_has_key(prefs, PREF_GROUP_CONNECTION, "autoping", NULL)) {
return 60;
} else {
return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "autoping", NULL);
}
}
void
prefs_set_autoping(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "autoping", value);
_save_prefs();
}
gint
prefs_get_autoping_timeout(void)
{
if (!g_key_file_has_key(prefs, PREF_GROUP_CONNECTION, "autoping.timeout", NULL)) {
return 10;
} else {
return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "autoping.timeout", NULL);
}
}
void
prefs_set_autoping_timeout(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "autoping.timeout", value);
_save_prefs();
}
gint
prefs_get_autoaway_time(void)
{
gint result = g_key_file_get_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.awaytime", NULL);
if (result == 0) {
return 15;
} else {
return result;
}
}
gint
prefs_get_autoxa_time(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.xatime", NULL);
}
void
prefs_set_autoaway_time(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.awaytime", value);
_save_prefs();
}
void
prefs_set_autoxa_time(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.xatime", value);
_save_prefs();
}
void
prefs_set_occupants_size(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_UI, "occupants.size", value);
_save_prefs();
}
gint
prefs_get_occupants_size(void)
{
gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "occupants.size", NULL);
if (result > 99 || result < 1) {
return 15;
} else {
return result;
}
}
void
prefs_set_roster_size(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_UI, "roster.size", value);
_save_prefs();
}
gint
prefs_get_roster_size(void)
{
gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "roster.size", NULL);
if (result > 99 || result < 1) {
return 25;
} else {
return result;
}
}
char
prefs_get_otr_char(void)
{
char result = '~';
char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_OTR, "otr.char", NULL);
if (!resultstr) {
result = '~';
} else {
result = resultstr[0];
}
free(resultstr);
return result;
}
void
prefs_set_otr_char(char ch)
{
char str[2];
str[0] = ch;
str[1] = '\0';
g_key_file_set_string(prefs, PREF_GROUP_OTR, "otr.char", str);
_save_prefs();
}
char
prefs_get_pgp_char(void)
{
char result = '~';
char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_PGP, "pgp.char", NULL);
if (!resultstr) {
result = '~';
} else {
result = resultstr[0];
}
free(resultstr);
return result;
}
void
prefs_set_pgp_char(char ch)
{
char str[2];
str[0] = ch;
str[1] = '\0';
g_key_file_set_string(prefs, PREF_GROUP_PGP, "pgp.char", str);
_save_prefs();
}
char
prefs_get_roster_header_char(void)
{
char result = 0;
char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.header.char", NULL);
if (!resultstr) {
result = 0;
} else {
result = resultstr[0];
}
free(resultstr);
return result;
}
void
prefs_set_roster_header_char(char ch)
{
char str[2];
str[0] = ch;
str[1] = '\0';
g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.header.char", str);
_save_prefs();
}
void
prefs_clear_roster_header_char(void)
{
g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.header.char", NULL);
_save_prefs();
}
char
prefs_get_roster_contact_char(void)
{
char result = 0;
char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.contact.char", NULL);
if (!resultstr) {
result = 0;
} else {
result = resultstr[0];
}
free(resultstr);
return result;
}
void
prefs_set_roster_contact_char(char ch)
{
char str[2];
str[0] = ch;
str[1] = '\0';
g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.contact.char", str);
_save_prefs();
}
void
prefs_clear_roster_contact_char(void)
{
g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.contact.char", NULL);
_save_prefs();
}
char
prefs_get_roster_resource_char(void)
{
char result = 0;
char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.resource.char", NULL);
if (!resultstr) {
result = 0;
} else {
result = resultstr[0];
}
free(resultstr);
return result;
}
void
prefs_set_roster_resource_char(char ch)
{
char str[2];
str[0] = ch;
str[1] = '\0';
g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.resource.char", str);
_save_prefs();
}
void
prefs_clear_roster_resource_char(void)
{
g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.resource.char", NULL);
_save_prefs();
}
char
prefs_get_roster_private_char(void)
{
char result = 0;
char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.private.char", NULL);
if (!resultstr) {
result = 0;
} else {
result = resultstr[0];
}
free(resultstr);
return result;
}
void
prefs_set_roster_private_char(char ch)
{
char str[2];
str[0] = ch;
str[1] = '\0';
g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.private.char", str);
_save_prefs();
}
void
prefs_clear_roster_private_char(void)
{
g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.private.char", NULL);
_save_prefs();
}
char
prefs_get_roster_room_char(void)
{
char result = 0;
char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_UI, "roster.rooms.char", NULL);
if (!resultstr) {
result = 0;
} else {
result = resultstr[0];
}
free(resultstr);
return result;
}
void
prefs_set_roster_room_char(char ch)
{
char str[2];
str[0] = ch;
str[1] = '\0';
g_key_file_set_string(prefs, PREF_GROUP_UI, "roster.rooms.char", str);
_save_prefs();
}
void
prefs_clear_roster_room_char(void)
{
g_key_file_remove_key(prefs, PREF_GROUP_UI, "roster.rooms.char", NULL);
_save_prefs();
}
gint
prefs_get_roster_contact_indent(void)
{
if (!g_key_file_has_key(prefs, PREF_GROUP_UI, "roster.contact.indent", NULL)) {
return 2;
}
gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "roster.contact.indent", NULL);
if (result < 0) {
result = 0;
}
return result;
}
void
prefs_set_roster_contact_indent(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_UI, "roster.contact.indent", value);
_save_prefs();
}
gint
prefs_get_roster_resource_indent(void)
{
if (!g_key_file_has_key(prefs, PREF_GROUP_UI, "roster.resource.indent", NULL)) {
return 2;
}
gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "roster.resource.indent", NULL);
if (result < 0) {
result = 0;
}
return result;
}
void
prefs_set_roster_resource_indent(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_UI, "roster.resource.indent", value);
_save_prefs();
}
gint
prefs_get_roster_presence_indent(void)
{
if (!g_key_file_has_key(prefs, PREF_GROUP_UI, "roster.presence.indent", NULL)) {
return 2;
}
gint result = g_key_file_get_integer(prefs, PREF_GROUP_UI, "roster.presence.indent", NULL);
if (result < -1) {
result = 0;
}
return result;
}
void
prefs_set_roster_presence_indent(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_UI, "roster.presence.indent", value);
_save_prefs();
}
gboolean
prefs_add_room_notify_trigger(const char * const text)
{
gboolean res = conf_string_list_add(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", text);
_save_prefs();
if (res) {
autocomplete_add(room_trigger_ac, text);
}
return res;
}
gboolean
prefs_remove_room_notify_trigger(const char * const text)
{
gboolean res = conf_string_list_remove(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", text);
_save_prefs();
if (res) {
autocomplete_remove(room_trigger_ac, text);
}
return res;
}
GList*
prefs_get_room_notify_triggers(void)
{
GList *result = NULL;
gsize len = 0;
gchar **triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL);
int i;
for (i = 0; i < len; i++) {
result = g_list_append(result, strdup(triggers[i]));
}
g_strfreev(triggers);
return result;
}
gboolean
prefs_add_alias(const char *const name, const char *const value)
{
if (g_key_file_has_key(prefs, PREF_GROUP_ALIAS, name, NULL)) {
return FALSE;
} else {
g_key_file_set_string(prefs, PREF_GROUP_ALIAS, name, value);
_save_prefs();
return TRUE;
}
}
char*
prefs_get_alias(const char *const name)
{
return g_key_file_get_string(prefs, PREF_GROUP_ALIAS, name, NULL);
}
gboolean
prefs_remove_alias(const char *const name)
{
if (!g_key_file_has_key(prefs, PREF_GROUP_ALIAS, name, NULL)) {
return FALSE;
} else {
g_key_file_remove_key(prefs, PREF_GROUP_ALIAS, name, NULL);
_save_prefs();
return TRUE;
}
}
static gint
_alias_cmp(gconstpointer *p1, gconstpointer *p2)
{
ProfAlias *alias1 = (ProfAlias*)p1;
ProfAlias *alias2 = (ProfAlias*)p2;
return strcmp(alias1->name, alias2->name);
}
GList*
prefs_get_aliases(void)
{
if (!g_key_file_has_group(prefs, PREF_GROUP_ALIAS)) {
return NULL;
} else {
GList *result = NULL;
gsize len;
gchar **keys = g_key_file_get_keys(prefs, PREF_GROUP_ALIAS, &len, NULL);
int i;
for (i = 0; i < len; i++) {
char *name = keys[i];
char *value = g_key_file_get_string(prefs, PREF_GROUP_ALIAS, name, NULL);
if (value) {
ProfAlias *alias = malloc(sizeof(struct prof_alias_t));
alias->name = strdup(name);
alias->value = strdup(value);
free(value);
result = g_list_insert_sorted(result, alias, (GCompareFunc)_alias_cmp);
}
}
g_strfreev(keys);
return result;
}
}
gchar*
prefs_get_inputrc(void)
{
gchar *xdg_config = xdg_get_config_home();
GString *inputrc_file = g_string_new(xdg_config);
g_free(xdg_config);
g_string_append(inputrc_file, "/profanity/inputrc");
if (g_file_test(inputrc_file->str, G_FILE_TEST_IS_REGULAR)) {
gchar *result = strdup(inputrc_file->str);
g_string_free(inputrc_file, TRUE);
return result;
}
return NULL;
}
void
_free_alias(ProfAlias *alias)
{
FREE_SET_NULL(alias->name);
FREE_SET_NULL(alias->value);
FREE_SET_NULL(alias);
}
void
prefs_free_aliases(GList *aliases)
{
g_list_free_full(aliases, (GDestroyNotify)_free_alias);
}
static void
_save_prefs(void)
{
gsize g_data_size;
gchar *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL);
gchar *xdg_config = xdg_get_config_home();
GString *base_str = g_string_new(xdg_config);
g_string_append(base_str, "/profanity/");
gchar *true_loc = get_file_or_linked(prefs_loc, base_str->str);
g_file_set_contents(true_loc, g_prefs_data, g_data_size, NULL);
g_chmod(prefs_loc, S_IRUSR | S_IWUSR);
g_free(xdg_config);
free(true_loc);
g_free(g_prefs_data);
g_string_free(base_str, TRUE);
}
static gchar*
_get_preferences_file(void)
{
gchar *xdg_config = xdg_get_config_home();
GString *prefs_file = g_string_new(xdg_config);
g_string_append(prefs_file, "/profanity/profrc");
gchar *result = strdup(prefs_file->str);
g_free(xdg_config);
g_string_free(prefs_file, TRUE);
return result;
}
// get the preference group for a specific preference
// for example the PREF_BEEP setting ("beep" in .profrc, see _get_key) belongs
// to the [ui] section.
static const char*
_get_group(preference_t pref)
{
switch (pref)
{
case PREF_SPLASH:
case PREF_BEEP:
case PREF_THEME:
case PREF_VERCHECK:
case PREF_TITLEBAR_SHOW:
case PREF_TITLEBAR_GOODBYE:
case PREF_FLASH:
case PREF_INTYPE:
case PREF_HISTORY:
case PREF_OCCUPANTS:
case PREF_OCCUPANTS_JID:
case PREF_STATUSES:
case PREF_STATUSES_CONSOLE:
case PREF_STATUSES_CHAT:
case PREF_STATUSES_MUC:
case PREF_MUC_PRIVILEGES:
case PREF_PRESENCE:
case PREF_WRAP:
case PREF_WINS_AUTO_TIDY:
case PREF_TIME_CONSOLE:
case PREF_TIME_CHAT:
case PREF_TIME_MUC:
case PREF_TIME_MUCCONFIG:
case PREF_TIME_PRIVATE:
case PREF_TIME_XMLCONSOLE:
case PREF_TIME_STATUSBAR:
case PREF_TIME_LASTACTIVITY:
case PREF_ROSTER:
case PREF_ROSTER_OFFLINE:
case PREF_ROSTER_RESOURCE:
case PREF_ROSTER_PRESENCE:
case PREF_ROSTER_STATUS:
case PREF_ROSTER_EMPTY:
case PREF_ROSTER_BY:
case PREF_ROSTER_ORDER:
case PREF_ROSTER_UNREAD:
case PREF_ROSTER_COUNT:
case PREF_ROSTER_COUNT_ZERO:
case PREF_ROSTER_PRIORITY:
case PREF_ROSTER_WRAP:
case PREF_ROSTER_RESOURCE_JOIN:
case PREF_ROSTER_CONTACTS:
case PREF_ROSTER_ROOMS:
case PREF_ROSTER_ROOMS_POS:
case PREF_ROSTER_ROOMS_ORDER:
case PREF_ROSTER_ROOMS_UNREAD:
case PREF_ROSTER_PRIVATE:
case PREF_RESOURCE_TITLE:
case PREF_RESOURCE_MESSAGE:
case PREF_ENC_WARN:
case PREF_INPBLOCK_DYNAMIC:
case PREF_TLS_SHOW:
case PREF_CONSOLE_MUC:
return PREF_GROUP_UI;
case PREF_STATES:
case PREF_OUTTYPE:
return PREF_GROUP_CHATSTATES;
case PREF_NOTIFY_TYPING:
case PREF_NOTIFY_TYPING_CURRENT:
case PREF_NOTIFY_CHAT:
case PREF_NOTIFY_CHAT_CURRENT:
case PREF_NOTIFY_CHAT_TEXT:
case PREF_NOTIFY_ROOM:
case PREF_NOTIFY_ROOM_MENTION:
case PREF_NOTIFY_ROOM_TRIGGER:
case PREF_NOTIFY_ROOM_CURRENT:
case PREF_NOTIFY_ROOM_TEXT:
case PREF_NOTIFY_INVITE:
case PREF_NOTIFY_SUB:
return PREF_GROUP_NOTIFICATIONS;
case PREF_CHLOG:
case PREF_GRLOG:
case PREF_LOG_ROTATE:
case PREF_LOG_SHARED:
return PREF_GROUP_LOGGING;
case PREF_AUTOAWAY_CHECK:
case PREF_AUTOAWAY_MODE:
case PREF_AUTOAWAY_MESSAGE:
case PREF_AUTOXA_MESSAGE:
case PREF_LASTACTIVITY:
return PREF_GROUP_PRESENCE;
case PREF_CONNECT_ACCOUNT:
case PREF_DEFAULT_ACCOUNT:
case PREF_CARBONS:
case PREF_RECEIPTS_SEND:
case PREF_RECEIPTS_REQUEST:
case PREF_TLS_CERTPATH:
return PREF_GROUP_CONNECTION;
case PREF_OTR_LOG:
case PREF_OTR_POLICY:
return PREF_GROUP_OTR;
case PREF_PGP_LOG:
return PREF_GROUP_PGP;
default:
return NULL;
}
}
// get the key used in .profrc for the preference
// for example the PREF_AUTOAWAY_MODE maps to "autoaway.mode" in .profrc
static const char*
_get_key(preference_t pref)
{
switch (pref)
{
case PREF_SPLASH:
return "splash";
case PREF_BEEP:
return "beep";
case PREF_THEME:
return "theme";
case PREF_VERCHECK:
return "vercheck";
case PREF_TITLEBAR_SHOW:
return "titlebar.show";
case PREF_TITLEBAR_GOODBYE:
return "titlebar.goodbye";
case PREF_FLASH:
return "flash";
case PREF_INTYPE:
return "intype";
case PREF_HISTORY:
return "history";
case PREF_CARBONS:
return "carbons";
case PREF_RECEIPTS_SEND:
return "receipts.send";
case PREF_RECEIPTS_REQUEST:
return "receipts.request";
case PREF_OCCUPANTS:
return "occupants";
case PREF_OCCUPANTS_JID:
return "occupants.jid";
case PREF_MUC_PRIVILEGES:
return "privileges";
case PREF_STATUSES:
return "statuses";
case PREF_STATUSES_CONSOLE:
return "statuses.console";
case PREF_STATUSES_CHAT:
return "statuses.chat";
case PREF_STATUSES_MUC:
return "statuses.muc";
case PREF_STATES:
return "enabled";
case PREF_OUTTYPE:
return "outtype";
case PREF_NOTIFY_TYPING:
return "typing";
case PREF_NOTIFY_TYPING_CURRENT:
return "typing.current";
case PREF_NOTIFY_CHAT:
return "message";
case PREF_NOTIFY_CHAT_CURRENT:
return "message.current";
case PREF_NOTIFY_CHAT_TEXT:
return "message.text";
case PREF_NOTIFY_ROOM:
return "room";
case PREF_NOTIFY_ROOM_TRIGGER:
return "room.trigger";
case PREF_NOTIFY_ROOM_MENTION:
return "room.mention";
case PREF_NOTIFY_ROOM_CURRENT:
return "room.current";
case PREF_NOTIFY_ROOM_TEXT:
return "room.text";
case PREF_NOTIFY_INVITE:
return "invite";
case PREF_NOTIFY_SUB:
return "sub";
case PREF_CHLOG:
return "chlog";
case PREF_GRLOG:
return "grlog";
case PREF_AUTOAWAY_CHECK:
return "autoaway.check";
case PREF_AUTOAWAY_MODE:
return "autoaway.mode";
case PREF_AUTOAWAY_MESSAGE:
return "autoaway.awaymessage";
case PREF_AUTOXA_MESSAGE:
return "autoaway.xamessage";
case PREF_CONNECT_ACCOUNT:
return "account";
case PREF_DEFAULT_ACCOUNT:
return "defaccount";
case PREF_OTR_LOG:
return "log";
case PREF_OTR_POLICY:
return "policy";
case PREF_LOG_ROTATE:
return "rotate";
case PREF_LOG_SHARED:
return "shared";
case PREF_PRESENCE:
return "presence";
case PREF_WRAP:
return "wrap";
case PREF_WINS_AUTO_TIDY:
return "wins.autotidy";
case PREF_TIME_CONSOLE:
return "time.console";
case PREF_TIME_CHAT:
return "time.chat";
case PREF_TIME_MUC:
return "time.muc";
case PREF_TIME_MUCCONFIG:
return "time.mucconfig";
case PREF_TIME_PRIVATE:
return "time.private";
case PREF_TIME_XMLCONSOLE:
return "time.xmlconsole";
case PREF_TIME_STATUSBAR:
return "time.statusbar";
case PREF_TIME_LASTACTIVITY:
return "time.lastactivity";
case PREF_ROSTER:
return "roster";
case PREF_ROSTER_OFFLINE:
return "roster.offline";
case PREF_ROSTER_RESOURCE:
return "roster.resource";
case PREF_ROSTER_PRESENCE:
return "roster.presence";
case PREF_ROSTER_STATUS:
return "roster.status";
case PREF_ROSTER_EMPTY:
return "roster.empty";
case PREF_ROSTER_BY:
return "roster.by";
case PREF_ROSTER_ORDER:
return "roster.order";
case PREF_ROSTER_UNREAD:
return "roster.unread";
case PREF_ROSTER_COUNT:
return "roster.count";
case PREF_ROSTER_COUNT_ZERO:
return "roster.count.zero";
case PREF_ROSTER_PRIORITY:
return "roster.priority";
case PREF_ROSTER_WRAP:
return "roster.wrap";
case PREF_ROSTER_RESOURCE_JOIN:
return "roster.resource.join";
case PREF_ROSTER_CONTACTS:
return "roster.contacts";
case PREF_ROSTER_ROOMS:
return "roster.rooms";
case PREF_ROSTER_ROOMS_POS:
return "roster.rooms.pos";
case PREF_ROSTER_ROOMS_ORDER:
return "roster.rooms.order";
case PREF_ROSTER_ROOMS_UNREAD:
return "roster.rooms.unread";
case PREF_ROSTER_PRIVATE:
return "roster.private";
case PREF_RESOURCE_TITLE:
return "resource.title";
case PREF_RESOURCE_MESSAGE:
return "resource.message";
case PREF_INPBLOCK_DYNAMIC:
return "inpblock.dynamic";
case PREF_ENC_WARN:
return "enc.warn";
case PREF_PGP_LOG:
return "log";
case PREF_TLS_CERTPATH:
return "tls.certpath";
case PREF_TLS_SHOW:
return "tls.show";
case PREF_LASTACTIVITY:
return "lastactivity";
case PREF_CONSOLE_MUC:
return "console.muc";
default:
return NULL;
}
}
// the default setting for a boolean type preference
// if it is not specified in .profrc
static gboolean
_get_default_boolean(preference_t pref)
{
switch (pref)
{
case PREF_ENC_WARN:
case PREF_AUTOAWAY_CHECK:
case PREF_LOG_ROTATE:
case PREF_LOG_SHARED:
case PREF_NOTIFY_CHAT:
case PREF_NOTIFY_CHAT_CURRENT:
case PREF_NOTIFY_ROOM:
case PREF_NOTIFY_ROOM_CURRENT:
case PREF_NOTIFY_TYPING:
case PREF_NOTIFY_TYPING_CURRENT:
case PREF_NOTIFY_SUB:
case PREF_NOTIFY_INVITE:
case PREF_SPLASH:
case PREF_OCCUPANTS:
case PREF_MUC_PRIVILEGES:
case PREF_PRESENCE:
case PREF_WRAP:
case PREF_WINS_AUTO_TIDY:
case PREF_INPBLOCK_DYNAMIC:
case PREF_RESOURCE_TITLE:
case PREF_RESOURCE_MESSAGE:
case PREF_ROSTER:
case PREF_ROSTER_OFFLINE:
case PREF_ROSTER_EMPTY:
case PREF_ROSTER_COUNT_ZERO:
case PREF_ROSTER_PRIORITY:
case PREF_ROSTER_RESOURCE_JOIN:
case PREF_ROSTER_CONTACTS:
case PREF_ROSTER_ROOMS:
case PREF_TLS_SHOW:
case PREF_LASTACTIVITY:
return TRUE;
default:
return FALSE;
}
}
// the default setting for a string type preference
// if it is not specified in .profrc
static char*
_get_default_string(preference_t pref)
{
switch (pref)
{
case PREF_AUTOAWAY_MODE:
return "off";
case PREF_OTR_LOG:
return "redact";
case PREF_OTR_POLICY:
return "manual";
case PREF_STATUSES_CONSOLE:
case PREF_STATUSES_CHAT:
case PREF_STATUSES_MUC:
return "all";
case PREF_ROSTER_BY:
return "presence";
case PREF_ROSTER_COUNT:
return "unread";
case PREF_ROSTER_ORDER:
return "presence";
case PREF_ROSTER_UNREAD:
return "after";
case PREF_ROSTER_ROOMS_POS:
return "last";
case PREF_ROSTER_ROOMS_ORDER:
return "name";
case PREF_ROSTER_ROOMS_UNREAD:
return "after";
case PREF_ROSTER_PRIVATE:
return "room";
case PREF_TIME_CONSOLE:
return "%H:%M:%S";
case PREF_TIME_CHAT:
return "%H:%M:%S";
case PREF_TIME_MUC:
return "%H:%M:%S";
case PREF_TIME_MUCCONFIG:
return "%H:%M:%S";
case PREF_TIME_PRIVATE:
return "%H:%M:%S";
case PREF_TIME_XMLCONSOLE:
return "%H:%M:%S";
case PREF_TIME_STATUSBAR:
return "%H:%M";
case PREF_TIME_LASTACTIVITY:
return "%d/%m/%y %H:%M:%S";
case PREF_PGP_LOG:
return "redact";
case PREF_CONSOLE_MUC:
return "all";
default:
return NULL;
}
}
|