]>
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 static struct swit switches
[] = {
40 static int find_delim (int, struct smsg
*);
41 static void burst (struct msgs
**, int, struct smsg
*, int, int, int, char *);
42 static void cpybrst (FILE *, FILE *, char *, char *, int);
45 * A macro to check to see if we have reached a message delimiter
46 * (an encapsulation boundary, EB, in RFC 934 parlance).
48 * According to RFC 934, an EB is simply a line which starts with
49 * a "-" and is NOT followed by a space. So even a single "-" on a line
50 * by itself would be an EB.
53 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
56 main (int argc
, char **argv
)
58 int inplace
= 0, quietsw
= 0, verbosw
= 0;
59 int msgp
= 0, hi
, msgnum
, numburst
;
60 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
61 char **argp
, **arguments
, *msgs
[MAXARGS
];
66 setlocale(LC_ALL
, "");
68 invo_name
= r1bindex (argv
[0], '/');
70 /* read user profile/context */
73 arguments
= getarguments (invo_name
, argc
, argv
, 1);
76 while ((cp
= *argp
++)) {
78 switch (smatch (++cp
, switches
)) {
80 ambigsw (cp
, switches
);
83 adios (NULL
, "-%s unknown\n", cp
);
86 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
88 print_help (buf
, switches
, 1);
91 print_version(invo_name
);
116 if (*cp
== '+' || *cp
== '@') {
118 adios (NULL
, "only one folder at a time!");
120 folder
= pluspath (cp
);
126 if (!context_find ("path"))
127 free (path ("./", TFOLDER
));
129 msgs
[msgp
++] = "cur";
131 folder
= getfolder (1);
132 maildir
= m_maildir (folder
);
134 if (chdir (maildir
) == NOTOK
)
135 adios (maildir
, "unable to change directory to");
137 /* read folder and create message structure */
138 if (!(mp
= folder_read (folder
)))
139 adios (NULL
, "unable to read folder %s", folder
);
141 /* check for empty folder */
143 adios (NULL
, "no messages in %s", folder
);
145 /* parse all the message ranges/sequences and set SELECTED */
146 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
147 if (!m_convert (mp
, msgs
[msgnum
]))
149 seq_setprev (mp
); /* set the previous-sequence */
151 smsgs
= (struct smsg
*)
152 calloc ((size_t) (MAXFOLDER
+ 2), sizeof(*smsgs
));
154 adios (NULL
, "unable to allocate burst storage");
158 /* burst all the SELECTED messages */
159 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
160 if (is_selected (mp
, msgnum
)) {
161 if ((numburst
= find_delim (msgnum
, smsgs
)) >= 1) {
163 printf ("%d message%s exploded from digest %d\n",
164 numburst
, numburst
> 1 ? "s" : "", msgnum
);
165 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
, maildir
);
169 admonish (NULL
, "message %d not in digest format",
171 } /* this pair of braces was missing before 1999-07-15 */
173 adios (NULL
, "burst() botch -- you lose big");
178 free ((char *) smsgs
);
179 context_replace (pfolder
, folder
); /* update current folder */
182 * If -inplace is given, then the first message burst becomes
183 * the current message (which will now show a table of contents).
184 * Otherwise, the first message extracted from the first digest
185 * becomes the current message.
188 if (mp
->lowsel
!= mp
->curmsg
)
189 seq_setcur (mp
, mp
->lowsel
);
191 if (hi
<= mp
->hghmsg
)
195 seq_save (mp
); /* synchronize message sequences */
196 context_save (); /* save the context file */
197 folder_free (mp
); /* free folder/message structure */
204 * Scan the message and find the beginning and
205 * end of all the messages in the digest.
209 find_delim (int msgnum
, struct smsg
*smsgs
)
211 int wasdlm
= 0, msgp
;
217 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
218 adios (msgnam
, "unable to read message");
220 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
222 * We're either at the beginning of the whole message, or
223 * we're just past the delimiter of the last message.
224 * Swallow lines until we get to something that's not a newline
226 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
227 pos
+= (long) strlen (buffer
);
232 * Reset to the beginning of the last non-blank line, and save our
233 * starting position. This is where the encapsulated message
236 fseeko (in
, pos
, SEEK_SET
);
237 smsgs
[msgp
].s_start
= pos
;
240 * Read in lines until we get to a message delimiter.
242 * Previously we checked to make sure the preceeding line and
243 * next line was a newline. That actually does not comply with
244 * RFC 934, so make sure we break on a message delimiter even
245 * if the previous character was NOT a newline.
247 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
248 if ((wasdlm
= CHECKDELIM(buffer
)))
251 pos
+= (long) strlen (buffer
);
255 * Only count as a new message if we got the message delimiter.
256 * Swallow a blank line if it was right before the message delimiter.
258 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
259 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
264 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
265 msgp
++; /* fake "End of XXX Digest" */
270 pos
+= (long) strlen (buffer
);
274 return (msgp
- 1); /* return the number of messages burst */
279 * Burst out the messages in the digest into the folder
283 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
284 int inplace
, int verbosw
, char *maildir
)
288 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
293 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
294 adios (msgnam
, "unable to read message");
297 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
301 * See if we have enough space in the folder
302 * structure for all the new messages.
304 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
305 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
306 adios (NULL
, "unable to allocate folder storage");
309 j
= mp
->hghmsg
; /* old value */
310 mp
->hghmsg
+= numburst
;
311 mp
->nummsg
+= numburst
;
314 * If this is not the highest SELECTED message, then
315 * increment mp->hghsel by numburst, since the highest
316 * SELECTED is about to be slid down by that amount.
318 if (msgnum
< mp
->hghsel
)
319 mp
->hghsel
+= numburst
;
322 * If -inplace is given, renumber the messages after the
323 * source message, to make room for each of the messages
324 * contained within the digest.
326 * This is equivalent to refiling a message from the point
327 * of view of the external hooks.
330 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
331 strncpy (f1
, m_name (i
), sizeof(f1
));
332 strncpy (f2
, m_name (j
), sizeof(f2
));
333 if (does_exist (mp
, j
)) {
335 printf ("message %d becomes message %d\n", j
, i
);
337 if (rename (f2
, f1
) == NOTOK
)
338 admonish (f1
, "unable to rename %s to", f2
);
340 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
341 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
342 ext_hook("ref-hook", f1
, f2
);
344 copy_msg_flags (mp
, i
, j
);
345 clear_msg_flags (mp
, j
);
346 mp
->msgflags
|= SEQMOD
;
351 unset_selected (mp
, msgnum
);
353 /* new hghmsg is hghmsg + numburst
355 * At this point, there is an array of numburst smsgs, each element of
356 * which contains the starting and stopping offsets (seeks) of the message
357 * in the digest. The inplace flag is set if the original digest is replaced
358 * by a message containing the table of contents. smsgs[0] is that table of
359 * contents. Go through the message numbers in reverse order (high to low).
361 * Set f1 to the name of the destination message, f2 to the name of a scratch
362 * file. Extract a message from the digest to the scratch file. Move the
363 * original message to a backup file if the destination message number is the
364 * same as the number of the original message, which only happens if the
365 * inplace flag is set. Then move the scratch file to the destination message.
367 * Moving the original message to the backup file is equivalent to deleting the
368 * message from the point of view of the external hooks. And bursting each
369 * message is equivalent to adding a new message.
372 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
373 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
374 strncpy (f1
, m_name (i
), sizeof(f1
));
375 strncpy (f2
, m_mktemp(invo_name
, NULL
, &out
), sizeof(f2
));
377 if (verbosw
&& i
!= msgnum
)
378 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
381 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
382 cpybrst (in
, out
, msgnam
, f2
,
383 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
));
387 strncpy (f3
, m_backup (f1
), sizeof(f3
));
388 if (rename (f1
, f3
) == NOTOK
)
389 admonish (f3
, "unable to rename %s to", f1
);
391 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
392 ext_hook("del-hook", f3
, (char *)0);
394 if (rename (f2
, f1
) == NOTOK
)
395 admonish (f1
, "unable to rename %s to", f2
);
397 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
398 ext_hook("add-hook", f3
, (char *)0);
400 copy_msg_flags (mp
, i
, msgnum
);
401 mp
->msgflags
|= SEQMOD
;
413 * Copy a mesage which is being burst out of a digest.
414 * It will remove any "dashstuffing" in the message.
418 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
)
420 register int c
, state
;
422 for (state
= S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
457 state
= (c
== '\n') ? S1
: S2
;
466 if (ferror (in
) && !feof (in
))
467 adios (ifile
, "error reading");
469 adios (ofile
, "error writing");