]>
diplodocus.org Git - nmh/blob - uip/burst.c
8c704a2378594dbe8882eefe2b9ec57d1fbe5e95
2 * burst.c -- explode digests into individual messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/mhparse.h>
13 #define BURST_SWITCHES \
14 X("inplace", 0, INPLSW) \
15 X("noinplace", 0, NINPLSW) \
16 X("mime", 0, MIMESW) \
17 X("nomime", 0, NMIMESW) \
18 X("automime", 0, AUTOMIMESW) \
19 X("quiet", 0, QIETSW) \
20 X("noquiet", 0, NQIETSW) \
21 X("verbose", 0, VERBSW) \
22 X("noverbose", 0, NVERBSW) \
23 X("version", 0, VERSIONSW) \
24 X("help", 0, HELPSW) \
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(BURST
);
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(BURST
, switches
);
40 * For the MIME parsing routines
48 static int find_delim (int, struct smsg
*, int *);
49 static void find_mime_parts (CT
, struct smsg
*, int *);
50 static void burst (struct msgs
**, int, struct smsg
*, int, int, int,
52 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
55 * A macro to check to see if we have reached a message delimiter
56 * (an encapsulation boundary, EB, in RFC 934 parlance).
58 * According to RFC 934, an EB is simply a line which starts with
59 * a "-" and is NOT followed by a space. So even a single "-" on a line
60 * by itself would be an EB.
63 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
66 main (int argc
, char **argv
)
68 int inplace
= 0, quietsw
= 0, verbosw
= 0, mimesw
= 1;
69 int hi
, msgnum
, numburst
;
70 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
71 char **argp
, **arguments
;
72 struct msgs_array msgs
= { 0, 0, NULL
};
76 if (nmh_init(argv
[0], 1)) { return 1; }
78 arguments
= getarguments (invo_name
, argc
, argv
, 1);
81 while ((cp
= *argp
++)) {
83 switch (smatch (++cp
, switches
)) {
85 ambigsw (cp
, switches
);
88 adios (NULL
, "-%s unknown\n", cp
);
91 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
93 print_help (buf
, switches
, 1);
96 print_version(invo_name
);
131 if (*cp
== '+' || *cp
== '@') {
133 adios (NULL
, "only one folder at a time!");
135 folder
= pluspath (cp
);
137 app_msgarg(&msgs
, cp
);
141 if (!context_find ("path"))
142 free (path ("./", TFOLDER
));
144 app_msgarg(&msgs
, "cur");
146 folder
= getfolder (1);
147 maildir
= m_maildir (folder
);
149 if (chdir (maildir
) == NOTOK
)
150 adios (maildir
, "unable to change directory to");
152 /* read folder and create message structure */
153 if (!(mp
= folder_read (folder
, 1)))
154 adios (NULL
, "unable to read folder %s", folder
);
156 /* check for empty folder */
158 adios (NULL
, "no messages in %s", folder
);
160 /* parse all the message ranges/sequences and set SELECTED */
161 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
162 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
164 seq_setprev (mp
); /* set the previous-sequence */
166 smsgs
= mh_xcalloc(MAXFOLDER
+ 2, sizeof *smsgs
);
170 /* burst all the SELECTED messages */
171 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
172 if (is_selected (mp
, msgnum
)) {
173 if ((numburst
= find_delim (msgnum
, smsgs
, &mimesw
)) >= 1) {
175 printf ("%d message%s exploded from digest %d\n",
176 numburst
, numburst
> 1 ? "s" : "", msgnum
);
177 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
,
182 inform("message %d not in digest format, continuing...",
184 } /* this pair of braces was missing before 1999-07-15 */
186 adios (NULL
, "burst() botch -- you lose big");
192 context_replace (pfolder
, folder
); /* update current folder */
195 * If -inplace is given, then the first message burst becomes
196 * the current message (which will now show a table of contents).
197 * Otherwise, the first message extracted from the first digest
198 * becomes the current message.
201 if (mp
->lowsel
!= mp
->curmsg
)
202 seq_setcur (mp
, mp
->lowsel
);
204 if (hi
<= mp
->hghmsg
)
208 seq_save (mp
); /* synchronize message sequences */
209 context_save (); /* save the context file */
210 folder_free (mp
); /* free folder/message structure */
217 * Scan the message and find the beginning and
218 * end of all the messages in the digest.
220 * If requested, see if the message is MIME-formatted and contains any
221 * message/rfc822 parts; if so, burst those parts.
225 find_delim (int msgnum
, struct smsg
*smsgs
, int *mimesw
)
227 int wasdlm
= 0, msgp
;
234 msgnam
= m_name (msgnum
);
237 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
241 content
= parse_mime(msgnam
);
242 if (! content
&& *mimesw
== 2)
245 smsgs
[0].s_start
= 0;
246 smsgs
[0].s_stop
= content
->c_begin
- 1;
248 find_mime_parts(content
, smsgs
, &msgp
);
249 free_content(content
);
250 if (msgp
== 1 && *mimesw
== 2) {
251 adios (msgnam
, "does not have any message/rfc822 parts");
252 } else if (msgp
> 1) {
261 if ((in
= fopen (msgnam
, "r")) == NULL
)
262 adios (msgnam
, "unable to read message");
264 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
266 * We're either at the beginning of the whole message, or
267 * we're just past the delimiter of the last message.
268 * Swallow lines until we get to something that's not a newline
270 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
271 pos
+= (long) strlen (buffer
);
276 * Reset to the beginning of the last non-blank line, and save our
277 * starting position. This is where the encapsulated message
280 fseeko (in
, pos
, SEEK_SET
);
281 smsgs
[msgp
].s_start
= pos
;
284 * Read in lines until we get to a message delimiter.
286 * Previously we checked to make sure the preceding line and
287 * next line was a newline. That actually does not comply with
288 * RFC 934, so make sure we break on a message delimiter even
289 * if the previous character was NOT a newline.
291 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
292 if ((wasdlm
= CHECKDELIM(buffer
)))
295 pos
+= (long) strlen (buffer
);
299 * Only count as a new message if we got the message delimiter.
300 * Swallow a blank line if it was right before the message delimiter.
302 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
303 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
308 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
309 msgp
++; /* fake "End of XXX Digest" */
314 pos
+= (long) strlen (buffer
);
318 return (msgp
- 1); /* return the number of messages burst */
323 * Find any MIME content in the message that is a message/rfc822 and add
324 * it to the list of messages to burst.
328 find_mime_parts (CT content
, struct smsg
*smsgs
, int *msgp
)
334 * If we have a message/rfc822, then it's easy.
337 if (content
->c_type
== CT_MESSAGE
&&
338 content
->c_subtype
== MESSAGE_RFC822
) {
339 smsgs
[*msgp
].s_start
= content
->c_begin
;
340 smsgs
[*msgp
].s_stop
= content
->c_end
;
346 * Otherwise, if we do have multiparts, try all of the sub-parts.
349 if (content
->c_type
== CT_MULTIPART
) {
350 m
= (struct multipart
*) content
->c_ctparams
;
352 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
353 find_mime_parts(part
->mp_part
, smsgs
, msgp
);
361 * Burst out the messages in the digest into the folder
365 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
366 int inplace
, int verbosw
, char *maildir
, int mimesw
)
370 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
375 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
376 adios (msgnam
, "unable to read message");
379 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
383 * See if we have enough space in the folder
384 * structure for all the new messages.
386 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
387 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
388 adios (NULL
, "unable to allocate folder storage");
391 j
= mp
->hghmsg
; /* old value */
392 mp
->hghmsg
+= numburst
;
393 mp
->nummsg
+= numburst
;
396 * If this is not the highest SELECTED message, then
397 * increment mp->hghsel by numburst, since the highest
398 * SELECTED is about to be slid down by that amount.
400 if (msgnum
< mp
->hghsel
)
401 mp
->hghsel
+= numburst
;
404 * If -inplace is given, renumber the messages after the
405 * source message, to make room for each of the messages
406 * contained within the digest.
408 * This is equivalent to refiling a message from the point
409 * of view of the external hooks.
412 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
413 strncpy (f1
, m_name (i
), sizeof(f1
));
414 strncpy (f2
, m_name (j
), sizeof(f2
));
415 if (does_exist (mp
, j
)) {
417 printf ("message %d becomes message %d\n", j
, i
);
419 if (rename (f2
, f1
) == NOTOK
)
420 admonish (f1
, "unable to rename %s to", f2
);
422 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
423 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
424 ext_hook("ref-hook", f1
, f2
);
426 copy_msg_flags (mp
, i
, j
);
427 clear_msg_flags (mp
, j
);
428 mp
->msgflags
|= SEQMOD
;
433 unset_selected (mp
, msgnum
);
435 /* new hghmsg is hghmsg + numburst
437 * At this point, there is an array of numburst smsgs, each element of
438 * which contains the starting and stopping offsets (seeks) of the message
439 * in the digest. The inplace flag is set if the original digest is replaced
440 * by a message containing the table of contents. smsgs[0] is that table of
441 * contents. Go through the message numbers in reverse order (high to low).
443 * Set f1 to the name of the destination message, f2 to the name of a scratch
444 * file. Extract a message from the digest to the scratch file. Move the
445 * original message to a backup file if the destination message number is the
446 * same as the number of the original message, which only happens if the
447 * inplace flag is set. Then move the scratch file to the destination message.
449 * Moving the original message to the backup file is equivalent to deleting the
450 * message from the point of view of the external hooks. And bursting each
451 * message is equivalent to adding a new message.
454 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
455 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
458 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
459 adios(NULL
, "unable to create temporary file in %s",
462 strncpy (f2
, tempfile
, sizeof(f2
));
463 strncpy (f1
, m_name (i
), sizeof(f1
));
465 if (verbosw
&& i
!= msgnum
)
466 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
469 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
470 cpybrst (in
, out
, msgnam
, f2
,
471 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
), mimesw
);
475 strncpy (f3
, m_backup (f1
), sizeof(f3
));
476 if (rename (f1
, f3
) == NOTOK
)
477 admonish (f3
, "unable to rename %s to", f1
);
479 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
480 ext_hook("del-hook", f3
, NULL
);
482 if (rename (f2
, f1
) == NOTOK
)
483 admonish (f1
, "unable to rename %s to", f2
);
485 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
486 ext_hook("add-hook", f3
, NULL
);
488 copy_msg_flags (mp
, i
, msgnum
);
489 mp
->msgflags
|= SEQMOD
;
502 * Copy a message which is being burst out of a digest.
503 * It will remove any "dashstuffing" in the message.
507 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
, int mime
)
511 for (state
= mime
? S4
: S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
548 state
= (c
== '\n') ? S1
: S2
;
561 if (ferror (in
) && !feof (in
))
562 adios (ifile
, "error reading");
564 adios (ofile
, "error writing");