]>
diplodocus.org Git - nmh/blob - uip/burst.c
c0541e8db453e363cdb01051acfe3b57e6545696
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/folder_free.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
19 #include "h/mhparse.h"
21 #include "sbr/m_maildir.h"
22 #include "sbr/m_mktemp.h"
25 #define BURST_SWITCHES \
26 X("inplace", 0, INPLSW) \
27 X("noinplace", 0, NINPLSW) \
28 X("mime", 0, MIMESW) \
29 X("nomime", 0, NMIMESW) \
30 X("automime", 0, AUTOMIMESW) \
31 X("quiet", 0, QIETSW) \
32 X("noquiet", 0, NQIETSW) \
33 X("verbose", 0, VERBSW) \
34 X("noverbose", 0, NVERBSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(BURST
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(BURST
, switches
);
52 * For the MIME parsing routines
60 static int find_delim (int, struct smsg
*, int *);
61 static void find_mime_parts (CT
, struct smsg
*, int *);
62 static void burst(struct msgs
**, int, struct smsg
*, int, bool, bool,
64 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
67 * A macro to check to see if we have reached a message delimiter
68 * (an encapsulation boundary, EB, in RFC 934 parlance).
70 * According to RFC 934, an EB is simply a line which starts with
71 * a "-" and is NOT followed by a space. So even a single "-" on a line
72 * by itself would be an EB.
75 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
78 main (int argc
, char **argv
)
84 int hi
, msgnum
, numburst
;
85 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
86 char **argp
, **arguments
;
87 struct msgs_array msgs
= { 0, 0, NULL
};
91 if (nmh_init(argv
[0], true, true)) { return 1; }
93 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 die("-%s unknown\n", cp
);
106 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
146 if (*cp
== '+' || *cp
== '@') {
148 die("only one folder at a time!");
149 folder
= pluspath (cp
);
151 app_msgarg(&msgs
, cp
);
155 if (!context_find ("path"))
156 free (path ("./", TFOLDER
));
158 app_msgarg(&msgs
, "cur");
160 folder
= getfolder (1);
161 maildir
= m_maildir (folder
);
163 if (chdir (maildir
) == NOTOK
)
164 adios (maildir
, "unable to change directory to");
166 /* read folder and create message structure */
167 if (!(mp
= folder_read (folder
, 1)))
168 die("unable to read folder %s", folder
);
170 /* check for empty folder */
172 die("no messages in %s", folder
);
174 /* parse all the message ranges/sequences and set SELECTED */
175 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
176 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
178 seq_setprev (mp
); /* set the previous-sequence */
180 smsgs
= mh_xcalloc(MAXFOLDER
+ 2, sizeof *smsgs
);
184 /* burst all the SELECTED messages */
185 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
186 if (is_selected (mp
, msgnum
)) {
187 if ((numburst
= find_delim (msgnum
, smsgs
, &mimesw
)) >= 1) {
189 printf ("%d message%s exploded from digest %d\n",
190 numburst
, PLURALS(numburst
), msgnum
);
191 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
,
196 inform("message %d not in digest format, continuing...",
198 } /* this pair of braces was missing before 1999-07-15 */
200 die("burst() botch -- you lose big");
206 context_replace (pfolder
, folder
); /* update current folder */
209 * If -inplace is given, then the first message burst becomes
210 * the current message (which will now show a table of contents).
211 * Otherwise, the first message extracted from the first digest
212 * becomes the current message.
215 if (mp
->lowsel
!= mp
->curmsg
)
216 seq_setcur (mp
, mp
->lowsel
);
218 if (hi
<= mp
->hghmsg
)
222 seq_save (mp
); /* synchronize message sequences */
223 context_save (); /* save the context file */
224 folder_free (mp
); /* free folder/message structure */
231 * Scan the message and find the beginning and
232 * end of all the messages in the digest.
234 * If requested, see if the message is MIME-formatted and contains any
235 * message/rfc822 parts; if so, burst those parts.
239 find_delim (int msgnum
, struct smsg
*smsgs
, int *mimesw
)
241 int wasdlm
= 0, msgp
;
248 msgnam
= m_name (msgnum
);
251 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
255 content
= parse_mime(msgnam
);
256 if (! content
&& *mimesw
== 2)
259 smsgs
[0].s_start
= 0;
260 smsgs
[0].s_stop
= content
->c_begin
- 1;
262 find_mime_parts(content
, smsgs
, &msgp
);
263 free_content(content
);
264 if (msgp
== 1 && *mimesw
== 2)
265 adios (msgnam
, "does not have any message/rfc822 parts");
275 if ((in
= fopen (msgnam
, "r")) == NULL
)
276 adios (msgnam
, "unable to read message");
278 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
280 * We're either at the beginning of the whole message, or
281 * we're just past the delimiter of the last message.
282 * Swallow lines until we get to something that's not a newline
284 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
285 pos
+= (long) strlen (buffer
);
290 * Reset to the beginning of the last non-blank line, and save our
291 * starting position. This is where the encapsulated message
294 fseeko (in
, pos
, SEEK_SET
);
295 smsgs
[msgp
].s_start
= pos
;
298 * Read in lines until we get to a message delimiter.
300 * Previously we checked to make sure the preceding line and
301 * next line was a newline. That actually does not comply with
302 * RFC 934, so make sure we break on a message delimiter even
303 * if the previous character was NOT a newline.
305 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
306 if ((wasdlm
= CHECKDELIM(buffer
)))
308 pos
+= (long) strlen (buffer
);
312 * Only count as a new message if we got the message delimiter.
313 * Swallow a blank line if it was right before the message delimiter.
315 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
316 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
321 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
322 msgp
++; /* fake "End of XXX Digest" */
327 pos
+= (long) strlen (buffer
);
331 return msgp
- 1; /* return the number of messages burst */
336 * Find any MIME content in the message that is a message/rfc822 and add
337 * it to the list of messages to burst.
341 find_mime_parts (CT content
, struct smsg
*smsgs
, int *msgp
)
347 * If we have a message/rfc822, then it's easy.
350 if (content
->c_type
== CT_MESSAGE
&&
351 content
->c_subtype
== MESSAGE_RFC822
) {
352 smsgs
[*msgp
].s_start
= content
->c_begin
;
353 smsgs
[*msgp
].s_stop
= content
->c_end
;
359 * Otherwise, if we do have multiparts, try all of the sub-parts.
362 if (content
->c_type
== CT_MULTIPART
) {
363 m
= (struct multipart
*) content
->c_ctparams
;
365 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
366 find_mime_parts(part
->mp_part
, smsgs
, msgp
);
372 * Burst out the messages in the digest into the folder
376 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
377 bool inplace
, bool verbosw
, char *maildir
, int mimesw
)
381 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
386 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
387 adios (msgnam
, "unable to read message");
390 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
394 * See if we have enough space in the folder
395 * structure for all the new messages.
397 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
398 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
399 die("unable to allocate folder storage");
402 j
= mp
->hghmsg
; /* old value */
403 mp
->hghmsg
+= numburst
;
404 mp
->nummsg
+= numburst
;
407 * If this is not the highest SELECTED message, then
408 * increment mp->hghsel by numburst, since the highest
409 * SELECTED is about to be slid down by that amount.
411 if (msgnum
< mp
->hghsel
)
412 mp
->hghsel
+= numburst
;
415 * If -inplace is given, renumber the messages after the
416 * source message, to make room for each of the messages
417 * contained within the digest.
419 * This is equivalent to refiling a message from the point
420 * of view of the external hooks.
423 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
424 strncpy (f1
, m_name (i
), sizeof(f1
));
425 strncpy (f2
, m_name (j
), sizeof(f2
));
426 if (does_exist (mp
, j
)) {
428 printf ("message %d becomes message %d\n", j
, i
);
430 if (rename (f2
, f1
) == NOTOK
)
431 admonish (f1
, "unable to rename %s to", f2
);
433 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
434 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
435 ext_hook("ref-hook", f1
, f2
);
437 copy_msg_flags (mp
, i
, j
);
438 clear_msg_flags (mp
, j
);
439 mp
->msgflags
|= SEQMOD
;
444 unset_selected (mp
, msgnum
);
446 /* new hghmsg is hghmsg + numburst
448 * At this point, there is an array of numburst smsgs, each element of
449 * which contains the starting and stopping offsets (seeks) of the message
450 * in the digest. The inplace flag is set if the original digest is replaced
451 * by a message containing the table of contents. smsgs[0] is that table of
452 * contents. Go through the message numbers in reverse order (high to low).
454 * Set f1 to the name of the destination message, f2 to the name of a scratch
455 * file. Extract a message from the digest to the scratch file. Move the
456 * original message to a backup file if the destination message number is the
457 * same as the number of the original message, which only happens if the
458 * inplace flag is set. Then move the scratch file to the destination message.
460 * Moving the original message to the backup file is equivalent to deleting the
461 * message from the point of view of the external hooks. And bursting each
462 * message is equivalent to adding a new message.
465 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
466 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
469 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
470 die("unable to create temporary file in %s",
473 strncpy (f2
, tempfile
, sizeof(f2
));
474 strncpy (f1
, m_name (i
), sizeof(f1
));
476 if (verbosw
&& i
!= msgnum
)
477 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
480 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
481 cpybrst (in
, out
, msgnam
, f2
,
482 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
), mimesw
);
486 strncpy (f3
, m_backup (f1
), sizeof(f3
));
487 if (rename (f1
, f3
) == NOTOK
)
488 admonish (f3
, "unable to rename %s to", f1
);
490 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
491 ext_hook("del-hook", f3
, NULL
);
493 if (rename (f2
, f1
) == NOTOK
)
494 admonish (f1
, "unable to rename %s to", f2
);
496 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
497 ext_hook("add-hook", f3
, NULL
);
499 copy_msg_flags (mp
, i
, msgnum
);
500 mp
->msgflags
|= SEQMOD
;
513 * Copy a message which is being burst out of a digest.
514 * It will remove any "dashstuffing" in the message.
518 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
, int mime
)
522 for (state
= mime
? S4
: S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
559 state
= (c
== '\n') ? S1
: S2
;
572 if (ferror (in
) && !feof (in
))
573 adios (ifile
, "error reading");
575 adios (ofile
, "error writing");