Fix out-of-bounds error when incorporating email from stdin
[mmh] / sbr / seq_read.c
1 /*
2 ** seq_read.c -- read the .mh_sequence file and
3 **            -- initialize sequence information
4 **
5 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
8 */
9
10 #include <sysexits.h>
11 #include <h/mh.h>
12 #include <h/utils.h>
13
14 /*
15 ** static prototypes
16 */
17 static int seq_init(struct msgs *, char *, char *);
18 static void seq_public(struct msgs *);
19 static void seq_private(struct msgs *);
20
21
22 /*
23 ** Get the sequence information for this folder from
24 ** .mh_sequences (or equivalent specified in .mh_profile)
25 ** or context file (for private sequences).
26 */
27
28 void
29 seq_read(struct msgs *mp)
30 {
31         /*
32         ** Initialize the list of sequence names.  Go ahead and
33         ** add the cur sequence to the list of sequences.
34         */
35         mp->msgattrs[0] = mh_xstrdup(seq_cur);
36         mp->msgattrs[1] = NULL;
37         make_all_public(mp);  /* initially, make all public */
38
39         /* If folder is empty, don't scan for sequence information */
40         if (mp->nummsg == 0)
41                 return;
42
43         /* Initialize the public sequences */
44         seq_public(mp);
45
46         /* Initialize the private sequences */
47         seq_private(mp);
48 }
49
50
51 /*
52 ** read folder's sequences file for public sequences
53 */
54
55 static void
56 seq_public(struct msgs *mp)
57 {
58         enum state state;
59         struct field f = {{0}};
60         char seqfile[PATH_MAX];
61         FILE *fp;
62
63         /*
64         ** If public sequences are disabled (e.g. the user has defined
65         ** an empty `Mh-Sequences' profile entry), then just return.
66         */
67         if (mh_seq == NULL || *mh_seq == '\0')
68                 return;
69
70         /* get filename of sequence file */
71         snprintf(seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
72
73         if ((fp = lkfopen(seqfile, "r")) == NULL)
74                 return;
75
76         /* Use m_getfld to scan sequence file */
77         for (state = FLD2;;) {
78                 switch (state = m_getfld2(state, &f, fp)) {
79                 case LENERR2:
80                         state = FLD2;
81                         /* FALL */
82                 case FLD2:
83                         seq_init(mp, mh_xstrdup(f.name), trimcpy(f.value));
84                         continue;
85
86                 case BODY2:
87                         adios(EX_CONFIG, NULL, "no blank lines are permitted in %s",
88                                         seqfile);
89                         /* FALL */
90
91                 case FILEEOF2:
92                         break;
93
94                 default:
95                         adios(EX_CONFIG, NULL, "%s is poorly formatted", seqfile);
96                 }
97                 break;
98         }
99
100         lkfclose(fp, seqfile);
101 }
102
103
104 /*
105 ** Scan profile/context list for private sequences.
106 **
107 ** We search the context list for all keys that look like
108 ** "atr-seqname-folderpath", and add them as private sequences.
109 */
110
111 static void
112 seq_private(struct msgs *mp)
113 {
114         int i, j, alen, plen;
115         char *cp;
116         struct node *np;
117
118         alen = strlen("atr-");
119         plen = strlen(mp->foldpath) + 1;
120
121         for (np = m_defs; np; np = np->n_next) {
122                 if (strncmp(np->n_name, "atr-", alen)==0 &&
123                                 (j = strlen(np->n_name) - plen) > alen &&
124                                 *(np->n_name + j) == '-' &&
125                                 strcmp(mp->foldpath, np->n_name + j + 1)==0) {
126                         cp = mh_xstrdup(np->n_name + alen);
127                         *(cp + j - alen) = '\0';
128                         if ((i = seq_init(mp, cp, mh_xstrdup(np->n_field))) != -1)
129                                 make_seq_private(mp, i);
130                 }
131         }
132 }
133
134
135 /*
136 ** Add the name of sequence to the list of folder sequences.
137 ** Then parse the list of message ranges for this
138 ** sequence, and setup the various bit flags for each
139 ** message in the sequence.
140 **
141 ** Return internal index for the sequence if successful.
142 ** Return -1 on error.
143 */
144
145 static int
146 seq_init(struct msgs *mp, char *name, char *field)
147 {
148         unsigned int i;
149         int j, k, is_current;
150         char *cp, **ap;
151
152         /*
153         ** Check if this is the cur sequence,
154         ** so we can do some special things.
155         */
156         is_current = (strcmp(seq_cur, name)==0);
157
158         /*
159         ** Search for this sequence name to see if we've seen
160         ** it already.  If we've seen this sequence before,
161         ** then clear the bit for this sequence from all the
162         ** mesages in this folder.
163         */
164         for (i = 0; mp->msgattrs[i]; i++) {
165                 if (strcmp(mp->msgattrs[i], name)==0) {
166                         for (j = mp->lowmsg; j <= mp->hghmsg; j++)
167                                 clear_sequence(mp, i, j);
168                         break;
169                 }
170         }
171
172         /* Return error, if too many sequences */
173         if (i >= NUMATTRS) {
174                 mh_free0(&name);
175                 mh_free0(&field);
176                 return -1;
177         }
178
179         /*
180         ** If we've already seen this sequence name, just free the
181         ** name string.  Else add it to the list of sequence names.
182         */
183         if (mp->msgattrs[i]) {
184                 mh_free0(&name);
185         } else {
186                 mp->msgattrs[i] = name;
187                 mp->msgattrs[i + 1] = NULL;
188         }
189
190         /*
191         ** Split up the different message ranges at whitespace
192         */
193         for (ap = brkstring(field, " ", "\n"); *ap; ap++) {
194                 if ((cp = strchr(*ap, '-')))
195                         *cp++ = '\0';
196                 if ((j = m_atoi(*ap)) > 0) {
197                         k = cp ? m_atoi(cp) : j;
198
199                         /*
200                         ** Keep mp->curmsg and cur sequence in sync.  Unlike
201                         ** other sequences, this message doesn't need to exist.
202                         ** Think about the series of command (rmm; next) to
203                         ** understand why this can be the case.  But if it does
204                         ** exist, we will still set the bit flag for it like
205                         ** other sequences.
206                         */
207                         if (is_current)
208                                 mp->curmsg = j;
209                         /*
210                         ** We iterate through messages in this range
211                         ** and flip on bit for this sequence.
212                         */
213                         for (; j <= k; j++) {
214                                 if (j >= mp->lowmsg && j <= mp->hghmsg &&
215                                                 does_exist(mp, j))
216                                         add_sequence(mp, i, j);
217                         }
218                 }
219         }
220
221         mh_free0(&field);  /* free string containing message ranges */
222         return i;
223 }