]>
diplodocus.org Git - nmh/blob - uip/burst.c
cfb3aba39247037d3f8b7923bc172019f7705f6b
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_read.h"
10 #include "sbr/folder_realloc.h"
11 #include "sbr/folder_free.h"
12 #include "sbr/context_save.h"
13 #include "sbr/context_replace.h"
14 #include "sbr/context_find.h"
15 #include "sbr/ambigsw.h"
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/error.h"
21 #include "h/mhparse.h"
23 #include "sbr/m_maildir.h"
24 #include "sbr/m_mktemp.h"
27 #define BURST_SWITCHES \
28 X("inplace", 0, INPLSW) \
29 X("noinplace", 0, NINPLSW) \
30 X("mime", 0, MIMESW) \
31 X("nomime", 0, NMIMESW) \
32 X("automime", 0, AUTOMIMESW) \
33 X("quiet", 0, QIETSW) \
34 X("noquiet", 0, NQIETSW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(BURST
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(BURST
, switches
);
54 * For the MIME parsing routines
62 static int find_delim (int, struct smsg
*, int *);
63 static void find_mime_parts (CT
, struct smsg
*, int *);
64 static void burst(struct msgs
**, int, struct smsg
*, int, bool, bool,
66 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
69 * A macro to check to see if we have reached a message delimiter
70 * (an encapsulation boundary, EB, in RFC 934 parlance).
72 * According to RFC 934, an EB is simply a line which starts with
73 * a "-" and is NOT followed by a space. So even a single "-" on a line
74 * by itself would be an EB.
77 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
80 main (int argc
, char **argv
)
86 int hi
, msgnum
, numburst
;
87 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
88 char **argp
, **arguments
;
89 struct msgs_array msgs
= { 0, 0, NULL
};
93 if (nmh_init(argv
[0], true, true)) { return 1; }
95 arguments
= getarguments (invo_name
, argc
, argv
, 1);
98 while ((cp
= *argp
++)) {
100 switch (smatch (++cp
, switches
)) {
102 ambigsw (cp
, switches
);
105 die("-%s unknown\n", cp
);
108 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
110 print_help (buf
, switches
, 1);
113 print_version(invo_name
);
148 if (*cp
== '+' || *cp
== '@') {
150 die("only one folder at a time!");
151 folder
= pluspath (cp
);
153 app_msgarg(&msgs
, cp
);
157 if (!context_find ("path"))
158 free (path ("./", TFOLDER
));
160 app_msgarg(&msgs
, "cur");
162 folder
= getfolder (1);
163 maildir
= m_maildir (folder
);
165 if (chdir (maildir
) == NOTOK
)
166 adios (maildir
, "unable to change directory to");
168 /* read folder and create message structure */
169 if (!(mp
= folder_read (folder
, 1)))
170 die("unable to read folder %s", folder
);
172 /* check for empty folder */
174 die("no messages in %s", folder
);
176 /* parse all the message ranges/sequences and set SELECTED */
177 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
178 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
180 seq_setprev (mp
); /* set the previous-sequence */
182 smsgs
= mh_xcalloc(MAXFOLDER
+ 2, sizeof *smsgs
);
186 /* burst all the SELECTED messages */
187 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
188 if (is_selected (mp
, msgnum
)) {
189 if ((numburst
= find_delim (msgnum
, smsgs
, &mimesw
)) >= 1) {
191 printf ("%d message%s exploded from digest %d\n",
192 numburst
, PLURALS(numburst
), msgnum
);
193 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
,
198 inform("message %d not in digest format, continuing...",
200 } /* this pair of braces was missing before 1999-07-15 */
202 die("burst() botch -- you lose big");
208 context_replace (pfolder
, folder
); /* update current folder */
211 * If -inplace is given, then the first message burst becomes
212 * the current message (which will now show a table of contents).
213 * Otherwise, the first message extracted from the first digest
214 * becomes the current message.
217 if (mp
->lowsel
!= mp
->curmsg
)
218 seq_setcur (mp
, mp
->lowsel
);
220 if (hi
<= mp
->hghmsg
)
224 seq_save (mp
); /* synchronize message sequences */
225 context_save (); /* save the context file */
226 folder_free (mp
); /* free folder/message structure */
233 * Scan the message and find the beginning and
234 * end of all the messages in the digest.
236 * If requested, see if the message is MIME-formatted and contains any
237 * message/rfc822 parts; if so, burst those parts.
241 find_delim (int msgnum
, struct smsg
*smsgs
, int *mimesw
)
243 int wasdlm
= 0, msgp
;
250 msgnam
= m_name (msgnum
);
253 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
257 content
= parse_mime(msgnam
);
258 if (! content
&& *mimesw
== 2)
261 smsgs
[0].s_start
= 0;
262 smsgs
[0].s_stop
= content
->c_begin
- 1;
264 find_mime_parts(content
, smsgs
, &msgp
);
265 free_content(content
);
266 if (msgp
== 1 && *mimesw
== 2)
267 adios (msgnam
, "does not have any message/rfc822 parts");
277 if ((in
= fopen (msgnam
, "r")) == NULL
)
278 adios (msgnam
, "unable to read message");
280 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
282 * We're either at the beginning of the whole message, or
283 * we're just past the delimiter of the last message.
284 * Swallow lines until we get to something that's not a newline
286 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
287 pos
+= (long) strlen (buffer
);
292 * Reset to the beginning of the last non-blank line, and save our
293 * starting position. This is where the encapsulated message
296 fseeko (in
, pos
, SEEK_SET
);
297 smsgs
[msgp
].s_start
= pos
;
300 * Read in lines until we get to a message delimiter.
302 * Previously we checked to make sure the preceding line and
303 * next line was a newline. That actually does not comply with
304 * RFC 934, so make sure we break on a message delimiter even
305 * if the previous character was NOT a newline.
307 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
308 if ((wasdlm
= CHECKDELIM(buffer
)))
310 pos
+= (long) strlen (buffer
);
314 * Only count as a new message if we got the message delimiter.
315 * Swallow a blank line if it was right before the message delimiter.
317 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
318 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
323 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
324 msgp
++; /* fake "End of XXX Digest" */
329 pos
+= (long) strlen (buffer
);
333 return msgp
- 1; /* return the number of messages burst */
338 * Find any MIME content in the message that is a message/rfc822 and add
339 * it to the list of messages to burst.
343 find_mime_parts (CT content
, struct smsg
*smsgs
, int *msgp
)
349 * If we have a message/rfc822, then it's easy.
352 if (content
->c_type
== CT_MESSAGE
&&
353 content
->c_subtype
== MESSAGE_RFC822
) {
354 smsgs
[*msgp
].s_start
= content
->c_begin
;
355 smsgs
[*msgp
].s_stop
= content
->c_end
;
361 * Otherwise, if we do have multiparts, try all of the sub-parts.
364 if (content
->c_type
== CT_MULTIPART
) {
365 m
= (struct multipart
*) content
->c_ctparams
;
367 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
368 find_mime_parts(part
->mp_part
, smsgs
, msgp
);
374 * Burst out the messages in the digest into the folder
378 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
379 bool inplace
, bool verbosw
, char *maildir
, int mimesw
)
383 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
388 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
389 adios (msgnam
, "unable to read message");
392 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
396 * See if we have enough space in the folder
397 * structure for all the new messages.
399 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
400 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
401 die("unable to allocate folder storage");
404 j
= mp
->hghmsg
; /* old value */
405 mp
->hghmsg
+= numburst
;
406 mp
->nummsg
+= numburst
;
409 * If this is not the highest SELECTED message, then
410 * increment mp->hghsel by numburst, since the highest
411 * SELECTED is about to be slid down by that amount.
413 if (msgnum
< mp
->hghsel
)
414 mp
->hghsel
+= numburst
;
417 * If -inplace is given, renumber the messages after the
418 * source message, to make room for each of the messages
419 * contained within the digest.
421 * This is equivalent to refiling a message from the point
422 * of view of the external hooks.
425 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
426 strncpy (f1
, m_name (i
), sizeof(f1
));
427 strncpy (f2
, m_name (j
), sizeof(f2
));
428 if (does_exist (mp
, j
)) {
430 printf ("message %d becomes message %d\n", j
, i
);
432 if (rename (f2
, f1
) == NOTOK
)
433 admonish (f1
, "unable to rename %s to", f2
);
435 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
436 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
437 ext_hook("ref-hook", f1
, f2
);
439 copy_msg_flags (mp
, i
, j
);
440 clear_msg_flags (mp
, j
);
441 mp
->msgflags
|= SEQMOD
;
446 unset_selected (mp
, msgnum
);
448 /* new hghmsg is hghmsg + numburst
450 * At this point, there is an array of numburst smsgs, each element of
451 * which contains the starting and stopping offsets (seeks) of the message
452 * in the digest. The inplace flag is set if the original digest is replaced
453 * by a message containing the table of contents. smsgs[0] is that table of
454 * contents. Go through the message numbers in reverse order (high to low).
456 * Set f1 to the name of the destination message, f2 to the name of a scratch
457 * file. Extract a message from the digest to the scratch file. Move the
458 * original message to a backup file if the destination message number is the
459 * same as the number of the original message, which only happens if the
460 * inplace flag is set. Then move the scratch file to the destination message.
462 * Moving the original message to the backup file is equivalent to deleting the
463 * message from the point of view of the external hooks. And bursting each
464 * message is equivalent to adding a new message.
467 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
468 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
471 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
472 die("unable to create temporary file in %s",
475 strncpy (f2
, tempfile
, sizeof(f2
));
476 strncpy (f1
, m_name (i
), sizeof(f1
));
478 if (verbosw
&& i
!= msgnum
)
479 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
482 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
483 cpybrst (in
, out
, msgnam
, f2
,
484 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
), mimesw
);
488 strncpy (f3
, m_backup (f1
), sizeof(f3
));
489 if (rename (f1
, f3
) == NOTOK
)
490 admonish (f3
, "unable to rename %s to", f1
);
492 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
493 ext_hook("del-hook", f3
, NULL
);
495 if (rename (f2
, f1
) == NOTOK
)
496 admonish (f1
, "unable to rename %s to", f2
);
498 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
499 ext_hook("add-hook", f3
, NULL
);
501 copy_msg_flags (mp
, i
, msgnum
);
502 mp
->msgflags
|= SEQMOD
;
515 * Copy a message which is being burst out of a digest.
516 * It will remove any "dashstuffing" in the message.
520 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
, int mime
)
524 for (state
= mime
? S4
: S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
561 state
= (c
== '\n') ? S1
: S2
;
574 if (ferror (in
) && !feof (in
))
575 adios (ifile
, "error reading");
577 adios (ofile
, "error writing");