X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=uip%2Fburst.c;h=2c741c7ff6a816c46263debb37024996d9db5b8e;hb=bc7839e29d6a4906bd0b8909fd12bb01933b143c;hp=4f654fbaade96b79f00e0f9bf4df615071a15065;hpb=017a82124bf2ea39ced5aa4c8f969c18b3c2fb90;p=mmh diff --git a/uip/burst.c b/uip/burst.c index 4f654fb..2c741c7 100644 --- a/uip/burst.c +++ b/uip/burst.c @@ -2,7 +2,9 @@ /* * burst.c -- explode digests into individual messages * - * $Id$ + * This code is Copyright (c) 2002, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. */ #include @@ -27,8 +29,6 @@ static struct swit switches[] = { { NULL, 0 } }; -static char delim3[] = "-------"; - struct smsg { long s_start; long s_stop; @@ -38,9 +38,19 @@ struct smsg { * static prototypes */ static int find_delim (int, struct smsg *); -static void burst (struct msgs **, int, struct smsg *, int, int, int); +static void burst (struct msgs **, int, struct smsg *, int, int, int, char *); static void cpybrst (FILE *, FILE *, char *, char *, int); +/* + * A macro to check to see if we have reached a message delimiter + * (an encapsulation boundary, EB, in RFC 934 parlance). + * + * According to RFC 934, an EB is simply a line which starts with + * a "-" and is NOT followed by a space. So even a single "-" on a line + * by itself would be an EB. + */ + +#define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ') int main (int argc, char **argv) @@ -107,7 +117,7 @@ main (int argc, char **argv) if (folder) adios (NULL, "only one folder at a time!"); else - folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF); + folder = pluspath (cp); } else { msgs[msgp++] = cp; } @@ -152,7 +162,7 @@ main (int argc, char **argv) if (verbosw) printf ("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum); - burst (&mp, msgnum, smsgs, numburst, inplace, verbosw); + burst (&mp, msgnum, smsgs, numburst, inplace, verbosw, maildir); } else { if (numburst == 0) { if (!quietsw) @@ -185,7 +195,8 @@ main (int argc, char **argv) seq_save (mp); /* synchronize message sequences */ context_save (); /* save the context file */ folder_free (mp); /* free folder/message structure */ - return done (0); + done (0); + return 1; } @@ -197,15 +208,13 @@ main (int argc, char **argv) static int find_delim (int msgnum, struct smsg *smsgs) { - int ld3, wasdlm, msgp; + int wasdlm, msgp; long pos; char c, *msgnam; int cc; char buffer[BUFSIZ]; FILE *in; - ld3 = strlen (delim3); - if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL) adios (msgnam, "unable to read message"); @@ -218,7 +227,7 @@ find_delim (int msgnum, struct smsg *smsgs) smsgs[msgp].s_start = pos; for (c = 0; fgets (buffer, sizeof(buffer), in); c = buffer[0]) { - if (strncmp (buffer, delim3, ld3) == 0 + if (CHECKDELIM(buffer) && (msgp == 1 || c == '\n') && ((cc = peekc (in)) == '\n' || cc == EOF)) break; @@ -226,7 +235,7 @@ find_delim (int msgnum, struct smsg *smsgs) pos += (long) strlen (buffer); } - wasdlm = strncmp (buffer, delim3, ld3) == 0; + wasdlm = CHECKDELIM(buffer); if (smsgs[msgp].s_start != pos) smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos; if (feof (in)) { @@ -252,7 +261,7 @@ find_delim (int msgnum, struct smsg *smsgs) static void burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst, - int inplace, int verbosw) + int inplace, int verbosw, char *maildir) { int i, j, mode; char *msgnam; @@ -264,7 +273,8 @@ burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst, if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL) adios (msgnam, "unable to read message"); - mode = fstat (fileno(in), &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot(); + mode = + fstat (fileno(in), &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot(); mp = *mpp; /* @@ -292,6 +302,9 @@ burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst, * If -inplace is given, renumber the messages after the * source message, to make room for each of the messages * contained within the digest. + * + * This is equivalent to refiling a message from the point + * of view of the external hooks. */ if (inplace) { for (i = mp->hghmsg; j > msgnum; i--, j--) { @@ -303,6 +316,11 @@ burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst, if (rename (f2, f1) == NOTOK) admonish (f1, "unable to rename %s to", f2); + + (void)snprintf(f1, sizeof (f1), "%s/%d", maildir, i); + (void)snprintf(f2, sizeof (f2), "%s/%d", maildir, j); + ext_hook("ref-hook", f1, f2); + copy_msg_flags (mp, i, j); clear_msg_flags (mp, j); mp->msgflags |= SEQMOD; @@ -312,16 +330,33 @@ burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst, unset_selected (mp, msgnum); - /* new hghmsg is hghmsg + numburst */ + /* 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_scratch ("", invo_name), sizeof(f2)); + 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); - 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, @@ -332,9 +367,16 @@ burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst, 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; }