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.
16 static struct swit switches[] = {
28 static char delim3[] = "-------";
38 static int find_delim(int, struct smsg *);
39 static void burst(struct msgs **, int, struct smsg *, int, int, char *);
40 static void cpybrst(FILE *, FILE *, char *, char *, int);
44 main(int argc, char **argv)
47 int msgp = 0, hi, msgnum, numburst;
48 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
49 char **argp, **arguments, *msgs[MAXARGS];
53 setlocale(LC_ALL, "");
54 invo_name = mhbasename(argv[0]);
56 /* read user profile/context */
59 arguments = getarguments(invo_name, argc, argv, 1);
62 while ((cp = *argp++)) {
64 switch (smatch(++cp, switches)) {
66 ambigsw(cp, switches);
69 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
72 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
73 print_help(buf, switches, 1);
74 exit(argc == 2 ? EX_OK : EX_USAGE);
76 print_version(invo_name);
77 exit(argc == 2 ? EX_OK : EX_USAGE);
87 if (*cp == '+' || *cp == '@') {
89 adios(EX_USAGE, NULL, "only one folder at a time!");
91 folder = getcpy(expandfol(cp));
98 msgs[msgp++] = seq_cur;
100 folder = getcurfol();
101 maildir = toabsdir(folder);
103 if (chdir(maildir) == NOTOK)
104 adios(EX_SOFTWARE, maildir, "unable to change directory to");
106 /* read folder and create message structure */
107 if (!(mp = folder_read(folder)))
108 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
110 /* check for empty folder */
112 adios(EX_DATAERR, NULL, "no messages in %s", folder);
114 /* parse all the message ranges/sequences and set SELECTED */
115 for (msgnum = 0; msgnum < msgp; msgnum++)
116 if (!m_convert(mp, msgs[msgnum]))
118 seq_setprev(mp); /* set the previous-sequence */
120 smsgs = (struct smsg *)
121 mh_xcalloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
123 adios(EX_OSERR, NULL, "unable to allocate burst storage");
127 /* burst all the SELECTED messages */
128 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
129 if (is_selected(mp, msgnum)) {
130 if ((numburst = find_delim(msgnum, smsgs)) > 0) {
132 printf("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum);
133 burst(&mp, msgnum, smsgs, numburst, verbosw, maildir);
134 } else if (numburst == 0) {
135 admonish(NULL, "message %d not in digest format", msgnum);
137 adios(EX_SOFTWARE, NULL, "burst() botch -- you lose big");
143 context_replace(curfolder, folder);
146 ** The first message extracted from the first digest
147 ** becomes the current message.
149 if (hi <= mp->hghmsg) {
161 ** Scan the message and find the beginning and
162 ** end of all the messages in the digest.
165 find_delim(int msgnum, struct smsg *smsgs)
167 int ld3, wasdlm, msgp;
174 ld3 = strlen(delim3);
176 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
177 adios(EX_IOERR, msgnam, "unable to read message");
179 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER; msgp++) {
180 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
181 pos += (long) strlen(buffer);
185 fseek(in, pos, SEEK_SET);
186 smsgs[msgp].s_start = pos;
188 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
189 if (strncmp(buffer, delim3, ld3) == 0
190 && (msgp == 1 || c == '\n')) {
193 if (cc == '\n' || cc == EOF) {
197 pos += (long) strlen(buffer);
200 wasdlm = strncmp(buffer, delim3, ld3) == 0;
201 if (smsgs[msgp].s_start != pos)
202 smsgs[msgp].s_stop = (c == '\n' && wasdlm) ?
207 pos += (long) strlen(buffer);
211 return (msgp > 0) ? msgp-1 : 0; /* toss "End of XXX Digest" */
216 ** Burst out the messages in the digest into the folder
219 burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
220 int verbosw, char *maildir)
224 char destfil[BUFSIZ];
229 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
230 adios(EX_IOERR, msgnam, "unable to read message");
232 mode = fstat(fileno(in), &st) != NOTOK ?
233 (int)(st.st_mode & 0777) : m_gmprot();
237 ** Ensure we have enough space in the folder
238 ** structure for all the new messages.
240 if ((mp->hghmsg + numburst > mp->hghoff) && !(mp = folder_realloc(mp,
241 mp->lowoff, mp->hghmsg + numburst)))
242 adios(EX_OSERR, NULL, "unable to allocate folder storage");
246 ** At this point, there is an array of numburst smsgs, each
247 ** element of which contains the starting and stopping offsets
248 ** (seeks) of the message in the digest. Go through the message
249 ** numbers. Ignore smsgs[0], which is the table of contents.
251 for (j=1, i=mp->hghmsg+1; j<=numburst; j++, i++) {
252 snprintf(destfil, sizeof destfil, "%s/%d", maildir, i);
253 if (!(out = fopen(destfil, "w"))) {
254 adios(EX_IOERR, destfil, "unable to open");
257 printf("message %d of digest %d becomes message %d\n",
260 chmod(destfil, mode);
261 fseek(in, smsgs[j].s_start, SEEK_SET);
262 cpybrst(in, out, msgnam, destfil,
263 (int) (smsgs[j].s_stop - smsgs[j].s_start));
268 ** Bursting each message is equivalent to adding a
269 ** new message from the point of view of the external hooks.
271 ext_hook("add-hook", destfil, NULL);
272 copy_msg_flags(mp, i, msgnum);
273 mp->msgflags |= SEQMOD;
280 ** Copy a message which is being burst out of a digest.
281 ** It will remove any "dashstuffing" in the message.
284 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
287 enum { S1, S2, S3 } state;
289 for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
324 state = (c == '\n') ? S1 : S2;
333 if (ferror(in) && !feof(in))
334 adios(EX_IOERR, ifile, "error reading");
336 adios(EX_IOERR, ofile, "error writing");