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