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);
64 adios(NULL, "-%s unknown\n", cp);
67 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
68 print_help(buf, switches, 1);
71 print_version(invo_name);
82 if (*cp == '+' || *cp == '@') {
84 adios(NULL, "only one folder at a time!");
86 folder = getcpy(expandfol(cp));
93 msgs[msgp++] = seq_cur;
96 maildir = toabsdir(folder);
98 if (chdir(maildir) == NOTOK)
99 adios(maildir, "unable to change directory to");
101 /* read folder and create message structure */
102 if (!(mp = folder_read(folder)))
103 adios(NULL, "unable to read folder %s", folder);
105 /* check for empty folder */
107 adios(NULL, "no messages in %s", folder);
109 /* parse all the message ranges/sequences and set SELECTED */
110 for (msgnum = 0; msgnum < msgp; msgnum++)
111 if (!m_convert(mp, msgs[msgnum]))
113 seq_setprev(mp); /* set the previous-sequence */
115 smsgs = (struct smsg *)
116 calloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
118 adios(NULL, "unable to allocate burst storage");
122 /* burst all the SELECTED messages */
123 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
124 if (is_selected(mp, msgnum)) {
125 if ((numburst = find_delim(msgnum, smsgs)) > 0) {
127 printf("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum);
128 burst(&mp, msgnum, smsgs, numburst, verbosw, maildir);
129 } else if (numburst == 0) {
130 admonish(NULL, "message %d not in digest format", msgnum);
132 adios(NULL, "burst() botch -- you lose big");
138 context_replace(curfolder, folder);
141 ** The first message extracted from the first digest
142 ** becomes the current message.
144 if (hi <= mp->hghmsg) {
157 ** Scan the message and find the beginning and
158 ** end of all the messages in the digest.
161 find_delim(int msgnum, struct smsg *smsgs)
163 int ld3, wasdlm, msgp;
170 ld3 = strlen(delim3);
172 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
173 adios(msgnam, "unable to read message");
175 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER; msgp++) {
176 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
177 pos += (long) strlen(buffer);
181 fseek(in, pos, SEEK_SET);
182 smsgs[msgp].s_start = pos;
184 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
185 if (strncmp(buffer, delim3, ld3) == 0
186 && (msgp == 1 || c == '\n')) {
189 if (cc == '\n' || cc == EOF) {
193 pos += (long) strlen(buffer);
196 wasdlm = strncmp(buffer, delim3, ld3) == 0;
197 if (smsgs[msgp].s_start != pos)
198 smsgs[msgp].s_stop = (c == '\n' && wasdlm) ?
203 pos += (long) strlen(buffer);
207 return (msgp > 0) ? msgp-1 : 0; /* toss "End of XXX Digest" */
212 ** Burst out the messages in the digest into the folder
215 burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
216 int verbosw, char *maildir)
220 char destfil[BUFSIZ];
225 if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
226 adios(msgnam, "unable to read message");
228 mode = fstat(fileno(in), &st) != NOTOK ?
229 (int)(st.st_mode & 0777) : m_gmprot();
233 ** Ensure we have enough space in the folder
234 ** structure for all the new messages.
236 if ((mp->hghmsg + numburst > mp->hghoff) && !(mp = folder_realloc(mp,
237 mp->lowoff, mp->hghmsg + numburst)))
238 adios(NULL, "unable to allocate folder storage");
242 ** At this point, there is an array of numburst smsgs, each
243 ** element of which contains the starting and stopping offsets
244 ** (seeks) of the message in the digest. Go through the message
245 ** numbers. Ignore smsgs[0], which is the table of contents.
247 for (j=1, i=mp->hghmsg+1; j<=numburst; j++, i++) {
248 snprintf(destfil, sizeof destfil, "%s/%d", maildir, i);
249 if (!(out = fopen(destfil, "w"))) {
250 adios(destfil, "unable to open");
253 printf("message %d of digest %d becomes message %d\n",
256 chmod(destfil, mode);
257 fseek(in, smsgs[j].s_start, SEEK_SET);
258 cpybrst(in, out, msgnam, destfil,
259 (int) (smsgs[j].s_stop - smsgs[j].s_start));
264 ** Bursting each message is equivalent to adding a
265 ** new message from the point of view of the external hooks.
267 ext_hook("add-hook", destfil, NULL);
268 copy_msg_flags(mp, i, msgnum);
269 mp->msgflags |= SEQMOD;
276 ** Copy a message which is being burst out of a digest.
277 ** It will remove any "dashstuffing" in the message.
280 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
283 enum { S1, S2, S3 } state;
285 for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
320 state = (c == '\n') ? S1 : S2;
329 if (ferror(in) && !feof(in))
330 adios(ifile, "error reading");
332 adios(ofile, "error writing");