119a936a9cd60bb4e012b17bc41b8758641afa7d
[mmh] / uip / burst.c
1 /*
2 ** burst.c -- explode digests into individual messages
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <unistd.h>
12 #include <sys/stat.h>
13 #include <locale.h>
14 #include <sysexits.h>
15
16 static struct swit switches[] = {
17 #define VERBSW 0
18         { "verbose", 0 },
19 #define NVERBSW 1
20         { "noverbose", 2 },
21 #define VERSIONSW 2
22         { "Version", 0 },
23 #define HELPSW 3
24         { "help", 0 },
25         { NULL, 0 }
26 };
27
28 static char delim3[] = "-------";
29
30 struct smsg {
31         long s_start;
32         long s_stop;
33 };
34
35 /*
36 ** static prototypes
37 */
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);
41
42
43 int
44 main(int argc, char **argv)
45 {
46         int verbosw = 0;
47         int msgp = 0, hi, msgnum, numburst;
48         char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
49         char **argp, **arguments, *msgs[MAXARGS];
50         struct smsg *smsgs;
51         struct msgs *mp;
52
53         setlocale(LC_ALL, "");
54         invo_name = mhbasename(argv[0]);
55
56         /* read user profile/context */
57         context_read();
58
59         arguments = getarguments(invo_name, argc, argv, 1);
60         argp = arguments;
61
62         while ((cp = *argp++)) {
63                 if (*cp == '-') {
64                         switch (smatch(++cp, switches)) {
65                         case AMBIGSW:
66                                 ambigsw(cp, switches);
67                                 exit(EX_USAGE);
68                         case UNKWNSW:
69                                 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
70
71                         case HELPSW:
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);
75                         case VERSIONSW:
76                                 print_version(invo_name);
77                                 exit(argc == 2 ? EX_OK : EX_USAGE);
78
79                         case VERBSW:
80                                 verbosw++;
81                                 continue;
82                         case NVERBSW:
83                                 verbosw = 0;
84                                 continue;
85                         }
86                 }
87                 if (*cp == '+' || *cp == '@') {
88                         if (folder)
89                                 adios(EX_USAGE, NULL, "only one folder at a time!");
90                         else
91                                 folder = getcpy(expandfol(cp));
92                 } else {
93                         msgs[msgp++] = cp;
94                 }
95         }
96
97         if (!msgp)
98                 msgs[msgp++] = seq_cur;
99         if (!folder)
100                 folder = getcurfol();
101         maildir = toabsdir(folder);
102
103         if (chdir(maildir) == NOTOK)
104                 adios(EX_SOFTWARE, maildir, "unable to change directory to");
105
106         /* read folder and create message structure */
107         if (!(mp = folder_read(folder)))
108                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
109
110         /* check for empty folder */
111         if (mp->nummsg == 0)
112                 adios(EX_DATAERR, NULL, "no messages in %s", folder);
113
114         /* parse all the message ranges/sequences and set SELECTED */
115         for (msgnum = 0; msgnum < msgp; msgnum++)
116                 if (!m_convert(mp, msgs[msgnum]))
117                         exit(EX_SOFTWARE);
118         seq_setprev(mp);  /* set the previous-sequence */
119
120         smsgs = mh_xcalloc(MAXFOLDER + 2, sizeof(*smsgs));
121
122         hi = mp->hghmsg + 1;
123
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) {
128                                 if (verbosw)
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);
133                         } else {
134                                 adios(EX_SOFTWARE, NULL, "burst() botch -- you lose big");
135                         }
136                 }
137         }
138
139         free(smsgs);
140         context_replace(curfolder, folder);
141
142         /*
143         ** The first message extracted from the first digest
144         ** becomes the current message.
145         */
146         if (hi <= mp->hghmsg) {
147                 seq_setcur(mp, hi);
148         }
149
150         seq_save(mp);
151         context_save();
152         folder_free(mp);
153         return 0;
154 }
155
156
157 /*
158 ** Scan the message and find the beginning and
159 ** end of all the messages in the digest.
160 */
161 static int
162 find_delim(int msgnum, struct smsg *smsgs)
163 {
164         int ld3, wasdlm, msgp;
165         long pos;
166         char c, *msgnam;
167         int cc;
168         char buffer[BUFSIZ];
169         FILE *in;
170
171         ld3 = strlen(delim3);
172
173         if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
174                 adios(EX_IOERR, msgnam, "unable to read message");
175
176         for (msgp = 0, pos = 0L; msgp <= MAXFOLDER; msgp++) {
177                 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
178                         pos += (long) strlen(buffer);
179                 if (feof(in)) {
180                         break;
181                 }
182                 fseek(in, pos, SEEK_SET);
183                 smsgs[msgp].s_start = pos;
184
185                 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
186                         if (strncmp(buffer, delim3, ld3) == 0
187                                         && (msgp == 1 || c == '\n')) {
188                                 cc = getc(in);
189                                 ungetc(cc, in);
190                                 if (cc == '\n' || cc == EOF) {
191                                         break;
192                                 }
193                         } else
194                                 pos += (long) strlen(buffer);
195                 }
196
197                 wasdlm = strncmp(buffer, delim3, ld3) == 0;
198                 if (smsgs[msgp].s_start != pos)
199                         smsgs[msgp].s_stop = (c == '\n' && wasdlm) ?
200                                         pos - 1 : pos;
201                 if (feof(in)) {
202                         break;
203                 }
204                 pos += (long) strlen(buffer);
205         }
206
207         fclose(in);
208         return (msgp > 0) ? msgp-1 : 0;  /* toss "End of XXX Digest" */
209 }
210
211
212 /*
213 ** Burst out the messages in the digest into the folder
214 */
215 static void
216 burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
217         int verbosw, char *maildir)
218 {
219         int i, j, mode;
220         char *msgnam;
221         char destfil[BUFSIZ];
222         FILE *in, *out;
223         struct stat st;
224         struct msgs *mp;
225
226         if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
227                 adios(EX_IOERR, msgnam, "unable to read message");
228
229         mode = fstat(fileno(in), &st) != NOTOK ?
230                         (int)(st.st_mode & 0777) : m_gmprot();
231         mp = *mpp;
232
233         /*
234         ** Ensure we have enough space in the folder
235         ** structure for all the new messages.
236         */
237         if ((mp->hghmsg + numburst > mp->hghoff) && !(mp = folder_realloc(mp,
238                         mp->lowoff, mp->hghmsg + numburst)))
239                 adios(EX_OSERR, NULL, "unable to allocate folder storage");
240         *mpp = mp;
241
242         /*
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.
247         */
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(EX_IOERR, destfil, "unable to open");
252                 }
253                 if (verbosw) {
254                         printf("message %d of digest %d becomes message %d\n",
255                                         j, msgnum, i);
256                 }
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));
261                 fclose(out);
262                 mp->hghmsg++;
263                 mp->nummsg++;
264                 /*
265                 ** Bursting each message is equivalent to adding a
266                 ** new message from the point of view of the external hooks.
267                 */
268                 ext_hook("add-hook", destfil, NULL);
269                 copy_msg_flags(mp, i, msgnum);
270                 mp->msgflags |= SEQMOD;
271         }
272         fclose(in);
273 }
274
275
276 /*
277 ** Copy a message which is being burst out of a digest.
278 ** It will remove any "dashstuffing" in the message.
279 */
280 static void
281 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
282 {
283         int c;
284         enum { S1, S2, S3 } state;
285
286         for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
287                 if (c == 0)
288                         continue;
289                 switch (state) {
290                 case S1:
291                         switch (c) {
292                         case '-':
293                                 state = S3;
294                                 break;
295
296                         default:
297                                 state = S2;
298                         case '\n':
299                                 fputc(c, out);
300                                 break;
301                         }
302                         break;
303
304                 case S2:
305                         switch (c) {
306                         case '\n':
307                                 state = S1;
308                         default:
309                                 fputc(c, out);
310                                 break;
311                         }
312                         break;
313
314                 case S3:
315                         switch (c) {
316                         case ' ':
317                                 state = S2;
318                                 break;
319
320                         default:
321                                 state = (c == '\n') ? S1 : S2;
322                                 fputc('-', out);
323                                 fputc(c, out);
324                                 break;
325                         }
326                         break;
327                 }
328         }
329
330         if (ferror(in) && !feof(in))
331                 adios(EX_IOERR, ifile, "error reading");
332         if (ferror(out))
333                 adios(EX_IOERR, ofile, "error writing");
334 }