]>
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.
13 static struct swit switches
[] = {
41 static int find_delim (int, struct smsg
*);
42 static void burst (struct msgs
**, int, struct smsg
*, int, int, int, char *);
43 static void cpybrst (FILE *, FILE *, char *, char *, int);
46 * A macro to check to see if we have reached a message delimiter
47 * (an encapsulation boundary, EB, in RFC 934 parlance).
49 * According to RFC 934, an EB is simply a line which starts with
50 * a "-" and is NOT followed by a space. So even a single "-" on a line
51 * by itself would be an EB.
54 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
57 main (int argc
, char **argv
)
59 int inplace
= 0, quietsw
= 0, verbosw
= 0;
60 int hi
, msgnum
, numburst
;
61 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
62 char **argp
, **arguments
;
63 struct msgs_array msgs
= { 0, 0, NULL
};
68 setlocale(LC_ALL
, "");
70 invo_name
= r1bindex (argv
[0], '/');
72 /* read user profile/context */
75 arguments
= getarguments (invo_name
, argc
, argv
, 1);
78 while ((cp
= *argp
++)) {
80 switch (smatch (++cp
, switches
)) {
82 ambigsw (cp
, switches
);
85 adios (NULL
, "-%s unknown\n", cp
);
88 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
90 print_help (buf
, switches
, 1);
93 print_version(invo_name
);
118 if (*cp
== '+' || *cp
== '@') {
120 adios (NULL
, "only one folder at a time!");
122 folder
= pluspath (cp
);
124 app_msgarg(&msgs
, cp
);
128 if (!context_find ("path"))
129 free (path ("./", TFOLDER
));
131 app_msgarg(&msgs
, "cur");
133 folder
= getfolder (1);
134 maildir
= m_maildir (folder
);
136 if (chdir (maildir
) == NOTOK
)
137 adios (maildir
, "unable to change directory to");
139 /* read folder and create message structure */
140 if (!(mp
= folder_read (folder
)))
141 adios (NULL
, "unable to read folder %s", folder
);
143 /* check for empty folder */
145 adios (NULL
, "no messages in %s", folder
);
147 /* parse all the message ranges/sequences and set SELECTED */
148 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
149 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
151 seq_setprev (mp
); /* set the previous-sequence */
153 smsgs
= (struct smsg
*)
154 calloc ((size_t) (MAXFOLDER
+ 2), sizeof(*smsgs
));
156 adios (NULL
, "unable to allocate burst storage");
160 /* burst all the SELECTED messages */
161 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
162 if (is_selected (mp
, msgnum
)) {
163 if ((numburst
= find_delim (msgnum
, smsgs
)) >= 1) {
165 printf ("%d message%s exploded from digest %d\n",
166 numburst
, numburst
> 1 ? "s" : "", msgnum
);
167 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
, maildir
);
171 admonish (NULL
, "message %d not in digest format",
173 } /* this pair of braces was missing before 1999-07-15 */
175 adios (NULL
, "burst() botch -- you lose big");
180 free ((char *) smsgs
);
181 context_replace (pfolder
, folder
); /* update current folder */
184 * If -inplace is given, then the first message burst becomes
185 * the current message (which will now show a table of contents).
186 * Otherwise, the first message extracted from the first digest
187 * becomes the current message.
190 if (mp
->lowsel
!= mp
->curmsg
)
191 seq_setcur (mp
, mp
->lowsel
);
193 if (hi
<= mp
->hghmsg
)
197 seq_save (mp
); /* synchronize message sequences */
198 context_save (); /* save the context file */
199 folder_free (mp
); /* free folder/message structure */
206 * Scan the message and find the beginning and
207 * end of all the messages in the digest.
211 find_delim (int msgnum
, struct smsg
*smsgs
)
213 int wasdlm
= 0, msgp
;
219 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
220 adios (msgnam
, "unable to read message");
222 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
224 * We're either at the beginning of the whole message, or
225 * we're just past the delimiter of the last message.
226 * Swallow lines until we get to something that's not a newline
228 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
229 pos
+= (long) strlen (buffer
);
234 * Reset to the beginning of the last non-blank line, and save our
235 * starting position. This is where the encapsulated message
238 fseeko (in
, pos
, SEEK_SET
);
239 smsgs
[msgp
].s_start
= pos
;
242 * Read in lines until we get to a message delimiter.
244 * Previously we checked to make sure the preceeding line and
245 * next line was a newline. That actually does not comply with
246 * RFC 934, so make sure we break on a message delimiter even
247 * if the previous character was NOT a newline.
249 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
250 if ((wasdlm
= CHECKDELIM(buffer
)))
253 pos
+= (long) strlen (buffer
);
257 * Only count as a new message if we got the message delimiter.
258 * Swallow a blank line if it was right before the message delimiter.
260 if (smsgs
[msgp
].s_start
!= pos
&& wasdlm
)
261 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
266 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
267 msgp
++; /* fake "End of XXX Digest" */
272 pos
+= (long) strlen (buffer
);
276 return (msgp
- 1); /* return the number of messages burst */
281 * Burst out the messages in the digest into the folder
285 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
286 int inplace
, int verbosw
, char *maildir
)
290 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
295 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
296 adios (msgnam
, "unable to read message");
299 fstat (fileno(in
), &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot();
303 * See if we have enough space in the folder
304 * structure for all the new messages.
306 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
307 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
308 adios (NULL
, "unable to allocate folder storage");
311 j
= mp
->hghmsg
; /* old value */
312 mp
->hghmsg
+= numburst
;
313 mp
->nummsg
+= numburst
;
316 * If this is not the highest SELECTED message, then
317 * increment mp->hghsel by numburst, since the highest
318 * SELECTED is about to be slid down by that amount.
320 if (msgnum
< mp
->hghsel
)
321 mp
->hghsel
+= numburst
;
324 * If -inplace is given, renumber the messages after the
325 * source message, to make room for each of the messages
326 * contained within the digest.
328 * This is equivalent to refiling a message from the point
329 * of view of the external hooks.
332 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
333 strncpy (f1
, m_name (i
), sizeof(f1
));
334 strncpy (f2
, m_name (j
), sizeof(f2
));
335 if (does_exist (mp
, j
)) {
337 printf ("message %d becomes message %d\n", j
, i
);
339 if (rename (f2
, f1
) == NOTOK
)
340 admonish (f1
, "unable to rename %s to", f2
);
342 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
343 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
344 ext_hook("ref-hook", f1
, f2
);
346 copy_msg_flags (mp
, i
, j
);
347 clear_msg_flags (mp
, j
);
348 mp
->msgflags
|= SEQMOD
;
353 unset_selected (mp
, msgnum
);
355 /* new hghmsg is hghmsg + numburst
357 * At this point, there is an array of numburst smsgs, each element of
358 * which contains the starting and stopping offsets (seeks) of the message
359 * in the digest. The inplace flag is set if the original digest is replaced
360 * by a message containing the table of contents. smsgs[0] is that table of
361 * contents. Go through the message numbers in reverse order (high to low).
363 * Set f1 to the name of the destination message, f2 to the name of a scratch
364 * file. Extract a message from the digest to the scratch file. Move the
365 * original message to a backup file if the destination message number is the
366 * same as the number of the original message, which only happens if the
367 * inplace flag is set. Then move the scratch file to the destination message.
369 * Moving the original message to the backup file is equivalent to deleting the
370 * message from the point of view of the external hooks. And bursting each
371 * message is equivalent to adding a new message.
374 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
375 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
376 strncpy (f1
, m_name (i
), sizeof(f1
));
377 strncpy (f2
, m_mktemp(invo_name
, NULL
, &out
), sizeof(f2
));
379 if (verbosw
&& i
!= msgnum
)
380 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
383 fseeko (in
, smsgs
[j
].s_start
, SEEK_SET
);
384 cpybrst (in
, out
, msgnam
, f2
,
385 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
));
389 strncpy (f3
, m_backup (f1
), sizeof(f3
));
390 if (rename (f1
, f3
) == NOTOK
)
391 admonish (f3
, "unable to rename %s to", f1
);
393 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
394 ext_hook("del-hook", f3
, (char *)0);
396 if (rename (f2
, f1
) == NOTOK
)
397 admonish (f1
, "unable to rename %s to", f2
);
399 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
400 ext_hook("add-hook", f3
, (char *)0);
402 copy_msg_flags (mp
, i
, msgnum
);
403 mp
->msgflags
|= SEQMOD
;
415 * Copy a mesage which is being burst out of a digest.
416 * It will remove any "dashstuffing" in the message.
420 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
)
422 register int c
, state
;
424 for (state
= S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
459 state
= (c
== '\n') ? S1
: S2
;
468 if (ferror (in
) && !feof (in
))
469 adios (ifile
, "error reading");
471 adios (ofile
, "error writing");