]>
diplodocus.org Git - nmh/blob - uip/burst.c
d7983697d053a365ee66bd687c56f7ed26e1954c
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/context_find.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
16 #include "h/mhparse.h"
18 #include "sbr/m_maildir.h"
19 #include "sbr/m_mktemp.h"
22 #define BURST_SWITCHES \
23 X("inplace", 0, INPLSW) \
24 X("noinplace", 0, NINPLSW) \
25 X("mime", 0, MIMESW) \
26 X("nomime", 0, NMIMESW) \
27 X("automime", 0, AUTOMIMESW) \
28 X("quiet", 0, QIETSW) \
29 X("noquiet", 0, NQIETSW) \
30 X("verbose", 0, VERBSW) \
31 X("noverbose", 0, NVERBSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(BURST
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(BURST
, switches
);
49 * For the MIME parsing routines
57 static int find_delim (int, struct smsg
*, int *);
58 static void find_mime_parts (CT
, struct smsg
*, int *);
59 static void burst(struct msgs
**, int, struct smsg
*, int, bool, bool,
61 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
64 * A macro to check to see if we have reached a message delimiter
65 * (an encapsulation boundary, EB, in RFC 934 parlance).
67 * According to RFC 934, an EB is simply a line which starts with
68 * a "-" and is NOT followed by a space. So even a single "-" on a line
69 * by itself would be an EB.
72 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
75 main (int argc
, char **argv
)
81 int hi
, msgnum
, numburst
;
82 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
83 char **argp
, **arguments
;
84 struct msgs_array msgs
= { 0, 0, NULL
};
88 if (nmh_init(argv
[0], true, true)) { return 1; }
90 arguments
= getarguments (invo_name
, argc
, argv
, 1);
93 while ((cp
= *argp
++)) {
95 switch (smatch (++cp
, switches
)) {
97 ambigsw (cp
, switches
);
100 die("-%s unknown\n", cp
);
103 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
105 print_help (buf
, switches
, 1);
108 print_version(invo_name
);
143 if (*cp
== '+' || *cp
== '@') {
145 die("only one folder at a time!");
146 folder
= pluspath (cp
);
148 app_msgarg(&msgs
, cp
);
152 if (!context_find ("path"))
153 free (path ("./", TFOLDER
));
155 app_msgarg(&msgs
, "cur");
157 folder
= getfolder (1);
158 maildir
= m_maildir (folder
);
160 if (chdir (maildir
) == NOTOK
)
161 adios (maildir
, "unable to change directory to");
163 /* read folder and create message structure */
164 if (!(mp
= folder_read (folder
, 1)))
165 die("unable to read folder %s", folder
);
167 /* check for empty folder */
169 die("no messages in %s", folder
);
171 /* parse all the message ranges/sequences and set SELECTED */
172 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
173 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
175 seq_setprev (mp
); /* set the previous-sequence */
177 smsgs
= mh_xcalloc(MAXFOLDER
+ 2, sizeof *smsgs
);
181 /* burst all the SELECTED messages */
182 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
183 if (is_selected (mp
, msgnum
)) {
184 if ((numburst
= find_delim (msgnum
, smsgs
, &mimesw
)) >= 1) {
186 printf ("%d message%s exploded from digest %d\n",
187 numburst
, PLURALS(numburst
), msgnum
);
188 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
,
193 inform("message %d not in digest format, continuing...",
195 } /* this pair of braces was missing before 1999-07-15 */
197 die("burst() botch -- you lose big");
203 context_replace (pfolder
, folder
); /* update current folder */
206 * If -inplace is given, then the first message burst becomes
207 * the current message (which will now show a table of contents).
208 * Otherwise, the first message extracted from the first digest
209 * becomes the current message.
212 if (mp
->lowsel
!= mp
->curmsg
)
213 seq_setcur (mp
, mp
->lowsel
);
215 if (hi
<= mp
->hghmsg
)
219 seq_save (mp
); /* synchronize message sequences */
220 context_save (); /* save the context file */
221 folder_free (mp
); /* free folder/message structure */
228 * Scan the message and find the beginning and
229 * end of all the messages in the digest.
231 * If requested, see if the message is MIME-formatted and contains any
232 * message/rfc822 parts; if so, burst those parts.
236 find_delim (int msgnum
, struct smsg
*smsgs
, int *mimesw
)
238 int wasdlm
= 0, msgp
;
245 msgnam
= m_name (msgnum
);
248 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
252 content
= parse_mime(msgnam
);
253 if (! content
&& *mimesw
== 2)
256 smsgs
[0].s_start
= 0;
257 smsgs
[0].s_stop
= content
->c_begin
- 1;
259 find_mime_parts(content
, smsgs
, &msgp
);
260 free_content(content
);
261 if (msgp
== 1 && *mimesw
== 2)
262 adios (msgnam
, "does not have any message/rfc822 parts");
272 if ((in
= fopen (msgnam
, "r")) == NULL
)
273 adios (msgnam
, "unable to read message");
275 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
277 * We're either at the beginning of the whole message, or
278 * we're just past the delimiter of the last message.
279 * Swallow lines until we get to something that's not a newline
281 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
282 pos
+= (long) strlen (buffer
);
287 * Reset to the beginning of the last non-blank line, and save our
288 * starting position. This is where the encapsulated message
291 fseeko (in
, pos
, SEEK_SET
);
292 smsgs
[msgp
].s_start
= pos
;
295 * Read in lines until we get to a message delimiter.
297 * Previously we checked to make sure the preceding line and
298 * next line was a newline. That actually does not comply with
299 * RFC 934, so make sure we break on a message delimiter even
300 * if the previous character was NOT a newline.
302 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
303 if ((wasdlm
= CHECKDELIM(buffer
)))
305 pos
+= (long) strlen (buffer
);
309 * Only count as a new message if we got the message delimiter.
310 * Swallow a blank line if it was right before the message delimiter.
312 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
313 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
318 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
319 msgp
++; /* fake "End of XXX Digest" */
324 pos
+= (long) strlen (buffer
);
328 return msgp
- 1; /* return the number of messages burst */
333 * Find any MIME content in the message that is a message/rfc822 and add
334 * it to the list of messages to burst.
338 find_mime_parts (CT content
, struct smsg
*smsgs
, int *msgp
)
344 * If we have a message/rfc822, then it's easy.
347 if (content
->c_type
== CT_MESSAGE
&&
348 content
->c_subtype
== MESSAGE_RFC822
) {
349 smsgs
[*msgp
].s_start
= content
->c_begin
;
350 smsgs
[*msgp
].s_stop
= content
->c_end
;
356 * Otherwise, if we do have multiparts, try all of the sub-parts.
359 if (content
->c_type
== CT_MULTIPART
) {
360 m
= (struct multipart
*) content
->c_ctparams
;
362 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
363 find_mime_parts(part
->mp_part
, smsgs
, msgp
);
369 * Burst out the messages in the digest into the folder
373 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
374 bool inplace
, bool verbosw
, char *maildir
, int mimesw
)
378 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
383 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
384 adios (msgnam
, "unable to read message");
387 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
391 * See if we have enough space in the folder
392 * structure for all the new messages.
394 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
395 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
396 die("unable to allocate folder storage");
399 j
= mp
->hghmsg
; /* old value */
400 mp
->hghmsg
+= numburst
;
401 mp
->nummsg
+= numburst
;
404 * If this is not the highest SELECTED message, then
405 * increment mp->hghsel by numburst, since the highest
406 * SELECTED is about to be slid down by that amount.
408 if (msgnum
< mp
->hghsel
)
409 mp
->hghsel
+= numburst
;
412 * If -inplace is given, renumber the messages after the
413 * source message, to make room for each of the messages
414 * contained within the digest.
416 * This is equivalent to refiling a message from the point
417 * of view of the external hooks.
420 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
421 strncpy (f1
, m_name (i
), sizeof(f1
));
422 strncpy (f2
, m_name (j
), sizeof(f2
));
423 if (does_exist (mp
, j
)) {
425 printf ("message %d becomes message %d\n", j
, i
);
427 if (rename (f2
, f1
) == NOTOK
)
428 admonish (f1
, "unable to rename %s to", f2
);
430 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
431 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
432 ext_hook("ref-hook", f1
, f2
);
434 copy_msg_flags (mp
, i
, j
);
435 clear_msg_flags (mp
, j
);
436 mp
->msgflags
|= SEQMOD
;
441 unset_selected (mp
, msgnum
);
443 /* new hghmsg is hghmsg + numburst
445 * At this point, there is an array of numburst smsgs, each element of
446 * which contains the starting and stopping offsets (seeks) of the message
447 * in the digest. The inplace flag is set if the original digest is replaced
448 * by a message containing the table of contents. smsgs[0] is that table of
449 * contents. Go through the message numbers in reverse order (high to low).
451 * Set f1 to the name of the destination message, f2 to the name of a scratch
452 * file. Extract a message from the digest to the scratch file. Move the
453 * original message to a backup file if the destination message number is the
454 * same as the number of the original message, which only happens if the
455 * inplace flag is set. Then move the scratch file to the destination message.
457 * Moving the original message to the backup file is equivalent to deleting the
458 * message from the point of view of the external hooks. And bursting each
459 * message is equivalent to adding a new message.
462 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
463 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
466 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
467 die("unable to create temporary file in %s",
470 strncpy (f2
, tempfile
, sizeof(f2
));
471 strncpy (f1
, m_name (i
), sizeof(f1
));
473 if (verbosw
&& i
!= msgnum
)
474 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
477 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
478 cpybrst (in
, out
, msgnam
, f2
,
479 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
), mimesw
);
483 strncpy (f3
, m_backup (f1
), sizeof(f3
));
484 if (rename (f1
, f3
) == NOTOK
)
485 admonish (f3
, "unable to rename %s to", f1
);
487 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
488 ext_hook("del-hook", f3
, NULL
);
490 if (rename (f2
, f1
) == NOTOK
)
491 admonish (f1
, "unable to rename %s to", f2
);
493 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
494 ext_hook("add-hook", f3
, NULL
);
496 copy_msg_flags (mp
, i
, msgnum
);
497 mp
->msgflags
|= SEQMOD
;
510 * Copy a message which is being burst out of a digest.
511 * It will remove any "dashstuffing" in the message.
515 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
, int mime
)
519 for (state
= mime
? S4
: S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
556 state
= (c
== '\n') ? S1
: S2
;
569 if (ferror (in
) && !feof (in
))
570 adios (ifile
, "error reading");
572 adios (ofile
, "error writing");