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[] = {
23 static char delim3[] = "-------";
33 static int find_delim(int, struct smsg *);
34 static void burst(struct msgs **, int, struct smsg *, int, int, char *);
35 static void cpybrst(FILE *, FILE *, char *, char *, int);
39 main(int argc, char **argv)
42 int msgp = 0, hi, msgnum, numburst;
43 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
44 char **argp, **arguments, *msgs[MAXARGS];
48 setlocale(LC_ALL, "");
49 invo_name = mhbasename(argv[0]);
51 /* read user profile/context */
54 arguments = getarguments(invo_name, argc, argv, 1);
57 while ((cp = *argp++)) {
59 switch (smatch(++cp, switches)) {
61 ambigsw(cp, switches);
62 /* sysexits.h EX_USAGE */
65 adios(NULL, "-%s unknown\n", cp);
68 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
69 print_help(buf, switches, 1);
72 print_version(invo_name);
83 if (*cp == '+' || *cp == '@') {
85 adios(NULL, "only one folder at a time!");
87 folder = getcpy(expandfol(cp));
94 msgs[msgp++] = seq_cur;
97 maildir = toabsdir(folder);
99 if (chdir(maildir) == NOTOK)
100 adios(maildir, "unable to change directory to");
102 /* read folder and create message structure */
103 if (!(mp = folder_read(folder)))
104 adios(NULL, "unable to read folder %s", folder);
106 /* check for empty folder */
108 adios(NULL, "no messages in %s", folder);
110 /* parse all the message ranges/sequences and set SELECTED */
111 for (msgnum = 0; msgnum < msgp; msgnum++)
112 if (!m_convert(mp, msgs[msgnum]))
113 /* sysexits.h EX_USAGE */
115 seq_setprev(mp); /* set the previous-sequence */
117 smsgs = (struct smsg *)
118 calloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
120 adios(NULL, "unable to allocate burst storage");
124 /* burst all the SELECTED messages */
125 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
126 if (is_selected(mp, msgnum)) {
127 if ((numburst = find_delim(msgnum, smsgs)) > 0) {
129 printf("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum);
130 burst(&mp, msgnum, smsgs, numburst, verbosw, maildir);
131 } else if (numburst == 0) {
132 admonish(NULL, "message %d not in digest format", msgnum);
134 adios(NULL, "burst() botch -- you lose big");
140 context_replace(curfolder, folder);
143 ** The first message extracted from the first digest
144 ** becomes the current message.
146 if (hi <= mp->hghmsg) {
158 ** Scan the message and find the beginning and
159 ** end of all the messages in the digest.
162 find_delim(int msgnum, struct smsg *smsgs)
164 int ld3, wasdlm, msgp;
171 ld3 = strlen(delim3);
173 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
174 adios(msgnam, "unable to read message");
176 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER; msgp++) {
177 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
178 pos += (long) strlen(buffer);
182 fseek(in, pos, SEEK_SET);
183 smsgs[msgp].s_start = pos;
185 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
186 if (strncmp(buffer, delim3, ld3) == 0
187 && (msgp == 1 || c == '\n')) {
190 if (cc == '\n' || cc == EOF) {
194 pos += (long) strlen(buffer);
197 wasdlm = strncmp(buffer, delim3, ld3) == 0;
198 if (smsgs[msgp].s_start != pos)
199 smsgs[msgp].s_stop = (c == '\n' && wasdlm) ?
204 pos += (long) strlen(buffer);
208 return (msgp > 0) ? msgp-1 : 0; /* toss "End of XXX Digest" */
213 ** Burst out the messages in the digest into the folder
216 burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
217 int verbosw, char *maildir)
221 char destfil[BUFSIZ];
226 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
227 adios(msgnam, "unable to read message");
229 mode = fstat(fileno(in), &st) != NOTOK ?
230 (int)(st.st_mode & 0777) : m_gmprot();
234 ** Ensure we have enough space in the folder
235 ** structure for all the new messages.
237 if ((mp->hghmsg + numburst > mp->hghoff) && !(mp = folder_realloc(mp,
238 mp->lowoff, mp->hghmsg + numburst)))
239 adios(NULL, "unable to allocate folder storage");
243 ** At this point, there is an array of numburst smsgs, each
244 ** element of which contains the starting and stopping offsets
245 ** (seeks) of the message in the digest. Go through the message
246 ** numbers. Ignore smsgs[0], which is the table of contents.
248 for (j=1, i=mp->hghmsg+1; j<=numburst; j++, i++) {
249 snprintf(destfil, sizeof destfil, "%s/%d", maildir, i);
250 if (!(out = fopen(destfil, "w"))) {
251 adios(destfil, "unable to open");
254 printf("message %d of digest %d becomes message %d\n",
257 chmod(destfil, mode);
258 fseek(in, smsgs[j].s_start, SEEK_SET);
259 cpybrst(in, out, msgnam, destfil,
260 (int) (smsgs[j].s_stop - smsgs[j].s_start));
265 ** Bursting each message is equivalent to adding a
266 ** new message from the point of view of the external hooks.
268 ext_hook("add-hook", destfil, NULL);
269 copy_msg_flags(mp, i, msgnum);
270 mp->msgflags |= SEQMOD;
277 ** Copy a message which is being burst out of a digest.
278 ** It will remove any "dashstuffing" in the message.
281 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
284 enum { S1, S2, S3 } state;
286 for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
321 state = (c == '\n') ? S1 : S2;
330 if (ferror(in) && !feof(in))
331 adios(ifile, "error reading");
333 adios(ofile, "error writing");