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