]>
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
[] = {
32 static char delim3
[] = "-------";
42 static int find_delim (int, struct smsg
*);
43 static void burst (struct msgs
**, int, struct smsg
*, int, int, int, char *);
44 static void cpybrst (FILE *, FILE *, char *, char *, int);
48 main (int argc
, char **argv
)
50 int inplace
= 0, quietsw
= 0, verbosw
= 0;
51 int msgp
= 0, hi
, msgnum
, numburst
;
52 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
53 char **argp
, **arguments
, *msgs
[MAXARGS
];
58 setlocale(LC_ALL
, "");
60 invo_name
= r1bindex (argv
[0], '/');
62 /* read user profile/context */
65 arguments
= getarguments (invo_name
, argc
, argv
, 1);
68 while ((cp
= *argp
++)) {
70 switch (smatch (++cp
, switches
)) {
72 ambigsw (cp
, switches
);
75 adios (NULL
, "-%s unknown\n", cp
);
78 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
80 print_help (buf
, switches
, 1);
83 print_version(invo_name
);
108 if (*cp
== '+' || *cp
== '@') {
110 adios (NULL
, "only one folder at a time!");
112 folder
= pluspath (cp
);
118 if (!context_find ("path"))
119 free (path ("./", TFOLDER
));
121 msgs
[msgp
++] = "cur";
123 folder
= getfolder (1);
124 maildir
= m_maildir (folder
);
126 if (chdir (maildir
) == NOTOK
)
127 adios (maildir
, "unable to change directory to");
129 /* read folder and create message structure */
130 if (!(mp
= folder_read (folder
)))
131 adios (NULL
, "unable to read folder %s", folder
);
133 /* check for empty folder */
135 adios (NULL
, "no messages in %s", folder
);
137 /* parse all the message ranges/sequences and set SELECTED */
138 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
139 if (!m_convert (mp
, msgs
[msgnum
]))
141 seq_setprev (mp
); /* set the previous-sequence */
143 smsgs
= (struct smsg
*)
144 calloc ((size_t) (MAXFOLDER
+ 2), sizeof(*smsgs
));
146 adios (NULL
, "unable to allocate burst storage");
150 /* burst all the SELECTED messages */
151 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
152 if (is_selected (mp
, msgnum
)) {
153 if ((numburst
= find_delim (msgnum
, smsgs
)) >= 1) {
155 printf ("%d message%s exploded from digest %d\n",
156 numburst
, numburst
> 1 ? "s" : "", msgnum
);
157 burst (&mp
, msgnum
, smsgs
, numburst
, inplace
, verbosw
, maildir
);
161 admonish (NULL
, "message %d not in digest format",
163 } /* this pair of braces was missing before 1999-07-15 */
165 adios (NULL
, "burst() botch -- you lose big");
170 free ((char *) smsgs
);
171 context_replace (pfolder
, folder
); /* update current folder */
174 * If -inplace is given, then the first message burst becomes
175 * the current message (which will now show a table of contents).
176 * Otherwise, the first message extracted from the first digest
177 * becomes the current message.
180 if (mp
->lowsel
!= mp
->curmsg
)
181 seq_setcur (mp
, mp
->lowsel
);
183 if (hi
<= mp
->hghmsg
)
187 seq_save (mp
); /* synchronize message sequences */
188 context_save (); /* save the context file */
189 folder_free (mp
); /* free folder/message structure */
196 * Scan the message and find the beginning and
197 * end of all the messages in the digest.
201 find_delim (int msgnum
, struct smsg
*smsgs
)
203 int ld3
, wasdlm
, msgp
;
210 ld3
= strlen (delim3
);
212 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
213 adios (msgnam
, "unable to read message");
215 for (msgp
= 0, pos
= 0L; msgp
<= MAXFOLDER
;) {
216 while (fgets (buffer
, sizeof(buffer
), in
) && buffer
[0] == '\n')
217 pos
+= (long) strlen (buffer
);
220 fseek (in
, pos
, SEEK_SET
);
221 smsgs
[msgp
].s_start
= pos
;
223 for (c
= 0; fgets (buffer
, sizeof(buffer
), in
); c
= buffer
[0]) {
224 if (strncmp (buffer
, delim3
, ld3
) == 0
225 && (msgp
== 1 || c
== '\n')
226 && ((cc
= peekc (in
)) == '\n' || cc
== EOF
))
229 pos
+= (long) strlen (buffer
);
232 wasdlm
= strncmp (buffer
, delim3
, ld3
) == 0;
233 if (smsgs
[msgp
].s_start
!= pos
)
234 smsgs
[msgp
++].s_stop
= (c
== '\n' && wasdlm
) ? pos
- 1 : pos
;
238 smsgs
[msgp
- 1].s_stop
-= ((long) strlen (buffer
) + 1);
239 msgp
++; /* fake "End of XXX Digest" */
244 pos
+= (long) strlen (buffer
);
248 return (msgp
- 1); /* toss "End of XXX Digest" */
253 * Burst out the messages in the digest into the folder
257 burst (struct msgs
**mpp
, int msgnum
, struct smsg
*smsgs
, int numburst
,
258 int inplace
, int verbosw
, char *maildir
)
262 char f1
[BUFSIZ
], f2
[BUFSIZ
], f3
[BUFSIZ
];
267 if ((in
= fopen (msgnam
= m_name (msgnum
), "r")) == NULL
)
268 adios (msgnam
, "unable to read message");
270 mode
= fstat (fileno(in
), &st
) != NOTOK
? (st
.st_mode
& 0777) : m_gmprot();
274 * See if we have enough space in the folder
275 * structure for all the new messages.
277 if ((mp
->hghmsg
+ numburst
> mp
->hghoff
) &&
278 !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ numburst
)))
279 adios (NULL
, "unable to allocate folder storage");
282 j
= mp
->hghmsg
; /* old value */
283 mp
->hghmsg
+= numburst
;
284 mp
->nummsg
+= numburst
;
287 * If this is not the highest SELECTED message, then
288 * increment mp->hghsel by numburst, since the highest
289 * SELECTED is about to be slid down by that amount.
291 if (msgnum
< mp
->hghsel
)
292 mp
->hghsel
+= numburst
;
295 * If -inplace is given, renumber the messages after the
296 * source message, to make room for each of the messages
297 * contained within the digest.
299 * This is equivalent to refiling a message from the point
300 * of view of the external hooks.
303 for (i
= mp
->hghmsg
; j
> msgnum
; i
--, j
--) {
304 strncpy (f1
, m_name (i
), sizeof(f1
));
305 strncpy (f2
, m_name (j
), sizeof(f2
));
306 if (does_exist (mp
, j
)) {
308 printf ("message %d becomes message %d\n", j
, i
);
310 if (rename (f2
, f1
) == NOTOK
)
311 admonish (f1
, "unable to rename %s to", f2
);
313 (void)snprintf(f1
, sizeof (f1
), "%s/%d", maildir
, i
);
314 (void)snprintf(f2
, sizeof (f2
), "%s/%d", maildir
, j
);
315 ext_hook("ref-hook", f1
, f2
);
317 copy_msg_flags (mp
, i
, j
);
318 clear_msg_flags (mp
, j
);
319 mp
->msgflags
|= SEQMOD
;
324 unset_selected (mp
, msgnum
);
326 /* new hghmsg is hghmsg + numburst
328 * At this point, there is an array of numburst smsgs, each element of
329 * which contains the starting and stopping offsets (seeks) of the message
330 * in the digest. The inplace flag is set if the original digest is replaced
331 * by a message containing the table of contents. smsgs[0] is that table of
332 * contents. Go through the message numbers in reverse order (high to low).
334 * Set f1 to the name of the destination message, f2 to the name of a scratch
335 * file. Extract a message from the digest to the scratch file. Move the
336 * original message to a backup file if the destination message number is the
337 * same as the number of the original message, which only happens if the
338 * inplace flag is set. Then move the scratch file to the destination message.
340 * Moving the original message to the backup file is equivalent to deleting the
341 * message from the point of view of the external hooks. And bursting each
342 * message is equivalent to adding a new message.
345 i
= inplace
? msgnum
+ numburst
: mp
->hghmsg
;
346 for (j
= numburst
; j
>= (inplace
? 0 : 1); i
--, j
--) {
347 strncpy (f1
, m_name (i
), sizeof(f1
));
348 strncpy (f2
, m_mktemp(invo_name
, NULL
, &out
), sizeof(f2
));
350 if (verbosw
&& i
!= msgnum
)
351 printf ("message %d of digest %d becomes message %d\n", j
, msgnum
, i
);
354 fseek (in
, smsgs
[j
].s_start
, SEEK_SET
);
355 cpybrst (in
, out
, msgnam
, f2
,
356 (int) (smsgs
[j
].s_stop
- smsgs
[j
].s_start
));
360 strncpy (f3
, m_backup (f1
), sizeof(f3
));
361 if (rename (f1
, f3
) == NOTOK
)
362 admonish (f3
, "unable to rename %s to", f1
);
364 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
365 ext_hook("del-hook", f3
, (char *)0);
367 if (rename (f2
, f1
) == NOTOK
)
368 admonish (f1
, "unable to rename %s to", f2
);
370 (void)snprintf(f3
, sizeof (f3
), "%s/%d", maildir
, i
);
371 ext_hook("add-hook", f3
, (char *)0);
373 copy_msg_flags (mp
, i
, msgnum
);
374 mp
->msgflags
|= SEQMOD
;
386 * Copy a mesage which is being burst out of a digest.
387 * It will remove any "dashstuffing" in the message.
391 cpybrst (FILE *in
, FILE *out
, char *ifile
, char *ofile
, int len
)
393 register int c
, state
;
395 for (state
= S1
; (c
= fgetc (in
)) != EOF
&& len
> 0; len
--) {
430 state
= (c
== '\n') ? S1
: S2
;
439 if (ferror (in
) && !feof (in
))
440 adios (ifile
, "error reading");
442 adios (ofile
, "error writing");