]> diplodocus.org Git - nmh/blob - uip/burst.c
Feed fileproc and mhlproc from rcvdist, send, and whatnow to post.
[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 = fstat (fileno(in), &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
271 mp = *mpp;
272
273 /*
274 * See if we have enough space in the folder
275 * structure for all the new messages.
276 */
277 if ((mp->hghmsg + numburst > mp->hghoff) &&
278 !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + numburst)))
279 adios (NULL, "unable to allocate folder storage");
280 *mpp = mp;
281
282 j = mp->hghmsg; /* old value */
283 mp->hghmsg += numburst;
284 mp->nummsg += numburst;
285
286 /*
287 * If this is not the highest SELECTED message, then
288 * increment mp->hghsel by numburst, since the highest
289 * SELECTED is about to be slid down by that amount.
290 */
291 if (msgnum < mp->hghsel)
292 mp->hghsel += numburst;
293
294 /*
295 * If -inplace is given, renumber the messages after the
296 * source message, to make room for each of the messages
297 * contained within the digest.
298 *
299 * This is equivalent to refiling a message from the point
300 * of view of the external hooks.
301 */
302 if (inplace) {
303 for (i = mp->hghmsg; j > msgnum; i--, j--) {
304 strncpy (f1, m_name (i), sizeof(f1));
305 strncpy (f2, m_name (j), sizeof(f2));
306 if (does_exist (mp, j)) {
307 if (verbosw)
308 printf ("message %d becomes message %d\n", j, i);
309
310 if (rename (f2, f1) == NOTOK)
311 admonish (f1, "unable to rename %s to", f2);
312
313 (void)snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
314 (void)snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
315 ext_hook("ref-hook", f1, f2);
316
317 copy_msg_flags (mp, i, j);
318 clear_msg_flags (mp, j);
319 mp->msgflags |= SEQMOD;
320 }
321 }
322 }
323
324 unset_selected (mp, msgnum);
325
326 /* new hghmsg is hghmsg + numburst
327 *
328 * At this point, there is an array of numburst smsgs, each element of
329 * which contains the starting and stopping offsets (seeks) of the message
330 * in the digest. The inplace flag is set if the original digest is replaced
331 * by a message containing the table of contents. smsgs[0] is that table of
332 * contents. Go through the message numbers in reverse order (high to low).
333 *
334 * Set f1 to the name of the destination message, f2 to the name of a scratch
335 * file. Extract a message from the digest to the scratch file. Move the
336 * original message to a backup file if the destination message number is the
337 * same as the number of the original message, which only happens if the
338 * inplace flag is set. Then move the scratch file to the destination message.
339 *
340 * Moving the original message to the backup file is equivalent to deleting the
341 * message from the point of view of the external hooks. And bursting each
342 * message is equivalent to adding a new message.
343 */
344
345 i = inplace ? msgnum + numburst : mp->hghmsg;
346 for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
347 strncpy (f1, m_name (i), sizeof(f1));
348 strncpy (f2, m_mktemp(invo_name, NULL, &out), sizeof(f2));
349
350 if (verbosw && i != msgnum)
351 printf ("message %d of digest %d becomes message %d\n", j, msgnum, i);
352
353 chmod (f2, mode);
354 fseek (in, smsgs[j].s_start, SEEK_SET);
355 cpybrst (in, out, msgnam, f2,
356 (int) (smsgs[j].s_stop - smsgs[j].s_start));
357 fclose (out);
358
359 if (i == msgnum) {
360 strncpy (f3, m_backup (f1), sizeof(f3));
361 if (rename (f1, f3) == NOTOK)
362 admonish (f3, "unable to rename %s to", f1);
363
364 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
365 ext_hook("del-hook", f3, (char *)0);
366 }
367 if (rename (f2, f1) == NOTOK)
368 admonish (f1, "unable to rename %s to", f2);
369
370 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
371 ext_hook("add-hook", f3, (char *)0);
372
373 copy_msg_flags (mp, i, msgnum);
374 mp->msgflags |= SEQMOD;
375 }
376
377 fclose (in);
378 }
379
380
381 #define S1 0
382 #define S2 1
383 #define S3 2
384
385 /*
386 * Copy a mesage which is being burst out of a digest.
387 * It will remove any "dashstuffing" in the message.
388 */
389
390 static void
391 cpybrst (FILE *in, FILE *out, char *ifile, char *ofile, int len)
392 {
393 register int c, state;
394
395 for (state = S1; (c = fgetc (in)) != EOF && len > 0; len--) {
396 if (c == 0)
397 continue;
398 switch (state) {
399 case S1:
400 switch (c) {
401 case '-':
402 state = S3;
403 break;
404
405 default:
406 state = S2;
407 case '\n':
408 fputc (c, out);
409 break;
410 }
411 break;
412
413 case S2:
414 switch (c) {
415 case '\n':
416 state = S1;
417 default:
418 fputc (c, out);
419 break;
420 }
421 break;
422
423 case S3:
424 switch (c) {
425 case ' ':
426 state = S2;
427 break;
428
429 default:
430 state = (c == '\n') ? S1 : S2;
431 fputc ('-', out);
432 fputc (c, out);
433 break;
434 }
435 break;
436 }
437 }
438
439 if (ferror (in) && !feof (in))
440 adios (ifile, "error reading");
441 if (ferror (out))
442 adios (ofile, "error writing");
443 }