]>
diplodocus.org Git - nmh/blob - uip/packf.c
486ea0ec52392fda7f4dcec97ab287cf67628634
1 /* packf.c -- pack a nmh folder into a file
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/seq_setcur.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/smatch.h"
12 #include "sbr/m_convert.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/folder_free.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/error.h"
25 #include "h/dropsbr.h"
28 #include "sbr/m_maildir.h"
30 #define PACKF_SWITCHES \
31 X("file name", 0, FILESW) \
32 X("mbox", 0, MBOXSW) \
33 X("mmdf", 0, MMDFSW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(PACKF
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
45 static int md
= NOTOK
;
46 static int mbx_style
= MBOX_FORMAT
;
48 static void mbxclose_done(int) NORETURN
;
54 main (int argc
, char **argv
)
57 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
58 char **argp
, **arguments
;
59 struct msgs_array msgs
= { 0, 0, NULL
};
63 if (nmh_init(argv
[0], true, true)) { return 1; }
65 set_done(mbxclose_done
);
67 arguments
= getarguments (invo_name
, argc
, argv
, 1);
73 while ((cp
= *argp
++)) {
75 switch (smatch (++cp
, switches
)) {
77 ambigsw (cp
, switches
);
80 die("-%s unknown", cp
);
83 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
85 print_help (buf
, switches
, 1);
88 print_version(invo_name
);
93 die("only one file at a time!");
94 if (!(file
= *argp
++) || *file
== '-')
95 die("missing argument to %s", argp
[-2]);
99 mbx_style
= MBOX_FORMAT
;
102 mbx_style
= MMDF_FORMAT
;
106 if (*cp
== '+' || *cp
== '@') {
108 die("only one folder at a time!");
109 folder
= pluspath (cp
);
111 app_msgarg(&msgs
, cp
);
116 file
= path (file
, TFILE
);
119 * Check if file to be created (or appended to)
120 * exists. If not, ask for confirmation.
122 if (stat (file
, &st
) == NOTOK
) {
124 adios (file
, "error on file");
125 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
126 if (!read_yes_or_no_if_tty (cp
))
131 if (!context_find ("path"))
132 free (path ("./", TFOLDER
));
134 /* default is to pack whole folder */
136 app_msgarg(&msgs
, "all");
139 folder
= getfolder (1);
140 maildir
= m_maildir (folder
);
142 if (chdir (maildir
) == NOTOK
)
143 adios (maildir
, "unable to change directory to ");
145 /* read folder and create message structure */
146 if (!(mp
= folder_read (folder
, 1)))
147 die("unable to read folder %s", folder
);
149 /* check for empty folder */
151 die("no messages in %s", folder
);
153 /* parse all the message ranges/sequences and set SELECTED */
154 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
155 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
157 seq_setprev (mp
); /* set the previous-sequence */
159 /* open and lock new maildrop file */
160 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
161 adios (file
, "unable to open");
163 /* copy all the SELECTED messages to the file */
164 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
165 if (is_selected(mp
, msgnum
)) {
166 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
167 admonish (msgnam
, "unable to read message");
171 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
172 adios (file
, "error writing to file");
177 /* close and unlock maildrop file */
178 mbx_close (file
, md
);
180 context_replace (pfolder
, folder
); /* update current folder */
181 if (mp
->hghsel
!= mp
->curmsg
)
182 seq_setcur (mp
, mp
->lowsel
);
184 context_save (); /* save the context file */
185 folder_free (mp
); /* free folder/message structure */
191 mbxclose_done (int status
)
193 mbx_close (file
, md
);