2 ** burst.c -- explode digests into individual messages
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.
11 static struct swit switches[] = {
31 static char delim3[] = "-------";
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);
47 main(int argc, char **argv)
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];
57 setlocale(LC_ALL, "");
59 invo_name = mhbasename(argv[0]);
61 /* read user profile/context */
64 arguments = getarguments(invo_name, argc, argv, 1);
67 while ((cp = *argp++)) {
69 switch (smatch(++cp, switches)) {
71 ambigsw(cp, switches);
74 adios(NULL, "-%s unknown\n", cp);
77 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
78 print_help(buf, switches, 1);
81 print_version(invo_name);
106 if (*cp == '+' || *cp == '@') {
108 adios(NULL, "only one folder at a time!");
110 folder = getcpy(expandfol(cp));
117 msgs[msgp++] = seq_cur;
119 folder = getcurfol();
120 maildir = toabsdir(folder);
122 if (chdir(maildir) == NOTOK)
123 adios(maildir, "unable to change directory to");
125 /* read folder and create message structure */
126 if (!(mp = folder_read(folder)))
127 adios(NULL, "unable to read folder %s", folder);
129 /* check for empty folder */
131 adios(NULL, "no messages in %s", folder);
133 /* parse all the message ranges/sequences and set SELECTED */
134 for (msgnum = 0; msgnum < msgp; msgnum++)
135 if (!m_convert(mp, msgs[msgnum]))
137 seq_setprev(mp); /* set the previous-sequence */
139 smsgs = (struct smsg *)
140 calloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
142 adios(NULL, "unable to allocate burst storage");
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) {
151 printf("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum);
152 burst(&mp, msgnum, smsgs, numburst, inplace, verbosw, maildir);
156 admonish(NULL, "message %d not in digest format", msgnum);
157 } /* this pair of braces was missing before 1999-07-15 */
159 adios(NULL, "burst() botch -- you lose big");
164 free((char *) smsgs);
165 context_replace(curfolder, folder); /* update current folder */
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.
174 if (mp->lowsel != mp->curmsg)
175 seq_setcur(mp, mp->lowsel);
177 if (hi <= mp->hghmsg)
181 seq_save(mp); /* synchronize message sequences */
182 context_save(); /* save the context file */
183 folder_free(mp); /* free folder/message structure */
190 ** Scan the message and find the beginning and
191 ** end of all the messages in the digest.
195 find_delim(int msgnum, struct smsg *smsgs)
197 int ld3, wasdlm, msgp;
204 ld3 = strlen(delim3);
206 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
207 adios(msgnam, "unable to read message");
209 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
210 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
211 pos += (long) strlen(buffer);
214 fseek(in, pos, SEEK_SET);
215 smsgs[msgp].s_start = pos;
217 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
218 if (strncmp(buffer, delim3, ld3) == 0
219 && (msgp == 1 || c == '\n')) {
222 if (cc == '\n' || cc == EOF) {
226 pos += (long) strlen(buffer);
229 wasdlm = strncmp(buffer, delim3, ld3) == 0;
230 if (smsgs[msgp].s_start != pos)
231 smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ?
236 pos += (long) strlen(buffer);
240 return (msgp - 1); /* toss "End of XXX Digest" */
245 ** Burst out the messages in the digest into the folder
249 burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
250 int inplace, int verbosw, char *maildir)
254 char f1[BUFSIZ], f2[BUFSIZ], f3[BUFSIZ];
259 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
260 adios(msgnam, "unable to read message");
262 mode = fstat(fileno(in), &st) != NOTOK ?
263 (st.st_mode & 0777) : m_gmprot();
267 ** See if we have enough space in the folder
268 ** structure for all the new messages.
270 if ((mp->hghmsg + numburst > mp->hghoff) &&
271 !(mp = folder_realloc(mp, mp->lowoff, mp->hghmsg + numburst)))
272 adios(NULL, "unable to allocate folder storage");
275 j = mp->hghmsg; /* old value */
276 mp->hghmsg += numburst;
277 mp->nummsg += numburst;
280 ** If this is not the highest SELECTED message, then
281 ** increment mp->hghsel by numburst, since the highest
282 ** SELECTED is about to be slid down by that amount.
284 if (msgnum < mp->hghsel)
285 mp->hghsel += numburst;
288 ** If -inplace is given, renumber the messages after the
289 ** source message, to make room for each of the messages
290 ** contained within the digest.
292 ** This is equivalent to refiling a message from the point
293 ** of view of the external hooks.
296 for (i = mp->hghmsg; j > msgnum; i--, j--) {
297 strncpy(f1, m_name(i), sizeof(f1));
298 strncpy(f2, m_name(j), sizeof(f2));
299 if (does_exist(mp, j)) {
301 printf("message %d becomes message %d\n", j, i);
303 if (rename(f2, f1) == NOTOK)
304 admonish(f1, "unable to rename %s to", f2);
306 snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
307 snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
308 ext_hook("ref-hook", f1, f2);
310 copy_msg_flags(mp, i, j);
311 clear_msg_flags(mp, j);
312 mp->msgflags |= SEQMOD;
317 unset_selected(mp, msgnum);
319 /* new hghmsg is hghmsg + numburst
321 ** At this point, there is an array of numburst smsgs, each
322 ** element of which contains the starting and stopping offsets
323 ** (seeks) of the message in the digest. The inplace flag is set
324 ** if the original digest is replaced by a message containing
325 ** the table of contents. smsgs[0] is that table of contents.
326 ** Go through the message numbers in reverse order (high to low).
328 ** Set f1 to the name of the destination message, f2 to the name
329 ** of a scratch file. Extract a message from the digest to the
330 ** scratch file. Move the original message to a backup file if
331 ** the destination message number is the same as the number of
332 ** the original message, which only happens if the inplace flag
333 ** is set. Then move the scratch file to the destination message.
335 ** Moving the original message to the backup file is equivalent
336 ** to deleting the message from the point of view of the external
337 ** hooks. And bursting each message is equivalent to adding a
341 i = inplace ? msgnum + numburst : mp->hghmsg;
342 for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
343 strncpy(f1, m_name(i), sizeof(f1));
344 strncpy(f2, m_mktemp(invo_name, NULL, &out), sizeof(f2));
346 if (verbosw && i != msgnum)
347 printf("message %d of digest %d becomes message %d\n",
351 fseek(in, smsgs[j].s_start, SEEK_SET);
352 cpybrst(in, out, msgnam, f2,
353 (int) (smsgs[j].s_stop - smsgs[j].s_start));
357 strncpy(f3, m_backup(f1), sizeof(f3));
358 if (rename(f1, f3) == NOTOK)
359 admonish(f3, "unable to rename %s to", f1);
361 snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
362 ext_hook("del-hook", f3, NULL);
364 if (rename(f2, f1) == NOTOK)
365 admonish(f1, "unable to rename %s to", f2);
367 snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
368 ext_hook("add-hook", f3, NULL);
370 copy_msg_flags(mp, i, msgnum);
371 mp->msgflags |= SEQMOD;
383 ** Copy a mesage which is being burst out of a digest.
384 ** It will remove any "dashstuffing" in the message.
388 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
390 register int c, state;
392 for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
427 state = (c == '\n') ? S1 : S2;
436 if (ferror(in) && !feof(in))
437 adios(ifile, "error reading");
439 adios(ofile, "error writing");