591d7f4b508c6404c9a04e55fb27cd95eb156c9e
[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 = (struct smsg *)
121                 mh_xcalloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
122         if (smsgs == NULL)
123                 adios(EX_OSERR, NULL, "unable to allocate burst storage");
124
125         hi = mp->hghmsg + 1;
126
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) {
131                                 if (verbosw)
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);
136                         } else {
137                                 adios(EX_SOFTWARE, NULL, "burst() botch -- you lose big");
138                         }
139                 }
140         }
141
142         free(smsgs);
143         context_replace(curfolder, folder);
144
145         /*
146         ** The first message extracted from the first digest
147         ** becomes the current message.
148         */
149         if (hi <= mp->hghmsg) {
150                 seq_setcur(mp, hi);
151         }
152
153         seq_save(mp);
154         context_save();
155         folder_free(mp);
156         return 0;
157 }
158
159
160 /*
161 ** Scan the message and find the beginning and
162 ** end of all the messages in the digest.
163 */
164 static int
165 find_delim(int msgnum, struct smsg *smsgs)
166 {
167         int ld3, wasdlm, msgp;
168         long pos;
169         char c, *msgnam;
170         int cc;
171         char buffer[BUFSIZ];
172         FILE *in;
173
174         ld3 = strlen(delim3);
175
176         if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
177                 adios(EX_IOERR, msgnam, "unable to read message");
178
179         for (msgp = 0, pos = 0L; msgp <= MAXFOLDER; msgp++) {
180                 while (fgets(buffer, sizeof(buffer), in) && buffer[0] == '\n')
181                         pos += (long) strlen(buffer);
182                 if (feof(in)) {
183                         break;
184                 }
185                 fseek(in, pos, SEEK_SET);
186                 smsgs[msgp].s_start = pos;
187
188                 for (c = 0; fgets(buffer, sizeof(buffer), in); c = buffer[0]) {
189                         if (strncmp(buffer, delim3, ld3) == 0
190                                         && (msgp == 1 || c == '\n')) {
191                                 cc = getc(in);
192                                 ungetc(cc, in);
193                                 if (cc == '\n' || cc == EOF) {
194                                         break;
195                                 }
196                         } else
197                                 pos += (long) strlen(buffer);
198                 }
199
200                 wasdlm = strncmp(buffer, delim3, ld3) == 0;
201                 if (smsgs[msgp].s_start != pos)
202                         smsgs[msgp].s_stop = (c == '\n' && wasdlm) ?
203                                         pos - 1 : pos;
204                 if (feof(in)) {
205                         break;
206                 }
207                 pos += (long) strlen(buffer);
208         }
209
210         fclose(in);
211         return (msgp > 0) ? msgp-1 : 0;  /* toss "End of XXX Digest" */
212 }
213
214
215 /*
216 ** Burst out the messages in the digest into the folder
217 */
218 static void
219 burst(struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
220         int verbosw, char *maildir)
221 {
222         int i, j, mode;
223         char *msgnam;
224         char destfil[BUFSIZ];
225         FILE *in, *out;
226         struct stat st;
227         struct msgs *mp;
228
229         if ((in = fopen(msgnam = m_name(msgnum), "r")) == NULL)
230                 adios(EX_IOERR, msgnam, "unable to read message");
231
232         mode = fstat(fileno(in), &st) != NOTOK ?
233                         (int)(st.st_mode & 0777) : m_gmprot();
234         mp = *mpp;
235
236         /*
237         ** Ensure we have enough space in the folder
238         ** structure for all the new messages.
239         */
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");
243         *mpp = mp;
244
245         /*
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.
250         */
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");
255                 }
256                 if (verbosw) {
257                         printf("message %d of digest %d becomes message %d\n",
258                                         j, msgnum, i);
259                 }
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));
264                 fclose(out);
265                 mp->hghmsg++;
266                 mp->nummsg++;
267                 /*
268                 ** Bursting each message is equivalent to adding a
269                 ** new message from the point of view of the external hooks.
270                 */
271                 ext_hook("add-hook", destfil, NULL);
272                 copy_msg_flags(mp, i, msgnum);
273                 mp->msgflags |= SEQMOD;
274         }
275         fclose(in);
276 }
277
278
279 /*
280 ** Copy a message which is being burst out of a digest.
281 ** It will remove any "dashstuffing" in the message.
282 */
283 static void
284 cpybrst(FILE *in, FILE *out, char *ifile, char *ofile, int len)
285 {
286         int c;
287         enum { S1, S2, S3 } state;
288
289         for (state = S1; (c = fgetc(in)) != EOF && len > 0; len--) {
290                 if (c == 0)
291                         continue;
292                 switch (state) {
293                 case S1:
294                         switch (c) {
295                         case '-':
296                                 state = S3;
297                                 break;
298
299                         default:
300                                 state = S2;
301                         case '\n':
302                                 fputc(c, out);
303                                 break;
304                         }
305                         break;
306
307                 case S2:
308                         switch (c) {
309                         case '\n':
310                                 state = S1;
311                         default:
312                                 fputc(c, out);
313                                 break;
314                         }
315                         break;
316
317                 case S3:
318                         switch (c) {
319                         case ' ':
320                                 state = S2;
321                                 break;
322
323                         default:
324                                 state = (c == '\n') ? S1 : S2;
325                                 fputc('-', out);
326                                 fputc(c, out);
327                                 break;
328                         }
329                         break;
330                 }
331         }
332
333         if (ferror(in) && !feof(in))
334                 adios(EX_IOERR, ifile, "error reading");
335         if (ferror(out))
336                 adios(EX_IOERR, ofile, "error writing");
337 }