s/pfolder/curfolder/g
[mmh] / uip / burst.c
1 /*
2 ** burst.c -- explode digests into individual messages
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11 static struct swit switches[] = {
12 #define INPLSW 0
13         { "inplace", 0 },
14 #define NINPLSW 1
15         { "noinplace", 0 },
16 #define QIETSW 2
17         { "quiet", 0 },
18 #define NQIETSW 3
19         { "noquiet", 0 },
20 #define VERBSW 4
21         { "verbose", 0 },
22 #define NVERBSW 5
23         { "noverbose", 0 },
24 #define VERSIONSW 6
25         { "version", 0 },
26 #define HELPSW 7
27         { "help", 0 },
28         { NULL, 0 }
29 };
30
31 static char delim3[] = "-------";
32
33 struct smsg {
34         long s_start;
35         long s_stop;
36 };
37
38 /*
39 ** static prototypes
40 */
41 static int find_delim(int, struct smsg *);
42 static void burst(struct msgs **, int, struct smsg *, int, int, int, char *);
43 static void cpybrst(FILE *, FILE *, char *, char *, int);
44
45
46 int
47 main(int argc, char **argv)
48 {
49         int inplace = 0, quietsw = 0, verbosw = 0;
50         int msgp = 0, hi, msgnum, numburst;
51         char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
52         char **argp, **arguments, *msgs[MAXARGS];
53         struct smsg *smsgs;
54         struct msgs *mp;
55
56 #ifdef LOCALE
57         setlocale(LC_ALL, "");
58 #endif
59         invo_name = mhbasename(argv[0]);
60
61         /* read user profile/context */
62         context_read();
63
64         arguments = getarguments(invo_name, argc, argv, 1);
65         argp = arguments;
66
67         while ((cp = *argp++)) {
68                 if (*cp == '-') {
69                         switch (smatch(++cp, switches)) {
70                         case AMBIGSW:
71                                 ambigsw(cp, switches);
72                                 done(1);
73                         case UNKWNSW:
74                                 adios(NULL, "-%s unknown\n", cp);
75
76                         case HELPSW:
77                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
78                                 print_help(buf, switches, 1);
79                                 done(1);
80                         case VERSIONSW:
81                                 print_version(invo_name);
82                                 done(1);
83
84                         case INPLSW:
85                                 inplace++;
86                                 continue;
87                         case NINPLSW:
88                                 inplace = 0;
89                                 continue;
90
91                         case QIETSW:
92                                 quietsw++;
93                                 continue;
94                         case NQIETSW:
95                                 quietsw = 0;
96                                 continue;
97
98                         case VERBSW:
99                                 verbosw++;
100                                 continue;
101                         case NVERBSW:
102                                 verbosw = 0;
103                                 continue;
104                         }
105                 }
106                 if (*cp == '+' || *cp == '@') {
107                         if (folder)
108                                 adios(NULL, "only one folder at a time!");
109                         else
110                                 folder = getcpy(expandfol(cp));
111                 } else {
112                         msgs[msgp++] = cp;
113                 }
114         }
115
116         if (!msgp)
117                 msgs[msgp++] = "cur";
118         if (!folder)
119                 folder = getcurfol();
120         maildir = toabsdir(folder);
121
122         if (chdir(maildir) == NOTOK)
123                 adios(maildir, "unable to change directory to");
124
125         /* read folder and create message structure */
126         if (!(mp = folder_read(folder)))
127                 adios(NULL, "unable to read folder %s", folder);
128
129         /* check for empty folder */
130         if (mp->nummsg == 0)
131                 adios(NULL, "no messages in %s", folder);
132
133         /* parse all the message ranges/sequences and set SELECTED */
134         for (msgnum = 0; msgnum < msgp; msgnum++)
135                 if (!m_convert(mp, msgs[msgnum]))
136                         done(1);
137         seq_setprev(mp);  /* set the previous-sequence */
138
139         smsgs = (struct smsg *)
140                 calloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
141         if (smsgs == NULL)
142                 adios(NULL, "unable to allocate burst storage");
143
144         hi = mp->hghmsg + 1;
145
146         /* burst all the SELECTED messages */
147         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
148                 if (is_selected(mp, msgnum)) {
149                         if ((numburst = find_delim(msgnum, smsgs)) >= 1) {
150                                 if (verbosw)
151                                         printf("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum);
152                                 burst(&mp, msgnum, smsgs, numburst, inplace, verbosw, maildir);
153                         } else {
154                                 if (numburst == 0) {
155                                         if (!quietsw)
156                                                 admonish(NULL, "message %d not in digest format", msgnum);
157                                 }  /* this pair of braces was missing before 1999-07-15 */
158                                 else
159                                         adios(NULL, "burst() botch -- you lose big");
160                         }
161                 }
162         }
163
164         free((char *) smsgs);
165         context_replace(curfolder, folder);  /* update current folder */
166
167         /*
168         ** If -inplace is given, then the first message burst becomes
169         ** the current message (which will now show a table of contents).
170         ** Otherwise, the first message extracted from the first digest
171         ** becomes the current message.
172         */
173         if (inplace) {
174                 if (mp->lowsel != mp->curmsg)
175                         seq_setcur(mp, mp->lowsel);
176         } else {
177                 if (hi <= mp->hghmsg)
178                         seq_setcur(mp, hi);
179         }
180
181         seq_save(mp);  /* synchronize message sequences */
182         context_save(); /* save the context file */
183         folder_free(mp); /* free folder/message structure */
184         done(0);
185         return 1;
186 }
187
188
189 /*
190 ** Scan the message and find the beginning and
191 ** end of all the messages in the digest.
192 */
193
194 static int
195 find_delim(int msgnum, struct smsg *smsgs)
196 {
197         int ld3, wasdlm, msgp;
198         long pos;
199         char c, *msgnam;
200         int cc;
201         char buffer[BUFSIZ];
202         FILE *in;
203
204         ld3 = strlen(delim3);
205
206         if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
207                 adios(msgnam, "unable to read message");
208
209         for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
210                 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
211                         pos += (long) strlen(buffer);
212                 if (feof(in))
213                         break;
214                 fseek(in, pos, SEEK_SET);
215                 smsgs[msgp].s_start = pos;
216
217                 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
218                         if (strncmp(buffer, delim3, ld3) == 0
219                                         && (msgp == 1 || c == '\n')
220                                         && ((cc = peekc(in)) == '\n' ||
221                                         cc == EOF))
222                                 break;
223                         else
224                                 pos += (long) strlen(buffer);
225                 }
226
227                 wasdlm = strncmp(buffer, delim3, ld3) == 0;
228                 if (smsgs[msgp].s_start != pos)
229                         smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ?
230                                         pos - 1 : pos;
231                 if (feof(in)) {
232 #if 0
233                         if (wasdlm) {
234                                 smsgs[msgp - 1].s_stop -=
235                                                 ((long) strlen(buffer) + 1);
236                                 msgp++;  /* fake "End of XXX Digest" */
237                         }
238 #endif
239                         break;
240                 }
241                 pos += (long) strlen(buffer);
242         }
243
244         fclose(in);
245         return (msgp - 1);  /* toss "End of XXX Digest" */
246 }
247
248
249 /*
250 ** Burst out the messages in the digest into the folder
251 */
252
253 static void
254 burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
255         int inplace, int verbosw, char *maildir)
256 {
257         int i, j, mode;
258         char *msgnam;
259         char f1[BUFSIZ], f2[BUFSIZ], f3[BUFSIZ];
260         FILE *in, *out;
261         struct stat st;
262         struct msgs *mp;
263
264         if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
265                 adios(msgnam, "unable to read message");
266
267         mode = fstat(fileno(in), &st) != NOTOK ?
268                         (st.st_mode & 0777) : m_gmprot();
269         mp = *mpp;
270
271         /*
272         ** See if we have enough space in the folder
273         ** structure for all the new messages.
274         */
275         if ((mp->hghmsg + numburst > mp->hghoff) &&
276                 !(mp = folder_realloc(mp, mp->lowoff, mp->hghmsg + numburst)))
277                 adios(NULL, "unable to allocate folder storage");
278         *mpp = mp;
279
280         j = mp->hghmsg;  /* old value */
281         mp->hghmsg += numburst;
282         mp->nummsg += numburst;
283
284         /*
285         ** If this is not the highest SELECTED message, then
286         ** increment mp->hghsel by numburst, since the highest
287         ** SELECTED is about to be slid down by that amount.
288         */
289         if (msgnum < mp->hghsel)
290                 mp->hghsel += numburst;
291
292         /*
293         ** If -inplace is given, renumber the messages after the
294         ** source message, to make room for each of the messages
295         ** contained within the digest.
296         **
297         ** This is equivalent to refiling a message from the point
298         ** of view of the external hooks.
299         */
300         if (inplace) {
301                 for (i = mp->hghmsg; j > msgnum; i--, j--) {
302                         strncpy(f1, m_name(i), sizeof(f1));
303                         strncpy(f2, m_name(j), sizeof(f2));
304                         if (does_exist(mp, j)) {
305                                 if (verbosw)
306                                         printf("message %d becomes message %d\n", j, i);
307
308                                 if (rename(f2, f1) == NOTOK)
309                                         admonish(f1, "unable to rename %s to", f2);
310
311                                 snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
312                                 snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
313                                 ext_hook("ref-hook", f1, f2);
314
315                                 copy_msg_flags(mp, i, j);
316                                 clear_msg_flags(mp, j);
317                                 mp->msgflags |= SEQMOD;
318                         }
319                 }
320         }
321
322         unset_selected(mp, msgnum);
323
324         /* new hghmsg is hghmsg + numburst
325         **
326         ** At this point, there is an array of numburst smsgs, each
327         ** element of which contains the starting and stopping offsets
328         ** (seeks) of the message in the digest.  The inplace flag is set
329         ** if the original digest is replaced by a message containing
330         ** the table of contents.  smsgs[0] is that table of contents.
331         ** Go through the message numbers in reverse order (high to low).
332         **
333         ** Set f1 to the name of the destination message, f2 to the name
334         ** of a scratch file.  Extract a message from the digest to the
335         ** scratch file.  Move the original message to a backup file if
336         ** the destination message number is the same as the number of
337         ** the original message, which only happens if the inplace flag
338         ** is set.  Then move the scratch file to the destination message.
339         **
340         ** Moving the original message to the backup file is equivalent
341         ** to deleting the message from the point of view of the external
342         ** hooks.  And bursting each message is equivalent to adding a
343         ** new message.
344         */
345
346         i = inplace ? msgnum + numburst : mp->hghmsg;
347         for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
348                 strncpy(f1, m_name(i), sizeof(f1));
349                 strncpy(f2, m_mktemp(invo_name, NULL, &out), sizeof(f2));
350
351                 if (verbosw && i != msgnum)
352                         printf("message %d of digest %d becomes message %d\n",
353                                         j, msgnum, i);
354
355                 chmod(f2, mode);
356                 fseek(in, smsgs[j].s_start, SEEK_SET);
357                 cpybrst(in, out, msgnam, f2,
358                                 (int) (smsgs[j].s_stop - smsgs[j].s_start));
359                 fclose(out);
360
361                 if (i == msgnum) {
362                         strncpy(f3, m_backup(f1), sizeof(f3));
363                         if (rename(f1, f3) == NOTOK)
364                                 admonish(f3, "unable to rename %s to", f1);
365
366                         (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
367                         ext_hook("del-hook", f3, (char *)0);
368                 }
369                 if (rename(f2, f1) == NOTOK)
370                         admonish(f1, "unable to rename %s to", f2);
371
372                 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
373                 ext_hook("add-hook", f3, (char *)0);
374
375                 copy_msg_flags(mp, i, msgnum);
376                 mp->msgflags |= SEQMOD;
377         }
378
379         fclose(in);
380 }
381
382
383 #define S1  0
384 #define S2  1
385 #define S3  2
386
387 /*
388 ** Copy a mesage which is being burst out of a digest.
389 ** It will remove any "dashstuffing" in the message.
390 */
391
392 static void
393 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
394 {
395         register int c, state;
396
397         for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
398                 if (c == 0)
399                         continue;
400                 switch (state) {
401                         case S1:
402                                 switch (c) {
403                                         case '-':
404                                                 state = S3;
405                                                 break;
406
407                                         default:
408                                                 state = S2;
409                                         case '\n':
410                                                 fputc(c, out);
411                                                 break;
412                                 }
413                                 break;
414
415                         case S2:
416                                 switch (c) {
417                                         case '\n':
418                                                 state = S1;
419                                         default:
420                                                 fputc(c, out);
421                                                 break;
422                                 }
423                                 break;
424
425                         case S3:
426                                 switch (c) {
427                                         case ' ':
428                                                 state = S2;
429                                                 break;
430
431                                         default:
432                                                 state = (c == '\n') ? S1 : S2;
433                                                 fputc('-', out);
434                                                 fputc(c, out);
435                                                 break;
436                                 }
437                                 break;
438                 }
439         }
440
441         if (ferror(in) && !feof(in))
442                 adios(ifile, "error reading");
443         if (ferror(out))
444                 adios(ofile, "error writing");
445 }