]>
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
)
218 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
219 adios (msgnam
, "unable to read message");
221 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
222 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
223 pos
+= (long) strlen (buffer
);
226 fseek (in
, pos
, SEEK_SET
);
227 smsgs
[msgp
].s_start
= pos
;
229 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
230 if (CHECKDELIM(buffer
)
231 && (msgp
== 1 || c
== '\n')
232 && ((cc
= peekc (in
)) == '\n' || cc
== EOF
))
235 pos
+= (long) strlen (buffer
);
238 wasdlm
= CHECKDELIM(buffer
);
239 if (smsgs
[msgp
].s_start
!= pos
)
240 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
244 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
245 msgp
++; /* fake "End of XXX Digest" */
250 pos
+= (long) strlen (buffer
);
254 return (msgp
- 1); /* toss "End of XXX Digest" */
259 * Burst out the messages in the digest into the folder
263 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
264 int inplace
, int verbosw
, char *maildir
)
268 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
273 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
274 adios (msgnam
, "unable to read message");
277 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
281 * See if we have enough space in the folder
282 * structure for all the new messages.
284 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
285 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
286 adios (NULL
, "unable to allocate folder storage");
289 j
= mp
->hghmsg
; /* old value */
290 mp
->hghmsg
+= numburst
;
291 mp
->nummsg
+= numburst
;
294 * If this is not the highest SELECTED message, then
295 * increment mp->hghsel by numburst, since the highest
296 * SELECTED is about to be slid down by that amount.
298 if (msgnum
< mp
->hghsel
)
299 mp
->hghsel
+= numburst
;
302 * If -inplace is given, renumber the messages after the
303 * source message, to make room for each of the messages
304 * contained within the digest.
306 * This is equivalent to refiling a message from the point
307 * of view of the external hooks.
310 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
311 strncpy (f1
, m_name (i
), sizeof(f1
));
312 strncpy (f2
, m_name (j
), sizeof(f2
));
313 if (does_exist (mp
, j
)) {
315 printf ("message %d becomes message %d\n", j
, i
);
317 if (rename (f2
, f1
) == NOTOK
)
318 admonish (f1
, "unable to rename %s to", f2
);
320 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
321 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
322 ext_hook("ref-hook", f1
, f2
);
324 copy_msg_flags (mp
, i
, j
);
325 clear_msg_flags (mp
, j
);
326 mp
->msgflags
|= SEQMOD
;
331 unset_selected (mp
, msgnum
);
333 /* new hghmsg is hghmsg + numburst
335 * At this point, there is an array of numburst smsgs, each element of
336 * which contains the starting and stopping offsets (seeks) of the message
337 * in the digest. The inplace flag is set if the original digest is replaced
338 * by a message containing the table of contents. smsgs[0] is that table of
339 * contents. Go through the message numbers in reverse order (high to low).
341 * Set f1 to the name of the destination message, f2 to the name of a scratch
342 * file. Extract a message from the digest to the scratch file. Move the
343 * original message to a backup file if the destination message number is the
344 * same as the number of the original message, which only happens if the
345 * inplace flag is set. Then move the scratch file to the destination message.
347 * Moving the original message to the backup file is equivalent to deleting the
348 * message from the point of view of the external hooks. And bursting each
349 * message is equivalent to adding a new message.
352 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
353 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
354 strncpy (f1
, m_name (i
), sizeof(f1
));
355 strncpy (f2
, m_mktemp(invo_name
, NULL
, &out
), sizeof(f2
));
357 if (verbosw
&& i
!= msgnum
)
358 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
361 fseek (in
, smsgs
[j
].s_start
, SEEK_SET
);
362 cpybrst (in
, out
, msgnam
, f2
,
363 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
));
367 strncpy (f3
, m_backup (f1
), sizeof(f3
));
368 if (rename (f1
, f3
) == NOTOK
)
369 admonish (f3
, "unable to rename %s to", f1
);
371 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
372 ext_hook("del-hook", f3
, (char *)0);
374 if (rename (f2
, f1
) == NOTOK
)
375 admonish (f1
, "unable to rename %s to", f2
);
377 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
378 ext_hook("add-hook", f3
, (char *)0);
380 copy_msg_flags (mp
, i
, msgnum
);
381 mp
->msgflags
|= SEQMOD
;
393 * Copy a mesage which is being burst out of a digest.
394 * It will remove any "dashstuffing" in the message.
398 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
)
400 register int c
, state
;
402 for (state
= S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
437 state
= (c
== '\n') ? S1
: S2
;
446 if (ferror (in
) && !feof (in
))
447 adios (ifile
, "error reading");
449 adios (ofile
, "error writing");