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