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 char *version=VERSION;
30 static char delim3[] = "-------";
40 static int find_delim(int, struct smsg *);
41 static void burst(struct msgs **, int, struct smsg *, int, int, char *);
42 static void cpybrst(FILE *, FILE *, char *, char *, int);
46 main(int argc, char **argv)
49 int msgp = 0, hi, msgnum, numburst;
50 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
51 char **argp, **arguments, *msgs[MAXARGS];
55 setlocale(LC_ALL, "");
56 invo_name = mhbasename(argv[0]);
58 /* read user profile/context */
61 arguments = getarguments(invo_name, argc, argv, 1);
64 while ((cp = *argp++)) {
66 switch (smatch(++cp, switches)) {
68 ambigsw(cp, switches);
71 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
74 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
75 print_help(buf, switches, 1);
76 exit(argc == 2 ? EX_OK : EX_USAGE);
78 print_version(invo_name);
79 exit(argc == 2 ? EX_OK : EX_USAGE);
89 if (*cp == '+' || *cp == '@') {
91 adios(EX_USAGE, NULL, "only one folder at a time!");
93 folder = mh_xstrdup(expandfol(cp));
100 msgs[msgp++] = seq_cur;
102 folder = getcurfol();
103 maildir = toabsdir(folder);
105 if (chdir(maildir) == NOTOK)
106 adios(EX_SOFTWARE, maildir, "unable to change directory to");
108 /* read folder and create message structure */
109 if (!(mp = folder_read(folder)))
110 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
112 /* check for empty folder */
114 adios(EX_DATAERR, NULL, "no messages in %s", folder);
116 /* parse all the message ranges/sequences and set SELECTED */
117 for (msgnum = 0; msgnum < msgp; msgnum++)
118 if (!m_convert(mp, msgs[msgnum]))
121 smsgs = mh_xcalloc(MAXFOLDER + 2, sizeof(*smsgs));
125 /* burst all the SELECTED messages */
126 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
127 if (is_selected(mp, msgnum)) {
128 if ((numburst = find_delim(msgnum, smsgs)) > 0) {
130 printf("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum);
131 burst(&mp, msgnum, smsgs, numburst, verbosw, maildir);
132 } else if (numburst == 0) {
133 admonish(NULL, "message %d not in digest format", msgnum);
135 adios(EX_SOFTWARE, NULL, "burst() botch -- you lose big");
141 context_replace(curfolder, folder);
144 ** The first message extracted from the first digest
145 ** becomes the current message.
147 if (hi <= mp->hghmsg) {
159 ** Scan the message and find the beginning and
160 ** end of all the messages in the digest.
163 find_delim(int msgnum, struct smsg *smsgs)
165 int ld3, wasdlm, msgp;
172 ld3 = strlen(delim3);
174 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
175 adios(EX_IOERR, msgnam, "unable to read message");
177 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER; msgp++) {
178 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
179 pos += (long) strlen(buffer);
183 fseek(in, pos, SEEK_SET);
184 smsgs[msgp].s_start = pos;
186 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
187 if (strncmp(buffer, delim3, ld3) == 0
188 && (msgp == 1 || c == '\n')) {
191 if (cc == '\n' || cc == EOF) {
195 pos += (long) strlen(buffer);
198 wasdlm = strncmp(buffer, delim3, ld3) == 0;
199 if (smsgs[msgp].s_start != pos)
200 smsgs[msgp].s_stop = (c == '\n' && wasdlm) ?
205 pos += (long) strlen(buffer);
209 return (msgp > 0) ? msgp-1 : 0; /* toss "End of XXX Digest" */
214 ** Burst out the messages in the digest into the folder
217 burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
218 int verbosw, char *maildir)
222 char destfil[BUFSIZ];
227 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
228 adios(EX_IOERR, msgnam, "unable to read message");
230 mode = fstat(fileno(in), &st) != NOTOK ?
231 (int)(st.st_mode & 0777) : m_gmprot();
235 ** Ensure we have enough space in the folder
236 ** structure for all the new messages.
238 if ((mp->hghmsg + numburst > mp->hghoff) && !(mp = folder_realloc(mp,
239 mp->lowoff, mp->hghmsg + numburst)))
240 adios(EX_OSERR, NULL, "unable to allocate folder storage");
244 ** At this point, there is an array of numburst smsgs, each
245 ** element of which contains the starting and stopping offsets
246 ** (seeks) of the message in the digest. Go through the message
247 ** numbers. Ignore smsgs[0], which is the table of contents.
249 for (j=1, i=mp->hghmsg+1; j<=numburst; j++, i++) {
250 snprintf(destfil, sizeof destfil, "%s/%d", maildir, i);
251 if (!(out = fopen(destfil, "w"))) {
252 adios(EX_IOERR, destfil, "unable to open");
255 printf("message %d of digest %d becomes message %d\n",
258 chmod(destfil, mode);
259 fseek(in, smsgs[j].s_start, SEEK_SET);
260 cpybrst(in, out, msgnam, destfil,
261 (int) (smsgs[j].s_stop - smsgs[j].s_start));
266 ** Bursting each message is equivalent to adding a
267 ** new message from the point of view of the external hooks.
269 ext_hook("add-hook", destfil, NULL);
270 copy_msg_flags(mp, i, msgnum);
271 mp->msgflags |= SEQMOD;
278 ** Copy a message which is being burst out of a digest.
279 ** It will remove any "dashstuffing" in the message.
282 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
285 enum { S1, S2, S3 } state;
287 for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
322 state = (c == '\n') ? S1 : S2;
331 if (ferror(in) && !feof(in))
332 adios(EX_IOERR, ifile, "error reading");
334 adios(EX_IOERR, ofile, "error writing");