]>
diplodocus.org Git - nmh/blob - uip/burst.c
1 /* burst.c -- explode digests into individual messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/m_convert.h"
10 #include "sbr/m_backup.h"
11 #include "sbr/getfolder.h"
12 #include "sbr/ext_hook.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/folder_realloc.h"
15 #include "sbr/folder_free.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/error.h"
25 #include "h/mhparse.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/m_mktemp.h"
31 #define BURST_SWITCHES \
32 X("inplace", 0, INPLSW) \
33 X("noinplace", 0, NINPLSW) \
34 X("mime", 0, MIMESW) \
35 X("nomime", 0, NMIMESW) \
36 X("automime", 0, AUTOMIMESW) \
37 X("quiet", 0, QIETSW) \
38 X("noquiet", 0, NQIETSW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(BURST
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(BURST
, switches
);
58 * For the MIME parsing routines
66 static int find_delim (int, struct smsg
*, int *);
67 static void find_mime_parts (CT
, struct smsg
*, int *);
68 static void burst(struct msgs
**, int, struct smsg
*, int, bool, bool,
70 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
73 * A macro to check to see if we have reached a message delimiter
74 * (an encapsulation boundary, EB, in RFC 934 parlance).
76 * According to RFC 934, an EB is simply a line which starts with
77 * a "-" and is NOT followed by a space. So even a single "-" on a line
78 * by itself would be an EB.
81 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
84 main (int argc
, char **argv
)
90 int hi
, msgnum
, numburst
;
91 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
92 char **argp
, **arguments
;
93 struct msgs_array msgs
= { 0, 0, NULL
};
97 if (nmh_init(argv
[0], true, true)) { return 1; }
99 arguments
= getarguments (invo_name
, argc
, argv
, 1);
102 while ((cp
= *argp
++)) {
104 switch (smatch (++cp
, switches
)) {
106 ambigsw (cp
, switches
);
109 die("-%s unknown\n", cp
);
112 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
114 print_help (buf
, switches
, 1);
117 print_version(invo_name
);
152 if (*cp
== '+' || *cp
== '@') {
154 die("only one folder at a time!");
155 folder
= pluspath (cp
);
157 app_msgarg(&msgs
, cp
);
161 if (!context_find ("path"))
162 free (path ("./", TFOLDER
));
164 app_msgarg(&msgs
, "cur");
166 folder
= getfolder (1);
167 maildir
= m_maildir (folder
);
169 if (chdir (maildir
) == NOTOK
)
170 adios (maildir
, "unable to change directory to");
172 /* read folder and create message structure */
173 if (!(mp
= folder_read (folder
, 1)))
174 die("unable to read folder %s", folder
);
176 /* check for empty folder */
178 die("no messages in %s", folder
);
180 /* parse all the message ranges/sequences and set SELECTED */
181 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
182 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
184 seq_setprev (mp
); /* set the previous-sequence */
186 smsgs
= mh_xcalloc(MAXFOLDER
+ 2, sizeof *smsgs
);
190 /* burst all the SELECTED messages */
191 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
192 if (is_selected (mp
, msgnum
)) {
193 if ((numburst
= find_delim (msgnum
, smsgs
, &mimesw
)) >= 1) {
195 printf ("%d message%s exploded from digest %d\n",
196 numburst
, PLURALS(numburst
), msgnum
);
197 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
,
202 inform("message %d not in digest format, continuing...",
204 } /* this pair of braces was missing before 1999-07-15 */
206 die("burst() botch -- you lose big");
212 context_replace (pfolder
, folder
); /* update current folder */
215 * If -inplace is given, then the first message burst becomes
216 * the current message (which will now show a table of contents).
217 * Otherwise, the first message extracted from the first digest
218 * becomes the current message.
221 if (mp
->lowsel
!= mp
->curmsg
)
222 seq_setcur (mp
, mp
->lowsel
);
224 if (hi
<= mp
->hghmsg
)
228 seq_save (mp
); /* synchronize message sequences */
229 context_save (); /* save the context file */
230 folder_free (mp
); /* free folder/message structure */
237 * Scan the message and find the beginning and
238 * end of all the messages in the digest.
240 * If requested, see if the message is MIME-formatted and contains any
241 * message/rfc822 parts; if so, burst those parts.
245 find_delim (int msgnum
, struct smsg
*smsgs
, int *mimesw
)
247 int wasdlm
= 0, msgp
;
254 msgnam
= m_name (msgnum
);
257 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
261 content
= parse_mime(msgnam
);
262 if (! content
&& *mimesw
== 2)
265 smsgs
[0].s_start
= 0;
266 smsgs
[0].s_stop
= content
->c_begin
- 1;
268 find_mime_parts(content
, smsgs
, &msgp
);
269 free_content(content
);
270 if (msgp
== 1 && *mimesw
== 2)
271 adios (msgnam
, "does not have any message/rfc822 parts");
281 if ((in
= fopen (msgnam
, "r")) == NULL
)
282 adios (msgnam
, "unable to read message");
284 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
286 * We're either at the beginning of the whole message, or
287 * we're just past the delimiter of the last message.
288 * Swallow lines until we get to something that's not a newline
290 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
291 pos
+= (long) strlen (buffer
);
296 * Reset to the beginning of the last non-blank line, and save our
297 * starting position. This is where the encapsulated message
300 fseeko (in
, pos
, SEEK_SET
);
301 smsgs
[msgp
].s_start
= pos
;
304 * Read in lines until we get to a message delimiter.
306 * Previously we checked to make sure the preceding line and
307 * next line was a newline. That actually does not comply with
308 * RFC 934, so make sure we break on a message delimiter even
309 * if the previous character was NOT a newline.
311 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
312 if ((wasdlm
= CHECKDELIM(buffer
)))
314 pos
+= (long) strlen (buffer
);
318 * Only count as a new message if we got the message delimiter.
319 * Swallow a blank line if it was right before the message delimiter.
321 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
322 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
327 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
328 msgp
++; /* fake "End of XXX Digest" */
333 pos
+= (long) strlen (buffer
);
337 return msgp
- 1; /* return the number of messages burst */
342 * Find any MIME content in the message that is a message/rfc822 and add
343 * it to the list of messages to burst.
347 find_mime_parts (CT content
, struct smsg
*smsgs
, int *msgp
)
353 * If we have a message/rfc822, then it's easy.
356 if (content
->c_type
== CT_MESSAGE
&&
357 content
->c_subtype
== MESSAGE_RFC822
) {
358 smsgs
[*msgp
].s_start
= content
->c_begin
;
359 smsgs
[*msgp
].s_stop
= content
->c_end
;
365 * Otherwise, if we do have multiparts, try all of the sub-parts.
368 if (content
->c_type
== CT_MULTIPART
) {
369 m
= (struct multipart
*) content
->c_ctparams
;
371 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
372 find_mime_parts(part
->mp_part
, smsgs
, msgp
);
378 * Burst out the messages in the digest into the folder
382 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
383 bool inplace
, bool verbosw
, char *maildir
, int mimesw
)
387 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
392 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
393 adios (msgnam
, "unable to read message");
396 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
400 * See if we have enough space in the folder
401 * structure for all the new messages.
403 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
404 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
405 die("unable to allocate folder storage");
408 j
= mp
->hghmsg
; /* old value */
409 mp
->hghmsg
+= numburst
;
410 mp
->nummsg
+= numburst
;
413 * If this is not the highest SELECTED message, then
414 * increment mp->hghsel by numburst, since the highest
415 * SELECTED is about to be slid down by that amount.
417 if (msgnum
< mp
->hghsel
)
418 mp
->hghsel
+= numburst
;
421 * If -inplace is given, renumber the messages after the
422 * source message, to make room for each of the messages
423 * contained within the digest.
425 * This is equivalent to refiling a message from the point
426 * of view of the external hooks.
429 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
430 strncpy (f1
, m_name (i
), sizeof(f1
));
431 strncpy (f2
, m_name (j
), sizeof(f2
));
432 if (does_exist (mp
, j
)) {
434 printf ("message %d becomes message %d\n", j
, i
);
436 if (rename (f2
, f1
) == NOTOK
)
437 admonish (f1
, "unable to rename %s to", f2
);
439 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
440 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
441 ext_hook("ref-hook", f1
, f2
);
443 copy_msg_flags (mp
, i
, j
);
444 clear_msg_flags (mp
, j
);
445 mp
->msgflags
|= SEQMOD
;
450 unset_selected (mp
, msgnum
);
452 /* new hghmsg is hghmsg + numburst
454 * At this point, there is an array of numburst smsgs, each element of
455 * which contains the starting and stopping offsets (seeks) of the message
456 * in the digest. The inplace flag is set if the original digest is replaced
457 * by a message containing the table of contents. smsgs[0] is that table of
458 * contents. Go through the message numbers in reverse order (high to low).
460 * Set f1 to the name of the destination message, f2 to the name of a scratch
461 * file. Extract a message from the digest to the scratch file. Move the
462 * original message to a backup file if the destination message number is the
463 * same as the number of the original message, which only happens if the
464 * inplace flag is set. Then move the scratch file to the destination message.
466 * Moving the original message to the backup file is equivalent to deleting the
467 * message from the point of view of the external hooks. And bursting each
468 * message is equivalent to adding a new message.
471 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
472 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
475 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
476 die("unable to create temporary file in %s",
479 strncpy (f2
, tempfile
, sizeof(f2
));
480 strncpy (f1
, m_name (i
), sizeof(f1
));
482 if (verbosw
&& i
!= msgnum
)
483 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
486 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
487 cpybrst (in
, out
, msgnam
, f2
,
488 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
), mimesw
);
492 strncpy (f3
, m_backup (f1
), sizeof(f3
));
493 if (rename (f1
, f3
) == NOTOK
)
494 admonish (f3
, "unable to rename %s to", f1
);
496 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
497 ext_hook("del-hook", f3
, NULL
);
499 if (rename (f2
, f1
) == NOTOK
)
500 admonish (f1
, "unable to rename %s to", f2
);
502 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
503 ext_hook("add-hook", f3
, NULL
);
505 copy_msg_flags (mp
, i
, msgnum
);
506 mp
->msgflags
|= SEQMOD
;
519 * Copy a message which is being burst out of a digest.
520 * It will remove any "dashstuffing" in the message.
524 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
, int mime
)
528 for (state
= mime
? S4
: S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
565 state
= (c
== '\n') ? S1
: S2
;
578 if (ferror (in
) && !feof (in
))
579 adios (ifile
, "error reading");
581 adios (ofile
, "error writing");