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