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];
49 setlocale(LC_ALL, "");
51 invo_name = mhbasename(argv[0]);
53 /* read user profile/context */
56 arguments = getarguments(invo_name, argc, argv, 1);
59 while ((cp = *argp++)) {
61 switch (smatch(++cp, switches)) {
63 ambigsw(cp, switches);
66 adios(NULL, "-%s unknown\n", cp);
69 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
70 print_help(buf, switches, 1);
73 print_version(invo_name);
84 if (*cp == '+' || *cp == '@') {
86 adios(NULL, "only one folder at a time!");
88 folder = getcpy(expandfol(cp));
95 msgs[msgp++] = seq_cur;
98 maildir = toabsdir(folder);
100 if (chdir(maildir) == NOTOK)
101 adios(maildir, "unable to change directory to");
103 /* read folder and create message structure */
104 if (!(mp = folder_read(folder)))
105 adios(NULL, "unable to read folder %s", folder);
107 /* check for empty folder */
109 adios(NULL, "no messages in %s", folder);
111 /* parse all the message ranges/sequences and set SELECTED */
112 for (msgnum = 0; msgnum < msgp; msgnum++)
113 if (!m_convert(mp, msgs[msgnum]))
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) {
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(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(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(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(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(ifile, "error reading");
334 adios(ofile, "error writing");