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.
15 static struct swit switches[] = {
27 static char delim3[] = "-------";
37 static int find_delim(int, struct smsg *);
38 static void burst(struct msgs **, int, struct smsg *, int, int, char *);
39 static void cpybrst(FILE *, FILE *, char *, char *, int);
43 main(int argc, char **argv)
46 int msgp = 0, hi, msgnum, numburst;
47 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
48 char **argp, **arguments, *msgs[MAXARGS];
52 setlocale(LC_ALL, "");
53 invo_name = mhbasename(argv[0]);
55 /* read user profile/context */
58 arguments = getarguments(invo_name, argc, argv, 1);
61 while ((cp = *argp++)) {
63 switch (smatch(++cp, switches)) {
65 ambigsw(cp, switches);
68 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
71 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
72 print_help(buf, switches, 1);
73 exit(argc == 2 ? EX_OK : EX_USAGE);
75 print_version(invo_name);
76 exit(argc == 2 ? EX_OK : EX_USAGE);
86 if (*cp == '+' || *cp == '@') {
88 adios(EX_USAGE, NULL, "only one folder at a time!");
90 folder = getcpy(expandfol(cp));
97 msgs[msgp++] = seq_cur;
100 maildir = toabsdir(folder);
102 if (chdir(maildir) == NOTOK)
103 adios(EX_SOFTWARE, maildir, "unable to change directory to");
105 /* read folder and create message structure */
106 if (!(mp = folder_read(folder)))
107 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
109 /* check for empty folder */
111 adios(EX_DATAERR, NULL, "no messages in %s", folder);
113 /* parse all the message ranges/sequences and set SELECTED */
114 for (msgnum = 0; msgnum < msgp; msgnum++)
115 if (!m_convert(mp, msgs[msgnum]))
117 seq_setprev(mp); /* set the previous-sequence */
119 smsgs = (struct smsg *)
120 calloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
122 adios(EX_OSERR, NULL, "unable to allocate burst storage");
126 /* burst all the SELECTED messages */
127 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
128 if (is_selected(mp, msgnum)) {
129 if ((numburst = find_delim(msgnum, smsgs)) > 0) {
131 printf("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum);
132 burst(&mp, msgnum, smsgs, numburst, verbosw, maildir);
133 } else if (numburst == 0) {
134 admonish(NULL, "message %d not in digest format", msgnum);
136 adios(EX_SOFTWARE, NULL, "burst() botch -- you lose big");
142 context_replace(curfolder, folder);
145 ** The first message extracted from the first digest
146 ** becomes the current message.
148 if (hi <= mp->hghmsg) {
160 ** Scan the message and find the beginning and
161 ** end of all the messages in the digest.
164 find_delim(int msgnum, struct smsg *smsgs)
166 int ld3, wasdlm, msgp;
173 ld3 = strlen(delim3);
175 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
176 adios(EX_IOERR, msgnam, "unable to read message");
178 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER; msgp++) {
179 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
180 pos += (long) strlen(buffer);
184 fseek(in, pos, SEEK_SET);
185 smsgs[msgp].s_start = pos;
187 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
188 if (strncmp(buffer, delim3, ld3) == 0
189 && (msgp == 1 || c == '\n')) {
192 if (cc == '\n' || cc == EOF) {
196 pos += (long) strlen(buffer);
199 wasdlm = strncmp(buffer, delim3, ld3) == 0;
200 if (smsgs[msgp].s_start != pos)
201 smsgs[msgp].s_stop = (c == '\n' && wasdlm) ?
206 pos += (long) strlen(buffer);
210 return (msgp > 0) ? msgp-1 : 0; /* toss "End of XXX Digest" */
215 ** Burst out the messages in the digest into the folder
218 burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
219 int verbosw, char *maildir)
223 char destfil[BUFSIZ];
228 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
229 adios(EX_IOERR, msgnam, "unable to read message");
231 mode = fstat(fileno(in), &st) != NOTOK ?
232 (int)(st.st_mode & 0777) : m_gmprot();
236 ** Ensure we have enough space in the folder
237 ** structure for all the new messages.
239 if ((mp->hghmsg + numburst > mp->hghoff) && !(mp = folder_realloc(mp,
240 mp->lowoff, mp->hghmsg + numburst)))
241 adios(EX_OSERR, NULL, "unable to allocate folder storage");
245 ** At this point, there is an array of numburst smsgs, each
246 ** element of which contains the starting and stopping offsets
247 ** (seeks) of the message in the digest. Go through the message
248 ** numbers. Ignore smsgs[0], which is the table of contents.
250 for (j=1, i=mp->hghmsg+1; j<=numburst; j++, i++) {
251 snprintf(destfil, sizeof destfil, "%s/%d", maildir, i);
252 if (!(out = fopen(destfil, "w"))) {
253 adios(EX_IOERR, destfil, "unable to open");
256 printf("message %d of digest %d becomes message %d\n",
259 chmod(destfil, mode);
260 fseek(in, smsgs[j].s_start, SEEK_SET);
261 cpybrst(in, out, msgnam, destfil,
262 (int) (smsgs[j].s_stop - smsgs[j].s_start));
267 ** Bursting each message is equivalent to adding a
268 ** new message from the point of view of the external hooks.
270 ext_hook("add-hook", destfil, NULL);
271 copy_msg_flags(mp, i, msgnum);
272 mp->msgflags |= SEQMOD;
279 ** Copy a message which is being burst out of a digest.
280 ** It will remove any "dashstuffing" in the message.
283 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
286 enum { S1, S2, S3 } state;
288 for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
323 state = (c == '\n') ? S1 : S2;
332 if (ferror(in) && !feof(in))
333 adios(EX_IOERR, ifile, "error reading");
335 adios(EX_IOERR, ofile, "error writing");