forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
3942 lines (3460 loc) · 101 KB
/
init.c
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
/**
* @file
* Config/command parsing
*
* @authors
* Copyright (C) 1996-2002,2010,2013,2016 Michael R. Elkins <me@mutt.org>
*
* @copyright
* This program 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 2 of the License, or (at your option) any later
* version.
*
* This program 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
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page init Config/command parsing
*
* Config/command parsing
*/
#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <pwd.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <wchar.h>
#include "mutt/mutt.h"
#include "email/lib.h"
#include "mutt.h"
#include "init.h"
#include "account.h"
#include "alias.h"
#include "context.h"
#include "filter.h"
#include "hcache/hcache.h"
#include "keymap.h"
#include "monitor.h"
#include "mutt_curses.h"
#include "mutt_menu.h"
#include "mutt_window.h"
#include "mx.h"
#include "myvar.h"
#include "ncrypt/ncrypt.h"
#include "options.h"
#include "protos.h"
#include "sidebar.h"
#include "version.h"
#ifdef USE_NOTMUCH
#include "notmuch/mutt_notmuch.h"
#endif
#ifdef USE_IMAP
#include "imap/imap.h"
#endif
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
/* LIFO designed to contain the list of config files that have been sourced and
* avoid cyclic sourcing */
static struct ListHead MuttrcStack = STAILQ_HEAD_INITIALIZER(MuttrcStack);
#define MAXERRS 128
#define NUMVARS mutt_array_size(MuttVars)
#define NUMCOMMANDS mutt_array_size(Commands)
/* Initial string that starts completion. No telling how much the user has
* typed so far. Allocate 1024 just to be sure! */
static char UserTyped[1024] = { 0 };
static int NumMatched = 0; /* Number of matches for completion */
static char Completed[256] = { 0 }; /* completed string (command or variable) */
static const char **Matches;
/* this is a lie until mutt_init runs: */
static int MatchesListsize = MAX(NUMVARS, NUMCOMMANDS) + 10;
#ifdef USE_NOTMUCH
/* List of tags found in last call to mutt_nm_query_complete(). */
static char **nm_tags;
#endif
/**
* enum GroupState - Type of email address group
*/
enum GroupState
{
GS_NONE, ///< Group is missing an argument
GS_RX, ///< Entry is a regular expression
GS_ADDR, ///< Entry is an address
};
/**
* add_to_stailq - Add a string to a list
* @param head String list
* @param str String to add
*
* @note Duplicate or empty strings will not be added
*/
static void add_to_stailq(struct ListHead *head, const char *str)
{
/* don't add a NULL or empty string to the list */
if (!str || (*str == '\0'))
return;
/* check to make sure the item is not already on this list */
struct ListNode *np = NULL;
STAILQ_FOREACH(np, head, entries)
{
if (mutt_str_strcasecmp(str, np->data) == 0)
{
return;
}
}
mutt_list_insert_tail(head, mutt_str_strdup(str));
}
/**
* alternates_clean - Clear the recipient valid flag of all emails
*/
static void alternates_clean(void)
{
if (!Context)
return;
for (int i = 0; i < Context->mailbox->msg_count; i++)
Context->mailbox->emails[i]->recip_valid = false;
}
/**
* attachments_clean - always wise to do what someone else did before
*/
static void attachments_clean(void)
{
if (!Context)
return;
for (int i = 0; i < Context->mailbox->msg_count; i++)
Context->mailbox->emails[i]->attach_valid = false;
}
/**
* matches_ensure_morespace - Allocate more space for auto-completion
* @param current Current allocation
*/
static void matches_ensure_morespace(int current)
{
if (current <= (MatchesListsize - 2))
return;
int base_space = MAX(NUMVARS, NUMCOMMANDS) + 1;
int extra_space = MatchesListsize - base_space;
extra_space *= 2;
const int space = base_space + extra_space;
mutt_mem_realloc(&Matches, space * sizeof(char *));
memset(&Matches[current + 1], 0, space - current);
MatchesListsize = space;
}
/**
* candidate - helper function for completion
* @param try User entered data for completion
* @param src Candidate for completion
* @param dest Completion result gets here
* @param dlen Length of dest buffer
*
* Changes the dest buffer if necessary/possible to aid completion.
*/
static void candidate(char *try, const char *src, char *dest, size_t dlen)
{
if (!dest || !try || !src)
return;
if (strstr(src, try) != src)
return;
matches_ensure_morespace(NumMatched);
Matches[NumMatched++] = src;
if (dest[0] == '\0')
mutt_str_strfcpy(dest, src, dlen);
else
{
int l;
for (l = 0; src[l] && src[l] == dest[l]; l++)
;
dest[l] = '\0';
}
}
/**
* clear_subject_mods - Clear out all modified email subjects
*/
static void clear_subject_mods(void)
{
if (!Context)
return;
for (int i = 0; i < Context->mailbox->msg_count; i++)
FREE(&Context->mailbox->emails[i]->env->disp_subj);
}
#ifdef USE_NOTMUCH
/**
* complete_all_nm_tags - Pass a list of Notmuch tags to the completion code
* @param pt List of all Notmuch tags
* @retval 0 Success
* @retval -1 Error
*/
static int complete_all_nm_tags(const char *pt)
{
int tag_count_1 = 0;
int tag_count_2 = 0;
NumMatched = 0;
mutt_str_strfcpy(UserTyped, pt, sizeof(UserTyped));
memset(Matches, 0, MatchesListsize);
memset(Completed, 0, sizeof(Completed));
nm_db_longrun_init(Context->mailbox, false);
/* Work out how many tags there are. */
if (nm_get_all_tags(Context->mailbox, NULL, &tag_count_1) || (tag_count_1 == 0))
goto done;
/* Free the old list, if any. */
if (nm_tags)
{
for (int i = 0; nm_tags[i]; i++)
FREE(&nm_tags[i]);
FREE(&nm_tags);
}
/* Allocate a new list, with sentinel. */
nm_tags = mutt_mem_malloc((tag_count_1 + 1) * sizeof(char *));
nm_tags[tag_count_1] = NULL;
/* Get all the tags. */
if (nm_get_all_tags(Context->mailbox, nm_tags, &tag_count_2) || (tag_count_1 != tag_count_2))
{
FREE(&nm_tags);
nm_tags = NULL;
nm_db_longrun_done(Context->mailbox);
return -1;
}
/* Put them into the completion machinery. */
for (int num = 0; num < tag_count_1; num++)
{
candidate(UserTyped, nm_tags[num], Completed, sizeof(Completed));
}
matches_ensure_morespace(NumMatched);
Matches[NumMatched++] = UserTyped;
done:
nm_db_longrun_done(Context->mailbox);
return 0;
}
#endif
/**
* execute_commands - Execute a set of NeoMutt commands
* @param p List of command strings
* @retval 0 Success, all the commands succeeded
* @retval -1 Error
*/
static int execute_commands(struct ListHead *p)
{
struct Buffer err, token;
mutt_buffer_init(&err);
err.dsize = 256;
err.data = mutt_mem_malloc(err.dsize);
mutt_buffer_init(&token);
struct ListNode *np = NULL;
STAILQ_FOREACH(np, p, entries)
{
if (mutt_parse_rc_line(np->data, &token, &err) == MUTT_CMD_ERROR)
{
mutt_error(_("Error in command line: %s"), err.data);
FREE(&token.data);
FREE(&err.data);
return -1;
}
}
FREE(&token.data);
FREE(&err.data);
return 0;
}
/**
* find_cfg - Find a config file
* @param home User's home directory
* @param xdg_cfg_home XDG home directory
* @retval ptr Success, first matching directory
* @retval NULL Error, no matching directories
*/
static char *find_cfg(const char *home, const char *xdg_cfg_home)
{
const char *names[] = {
"neomuttrc",
"muttrc",
NULL,
};
const char *locations[][2] = {
{ xdg_cfg_home, "neomutt/" },
{ xdg_cfg_home, "mutt/" },
{ home, ".neomutt/" },
{ home, ".mutt/" },
{ home, "." },
{ NULL, NULL },
};
for (int i = 0; locations[i][0] || locations[i][1]; i++)
{
if (!locations[i][0])
continue;
for (int j = 0; names[j]; j++)
{
char buf[256];
snprintf(buf, sizeof(buf), "%s/%s%s", locations[i][0], locations[i][1], names[j]);
if (access(buf, F_OK) == 0)
return mutt_str_strdup(buf);
}
}
return NULL;
}
#ifndef DOMAIN
/**
* getmailname - Try to retrieve the FQDN from mailname files
* @retval ptr Heap allocated string with the FQDN
* @retval NULL if no valid mailname file could be read
*/
static char *getmailname(void)
{
char *mailname = NULL;
static const char *mn_files[] = { "/etc/mailname", "/etc/mail/mailname" };
for (size_t i = 0; i < mutt_array_size(mn_files); i++)
{
FILE *fp = mutt_file_fopen(mn_files[i], "r");
if (!fp)
continue;
size_t len = 0;
mailname = mutt_file_read_line(NULL, &len, fp, NULL, 0);
mutt_file_fclose(&fp);
if (mailname && *mailname)
break;
FREE(&mailname);
}
return mailname;
}
#endif
/**
* get_hostname - Find the Fully-Qualified Domain Name
* @retval true Success
* @retval false Error, failed to find any name
*
* Use several methods to try to find the Fully-Qualified domain name of this host.
* If the user has already configured a hostname, this function will use it.
*/
static bool get_hostname(void)
{
char *str = NULL;
struct utsname utsname;
if (C_Hostname)
{
str = C_Hostname;
}
else
{
/* The call to uname() shouldn't fail, but if it does, the system is horribly
* broken, and the system's networking configuration is in an unreliable
* state. We should bail. */
if ((uname(&utsname)) == -1)
{
mutt_perror(_("unable to determine nodename via uname()"));
return false; // TEST09: can't test
}
str = utsname.nodename;
}
/* some systems report the FQDN instead of just the hostname */
char *dot = strchr(str, '.');
if (dot)
ShortHostname = mutt_str_substr_dup(str, dot);
else
ShortHostname = mutt_str_strdup(str);
if (!C_Hostname)
{
/* now get FQDN. Use configured domain first, DNS next, then uname */
#ifdef DOMAIN
/* we have a compile-time domain name, use that for C_Hostname */
C_Hostname =
mutt_mem_malloc(mutt_str_strlen(DOMAIN) + mutt_str_strlen(ShortHostname) + 2);
sprintf((char *) C_Hostname, "%s.%s", NONULL(ShortHostname), DOMAIN);
#else
C_Hostname = getmailname();
if (!C_Hostname)
{
char buffer[1024];
if (getdnsdomainname(buffer, sizeof(buffer)) == 0)
{
C_Hostname = mutt_mem_malloc(mutt_str_strlen(buffer) +
mutt_str_strlen(ShortHostname) + 2);
sprintf((char *) C_Hostname, "%s.%s", NONULL(ShortHostname), buffer);
}
else
{
/* DNS failed, use the nodename. Whether or not the nodename had a '.'
* in it, we can use the nodename as the FQDN. On hosts where DNS is
* not being used, e.g. small network that relies on hosts files, a
* short host name is all that is required for SMTP to work correctly.
* It could be wrong, but we've done the best we can, at this point the
* onus is on the user to provide the correct hostname if the nodename
* won't work in their network. */
C_Hostname = mutt_str_strdup(utsname.nodename);
}
}
#endif
}
if (C_Hostname)
cs_str_initial_set(Config, "hostname", C_Hostname, NULL);
return true;
}
/**
* parse_attach_list - Parse the "attachments" command
* @param buf Buffer for temporary storage
* @param s Buffer containing the attachments command
* @param head List of AttachMatch to add to
* @param err Buffer for error messages
* @retval enum e.g. #MUTT_CMD_SUCCESS
*/
static enum CommandResult parse_attach_list(struct Buffer *buf, struct Buffer *s,
struct ListHead *head, struct Buffer *err)
{
struct AttachMatch *a = NULL;
char *p = NULL;
char *tmpminor = NULL;
size_t len;
int ret;
do
{
mutt_extract_token(buf, s, 0);
if (!buf->data || (*buf->data == '\0'))
continue;
a = mutt_mem_malloc(sizeof(struct AttachMatch));
/* some cheap hacks that I expect to remove */
if (mutt_str_strcasecmp(buf->data, "any") == 0)
a->major = mutt_str_strdup("*/.*");
else if (mutt_str_strcasecmp(buf->data, "none") == 0)
a->major = mutt_str_strdup("cheap_hack/this_should_never_match");
else
a->major = mutt_str_strdup(buf->data);
p = strchr(a->major, '/');
if (p)
{
*p = '\0';
p++;
a->minor = p;
}
else
{
a->minor = "unknown";
}
len = strlen(a->minor);
tmpminor = mutt_mem_malloc(len + 3);
strcpy(&tmpminor[1], a->minor);
tmpminor[0] = '^';
tmpminor[len + 1] = '$';
tmpminor[len + 2] = '\0';
a->major_int = mutt_check_mime_type(a->major);
ret = REGCOMP(&a->minor_regex, tmpminor, REG_ICASE);
FREE(&tmpminor);
if (ret != 0)
{
regerror(ret, &a->minor_regex, err->data, err->dsize);
FREE(&a->major);
FREE(&a);
return MUTT_CMD_ERROR;
}
mutt_debug(LL_DEBUG3, "added %s/%s [%d]\n", a->major, a->minor, a->major_int);
mutt_list_insert_tail(head, (char *) a);
} while (MoreArgs(s));
attachments_clean();
return MUTT_CMD_SUCCESS;
}
/**
* parse_grouplist - Parse a group context
* @param ctx GroupList to add to
* @param buf Temporary Buffer space
* @param s Buffer containing string to be parsed
* @param data Flags associated with the command
* @param err Buffer for error messages
* @retval 0 Success
* @retval -1 Error
*/
static int parse_grouplist(struct GroupList *ctx, struct Buffer *buf,
struct Buffer *s, unsigned long data, struct Buffer *err)
{
while (mutt_str_strcasecmp(buf->data, "-group") == 0)
{
if (!MoreArgs(s))
{
mutt_buffer_strcpy(err, _("-group: no group name"));
goto bail;
}
mutt_extract_token(buf, s, 0);
mutt_grouplist_add(ctx, mutt_pattern_group(buf->data));
if (!MoreArgs(s))
{
mutt_buffer_strcpy(err, _("out of arguments"));
goto bail;
}
mutt_extract_token(buf, s, 0);
}
return 0;
bail:
return -1;
}
/**
* parse_replace_list - Parse a string replacement rule - Implements ::command_t
*/
static enum CommandResult parse_replace_list(struct Buffer *buf, struct Buffer *s,
unsigned long data, struct Buffer *err)
{
struct ReplaceList *list = (struct ReplaceList *) data;
struct Buffer templ = { 0 };
/* First token is a regex. */
if (!MoreArgs(s))
{
mutt_buffer_printf(err, _("%s: too few arguments"), "subjectrx");
return MUTT_CMD_WARNING;
}
mutt_extract_token(buf, s, 0);
/* Second token is a replacement template */
if (!MoreArgs(s))
{
mutt_buffer_printf(err, _("%s: too few arguments"), "subjectrx");
return MUTT_CMD_WARNING;
}
mutt_extract_token(&templ, s, 0);
if (mutt_replacelist_add(list, buf->data, templ.data, err) != 0)
{
FREE(&templ.data);
return MUTT_CMD_ERROR;
}
FREE(&templ.data);
return MUTT_CMD_SUCCESS;
}
/**
* parse_unattach_list - Parse the "unattachments" command
* @param buf Buffer for temporary storage
* @param s Buffer containing the unattachments command
* @param head List of AttachMatch to remove from
* @param err Buffer for error messages
* @retval #MUTT_CMD_SUCCESS Always
*/
static enum CommandResult parse_unattach_list(struct Buffer *buf, struct Buffer *s,
struct ListHead *head, struct Buffer *err)
{
struct AttachMatch *a = NULL;
char *tmp = NULL;
char *minor = NULL;
do
{
mutt_extract_token(buf, s, 0);
FREE(&tmp);
if (mutt_str_strcasecmp(buf->data, "any") == 0)
tmp = mutt_str_strdup("*/.*");
else if (mutt_str_strcasecmp(buf->data, "none") == 0)
tmp = mutt_str_strdup("cheap_hack/this_should_never_match");
else
tmp = mutt_str_strdup(buf->data);
minor = strchr(tmp, '/');
if (minor)
{
*minor = '\0';
minor++;
}
else
{
minor = "unknown";
}
const int major = mutt_check_mime_type(tmp);
struct ListNode *np, *tmp2;
STAILQ_FOREACH_SAFE(np, head, entries, tmp2)
{
a = (struct AttachMatch *) np->data;
mutt_debug(LL_DEBUG3, "check %s/%s [%d] : %s/%s [%d]\n", a->major,
a->minor, a->major_int, tmp, minor, major);
if ((a->major_int == major) && (mutt_str_strcasecmp(minor, a->minor) == 0))
{
mutt_debug(LL_DEBUG3, "removed %s/%s [%d]\n", a->major, a->minor, a->major_int);
regfree(&a->minor_regex);
FREE(&a->major);
STAILQ_REMOVE(head, np, ListNode, entries);
FREE(&np->data);
FREE(&np);
}
}
} while (MoreArgs(s));
FREE(&tmp);
attachments_clean();
return MUTT_CMD_SUCCESS;
}
/**
* parse_unreplace_list - Remove a string replacement rule - Implements ::command_t
*/
static enum CommandResult parse_unreplace_list(struct Buffer *buf, struct Buffer *s,
unsigned long data, struct Buffer *err)
{
struct ReplaceList *list = (struct ReplaceList *) data;
/* First token is a regex. */
if (!MoreArgs(s))
{
mutt_buffer_printf(err, _("%s: too few arguments"), "unsubjectrx");
return MUTT_CMD_WARNING;
}
mutt_extract_token(buf, s, 0);
/* "*" is a special case. */
if (mutt_str_strcmp(buf->data, "*") == 0)
{
mutt_replacelist_free(list);
return MUTT_CMD_SUCCESS;
}
mutt_replacelist_remove(list, buf->data);
return MUTT_CMD_SUCCESS;
}
/**
* print_attach_list - Print a list of attachments
* @param h List of attachments
* @param op Operation, e.g. '+', '-'
* @param name Attached/Inline, 'A', 'I'
* @retval 0 Always
*/
static int print_attach_list(struct ListHead *h, const char op, const char *name)
{
struct ListNode *np = NULL;
STAILQ_FOREACH(np, h, entries)
{
printf("attachments %c%s %s/%s\n", op, name,
((struct AttachMatch *) np->data)->major,
((struct AttachMatch *) np->data)->minor);
}
return 0;
}
/**
* remove_from_stailq - Remove an item, matching a string, from a List
* @param head Head of the List
* @param str String to match
*
* @note The string comparison is case-insensitive
*/
static void remove_from_stailq(struct ListHead *head, const char *str)
{
if (mutt_str_strcmp("*", str) == 0)
mutt_list_free(head); /* "unCMD *" means delete all current entries */
else
{
struct ListNode *np, *tmp;
STAILQ_FOREACH_SAFE(np, head, entries, tmp)
{
if (mutt_str_strcasecmp(str, np->data) == 0)
{
STAILQ_REMOVE(head, np, ListNode, entries);
FREE(&np->data);
FREE(&np);
break;
}
}
}
}
/**
* source_rc - Read an initialization file
* @param rcfile_path Path to initialization file
* @param err Buffer for error messages
* @retval <0 if neomutt should pause to let the user know
*/
static int source_rc(const char *rcfile_path, struct Buffer *err)
{
int line = 0, rc = 0, warnings = 0;
enum CommandResult line_rc;
struct Buffer token;
char *linebuf = NULL;
char *currentline = NULL;
char rcfile[PATH_MAX];
size_t buflen;
size_t rcfilelen;
bool ispipe;
pid_t pid;
mutt_str_strfcpy(rcfile, rcfile_path, sizeof(rcfile));
rcfilelen = mutt_str_strlen(rcfile);
if (rcfilelen == 0)
return -1;
ispipe = rcfile[rcfilelen - 1] == '|';
if (!ispipe)
{
struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
if (!mutt_path_to_absolute(rcfile, np ? NONULL(np->data) : ""))
{
mutt_error(_("Error: impossible to build path of '%s'"), rcfile_path);
return -1;
}
STAILQ_FOREACH(np, &MuttrcStack, entries)
{
if (mutt_str_strcmp(np->data, rcfile) == 0)
{
break;
}
}
if (!np)
{
mutt_list_insert_head(&MuttrcStack, mutt_str_strdup(rcfile));
}
else
{
mutt_error(_("Error: Cyclic sourcing of configuration file '%s'"), rcfile);
return -1;
}
}
mutt_debug(LL_DEBUG2, "Reading configuration file '%s'.\n", rcfile);
FILE *fp = mutt_open_read(rcfile, &pid);
if (!fp)
{
mutt_buffer_printf(err, "%s: %s", rcfile, strerror(errno));
return -1;
}
mutt_buffer_init(&token);
while ((linebuf = mutt_file_read_line(linebuf, &buflen, fp, &line, MUTT_CONT)))
{
const int conv = C_ConfigCharset && (*C_ConfigCharset) && C_Charset;
if (conv)
{
currentline = mutt_str_strdup(linebuf);
if (!currentline)
continue;
mutt_ch_convert_string(¤tline, C_ConfigCharset, C_Charset, 0);
}
else
currentline = linebuf;
mutt_buffer_reset(err);
line_rc = mutt_parse_rc_line(currentline, &token, err);
if (line_rc == MUTT_CMD_ERROR)
{
mutt_error(_("Error in %s, line %d: %s"), rcfile, line, err->data);
if (--rc < -MAXERRS)
{
if (conv)
FREE(¤tline);
break;
}
}
else if (line_rc == MUTT_CMD_WARNING)
{
/* Warning */
mutt_warning(_("Warning in %s, line %d: %s"), rcfile, line, err->data);
warnings++;
}
else if (line_rc == MUTT_CMD_FINISH)
{
break; /* Found "finish" command */
}
else
{
if (rc < 0)
rc = -1;
}
if (conv)
FREE(¤tline);
}
FREE(&token.data);
FREE(&linebuf);
mutt_file_fclose(&fp);
if (pid != -1)
mutt_wait_filter(pid);
if (rc)
{
/* the neomuttrc source keyword */
mutt_buffer_reset(err);
mutt_buffer_printf(err, (rc >= -MAXERRS) ? _("source: errors in %s") : _("source: reading aborted due to too many errors in %s"),
rcfile);
rc = -1;
}
else
{
/* Don't alias errors with warnings */
if (warnings > 0)
{
mutt_buffer_printf(err, ngettext("source: %d warning in %s", "source: %d warnings in %s", warnings),
warnings, rcfile);
rc = -2;
}
}
if (!ispipe && !STAILQ_EMPTY(&MuttrcStack))
{
struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
STAILQ_REMOVE_HEAD(&MuttrcStack, entries);
FREE(&np->data);
FREE(&np);
}
return rc;
}
/**
* parse_alias - Parse the 'alias' command - Implements ::command_t
*/
static enum CommandResult parse_alias(struct Buffer *buf, struct Buffer *s,
unsigned long data, struct Buffer *err)
{
struct Alias *tmp = NULL;
char *estr = NULL;
struct GroupList gc = STAILQ_HEAD_INITIALIZER(gc);
if (!MoreArgs(s))
{
mutt_buffer_strcpy(err, _("alias: no address"));
return MUTT_CMD_WARNING;
}
mutt_extract_token(buf, s, 0);
if (parse_grouplist(&gc, buf, s, data, err) == -1)
return MUTT_CMD_ERROR;
/* check to see if an alias with this name already exists */
TAILQ_FOREACH(tmp, &Aliases, entries)
{
if (mutt_str_strcasecmp(tmp->name, buf->data) == 0)
break;
}
if (!tmp)
{
/* create a new alias */
tmp = mutt_mem_calloc(1, sizeof(struct Alias));
tmp->name = mutt_str_strdup(buf->data);
TAILQ_INSERT_TAIL(&Aliases, tmp, entries);
/* give the main addressbook code a chance */
if (CurrentMenu == MENU_ALIAS)
OptMenuCaller = true;
}
else
{
mutt_alias_delete_reverse(tmp);
/* override the previous value */
mutt_addr_free(&tmp->addr);
if (CurrentMenu == MENU_ALIAS)
mutt_menu_set_current_redraw_full();
}
mutt_extract_token(buf, s, MUTT_TOKEN_QUOTE | MUTT_TOKEN_SPACE | MUTT_TOKEN_SEMICOLON);
mutt_debug(5, "Second token is '%s'.\n", buf->data);
tmp->addr = mutt_addr_parse_list2(tmp->addr, buf->data);
if (mutt_addrlist_to_intl(tmp->addr, &estr))
{
mutt_buffer_printf(err, _("Warning: Bad IDN '%s' in alias '%s'"), estr, tmp->name);
FREE(&estr);
goto bail;
}
mutt_grouplist_add_addrlist(&gc, tmp->addr);
mutt_alias_add_reverse(tmp);
if (C_DebugLevel > 2)
{
/* A group is terminated with an empty address, so check a->mailbox */
for (struct Address *a = tmp->addr; a && a->mailbox; a = a->next)
{
if (!a->group)
mutt_debug(5, " %s\n", a->mailbox);
else
mutt_debug(5, " Group %s\n", a->mailbox);
}
}
mutt_grouplist_destroy(&gc);
return MUTT_CMD_SUCCESS;
bail:
mutt_grouplist_destroy(&gc);
return MUTT_CMD_ERROR;
}
/**
* parse_alternates - Parse the 'alternates' command - Implements ::command_t
*/
static enum CommandResult parse_alternates(struct Buffer *buf, struct Buffer *s,
unsigned long data, struct Buffer *err)
{
struct GroupList gc = STAILQ_HEAD_INITIALIZER(gc);
alternates_clean();
do
{
mutt_extract_token(buf, s, 0);
if (parse_grouplist(&gc, buf, s, data, err) == -1)
goto bail;
mutt_regexlist_remove(&UnAlternates, buf->data);
if (mutt_regexlist_add(&Alternates, buf->data, REG_ICASE, err) != 0)
goto bail;
if (mutt_grouplist_add_regex(&gc, buf->data, REG_ICASE, err) != 0)
goto bail;
} while (MoreArgs(s));
mutt_grouplist_destroy(&gc);
return MUTT_CMD_SUCCESS;
bail:
mutt_grouplist_destroy(&gc);
return MUTT_CMD_ERROR;
}