folder: Never display column headers (removed -header).
[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", 0 },
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 #ifdef LOCALE
49         setlocale(LC_ALL, "");
50 #endif
51         invo_name = mhbasename(argv[0]);
52
53         /* read user profile/context */
54         context_read();
55
56         arguments = getarguments(invo_name, argc, argv, 1);
57         argp = arguments;
58
59         while ((cp = *argp++)) {
60                 if (*cp == '-') {
61                         switch (smatch(++cp, switches)) {
62                         case AMBIGSW:
63                                 ambigsw(cp, switches);
64                                 done(1);
65                         case UNKWNSW:
66                                 adios(NULL, "-%s unknown\n", cp);
67
68                         case HELPSW:
69                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
70                                 print_help(buf, switches, 1);
71                                 done(1);
72                         case VERSIONSW:
73                                 print_version(invo_name);
74                                 done(1);
75
76                         case VERBSW:
77                                 verbosw++;
78                                 continue;
79                         case NVERBSW:
80                                 verbosw = 0;
81                                 continue;
82                         }
83                 }
84                 if (*cp == '+' || *cp == '@') {
85                         if (folder)
86                                 adios(NULL, "only one folder at a time!");
87                         else
88                                 folder = getcpy(expandfol(cp));
89                 } else {
90                         msgs[msgp++] = cp;
91                 }
92         }
93
94         if (!msgp)
95                 msgs[msgp++] = seq_cur;
96         if (!folder)
97                 folder = getcurfol();
98         maildir = toabsdir(folder);
99
100         if (chdir(maildir) == NOTOK)
101                 adios(maildir, "unable to change directory to");
102
103         /* read folder and create message structure */
104         if (!(mp = folder_read(folder)))
105                 adios(NULL, "unable to read folder %s", folder);
106
107         /* check for empty folder */
108         if (mp->nummsg == 0)
109                 adios(NULL, "no messages in %s", folder);
110
111         /* parse all the message ranges/sequences and set SELECTED */
112         for (msgnum = 0; msgnum < msgp; msgnum++)
113                 if (!m_convert(mp, msgs[msgnum]))
114                         done(1);
115         seq_setprev(mp);  /* set the previous-sequence */
116
117         smsgs = (struct smsg *)
118                 calloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
119         if (smsgs == NULL)
120                 adios(NULL, "unable to allocate burst storage");
121
122         hi = mp->hghmsg + 1;
123
124         /* burst all the SELECTED messages */
125         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
126                 if (is_selected(mp, msgnum)) {
127                         if ((numburst = find_delim(msgnum, smsgs)) > 0) {
128                                 if (verbosw)
129                                         printf("%d message%s exploded from digest %d\n", numburst, numburst > 1 ? "s" : "", msgnum);
130                                 burst(&mp, msgnum, smsgs, numburst, verbosw, maildir);
131                         } else if (numburst == 0) {
132                                 admonish(NULL, "message %d not in digest format", msgnum);
133                         } else {
134                                 adios(NULL, "burst() botch -- you lose big");
135                         }
136                 }
137         }
138
139         free(smsgs);
140         context_replace(curfolder, folder);
141
142         /*
143         ** The first message extracted from the first digest
144         ** becomes the current message.
145         */
146         if (hi <= mp->hghmsg) {
147                 seq_setcur(mp, hi);
148         }
149
150         seq_save(mp);
151         context_save();
152         folder_free(mp);
153         done(0);
154         return 1;
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(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(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(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(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(ifile, "error reading");
333         if (ferror(out))
334                 adios(ofile, "error writing");
335 }