]>
diplodocus.org Git - nmh/blob - uip/burst.c
962e3171a022ce7d5935931aeea6e619522cf36f
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_backup.h"
10 #include "sbr/getfolder.h"
11 #include "sbr/ext_hook.h"
12 #include "sbr/folder_read.h"
13 #include "sbr/folder_realloc.h"
14 #include "sbr/folder_free.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_find.h"
18 #include "sbr/ambigsw.h"
20 #include "sbr/print_version.h"
21 #include "sbr/print_help.h"
22 #include "sbr/error.h"
24 #include "h/mhparse.h"
26 #include "sbr/m_maildir.h"
27 #include "sbr/m_mktemp.h"
30 #define BURST_SWITCHES \
31 X("inplace", 0, INPLSW) \
32 X("noinplace", 0, NINPLSW) \
33 X("mime", 0, MIMESW) \
34 X("nomime", 0, NMIMESW) \
35 X("automime", 0, AUTOMIMESW) \
36 X("quiet", 0, QIETSW) \
37 X("noquiet", 0, NQIETSW) \
38 X("verbose", 0, VERBSW) \
39 X("noverbose", 0, NVERBSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(BURST
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(BURST
, switches
);
57 * For the MIME parsing routines
65 static int find_delim (int, struct smsg
*, int *);
66 static void find_mime_parts (CT
, struct smsg
*, int *);
67 static void burst(struct msgs
**, int, struct smsg
*, int, bool, bool,
69 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
72 * A macro to check to see if we have reached a message delimiter
73 * (an encapsulation boundary, EB, in RFC 934 parlance).
75 * According to RFC 934, an EB is simply a line which starts with
76 * a "-" and is NOT followed by a space. So even a single "-" on a line
77 * by itself would be an EB.
80 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
83 main (int argc
, char **argv
)
89 int hi
, msgnum
, numburst
;
90 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
91 char **argp
, **arguments
;
92 struct msgs_array msgs
= { 0, 0, NULL
};
96 if (nmh_init(argv
[0], true, true)) { return 1; }
98 arguments
= getarguments (invo_name
, argc
, argv
, 1);
101 while ((cp
= *argp
++)) {
103 switch (smatch (++cp
, switches
)) {
105 ambigsw (cp
, switches
);
108 die("-%s unknown\n", cp
);
111 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
113 print_help (buf
, switches
, 1);
116 print_version(invo_name
);
151 if (*cp
== '+' || *cp
== '@') {
153 die("only one folder at a time!");
154 folder
= pluspath (cp
);
156 app_msgarg(&msgs
, cp
);
160 if (!context_find ("path"))
161 free (path ("./", TFOLDER
));
163 app_msgarg(&msgs
, "cur");
165 folder
= getfolder (1);
166 maildir
= m_maildir (folder
);
168 if (chdir (maildir
) == NOTOK
)
169 adios (maildir
, "unable to change directory to");
171 /* read folder and create message structure */
172 if (!(mp
= folder_read (folder
, 1)))
173 die("unable to read folder %s", folder
);
175 /* check for empty folder */
177 die("no messages in %s", folder
);
179 /* parse all the message ranges/sequences and set SELECTED */
180 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
181 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
183 seq_setprev (mp
); /* set the previous-sequence */
185 smsgs
= mh_xcalloc(MAXFOLDER
+ 2, sizeof *smsgs
);
189 /* burst all the SELECTED messages */
190 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
191 if (is_selected (mp
, msgnum
)) {
192 if ((numburst
= find_delim (msgnum
, smsgs
, &mimesw
)) >= 1) {
194 printf ("%d message%s exploded from digest %d\n",
195 numburst
, PLURALS(numburst
), msgnum
);
196 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
,
201 inform("message %d not in digest format, continuing...",
203 } /* this pair of braces was missing before 1999-07-15 */
205 die("burst() botch -- you lose big");
211 context_replace (pfolder
, folder
); /* update current folder */
214 * If -inplace is given, then the first message burst becomes
215 * the current message (which will now show a table of contents).
216 * Otherwise, the first message extracted from the first digest
217 * becomes the current message.
220 if (mp
->lowsel
!= mp
->curmsg
)
221 seq_setcur (mp
, mp
->lowsel
);
223 if (hi
<= mp
->hghmsg
)
227 seq_save (mp
); /* synchronize message sequences */
228 context_save (); /* save the context file */
229 folder_free (mp
); /* free folder/message structure */
236 * Scan the message and find the beginning and
237 * end of all the messages in the digest.
239 * If requested, see if the message is MIME-formatted and contains any
240 * message/rfc822 parts; if so, burst those parts.
244 find_delim (int msgnum
, struct smsg
*smsgs
, int *mimesw
)
246 int wasdlm
= 0, msgp
;
253 msgnam
= m_name (msgnum
);
256 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
260 content
= parse_mime(msgnam
);
261 if (! content
&& *mimesw
== 2)
264 smsgs
[0].s_start
= 0;
265 smsgs
[0].s_stop
= content
->c_begin
- 1;
267 find_mime_parts(content
, smsgs
, &msgp
);
268 free_content(content
);
269 if (msgp
== 1 && *mimesw
== 2)
270 adios (msgnam
, "does not have any message/rfc822 parts");
280 if ((in
= fopen (msgnam
, "r")) == NULL
)
281 adios (msgnam
, "unable to read message");
283 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
285 * We're either at the beginning of the whole message, or
286 * we're just past the delimiter of the last message.
287 * Swallow lines until we get to something that's not a newline
289 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
290 pos
+= (long) strlen (buffer
);
295 * Reset to the beginning of the last non-blank line, and save our
296 * starting position. This is where the encapsulated message
299 fseeko (in
, pos
, SEEK_SET
);
300 smsgs
[msgp
].s_start
= pos
;
303 * Read in lines until we get to a message delimiter.
305 * Previously we checked to make sure the preceding line and
306 * next line was a newline. That actually does not comply with
307 * RFC 934, so make sure we break on a message delimiter even
308 * if the previous character was NOT a newline.
310 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
311 if ((wasdlm
= CHECKDELIM(buffer
)))
313 pos
+= (long) strlen (buffer
);
317 * Only count as a new message if we got the message delimiter.
318 * Swallow a blank line if it was right before the message delimiter.
320 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
321 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
326 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
327 msgp
++; /* fake "End of XXX Digest" */
332 pos
+= (long) strlen (buffer
);
336 return msgp
- 1; /* return the number of messages burst */
341 * Find any MIME content in the message that is a message/rfc822 and add
342 * it to the list of messages to burst.
346 find_mime_parts (CT content
, struct smsg
*smsgs
, int *msgp
)
352 * If we have a message/rfc822, then it's easy.
355 if (content
->c_type
== CT_MESSAGE
&&
356 content
->c_subtype
== MESSAGE_RFC822
) {
357 smsgs
[*msgp
].s_start
= content
->c_begin
;
358 smsgs
[*msgp
].s_stop
= content
->c_end
;
364 * Otherwise, if we do have multiparts, try all of the sub-parts.
367 if (content
->c_type
== CT_MULTIPART
) {
368 m
= (struct multipart
*) content
->c_ctparams
;
370 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
371 find_mime_parts(part
->mp_part
, smsgs
, msgp
);
377 * Burst out the messages in the digest into the folder
381 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
382 bool inplace
, bool verbosw
, char *maildir
, int mimesw
)
386 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
391 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
392 adios (msgnam
, "unable to read message");
395 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
399 * See if we have enough space in the folder
400 * structure for all the new messages.
402 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
403 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
404 die("unable to allocate folder storage");
407 j
= mp
->hghmsg
; /* old value */
408 mp
->hghmsg
+= numburst
;
409 mp
->nummsg
+= numburst
;
412 * If this is not the highest SELECTED message, then
413 * increment mp->hghsel by numburst, since the highest
414 * SELECTED is about to be slid down by that amount.
416 if (msgnum
< mp
->hghsel
)
417 mp
->hghsel
+= numburst
;
420 * If -inplace is given, renumber the messages after the
421 * source message, to make room for each of the messages
422 * contained within the digest.
424 * This is equivalent to refiling a message from the point
425 * of view of the external hooks.
428 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
429 strncpy (f1
, m_name (i
), sizeof(f1
));
430 strncpy (f2
, m_name (j
), sizeof(f2
));
431 if (does_exist (mp
, j
)) {
433 printf ("message %d becomes message %d\n", j
, i
);
435 if (rename (f2
, f1
) == NOTOK
)
436 admonish (f1
, "unable to rename %s to", f2
);
438 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
439 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
440 ext_hook("ref-hook", f1
, f2
);
442 copy_msg_flags (mp
, i
, j
);
443 clear_msg_flags (mp
, j
);
444 mp
->msgflags
|= SEQMOD
;
449 unset_selected (mp
, msgnum
);
451 /* new hghmsg is hghmsg + numburst
453 * At this point, there is an array of numburst smsgs, each element of
454 * which contains the starting and stopping offsets (seeks) of the message
455 * in the digest. The inplace flag is set if the original digest is replaced
456 * by a message containing the table of contents. smsgs[0] is that table of
457 * contents. Go through the message numbers in reverse order (high to low).
459 * Set f1 to the name of the destination message, f2 to the name of a scratch
460 * file. Extract a message from the digest to the scratch file. Move the
461 * original message to a backup file if the destination message number is the
462 * same as the number of the original message, which only happens if the
463 * inplace flag is set. Then move the scratch file to the destination message.
465 * Moving the original message to the backup file is equivalent to deleting the
466 * message from the point of view of the external hooks. And bursting each
467 * message is equivalent to adding a new message.
470 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
471 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
474 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
475 die("unable to create temporary file in %s",
478 strncpy (f2
, tempfile
, sizeof(f2
));
479 strncpy (f1
, m_name (i
), sizeof(f1
));
481 if (verbosw
&& i
!= msgnum
)
482 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
485 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
486 cpybrst (in
, out
, msgnam
, f2
,
487 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
), mimesw
);
491 strncpy (f3
, m_backup (f1
), sizeof(f3
));
492 if (rename (f1
, f3
) == NOTOK
)
493 admonish (f3
, "unable to rename %s to", f1
);
495 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
496 ext_hook("del-hook", f3
, NULL
);
498 if (rename (f2
, f1
) == NOTOK
)
499 admonish (f1
, "unable to rename %s to", f2
);
501 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
502 ext_hook("add-hook", f3
, NULL
);
504 copy_msg_flags (mp
, i
, msgnum
);
505 mp
->msgflags
|= SEQMOD
;
518 * Copy a message which is being burst out of a digest.
519 * It will remove any "dashstuffing" in the message.
523 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
, int mime
)
527 for (state
= mime
? S4
: S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
564 state
= (c
== '\n') ? S1
: S2
;
577 if (ferror (in
) && !feof (in
))
578 adios (ifile
, "error reading");
580 adios (ofile
, "error writing");