Reformated comments and long lines
[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 = r1bindex (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]",
78                                                 invo_name);
79                                 print_help (buf, switches, 1);
80                                 done (1);
81                         case VERSIONSW:
82                                 print_version(invo_name);
83                                 done (1);
84
85                         case INPLSW:
86                                 inplace++;
87                                 continue;
88                         case NINPLSW:
89                                 inplace = 0;
90                                 continue;
91
92                         case QIETSW:
93                                 quietsw++;
94                                 continue;
95                         case NQIETSW:
96                                 quietsw = 0;
97                                 continue;
98
99                         case VERBSW:
100                                 verbosw++;
101                                 continue;
102                         case NVERBSW:
103                                 verbosw = 0;
104                                 continue;
105                         }
106                 }
107                 if (*cp == '+' || *cp == '@') {
108                         if (folder)
109                                 adios (NULL, "only one folder at a time!");
110                         else
111                                 folder = pluspath (cp);
112                 } else {
113                         msgs[msgp++] = cp;
114                 }
115         }
116
117         if (!context_find ("path"))
118                 free (path ("./", TFOLDER));
119         if (!msgp)
120                 msgs[msgp++] = "cur";
121         if (!folder)
122                 folder = getfolder (1);
123         maildir = m_maildir (folder);
124
125         if (chdir (maildir) == NOTOK)
126                 adios (maildir, "unable to change directory to");
127
128         /* read folder and create message structure */
129         if (!(mp = folder_read (folder)))
130                 adios (NULL, "unable to read folder %s", folder);
131
132         /* check for empty folder */
133         if (mp->nummsg == 0)
134                 adios (NULL, "no messages in %s", folder);
135
136         /* parse all the message ranges/sequences and set SELECTED */
137         for (msgnum = 0; msgnum < msgp; msgnum++)
138                 if (!m_convert (mp, msgs[msgnum]))
139                         done (1);
140         seq_setprev (mp);  /* set the previous-sequence */
141
142         smsgs = (struct smsg *)
143                 calloc ((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
144         if (smsgs == NULL)
145                 adios (NULL, "unable to allocate burst storage");
146
147         hi = mp->hghmsg + 1;
148
149         /* burst all the SELECTED messages */
150         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
151                 if (is_selected (mp, msgnum)) {
152                         if ((numburst = find_delim (msgnum, smsgs)) >= 1) {
153                                 if (verbosw)
154                                         printf ("%d message%s exploded from digest %d\n",
155                                                         numburst, numburst > 1 ? "s" : "", msgnum);
156                                 burst (&mp, msgnum, smsgs, numburst, inplace, verbosw, maildir);
157                         } else {
158                                 if (numburst == 0) {
159                                         if (!quietsw)
160                                                 admonish (NULL, "message %d not in digest format",
161                                                                   msgnum);
162                                 }  /* this pair of braces was missing before 1999-07-15 */
163                                 else
164                                         adios (NULL, "burst() botch -- you lose big");
165                         }
166                 }
167         }
168
169         free ((char *) smsgs);
170         context_replace (pfolder, folder);  /* update current folder */
171
172         /*
173         ** If -inplace is given, then the first message burst becomes
174         ** the current message (which will now show a table of contents).
175         ** Otherwise, the first message extracted from the first digest
176         ** becomes the current message.
177         */
178         if (inplace) {
179                 if (mp->lowsel != mp->curmsg)
180                         seq_setcur (mp, mp->lowsel);
181         } else {
182                 if (hi <= mp->hghmsg)
183                         seq_setcur (mp, hi);
184         }
185
186         seq_save (mp);  /* synchronize message sequences */
187         context_save (); /* save the context file */
188         folder_free (mp); /* free folder/message structure */
189         done (0);
190         return 1;
191 }
192
193
194 /*
195 ** Scan the message and find the beginning and
196 ** end of all the messages in the digest.
197 */
198
199 static int
200 find_delim (int msgnum, struct smsg *smsgs)
201 {
202         int ld3, wasdlm, msgp;
203         long pos;
204         char c, *msgnam;
205         int cc;
206         char buffer[BUFSIZ];
207         FILE *in;
208
209         ld3 = strlen (delim3);
210
211         if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
212                 adios (msgnam, "unable to read message");
213
214         for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
215                 while (fgets (buffer, sizeof(buffer), in) && buffer[0] == '\n')
216                         pos += (long) strlen (buffer);
217                 if (feof (in))
218                         break;
219                 fseek (in, pos, SEEK_SET);
220                 smsgs[msgp].s_start = pos;
221
222                 for (c = 0; fgets (buffer, sizeof(buffer), in); c = buffer[0]) {
223                         if (strncmp (buffer, delim3, ld3) == 0
224                                         && (msgp == 1 || c == '\n')
225                                         && ((cc = peekc (in)) == '\n' || cc == EOF))
226                                 break;
227                         else
228                                 pos += (long) strlen (buffer);
229                 }
230
231                 wasdlm = strncmp (buffer, delim3, ld3) == 0;
232                 if (smsgs[msgp].s_start != pos)
233                         smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos;
234                 if (feof (in)) {
235 #if 0
236                         if (wasdlm) {
237                                 smsgs[msgp - 1].s_stop -= ((long) strlen (buffer) + 1);
238                                 msgp++;  /* fake "End of XXX Digest" */
239                         }
240 #endif
241                         break;
242                 }
243                 pos += (long) strlen (buffer);
244         }
245
246         fclose (in);
247         return (msgp - 1);  /* toss "End of XXX Digest" */
248 }
249
250
251 /*
252 ** Burst out the messages in the digest into the folder
253 */
254
255 static void
256 burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
257         int inplace, int verbosw, char *maildir)
258 {
259         int i, j, mode;
260         char *msgnam;
261         char f1[BUFSIZ], f2[BUFSIZ], f3[BUFSIZ];
262         FILE *in, *out;
263         struct stat st;
264         struct msgs *mp;
265
266         if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
267                 adios (msgnam, "unable to read message");
268
269         mode = fstat (fileno(in), &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
270         mp = *mpp;
271
272         /*
273         ** See if we have enough space in the folder
274         ** structure for all the new messages.
275         */
276         if ((mp->hghmsg + numburst > mp->hghoff) &&
277                 !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + numburst)))
278                 adios (NULL, "unable to allocate folder storage");
279         *mpp = mp;
280
281         j = mp->hghmsg;  /* old value */
282         mp->hghmsg += numburst;
283         mp->nummsg += numburst;
284
285         /*
286         ** If this is not the highest SELECTED message, then
287         ** increment mp->hghsel by numburst, since the highest
288         ** SELECTED is about to be slid down by that amount.
289         */
290         if (msgnum < mp->hghsel)
291                 mp->hghsel += numburst;
292
293         /*
294         ** If -inplace is given, renumber the messages after the
295         ** source message, to make room for each of the messages
296         ** contained within the digest.
297         **
298         ** This is equivalent to refiling a message from the point
299         ** of view of the external hooks.
300         */
301         if (inplace) {
302                 for (i = mp->hghmsg; j > msgnum; i--, j--) {
303                         strncpy (f1, m_name (i), sizeof(f1));
304                         strncpy (f2, m_name (j), sizeof(f2));
305                         if (does_exist (mp, j)) {
306                                 if (verbosw)
307                                         printf ("message %d becomes message %d\n", j, i);
308
309                                 if (rename (f2, f1) == NOTOK)
310                                         admonish (f1, "unable to rename %s to", f2);
311
312                                 (void)snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
313                                 (void)snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
314                                 ext_hook("ref-hook", f1, f2);
315
316                                 copy_msg_flags (mp, i, j);
317                                 clear_msg_flags (mp, j);
318                                 mp->msgflags |= SEQMOD;
319                         }
320                 }
321         }
322
323         unset_selected (mp, msgnum);
324
325         /* new hghmsg is hghmsg + numburst
326         **
327         ** At this point, there is an array of numburst smsgs, each
328         ** element of which contains the starting and stopping offsets
329         ** (seeks) of the message in the digest.  The inplace flag is set
330         ** if the original digest is replaced by a message containing
331         ** the table of contents.  smsgs[0] is that table of contents.
332         ** Go through the message numbers in reverse order (high to low).
333         **
334         ** Set f1 to the name of the destination message, f2 to the name
335         ** of a scratch file.  Extract a message from the digest to the
336         ** scratch file.  Move the original message to a backup file if
337         ** the destination message number is the same as the number of
338         ** the original message, which only happens if the inplace flag
339         ** is set.  Then move the scratch file to the destination message.
340         **
341         ** Moving the original message to the backup file is equivalent
342         ** to deleting the message from the point of view of the external
343         ** hooks.  And bursting each message is equivalent to adding a
344         ** new message.
345         */
346
347         i = inplace ? msgnum + numburst : mp->hghmsg;
348         for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
349                 strncpy (f1, m_name (i), sizeof(f1));
350                 strncpy (f2, m_mktemp(invo_name, NULL, &out), sizeof(f2));
351
352                 if (verbosw && i != msgnum)
353                         printf ("message %d of digest %d becomes message %d\n", 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 }