remove Previus-Sequence support
[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 char *version=VERSION;
29
30 static char delim3[] = "-------";
31
32 struct smsg {
33         long s_start;
34         long s_stop;
35 };
36
37 /*
38 ** static prototypes
39 */
40 static int find_delim(int, struct smsg *);
41 static void burst(struct msgs **, int, struct smsg *, int, int, char *);
42 static void cpybrst(FILE *, FILE *, char *, char *, int);
43
44
45 int
46 main(int argc, char **argv)
47 {
48         int verbosw = 0;
49         int msgp = 0, hi, msgnum, numburst;
50         char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
51         char **argp, **arguments, *msgs[MAXARGS];
52         struct smsg *smsgs;
53         struct msgs *mp;
54
55         setlocale(LC_ALL, "");
56         invo_name = mhbasename(argv[0]);
57
58         /* read user profile/context */
59         context_read();
60
61         arguments = getarguments(invo_name, argc, argv, 1);
62         argp = arguments;
63
64         while ((cp = *argp++)) {
65                 if (*cp == '-') {
66                         switch (smatch(++cp, switches)) {
67                         case AMBIGSW:
68                                 ambigsw(cp, switches);
69                                 exit(EX_USAGE);
70                         case UNKWNSW:
71                                 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
72
73                         case HELPSW:
74                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
75                                 print_help(buf, switches, 1);
76                                 exit(argc == 2 ? EX_OK : EX_USAGE);
77                         case VERSIONSW:
78                                 print_version(invo_name);
79                                 exit(argc == 2 ? EX_OK : EX_USAGE);
80
81                         case VERBSW:
82                                 verbosw++;
83                                 continue;
84                         case NVERBSW:
85                                 verbosw = 0;
86                                 continue;
87                         }
88                 }
89                 if (*cp == '+' || *cp == '@') {
90                         if (folder)
91                                 adios(EX_USAGE, NULL, "only one folder at a time!");
92                         else
93                                 folder = mh_xstrdup(expandfol(cp));
94                 } else {
95                         msgs[msgp++] = cp;
96                 }
97         }
98
99         if (!msgp)
100                 msgs[msgp++] = seq_cur;
101         if (!folder)
102                 folder = getcurfol();
103         maildir = toabsdir(folder);
104
105         if (chdir(maildir) == NOTOK)
106                 adios(EX_SOFTWARE, maildir, "unable to change directory to");
107
108         /* read folder and create message structure */
109         if (!(mp = folder_read(folder)))
110                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
111
112         /* check for empty folder */
113         if (mp->nummsg == 0)
114                 adios(EX_DATAERR, NULL, "no messages in %s", folder);
115
116         /* parse all the message ranges/sequences and set SELECTED */
117         for (msgnum = 0; msgnum < msgp; msgnum++)
118                 if (!m_convert(mp, msgs[msgnum]))
119                         exit(EX_SOFTWARE);
120
121         smsgs = mh_xcalloc(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         mh_free0(&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 }