]> diplodocus.org Git - nmh/blob - uip/burst.c
Make the test suite work on systems other than Linux. Still needs work.
[nmh] / uip / burst.c
1
2 /*
3 * burst.c -- explode digests into individual messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11
12 static struct swit switches[] = {
13 #define INPLSW 0
14 { "inplace", 0 },
15 #define NINPLSW 1
16 { "noinplace", 0 },
17 #define QIETSW 2
18 { "quiet", 0 },
19 #define NQIETSW 3
20 { "noquiet", 0 },
21 #define VERBSW 4
22 { "verbose", 0 },
23 #define NVERBSW 5
24 { "noverbose", 0 },
25 #define VERSIONSW 6
26 { "version", 0 },
27 #define HELPSW 7
28 { "help", 0 },
29 { NULL, 0 }
30 };
31
32 static char delim3[] = "-------";
33
34 struct smsg {
35 long s_start;
36 long s_stop;
37 };
38
39 /*
40 * static prototypes
41 */
42 static int find_delim (int, struct smsg *);
43 static void burst (struct msgs **, int, struct smsg *, int, int, int, char *);
44 static void cpybrst (FILE *, FILE *, char *, char *, int);
45
46
47 int
48 main (int argc, char **argv)
49 {
50 int inplace = 0, quietsw = 0, verbosw = 0;
51 int msgp = 0, hi, msgnum, numburst;
52 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
53 char **argp, **arguments, *msgs[MAXARGS];
54 struct smsg *smsgs;
55 struct msgs *mp;
56
57 #ifdef LOCALE
58 setlocale(LC_ALL, "");
59 #endif
60 invo_name = r1bindex (argv[0], '/');
61
62 /* read user profile/context */
63 context_read();
64
65 arguments = getarguments (invo_name, argc, argv, 1);
66 argp = arguments;
67
68 while ((cp = *argp++)) {
69 if (*cp == '-') {
70 switch (smatch (++cp, switches)) {
71 case AMBIGSW:
72 ambigsw (cp, switches);
73 done (1);
74 case UNKWNSW:
75 adios (NULL, "-%s unknown\n", cp);
76
77 case HELPSW:
78 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
79 invo_name);
80 print_help (buf, switches, 1);
81 done (1);
82 case VERSIONSW:
83 print_version(invo_name);
84 done (1);
85
86 case INPLSW:
87 inplace++;
88 continue;
89 case NINPLSW:
90 inplace = 0;
91 continue;
92
93 case QIETSW:
94 quietsw++;
95 continue;
96 case NQIETSW:
97 quietsw = 0;
98 continue;
99
100 case VERBSW:
101 verbosw++;
102 continue;
103 case NVERBSW:
104 verbosw = 0;
105 continue;
106 }
107 }
108 if (*cp == '+' || *cp == '@') {
109 if (folder)
110 adios (NULL, "only one folder at a time!");
111 else
112 folder = pluspath (cp);
113 } else {
114 msgs[msgp++] = cp;
115 }
116 }
117
118 if (!context_find ("path"))
119 free (path ("./", TFOLDER));
120 if (!msgp)
121 msgs[msgp++] = "cur";
122 if (!folder)
123 folder = getfolder (1);
124 maildir = m_maildir (folder);
125
126 if (chdir (maildir) == NOTOK)
127 adios (maildir, "unable to change directory to");
128
129 /* read folder and create message structure */
130 if (!(mp = folder_read (folder)))
131 adios (NULL, "unable to read folder %s", folder);
132
133 /* check for empty folder */
134 if (mp->nummsg == 0)
135 adios (NULL, "no messages in %s", folder);
136
137 /* parse all the message ranges/sequences and set SELECTED */
138 for (msgnum = 0; msgnum < msgp; msgnum++)
139 if (!m_convert (mp, msgs[msgnum]))
140 done (1);
141 seq_setprev (mp); /* set the previous-sequence */
142
143 smsgs = (struct smsg *)
144 calloc ((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
145 if (smsgs == NULL)
146 adios (NULL, "unable to allocate burst storage");
147
148 hi = mp->hghmsg + 1;
149
150 /* burst all the SELECTED messages */
151 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
152 if (is_selected (mp, msgnum)) {
153 if ((numburst = find_delim (msgnum, smsgs)) >= 1) {
154 if (verbosw)
155 printf ("%d message%s exploded from digest %d\n",
156 numburst, numburst > 1 ? "s" : "", msgnum);
157 burst (&mp, msgnum, smsgs, numburst, inplace, verbosw, maildir);
158 } else {
159 if (numburst == 0) {
160 if (!quietsw)
161 admonish (NULL, "message %d not in digest format",
162 msgnum);
163 } /* this pair of braces was missing before 1999-07-15 */
164 else
165 adios (NULL, "burst() botch -- you lose big");
166 }
167 }
168 }
169
170 free ((char *) smsgs);
171 context_replace (pfolder, folder); /* update current folder */
172
173 /*
174 * If -inplace is given, then the first message burst becomes
175 * the current message (which will now show a table of contents).
176 * Otherwise, the first message extracted from the first digest
177 * becomes the current message.
178 */
179 if (inplace) {
180 if (mp->lowsel != mp->curmsg)
181 seq_setcur (mp, mp->lowsel);
182 } else {
183 if (hi <= mp->hghmsg)
184 seq_setcur (mp, hi);
185 }
186
187 seq_save (mp); /* synchronize message sequences */
188 context_save (); /* save the context file */
189 folder_free (mp); /* free folder/message structure */
190 done (0);
191 return 1;
192 }
193
194
195 /*
196 * Scan the message and find the beginning and
197 * end of all the messages in the digest.
198 */
199
200 static int
201 find_delim (int msgnum, struct smsg *smsgs)
202 {
203 int ld3, wasdlm, msgp;
204 long pos;
205 char c, *msgnam;
206 int cc;
207 char buffer[BUFSIZ];
208 FILE *in;
209
210 ld3 = strlen (delim3);
211
212 if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
213 adios (msgnam, "unable to read message");
214
215 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
216 while (fgets (buffer, sizeof(buffer), in) && buffer[0] == '\n')
217 pos += (long) strlen (buffer);
218 if (feof (in))
219 break;
220 fseek (in, pos, SEEK_SET);
221 smsgs[msgp].s_start = pos;
222
223 for (c = 0; fgets (buffer, sizeof(buffer), in); c = buffer[0]) {
224 if (strncmp (buffer, delim3, ld3) == 0
225 && (msgp == 1 || c == '\n')
226 && ((cc = peekc (in)) == '\n' || cc == EOF))
227 break;
228 else
229 pos += (long) strlen (buffer);
230 }
231
232 wasdlm = strncmp (buffer, delim3, ld3) == 0;
233 if (smsgs[msgp].s_start != pos)
234 smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos;
235 if (feof (in)) {
236 #if 0
237 if (wasdlm) {
238 smsgs[msgp - 1].s_stop -= ((long) strlen (buffer) + 1);
239 msgp++; /* fake "End of XXX Digest" */
240 }
241 #endif
242 break;
243 }
244 pos += (long) strlen (buffer);
245 }
246
247 fclose (in);
248 return (msgp - 1); /* toss "End of XXX Digest" */
249 }
250
251
252 /*
253 * Burst out the messages in the digest into the folder
254 */
255
256 static void
257 burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
258 int inplace, int verbosw, char *maildir)
259 {
260 int i, j, mode;
261 char *msgnam;
262 char f1[BUFSIZ], f2[BUFSIZ], f3[BUFSIZ];
263 FILE *in, *out;
264 struct stat st;
265 struct msgs *mp;
266
267 if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
268 adios (msgnam, "unable to read message");
269
270 mode =
271 fstat (fileno(in), &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot();
272 mp = *mpp;
273
274 /*
275 * See if we have enough space in the folder
276 * structure for all the new messages.
277 */
278 if ((mp->hghmsg + numburst > mp->hghoff) &&
279 !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + numburst)))
280 adios (NULL, "unable to allocate folder storage");
281 *mpp = mp;
282
283 j = mp->hghmsg; /* old value */
284 mp->hghmsg += numburst;
285 mp->nummsg += numburst;
286
287 /*
288 * If this is not the highest SELECTED message, then
289 * increment mp->hghsel by numburst, since the highest
290 * SELECTED is about to be slid down by that amount.
291 */
292 if (msgnum < mp->hghsel)
293 mp->hghsel += numburst;
294
295 /*
296 * If -inplace is given, renumber the messages after the
297 * source message, to make room for each of the messages
298 * contained within the digest.
299 *
300 * This is equivalent to refiling a message from the point
301 * of view of the external hooks.
302 */
303 if (inplace) {
304 for (i = mp->hghmsg; j > msgnum; i--, j--) {
305 strncpy (f1, m_name (i), sizeof(f1));
306 strncpy (f2, m_name (j), sizeof(f2));
307 if (does_exist (mp, j)) {
308 if (verbosw)
309 printf ("message %d becomes message %d\n", j, i);
310
311 if (rename (f2, f1) == NOTOK)
312 admonish (f1, "unable to rename %s to", f2);
313
314 (void)snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
315 (void)snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
316 ext_hook("ref-hook", f1, f2);
317
318 copy_msg_flags (mp, i, j);
319 clear_msg_flags (mp, j);
320 mp->msgflags |= SEQMOD;
321 }
322 }
323 }
324
325 unset_selected (mp, msgnum);
326
327 /* new hghmsg is hghmsg + numburst
328 *
329 * At this point, there is an array of numburst smsgs, each element of
330 * which contains the starting and stopping offsets (seeks) of the message
331 * in the digest. The inplace flag is set if the original digest is replaced
332 * by a message containing the table of contents. smsgs[0] is that table of
333 * contents. Go through the message numbers in reverse order (high to low).
334 *
335 * Set f1 to the name of the destination message, f2 to the name of a scratch
336 * file. Extract a message from the digest to the scratch file. Move the
337 * original message to a backup file if the destination message number is the
338 * same as the number of the original message, which only happens if the
339 * inplace flag is set. Then move the scratch file to the destination message.
340 *
341 * Moving the original message to the backup file is equivalent to deleting the
342 * message from the point of view of the external hooks. And bursting each
343 * message is equivalent to adding a new message.
344 */
345
346 i = inplace ? msgnum + numburst : mp->hghmsg;
347 for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
348 strncpy (f1, m_name (i), sizeof(f1));
349 strncpy (f2, m_mktemp(invo_name, NULL, &out), sizeof(f2));
350
351 if (verbosw && i != msgnum)
352 printf ("message %d of digest %d becomes message %d\n", j, msgnum, i);
353
354 chmod (f2, mode);
355 fseek (in, smsgs[j].s_start, SEEK_SET);
356 cpybrst (in, out, msgnam, f2,
357 (int) (smsgs[j].s_stop - smsgs[j].s_start));
358 fclose (out);
359
360 if (i == msgnum) {
361 strncpy (f3, m_backup (f1), sizeof(f3));
362 if (rename (f1, f3) == NOTOK)
363 admonish (f3, "unable to rename %s to", f1);
364
365 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
366 ext_hook("del-hook", f3, (char *)0);
367 }
368 if (rename (f2, f1) == NOTOK)
369 admonish (f1, "unable to rename %s to", f2);
370
371 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
372 ext_hook("add-hook", f3, (char *)0);
373
374 copy_msg_flags (mp, i, msgnum);
375 mp->msgflags |= SEQMOD;
376 }
377
378 fclose (in);
379 }
380
381
382 #define S1 0
383 #define S2 1
384 #define S3 2
385
386 /*
387 * Copy a mesage which is being burst out of a digest.
388 * It will remove any "dashstuffing" in the message.
389 */
390
391 static void
392 cpybrst (FILE *in, FILE *out, char *ifile, char *ofile, int len)
393 {
394 register int c, state;
395
396 for (state = S1; (c = fgetc (in)) != EOF && len > 0; len--) {
397 if (c == 0)
398 continue;
399 switch (state) {
400 case S1:
401 switch (c) {
402 case '-':
403 state = S3;
404 break;
405
406 default:
407 state = S2;
408 case '\n':
409 fputc (c, out);
410 break;
411 }
412 break;
413
414 case S2:
415 switch (c) {
416 case '\n':
417 state = S1;
418 default:
419 fputc (c, out);
420 break;
421 }
422 break;
423
424 case S3:
425 switch (c) {
426 case ' ':
427 state = S2;
428 break;
429
430 default:
431 state = (c == '\n') ? S1 : S2;
432 fputc ('-', out);
433 fputc (c, out);
434 break;
435 }
436 break;
437 }
438 }
439
440 if (ferror (in) && !feof (in))
441 adios (ifile, "error reading");
442 if (ferror (out))
443 adios (ofile, "error writing");
444 }