]>
diplodocus.org Git - nmh/blob - uip/burst.c
5cae730c6703eedf7ae4bbf1d3be8bddbe549781
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/getfolder.h"
10 #include "sbr/ext_hook.h"
11 #include "sbr/folder_read.h"
12 #include "sbr/folder_realloc.h"
13 #include "sbr/folder_free.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_replace.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
23 #include "h/mhparse.h"
25 #include "sbr/m_maildir.h"
26 #include "sbr/m_mktemp.h"
29 #define BURST_SWITCHES \
30 X("inplace", 0, INPLSW) \
31 X("noinplace", 0, NINPLSW) \
32 X("mime", 0, MIMESW) \
33 X("nomime", 0, NMIMESW) \
34 X("automime", 0, AUTOMIMESW) \
35 X("quiet", 0, QIETSW) \
36 X("noquiet", 0, NQIETSW) \
37 X("verbose", 0, VERBSW) \
38 X("noverbose", 0, NVERBSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(BURST
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(BURST
, switches
);
56 * For the MIME parsing routines
64 static int find_delim (int, struct smsg
*, int *);
65 static void find_mime_parts (CT
, struct smsg
*, int *);
66 static void burst(struct msgs
**, int, struct smsg
*, int, bool, bool,
68 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
71 * A macro to check to see if we have reached a message delimiter
72 * (an encapsulation boundary, EB, in RFC 934 parlance).
74 * According to RFC 934, an EB is simply a line which starts with
75 * a "-" and is NOT followed by a space. So even a single "-" on a line
76 * by itself would be an EB.
79 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
82 main (int argc
, char **argv
)
88 int hi
, msgnum
, numburst
;
89 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
90 char **argp
, **arguments
;
91 struct msgs_array msgs
= { 0, 0, NULL
};
95 if (nmh_init(argv
[0], true, true)) { return 1; }
97 arguments
= getarguments (invo_name
, argc
, argv
, 1);
100 while ((cp
= *argp
++)) {
102 switch (smatch (++cp
, switches
)) {
104 ambigsw (cp
, switches
);
107 die("-%s unknown\n", cp
);
110 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
112 print_help (buf
, switches
, 1);
115 print_version(invo_name
);
150 if (*cp
== '+' || *cp
== '@') {
152 die("only one folder at a time!");
153 folder
= pluspath (cp
);
155 app_msgarg(&msgs
, cp
);
159 if (!context_find ("path"))
160 free (path ("./", TFOLDER
));
162 app_msgarg(&msgs
, "cur");
164 folder
= getfolder (1);
165 maildir
= m_maildir (folder
);
167 if (chdir (maildir
) == NOTOK
)
168 adios (maildir
, "unable to change directory to");
170 /* read folder and create message structure */
171 if (!(mp
= folder_read (folder
, 1)))
172 die("unable to read folder %s", folder
);
174 /* check for empty folder */
176 die("no messages in %s", folder
);
178 /* parse all the message ranges/sequences and set SELECTED */
179 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
180 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
182 seq_setprev (mp
); /* set the previous-sequence */
184 smsgs
= mh_xcalloc(MAXFOLDER
+ 2, sizeof *smsgs
);
188 /* burst all the SELECTED messages */
189 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
190 if (is_selected (mp
, msgnum
)) {
191 if ((numburst
= find_delim (msgnum
, smsgs
, &mimesw
)) >= 1) {
193 printf ("%d message%s exploded from digest %d\n",
194 numburst
, PLURALS(numburst
), msgnum
);
195 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
,
200 inform("message %d not in digest format, continuing...",
202 } /* this pair of braces was missing before 1999-07-15 */
204 die("burst() botch -- you lose big");
210 context_replace (pfolder
, folder
); /* update current folder */
213 * If -inplace is given, then the first message burst becomes
214 * the current message (which will now show a table of contents).
215 * Otherwise, the first message extracted from the first digest
216 * becomes the current message.
219 if (mp
->lowsel
!= mp
->curmsg
)
220 seq_setcur (mp
, mp
->lowsel
);
222 if (hi
<= mp
->hghmsg
)
226 seq_save (mp
); /* synchronize message sequences */
227 context_save (); /* save the context file */
228 folder_free (mp
); /* free folder/message structure */
235 * Scan the message and find the beginning and
236 * end of all the messages in the digest.
238 * If requested, see if the message is MIME-formatted and contains any
239 * message/rfc822 parts; if so, burst those parts.
243 find_delim (int msgnum
, struct smsg
*smsgs
, int *mimesw
)
245 int wasdlm
= 0, msgp
;
252 msgnam
= m_name (msgnum
);
255 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
259 content
= parse_mime(msgnam
);
260 if (! content
&& *mimesw
== 2)
263 smsgs
[0].s_start
= 0;
264 smsgs
[0].s_stop
= content
->c_begin
- 1;
266 find_mime_parts(content
, smsgs
, &msgp
);
267 free_content(content
);
268 if (msgp
== 1 && *mimesw
== 2)
269 adios (msgnam
, "does not have any message/rfc822 parts");
279 if ((in
= fopen (msgnam
, "r")) == NULL
)
280 adios (msgnam
, "unable to read message");
282 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
284 * We're either at the beginning of the whole message, or
285 * we're just past the delimiter of the last message.
286 * Swallow lines until we get to something that's not a newline
288 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
289 pos
+= (long) strlen (buffer
);
294 * Reset to the beginning of the last non-blank line, and save our
295 * starting position. This is where the encapsulated message
298 fseeko (in
, pos
, SEEK_SET
);
299 smsgs
[msgp
].s_start
= pos
;
302 * Read in lines until we get to a message delimiter.
304 * Previously we checked to make sure the preceding line and
305 * next line was a newline. That actually does not comply with
306 * RFC 934, so make sure we break on a message delimiter even
307 * if the previous character was NOT a newline.
309 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
310 if ((wasdlm
= CHECKDELIM(buffer
)))
312 pos
+= (long) strlen (buffer
);
316 * Only count as a new message if we got the message delimiter.
317 * Swallow a blank line if it was right before the message delimiter.
319 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
320 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
325 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
326 msgp
++; /* fake "End of XXX Digest" */
331 pos
+= (long) strlen (buffer
);
335 return msgp
- 1; /* return the number of messages burst */
340 * Find any MIME content in the message that is a message/rfc822 and add
341 * it to the list of messages to burst.
345 find_mime_parts (CT content
, struct smsg
*smsgs
, int *msgp
)
351 * If we have a message/rfc822, then it's easy.
354 if (content
->c_type
== CT_MESSAGE
&&
355 content
->c_subtype
== MESSAGE_RFC822
) {
356 smsgs
[*msgp
].s_start
= content
->c_begin
;
357 smsgs
[*msgp
].s_stop
= content
->c_end
;
363 * Otherwise, if we do have multiparts, try all of the sub-parts.
366 if (content
->c_type
== CT_MULTIPART
) {
367 m
= (struct multipart
*) content
->c_ctparams
;
369 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
370 find_mime_parts(part
->mp_part
, smsgs
, msgp
);
376 * Burst out the messages in the digest into the folder
380 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
381 bool inplace
, bool verbosw
, char *maildir
, int mimesw
)
385 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
390 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
391 adios (msgnam
, "unable to read message");
394 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
398 * See if we have enough space in the folder
399 * structure for all the new messages.
401 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
402 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
403 die("unable to allocate folder storage");
406 j
= mp
->hghmsg
; /* old value */
407 mp
->hghmsg
+= numburst
;
408 mp
->nummsg
+= numburst
;
411 * If this is not the highest SELECTED message, then
412 * increment mp->hghsel by numburst, since the highest
413 * SELECTED is about to be slid down by that amount.
415 if (msgnum
< mp
->hghsel
)
416 mp
->hghsel
+= numburst
;
419 * If -inplace is given, renumber the messages after the
420 * source message, to make room for each of the messages
421 * contained within the digest.
423 * This is equivalent to refiling a message from the point
424 * of view of the external hooks.
427 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
428 strncpy (f1
, m_name (i
), sizeof(f1
));
429 strncpy (f2
, m_name (j
), sizeof(f2
));
430 if (does_exist (mp
, j
)) {
432 printf ("message %d becomes message %d\n", j
, i
);
434 if (rename (f2
, f1
) == NOTOK
)
435 admonish (f1
, "unable to rename %s to", f2
);
437 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
438 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
439 ext_hook("ref-hook", f1
, f2
);
441 copy_msg_flags (mp
, i
, j
);
442 clear_msg_flags (mp
, j
);
443 mp
->msgflags
|= SEQMOD
;
448 unset_selected (mp
, msgnum
);
450 /* new hghmsg is hghmsg + numburst
452 * At this point, there is an array of numburst smsgs, each element of
453 * which contains the starting and stopping offsets (seeks) of the message
454 * in the digest. The inplace flag is set if the original digest is replaced
455 * by a message containing the table of contents. smsgs[0] is that table of
456 * contents. Go through the message numbers in reverse order (high to low).
458 * Set f1 to the name of the destination message, f2 to the name of a scratch
459 * file. Extract a message from the digest to the scratch file. Move the
460 * original message to a backup file if the destination message number is the
461 * same as the number of the original message, which only happens if the
462 * inplace flag is set. Then move the scratch file to the destination message.
464 * Moving the original message to the backup file is equivalent to deleting the
465 * message from the point of view of the external hooks. And bursting each
466 * message is equivalent to adding a new message.
469 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
470 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
473 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
474 die("unable to create temporary file in %s",
477 strncpy (f2
, tempfile
, sizeof(f2
));
478 strncpy (f1
, m_name (i
), sizeof(f1
));
480 if (verbosw
&& i
!= msgnum
)
481 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
484 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
485 cpybrst (in
, out
, msgnam
, f2
,
486 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
), mimesw
);
490 strncpy (f3
, m_backup (f1
), sizeof(f3
));
491 if (rename (f1
, f3
) == NOTOK
)
492 admonish (f3
, "unable to rename %s to", f1
);
494 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
495 ext_hook("del-hook", f3
, NULL
);
497 if (rename (f2
, f1
) == NOTOK
)
498 admonish (f1
, "unable to rename %s to", f2
);
500 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
501 ext_hook("add-hook", f3
, NULL
);
503 copy_msg_flags (mp
, i
, msgnum
);
504 mp
->msgflags
|= SEQMOD
;
517 * Copy a message which is being burst out of a digest.
518 * It will remove any "dashstuffing" in the message.
522 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
, int mime
)
526 for (state
= mime
? S4
: S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
563 state
= (c
== '\n') ? S1
: S2
;
576 if (ferror (in
) && !feof (in
))
577 adios (ifile
, "error reading");
579 adios (ofile
, "error writing");