- }
-
- unset_selected (mp, msgnum);
-
- /* new hghmsg is hghmsg + numburst */
- i = inplace ? msgnum + numburst : mp->hghmsg;
- for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
- strncpy (f1, m_name (i), sizeof(f1));
- strncpy (f2, m_scratch ("", invo_name), sizeof(f2));
- if (verbosw && i != msgnum)
- printf ("message %d of digest %d becomes message %d\n", j, msgnum, i);
-
- if ((out = fopen (f2, "w")) == NULL)
- adios (f2, "unable to write message");
- chmod (f2, mode);
- fseek (in, smsgs[j].s_start, SEEK_SET);
- cpybrst (in, out, msgnam, f2,
- (int) (smsgs[j].s_stop - smsgs[j].s_start));
- fclose (out);
-
- if (i == msgnum) {
- strncpy (f3, m_backup (f1), sizeof(f3));
- if (rename (f1, f3) == NOTOK)
- admonish (f3, "unable to rename %s to", f1);
+
+ unset_selected(mp, msgnum);
+
+ /* new hghmsg is hghmsg + numburst
+ **
+ ** At this point, there is an array of numburst smsgs, each
+ ** element of which contains the starting and stopping offsets
+ ** (seeks) of the message in the digest. The inplace flag is set
+ ** if the original digest is replaced by a message containing
+ ** the table of contents. smsgs[0] is that table of contents.
+ ** Go through the message numbers in reverse order (high to low).
+ **
+ ** Set f1 to the name of the destination message, f2 to the name
+ ** of a scratch file. Extract a message from the digest to the
+ ** scratch file. Move the original message to a backup file if
+ ** the destination message number is the same as the number of
+ ** the original message, which only happens if the inplace flag
+ ** is set. Then move the scratch file to the destination message.
+ **
+ ** Moving the original message to the backup file is equivalent
+ ** to deleting the message from the point of view of the external
+ ** hooks. And bursting each message is equivalent to adding a
+ ** new message.
+ */
+
+ i = inplace ? msgnum + numburst : mp->hghmsg;
+ for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
+ strncpy(f1, m_name(i), sizeof(f1));
+ strncpy(f2, m_mktemp(invo_name, NULL, &out), sizeof(f2));
+
+ if (verbosw && i != msgnum)
+ printf("message %d of digest %d becomes message %d\n",
+ j, msgnum, i);
+
+ chmod(f2, mode);
+ fseek(in, smsgs[j].s_start, SEEK_SET);
+ cpybrst(in, out, msgnam, f2,
+ (int) (smsgs[j].s_stop - smsgs[j].s_start));
+ fclose(out);
+
+ if (i == msgnum) {
+ strncpy(f3, m_backup(f1), sizeof(f3));
+ if (rename(f1, f3) == NOTOK)
+ admonish(f3, "unable to rename %s to", f1);
+
+ (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
+ ext_hook("del-hook", f3, (char *)0);
+ }
+ if (rename(f2, f1) == NOTOK)
+ admonish(f1, "unable to rename %s to", f2);
+
+ (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
+ ext_hook("add-hook", f3, (char *)0);
+
+ copy_msg_flags(mp, i, msgnum);
+ mp->msgflags |= SEQMOD;