Removed the space between function names and the opening parenthesis.
[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]", 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 = pluspath(cp);
111                 } else {
112                         msgs[msgp++] = cp;
113                 }
114         }
115
116         if (!context_find("path"))
117                 free(path("./", TFOLDER));
118         if (!msgp)
119                 msgs[msgp++] = "cur";
120         if (!folder)
121                 folder = getfolder(1);
122         maildir = m_maildir(folder);
123
124         if (chdir(maildir) == NOTOK)
125                 adios(maildir, "unable to change directory to");
126
127         /* read folder and create message structure */
128         if (!(mp = folder_read(folder)))
129                 adios(NULL, "unable to read folder %s", folder);
130
131         /* check for empty folder */
132         if (mp->nummsg == 0)
133                 adios(NULL, "no messages in %s", folder);
134
135         /* parse all the message ranges/sequences and set SELECTED */
136         for (msgnum = 0; msgnum < msgp; msgnum++)
137                 if (!m_convert(mp, msgs[msgnum]))
138                         done(1);
139         seq_setprev(mp);  /* set the previous-sequence */
140
141         smsgs = (struct smsg *)
142                 calloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
143         if (smsgs == NULL)
144                 adios(NULL, "unable to allocate burst storage");
145
146         hi = mp->hghmsg + 1;
147
148         /* burst all the SELECTED messages */
149         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
150                 if (is_selected(mp, msgnum)) {
151                         if ((numburst = find_delim(msgnum, smsgs)) >= 1) {
152                                 if (verbosw)
153                                         printf("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum);
154                                 burst(&mp, msgnum, smsgs, numburst, inplace, verbosw, maildir);
155                         } else {
156                                 if (numburst == 0) {
157                                         if (!quietsw)
158                                                 admonish(NULL, "message %d not in digest format", msgnum);
159                                 }  /* this pair of braces was missing before 1999-07-15 */
160                                 else
161                                         adios(NULL, "burst() botch -- you lose big");
162                         }
163                 }
164         }
165
166         free((char *) smsgs);
167         context_replace(pfolder, folder);  /* update current folder */
168
169         /*
170         ** If -inplace is given, then the first message burst becomes
171         ** the current message (which will now show a table of contents).
172         ** Otherwise, the first message extracted from the first digest
173         ** becomes the current message.
174         */
175         if (inplace) {
176                 if (mp->lowsel != mp->curmsg)
177                         seq_setcur(mp, mp->lowsel);
178         } else {
179                 if (hi <= mp->hghmsg)
180                         seq_setcur(mp, hi);
181         }
182
183         seq_save(mp);  /* synchronize message sequences */
184         context_save(); /* save the context file */
185         folder_free(mp); /* free folder/message structure */
186         done(0);
187         return 1;
188 }
189
190
191 /*
192 ** Scan the message and find the beginning and
193 ** end of all the messages in the digest.
194 */
195
196 static int
197 find_delim(int msgnum, struct smsg *smsgs)
198 {
199         int ld3, wasdlm, msgp;
200         long pos;
201         char c, *msgnam;
202         int cc;
203         char buffer[BUFSIZ];
204         FILE *in;
205
206         ld3 = strlen(delim3);
207
208         if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
209                 adios(msgnam, "unable to read message");
210
211         for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
212                 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
213                         pos += (long) strlen(buffer);
214                 if (feof(in))
215                         break;
216                 fseek(in, pos, SEEK_SET);
217                 smsgs[msgp].s_start = pos;
218
219                 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
220                         if (strncmp(buffer, delim3, ld3) == 0
221                                         && (msgp == 1 || c == '\n')
222                                         && ((cc = peekc(in)) == '\n' ||
223                                         cc == EOF))
224                                 break;
225                         else
226                                 pos += (long) strlen(buffer);
227                 }
228
229                 wasdlm = strncmp(buffer, delim3, ld3) == 0;
230                 if (smsgs[msgp].s_start != pos)
231                         smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ?
232                                         pos - 1 : pos;
233                 if (feof(in)) {
234 #if 0
235                         if (wasdlm) {
236                                 smsgs[msgp - 1].s_stop -=
237                                                 ((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 ?
270                         (st.st_mode & 0777) : m_gmprot();
271         mp = *mpp;
272
273         /*
274         ** See if we have enough space in the folder
275         ** structure for all the new messages.
276         */
277         if ((mp->hghmsg + numburst > mp->hghoff) &&
278                 !(mp = folder_realloc(mp, mp->lowoff, mp->hghmsg + numburst)))
279                 adios(NULL, "unable to allocate folder storage");
280         *mpp = mp;
281
282         j = mp->hghmsg;  /* old value */
283         mp->hghmsg += numburst;
284         mp->nummsg += numburst;
285
286         /*
287         ** If this is not the highest SELECTED message, then
288         ** increment mp->hghsel by numburst, since the highest
289         ** SELECTED is about to be slid down by that amount.
290         */
291         if (msgnum < mp->hghsel)
292                 mp->hghsel += numburst;
293
294         /*
295         ** If -inplace is given, renumber the messages after the
296         ** source message, to make room for each of the messages
297         ** contained within the digest.
298         **
299         ** This is equivalent to refiling a message from the point
300         ** of view of the external hooks.
301         */
302         if (inplace) {
303                 for (i = mp->hghmsg; j > msgnum; i--, j--) {
304                         strncpy(f1, m_name(i), sizeof(f1));
305                         strncpy(f2, m_name(j), sizeof(f2));
306                         if (does_exist(mp, j)) {
307                                 if (verbosw)
308                                         printf("message %d becomes message %d\n", j, i);
309
310                                 if (rename(f2, f1) == NOTOK)
311                                         admonish(f1, "unable to rename %s to", f2);
312
313                                 snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
314                                 snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
315                                 ext_hook("ref-hook", f1, f2);
316
317                                 copy_msg_flags(mp, i, j);
318                                 clear_msg_flags(mp, j);
319                                 mp->msgflags |= SEQMOD;
320                         }
321                 }
322         }
323
324         unset_selected(mp, msgnum);
325
326         /* new hghmsg is hghmsg + numburst
327         **
328         ** At this point, there is an array of numburst smsgs, each
329         ** element of which contains the starting and stopping offsets
330         ** (seeks) of the message in the digest.  The inplace flag is set
331         ** if the original digest is replaced by a message containing
332         ** the table of contents.  smsgs[0] is that table of contents.
333         ** Go through the message numbers in reverse order (high to low).
334         **
335         ** Set f1 to the name of the destination message, f2 to the name
336         ** of a scratch file.  Extract a message from the digest to the
337         ** scratch file.  Move the original message to a backup file if
338         ** the destination message number is the same as the number of
339         ** the original message, which only happens if the inplace flag
340         ** is set.  Then move the scratch file to the destination message.
341         **
342         ** Moving the original message to the backup file is equivalent
343         ** to deleting the message from the point of view of the external
344         ** hooks.  And bursting each message is equivalent to adding a
345         ** new message.
346         */
347
348         i = inplace ? msgnum + numburst : mp->hghmsg;
349         for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
350                 strncpy(f1, m_name(i), sizeof(f1));
351                 strncpy(f2, m_mktemp(invo_name, NULL, &out), sizeof(f2));
352
353                 if (verbosw && i != msgnum)
354                         printf("message %d of digest %d becomes message %d\n",
355                                         j, msgnum, i);
356
357                 chmod(f2, mode);
358                 fseek(in, smsgs[j].s_start, SEEK_SET);
359                 cpybrst(in, out, msgnam, f2,
360                                 (int) (smsgs[j].s_stop - smsgs[j].s_start));
361                 fclose(out);
362
363                 if (i == msgnum) {
364                         strncpy(f3, m_backup(f1), sizeof(f3));
365                         if (rename(f1, f3) == NOTOK)
366                                 admonish(f3, "unable to rename %s to", f1);
367
368                         (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
369                         ext_hook("del-hook", f3, (char *)0);
370                 }
371                 if (rename(f2, f1) == NOTOK)
372                         admonish(f1, "unable to rename %s to", f2);
373
374                 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
375                 ext_hook("add-hook", f3, (char *)0);
376
377                 copy_msg_flags(mp, i, msgnum);
378                 mp->msgflags |= SEQMOD;
379         }
380
381         fclose(in);
382 }
383
384
385 #define S1  0
386 #define S2  1
387 #define S3  2
388
389 /*
390 ** Copy a mesage which is being burst out of a digest.
391 ** It will remove any "dashstuffing" in the message.
392 */
393
394 static void
395 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
396 {
397         register int c, state;
398
399         for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
400                 if (c == 0)
401                         continue;
402                 switch (state) {
403                         case S1:
404                                 switch (c) {
405                                         case '-':
406                                                 state = S3;
407                                                 break;
408
409                                         default:
410                                                 state = S2;
411                                         case '\n':
412                                                 fputc(c, out);
413                                                 break;
414                                 }
415                                 break;
416
417                         case S2:
418                                 switch (c) {
419                                         case '\n':
420                                                 state = S1;
421                                         default:
422                                                 fputc(c, out);
423                                                 break;
424                                 }
425                                 break;
426
427                         case S3:
428                                 switch (c) {
429                                         case ' ':
430                                                 state = S2;
431                                                 break;
432
433                                         default:
434                                                 state = (c == '\n') ? S1 : S2;
435                                                 fputc('-', out);
436                                                 fputc(c, out);
437                                                 break;
438                                 }
439                                 break;
440                 }
441         }
442
443         if (ferror(in) && !feof(in))
444                 adios(ifile, "error reading");
445         if (ferror(out))
446                 adios(ofile, "error writing");
447 }