Previously contained the code:
[mmh] / uip / burst.c
1
2 /*
3  * burst.c -- explode digests into individual messages
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9
10 static struct swit switches[] = {
11 #define INPLSW  0
12     { "inplace", 0 },
13 #define NINPLSW 1
14     { "noinplace", 0 },
15 #define QIETSW  2
16     { "quiet", 0 },
17 #define NQIETSW 3
18     { "noquiet", 0 },
19 #define VERBSW  4
20     { "verbose", 0 },
21 #define NVERBSW 5
22     { "noverbose", 0 },
23 #define VERSIONSW 6
24     { "version", 0 },
25 #define HELPSW  7
26     { "help", 4 },
27     { NULL, 0 }
28 };
29
30 static char delim3[] = "-------";
31
32 struct smsg {
33     long s_start;
34     long s_stop;
35 };
36
37 /*
38  * static prototypes
39  */
40 static int find_delim (int, struct smsg *);
41 static void burst (struct msgs **, int, struct smsg *, int, int, int);
42 static void cpybrst (FILE *, FILE *, char *, char *, int);
43
44
45 int
46 main (int argc, char **argv)
47 {
48     int inplace = 0, quietsw = 0, verbosw = 0;
49     int msgp = 0, hi, msgnum, numburst;
50     char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
51     char **argp, **arguments, *msgs[MAXARGS];
52     struct smsg *smsgs;
53     struct msgs *mp;
54
55 #ifdef LOCALE
56     setlocale(LC_ALL, "");
57 #endif
58     invo_name = r1bindex (argv[0], '/');
59
60     /* read user profile/context */
61     context_read();
62
63     arguments = getarguments (invo_name, argc, argv, 1);
64     argp = arguments;
65
66     while ((cp = *argp++)) {
67         if (*cp == '-') {
68             switch (smatch (++cp, switches)) {
69             case AMBIGSW: 
70                 ambigsw (cp, switches);
71                 done (1);
72             case UNKWNSW: 
73                 adios (NULL, "-%s unknown\n", cp);
74
75             case HELPSW: 
76                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
77                         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 = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
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",
154                             numburst, numburst > 1 ? "s" : "", msgnum);
155                 burst (&mp, msgnum, smsgs, numburst, inplace, verbosw);
156             } else {
157                 if (numburst == 0) {
158                     if (!quietsw)
159                         admonish (NULL, "message %d not in digest format",
160                                   msgnum);
161                 }  /* this pair of braces was missing before 1999-07-15 */
162                 else
163                     adios (NULL, "burst() botch -- you lose big");
164             }
165         }
166     }
167
168     free ((char *) smsgs);
169     context_replace (pfolder, folder);  /* update current folder */
170
171     /*
172      * If -inplace is given, then the first message burst becomes
173      * the current message (which will now show a table of contents).
174      * Otherwise, the first message extracted from the first digest
175      * becomes the current message.
176      */
177     if (inplace) {
178         if (mp->lowsel != mp->curmsg)
179             seq_setcur (mp, mp->lowsel);
180     } else {
181         if (hi <= mp->hghmsg)
182             seq_setcur (mp, hi);
183     }
184
185     seq_save (mp);      /* synchronize message sequences */
186     context_save ();    /* save the context file         */
187     folder_free (mp);   /* free folder/message structure */
188     return done (0);
189 }
190
191
192 /*
193  * Scan the message and find the beginning and
194  * end of all the messages in the digest.
195  */
196
197 static int
198 find_delim (int msgnum, struct smsg *smsgs)
199 {
200     int ld3, wasdlm, msgp;
201     long pos;
202     char c, *msgnam;
203     int cc;
204     char buffer[BUFSIZ];
205     FILE *in;
206
207     ld3 = strlen (delim3);
208
209     if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
210         adios (msgnam, "unable to read message");
211
212     for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
213         while (fgets (buffer, sizeof(buffer), in) && buffer[0] == '\n')
214             pos += (long) strlen (buffer);
215         if (feof (in))
216             break;
217         fseek (in, pos, SEEK_SET);
218         smsgs[msgp].s_start = pos;
219
220         for (c = 0; fgets (buffer, sizeof(buffer), in); c = buffer[0]) {
221             if (strncmp (buffer, delim3, ld3) == 0
222                     && (msgp == 1 || c == '\n')
223                     && ((cc = peekc (in)) == '\n' || 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) ? pos - 1 : pos;
232         if (feof (in)) {
233 #if 0
234             if (wasdlm) {
235                 smsgs[msgp - 1].s_stop -= ((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)
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 ? (st.st_mode & 0777) : m_gmprot();
268     mp = *mpp;
269
270     /*
271      * See if we have enough space in the folder
272      * structure for all the new messages.
273      */
274     if ((mp->hghmsg + numburst > mp->hghoff) &&
275         !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + numburst)))
276         adios (NULL, "unable to allocate folder storage");
277     *mpp = mp;
278
279     j = mp->hghmsg;             /* old value */
280     mp->hghmsg += numburst;
281     mp->nummsg += numburst;
282
283     /*
284      * If this is not the highest SELECTED message, then
285      * increment mp->hghsel by numburst, since the highest
286      * SELECTED is about to be slid down by that amount.
287      */
288     if (msgnum < mp->hghsel)
289         mp->hghsel += numburst;
290
291     /*
292      * If -inplace is given, renumber the messages after the
293      * source message, to make room for each of the messages
294      * contained within the digest.
295      */
296     if (inplace) {
297         for (i = mp->hghmsg; j > msgnum; i--, j--) {
298             strncpy (f1, m_name (i), sizeof(f1));
299             strncpy (f2, m_name (j), sizeof(f2));
300             if (does_exist (mp, j)) {
301                 if (verbosw)
302                     printf ("message %d becomes message %d\n", j, i);
303
304                 if (rename (f2, f1) == NOTOK)
305                     admonish (f1, "unable to rename %s to", f2);
306                 copy_msg_flags (mp, i, j);
307                 clear_msg_flags (mp, j);
308                 mp->msgflags |= SEQMOD;
309             }
310         }
311     }
312     
313     unset_selected (mp, msgnum);
314
315     /* new hghmsg is hghmsg + numburst */
316     i = inplace ? msgnum + numburst : mp->hghmsg;
317     for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
318         strncpy (f1, m_name (i), sizeof(f1));
319         strncpy (f2, m_scratch ("", invo_name), sizeof(f2));
320         if (verbosw && i != msgnum)
321             printf ("message %d of digest %d becomes message %d\n", j, msgnum, i);
322
323         if ((out = fopen (f2, "w")) == NULL)
324             adios (f2, "unable to write message");
325         chmod (f2, mode);
326         fseek (in, smsgs[j].s_start, SEEK_SET);
327         cpybrst (in, out, msgnam, f2,
328                 (int) (smsgs[j].s_stop - smsgs[j].s_start));
329         fclose (out);
330
331         if (i == msgnum) {
332             strncpy (f3, m_backup (f1), sizeof(f3));
333             if (rename (f1, f3) == NOTOK)
334                 admonish (f3, "unable to rename %s to", f1);
335         }
336         if (rename (f2, f1) == NOTOK)
337             admonish (f1, "unable to rename %s to", f2);
338         copy_msg_flags (mp, i, msgnum);
339         mp->msgflags |= SEQMOD;
340     }
341
342     fclose (in);
343 }
344
345
346 #define S1  0
347 #define S2  1
348 #define S3  2
349
350 /*
351  * Copy a mesage which is being burst out of a digest.
352  * It will remove any "dashstuffing" in the message.
353  */
354
355 static void
356 cpybrst (FILE *in, FILE *out, char *ifile, char *ofile, int len)
357 {
358     register int c, state;
359
360     for (state = S1; (c = fgetc (in)) != EOF && len > 0; len--) {
361         if (c == 0)
362             continue;
363         switch (state) {
364             case S1: 
365                 switch (c) {
366                     case '-': 
367                         state = S3;
368                         break;
369
370                     default: 
371                         state = S2;
372                     case '\n': 
373                         fputc (c, out);
374                         break;
375                 }
376                 break;
377
378             case S2: 
379                 switch (c) {
380                     case '\n': 
381                         state = S1;
382                     default: 
383                         fputc (c, out);
384                         break;
385                 }
386                 break;
387
388             case S3: 
389                 switch (c) {
390                     case ' ': 
391                         state = S2;
392                         break;
393
394                     default: 
395                         state = (c == '\n') ? S1 : S2;
396                         fputc ('-', out);
397                         fputc (c, out);
398                         break;
399                 }
400                 break;
401         }
402     }
403
404     if (ferror (in) && !feof (in))
405         adios (ifile, "error reading");
406     if (ferror (out))
407         adios (ofile, "error writing");
408 }