]>
diplodocus.org Git - nmh/blob - uip/burst.c
3 * burst.c -- explode digests into individual messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/mhparse.h>
14 #define BURST_SWITCHES \
15 X("inplace", 0, INPLSW) \
16 X("noinplace", 0, NINPLSW) \
17 X("mime", 0, MIMESW) \
18 X("nomime", 0, NMIMESW) \
19 X("automime", 0, AUTOMIMESW) \
20 X("quiet", 0, QIETSW) \
21 X("noquiet", 0, NQIETSW) \
22 X("verbose", 0, VERBSW) \
23 X("noverbose", 0, NVERBSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(BURST
);
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(BURST
, switches
);
41 * For the MIME parsing routines
50 static int find_delim (int, struct smsg
*, int *);
51 static void find_mime_parts (CT
, struct smsg
*, int *);
52 static void burst (struct msgs
**, int, struct smsg
*, int, int, int,
54 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
57 * A macro to check to see if we have reached a message delimiter
58 * (an encapsulation boundary, EB, in RFC 934 parlance).
60 * According to RFC 934, an EB is simply a line which starts with
61 * a "-" and is NOT followed by a space. So even a single "-" on a line
62 * by itself would be an EB.
65 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
68 main (int argc
, char **argv
)
70 int inplace
= 0, quietsw
= 0, verbosw
= 0, mimesw
= 1;
71 int hi
, msgnum
, numburst
;
72 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
73 char **argp
, **arguments
;
74 struct msgs_array msgs
= { 0, 0, NULL
};
78 setlocale(LC_ALL
, "");
79 invo_name
= r1bindex (argv
[0], '/');
81 /* read user profile/context */
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
87 while ((cp
= *argp
++)) {
89 switch (smatch (++cp
, switches
)) {
91 ambigsw (cp
, switches
);
94 adios (NULL
, "-%s unknown\n", cp
);
97 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
99 print_help (buf
, switches
, 1);
102 print_version(invo_name
);
137 if (*cp
== '+' || *cp
== '@') {
139 adios (NULL
, "only one folder at a time!");
141 folder
= pluspath (cp
);
143 app_msgarg(&msgs
, cp
);
147 if (!context_find ("path"))
148 free (path ("./", TFOLDER
));
150 app_msgarg(&msgs
, "cur");
152 folder
= getfolder (1);
153 maildir
= m_maildir (folder
);
155 if (chdir (maildir
) == NOTOK
)
156 adios (maildir
, "unable to change directory to");
158 /* read folder and create message structure */
159 if (!(mp
= folder_read (folder
, 1)))
160 adios (NULL
, "unable to read folder %s", folder
);
162 /* check for empty folder */
164 adios (NULL
, "no messages in %s", folder
);
166 /* parse all the message ranges/sequences and set SELECTED */
167 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
168 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
170 seq_setprev (mp
); /* set the previous-sequence */
172 smsgs
= (struct smsg
*)
173 calloc ((size_t) (MAXFOLDER
+ 2), sizeof(*smsgs
));
175 adios (NULL
, "unable to allocate burst storage");
179 /* burst all the SELECTED messages */
180 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
181 if (is_selected (mp
, msgnum
)) {
182 if ((numburst
= find_delim (msgnum
, smsgs
, &mimesw
)) >= 1) {
184 printf ("%d message%s exploded from digest %d\n",
185 numburst
, numburst
> 1 ? "s" : "", msgnum
);
186 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
,
191 admonish (NULL
, "message %d not in digest format",
193 } /* this pair of braces was missing before 1999-07-15 */
195 adios (NULL
, "burst() botch -- you lose big");
200 free ((char *) smsgs
);
201 context_replace (pfolder
, folder
); /* update current folder */
204 * If -inplace is given, then the first message burst becomes
205 * the current message (which will now show a table of contents).
206 * Otherwise, the first message extracted from the first digest
207 * becomes the current message.
210 if (mp
->lowsel
!= mp
->curmsg
)
211 seq_setcur (mp
, mp
->lowsel
);
213 if (hi
<= mp
->hghmsg
)
217 seq_save (mp
); /* synchronize message sequences */
218 context_save (); /* save the context file */
219 folder_free (mp
); /* free folder/message structure */
226 * Scan the message and find the beginning and
227 * end of all the messages in the digest.
229 * If requested, see if the message is MIME-formatted and contains any
230 * message/rfc822 parts; if so, burst those parts.
234 find_delim (int msgnum
, struct smsg
*smsgs
, int *mimesw
)
236 int wasdlm
= 0, msgp
;
243 msgnam
= m_name (msgnum
);
246 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
250 content
= parse_mime(msgnam
);
251 if (! content
&& *mimesw
== 2)
254 smsgs
[0].s_start
= 0;
255 smsgs
[0].s_stop
= content
->c_begin
- 1;
257 find_mime_parts(content
, smsgs
, &msgp
);
258 free_content(content
);
259 if (msgp
== 1 && *mimesw
== 2) {
260 adios (msgnam
, "does not have any message/rfc822 parts");
261 } else if (msgp
> 1) {
270 if ((in
= fopen (msgnam
, "r")) == NULL
)
271 adios (msgnam
, "unable to read message");
273 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
275 * We're either at the beginning of the whole message, or
276 * we're just past the delimiter of the last message.
277 * Swallow lines until we get to something that's not a newline
279 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
280 pos
+= (long) strlen (buffer
);
285 * Reset to the beginning of the last non-blank line, and save our
286 * starting position. This is where the encapsulated message
289 fseeko (in
, pos
, SEEK_SET
);
290 smsgs
[msgp
].s_start
= pos
;
293 * Read in lines until we get to a message delimiter.
295 * Previously we checked to make sure the preceeding line and
296 * next line was a newline. That actually does not comply with
297 * RFC 934, so make sure we break on a message delimiter even
298 * if the previous character was NOT a newline.
300 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
301 if ((wasdlm
= CHECKDELIM(buffer
)))
304 pos
+= (long) strlen (buffer
);
308 * Only count as a new message if we got the message delimiter.
309 * Swallow a blank line if it was right before the message delimiter.
311 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
312 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
317 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
318 msgp
++; /* fake "End of XXX Digest" */
323 pos
+= (long) strlen (buffer
);
327 return (msgp
- 1); /* return the number of messages burst */
332 * Find any MIME content in the message that is a message/rfc822 and add
333 * it to the list of messages to burst.
337 find_mime_parts (CT content
, struct smsg
*smsgs
, int *msgp
)
343 * If we have a message/rfc822, then it's easy.
346 if (content
->c_type
== CT_MESSAGE
&&
347 content
->c_subtype
== MESSAGE_RFC822
) {
348 smsgs
[*msgp
].s_start
= content
->c_begin
;
349 smsgs
[*msgp
].s_stop
= content
->c_end
;
355 * Otherwise, if we do have multiparts, try all of the sub-parts.
358 if (content
->c_type
== CT_MULTIPART
) {
359 m
= (struct multipart
*) content
->c_ctparams
;
361 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
362 find_mime_parts(part
->mp_part
, smsgs
, msgp
);
370 * Burst out the messages in the digest into the folder
374 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
375 int inplace
, int verbosw
, char *maildir
, int mimesw
)
379 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
384 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
385 adios (msgnam
, "unable to read message");
388 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
392 * See if we have enough space in the folder
393 * structure for all the new messages.
395 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
396 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
397 adios (NULL
, "unable to allocate folder storage");
400 j
= mp
->hghmsg
; /* old value */
401 mp
->hghmsg
+= numburst
;
402 mp
->nummsg
+= numburst
;
405 * If this is not the highest SELECTED message, then
406 * increment mp->hghsel by numburst, since the highest
407 * SELECTED is about to be slid down by that amount.
409 if (msgnum
< mp
->hghsel
)
410 mp
->hghsel
+= numburst
;
413 * If -inplace is given, renumber the messages after the
414 * source message, to make room for each of the messages
415 * contained within the digest.
417 * This is equivalent to refiling a message from the point
418 * of view of the external hooks.
421 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
422 strncpy (f1
, m_name (i
), sizeof(f1
));
423 strncpy (f2
, m_name (j
), sizeof(f2
));
424 if (does_exist (mp
, j
)) {
426 printf ("message %d becomes message %d\n", j
, i
);
428 if (rename (f2
, f1
) == NOTOK
)
429 admonish (f1
, "unable to rename %s to", f2
);
431 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
432 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
433 ext_hook("ref-hook", f1
, f2
);
435 copy_msg_flags (mp
, i
, j
);
436 clear_msg_flags (mp
, j
);
437 mp
->msgflags
|= SEQMOD
;
442 unset_selected (mp
, msgnum
);
444 /* new hghmsg is hghmsg + numburst
446 * At this point, there is an array of numburst smsgs, each element of
447 * which contains the starting and stopping offsets (seeks) of the message
448 * in the digest. The inplace flag is set if the original digest is replaced
449 * by a message containing the table of contents. smsgs[0] is that table of
450 * contents. Go through the message numbers in reverse order (high to low).
452 * Set f1 to the name of the destination message, f2 to the name of a scratch
453 * file. Extract a message from the digest to the scratch file. Move the
454 * original message to a backup file if the destination message number is the
455 * same as the number of the original message, which only happens if the
456 * inplace flag is set. Then move the scratch file to the destination message.
458 * Moving the original message to the backup file is equivalent to deleting the
459 * message from the point of view of the external hooks. And bursting each
460 * message is equivalent to adding a new message.
463 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
464 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
465 strncpy (f1
, m_name (i
), sizeof(f1
));
466 strncpy (f2
, m_mktemp(invo_name
, NULL
, &out
), sizeof(f2
));
468 if (verbosw
&& i
!= msgnum
)
469 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
472 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
473 cpybrst (in
, out
, msgnam
, f2
,
474 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
), mimesw
);
478 strncpy (f3
, m_backup (f1
), sizeof(f3
));
479 if (rename (f1
, f3
) == NOTOK
)
480 admonish (f3
, "unable to rename %s to", f1
);
482 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
483 ext_hook("del-hook", f3
, (char *)0);
485 if (rename (f2
, f1
) == NOTOK
)
486 admonish (f1
, "unable to rename %s to", f2
);
488 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
489 ext_hook("add-hook", f3
, (char *)0);
491 copy_msg_flags (mp
, i
, msgnum
);
492 mp
->msgflags
|= SEQMOD
;
505 * Copy a mesage which is being burst out of a digest.
506 * It will remove any "dashstuffing" in the message.
510 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
, int mime
)
512 register int c
, state
;
514 for (state
= mime
? S4
: S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
549 state
= (c
== '\n') ? S1
: S2
;
562 if (ferror (in
) && !feof (in
))
563 adios (ifile
, "error reading");
565 adios (ofile
, "error writing");