3 * burst.c -- explode digests into individual messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 static struct swit switches[] = {
34 static char delim3[] = "-------";
44 static int find_delim (int, struct smsg *);
45 static void burst (struct msgs **, int, struct smsg *, int, int, int, char *);
46 static void cpybrst (FILE *, FILE *, char *, char *, int);
50 main (int argc, char **argv)
52 int inplace = 0, quietsw = 0, verbosw = 0;
53 int msgp = 0, hi, msgnum, numburst;
54 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
55 char **argp, **arguments, *msgs[MAXARGS];
60 setlocale(LC_ALL, "");
62 invo_name = r1bindex (argv[0], '/');
64 /* read user profile/context */
67 arguments = getarguments (invo_name, argc, argv, 1);
70 while ((cp = *argp++)) {
72 switch (smatch (++cp, switches)) {
74 ambigsw (cp, switches);
77 adios (NULL, "-%s unknown\n", cp);
80 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
82 print_help (buf, switches, 1);
85 print_version(invo_name);
110 if (*cp == '+' || *cp == '@') {
112 adios (NULL, "only one folder at a time!");
114 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
120 if (!context_find ("path"))
121 free (path ("./", TFOLDER));
123 msgs[msgp++] = "cur";
125 folder = getfolder (1);
126 maildir = m_maildir (folder);
128 if (chdir (maildir) == NOTOK)
129 adios (maildir, "unable to change directory to");
131 /* read folder and create message structure */
132 if (!(mp = folder_read (folder)))
133 adios (NULL, "unable to read folder %s", folder);
135 /* check for empty folder */
137 adios (NULL, "no messages in %s", folder);
139 /* parse all the message ranges/sequences and set SELECTED */
140 for (msgnum = 0; msgnum < msgp; msgnum++)
141 if (!m_convert (mp, msgs[msgnum]))
143 seq_setprev (mp); /* set the previous-sequence */
145 smsgs = (struct smsg *)
146 calloc ((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
148 adios (NULL, "unable to allocate burst storage");
152 /* burst all the SELECTED messages */
153 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
154 if (is_selected (mp, msgnum)) {
155 if ((numburst = find_delim (msgnum, smsgs)) >= 1) {
157 printf ("%d message%s exploded from digest %d\n",
158 numburst, numburst > 1 ? "s" : "", msgnum);
159 burst (&mp, msgnum, smsgs, numburst, inplace, verbosw, maildir);
163 admonish (NULL, "message %d not in digest format",
165 } /* this pair of braces was missing before 1999-07-15 */
167 adios (NULL, "burst() botch -- you lose big");
172 free ((char *) smsgs);
173 context_replace (pfolder, folder); /* update current folder */
176 * If -inplace is given, then the first message burst becomes
177 * the current message (which will now show a table of contents).
178 * Otherwise, the first message extracted from the first digest
179 * becomes the current message.
182 if (mp->lowsel != mp->curmsg)
183 seq_setcur (mp, mp->lowsel);
185 if (hi <= mp->hghmsg)
189 seq_save (mp); /* synchronize message sequences */
190 context_save (); /* save the context file */
191 folder_free (mp); /* free folder/message structure */
197 * Scan the message and find the beginning and
198 * end of all the messages in the digest.
202 find_delim (int msgnum, struct smsg *smsgs)
204 int ld3, wasdlm, msgp;
211 ld3 = strlen (delim3);
213 if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
214 adios (msgnam, "unable to read message");
216 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
217 while (fgets (buffer, sizeof(buffer), in) && buffer[0] == '\n')
218 pos += (long) strlen (buffer);
221 fseek (in, pos, SEEK_SET);
222 smsgs[msgp].s_start = pos;
224 for (c = 0; fgets (buffer, sizeof(buffer), in); c = buffer[0]) {
225 if (strncmp (buffer, delim3, ld3) == 0
226 && (msgp == 1 || c == '\n')
227 && ((cc = peekc (in)) == '\n' || cc == EOF))
230 pos += (long) strlen (buffer);
233 wasdlm = strncmp (buffer, delim3, ld3) == 0;
234 if (smsgs[msgp].s_start != pos)
235 smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos;
239 smsgs[msgp - 1].s_stop -= ((long) strlen (buffer) + 1);
240 msgp++; /* fake "End of XXX Digest" */
245 pos += (long) strlen (buffer);
249 return (msgp - 1); /* toss "End of XXX Digest" */
254 * Burst out the messages in the digest into the folder
258 burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
259 int inplace, int verbosw, char *maildir)
263 char f1[BUFSIZ], f2[BUFSIZ], f3[BUFSIZ];
268 if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
269 adios (msgnam, "unable to read message");
271 mode = fstat (fileno(in), &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
275 * See if we have enough space in the folder
276 * structure for all the new messages.
278 if ((mp->hghmsg + numburst > mp->hghoff) &&
279 !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + numburst)))
280 adios (NULL, "unable to allocate folder storage");
283 j = mp->hghmsg; /* old value */
284 mp->hghmsg += numburst;
285 mp->nummsg += numburst;
288 * If this is not the highest SELECTED message, then
289 * increment mp->hghsel by numburst, since the highest
290 * SELECTED is about to be slid down by that amount.
292 if (msgnum < mp->hghsel)
293 mp->hghsel += numburst;
296 * If -inplace is given, renumber the messages after the
297 * source message, to make room for each of the messages
298 * contained within the digest.
300 * This is equivalent to refiling a message from the point
301 * of view of the external hooks.
304 for (i = mp->hghmsg; j > msgnum; i--, j--) {
305 strncpy (f1, m_name (i), sizeof(f1));
306 strncpy (f2, m_name (j), sizeof(f2));
307 if (does_exist (mp, j)) {
309 printf ("message %d becomes message %d\n", j, i);
311 if (rename (f2, f1) == NOTOK)
312 admonish (f1, "unable to rename %s to", f2);
314 (void)snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
315 (void)snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
316 ext_hook("ref-hook", f1, f2);
318 copy_msg_flags (mp, i, j);
319 clear_msg_flags (mp, j);
320 mp->msgflags |= SEQMOD;
325 unset_selected (mp, msgnum);
327 /* new hghmsg is hghmsg + numburst
329 * At this point, there is an array of numburst smsgs, each element of
330 * which contains the starting and stopping offsets (seeks) of the message
331 * in the digest. The inplace flag is set if the original digest is replaced
332 * by a message containing the table of contents. smsgs[0] is that table of
333 * contents. Go through the message numbers in reverse order (high to low).
335 * Set f1 to the name of the destination message, f2 to the name of a scratch
336 * file. Extract a message from the digest to the scratch file. Move the
337 * original message to a backup file if the destination message number is the
338 * same as the number of the original message, which only happens if the
339 * inplace flag is set. Then move the scratch file to the destination message.
341 * Moving the original message to the backup file is equivalent to deleting the
342 * message from the point of view of the external hooks. And bursting each
343 * message is equivalent to adding a new message.
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_scratch ("", invo_name), sizeof(f2));
350 if (verbosw && i != msgnum)
351 printf ("message %d of digest %d becomes message %d\n", j, msgnum, i);
353 if ((out = fopen (f2, "w")) == NULL)
354 adios (f2, "unable to write message");
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));
362 strncpy (f3, m_backup (f1), sizeof(f3));
363 if (rename (f1, f3) == NOTOK)
364 admonish (f3, "unable to rename %s to", f1);
366 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
367 ext_hook("del-hook", f3, (char *)0);
369 if (rename (f2, f1) == NOTOK)
370 admonish (f1, "unable to rename %s to", f2);
372 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
373 ext_hook("add-hook", f3, (char *)0);
375 copy_msg_flags (mp, i, msgnum);
376 mp->msgflags |= SEQMOD;
388 * Copy a mesage which is being burst out of a digest.
389 * It will remove any "dashstuffing" in the message.
393 cpybrst (FILE *in, FILE *out, char *ifile, char *ofile, int len)
395 register int c, state;
397 for (state = S1; (c = fgetc (in)) != EOF && len > 0; len--) {
432 state = (c == '\n') ? S1 : S2;
441 if (ferror (in) && !feof (in))
442 adios (ifile, "error reading");
444 adios (ofile, "error writing");