]>
diplodocus.org Git - nmh/blob - uip/packf.c
727e00dfdc83da5ca8fccb56ebd60ae8d11aeb31
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/getarguments.h"
10 #include "sbr/read_yes_or_no_if_tty.h"
11 #include "sbr/concat.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/folder_free.h"
20 #include "sbr/context_save.h"
21 #include "sbr/context_replace.h"
22 #include "sbr/context_find.h"
23 #include "sbr/ambigsw.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/error.h"
29 #include "h/dropsbr.h"
32 #include "sbr/m_maildir.h"
34 #define PACKF_SWITCHES \
35 X("file name", 0, FILESW) \
36 X("mbox", 0, MBOXSW) \
37 X("mmdf", 0, MMDFSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(PACKF
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
49 static int md
= NOTOK
;
50 static int mbx_style
= MBOX_FORMAT
;
52 static void mbxclose_done(int) NORETURN
;
58 main (int argc
, char **argv
)
61 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
62 char **argp
, **arguments
;
63 struct msgs_array msgs
= { 0, 0, NULL
};
67 if (nmh_init(argv
[0], true, true)) { return 1; }
69 set_done(mbxclose_done
);
71 arguments
= getarguments (invo_name
, argc
, argv
, 1);
77 while ((cp
= *argp
++)) {
79 switch (smatch (++cp
, switches
)) {
81 ambigsw (cp
, switches
);
84 die("-%s unknown", cp
);
87 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
89 print_help (buf
, switches
, 1);
92 print_version(invo_name
);
97 die("only one file at a time!");
98 if (!(file
= *argp
++) || *file
== '-')
99 die("missing argument to %s", argp
[-2]);
103 mbx_style
= MBOX_FORMAT
;
106 mbx_style
= MMDF_FORMAT
;
110 if (*cp
== '+' || *cp
== '@') {
112 die("only one folder at a time!");
113 folder
= pluspath (cp
);
115 app_msgarg(&msgs
, cp
);
120 file
= path (file
, TFILE
);
123 * Check if file to be created (or appended to)
124 * exists. If not, ask for confirmation.
126 if (stat (file
, &st
) == NOTOK
) {
128 adios (file
, "error on file");
129 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
130 if (!read_yes_or_no_if_tty (cp
))
135 if (!context_find ("path"))
136 free (path ("./", TFOLDER
));
138 /* default is to pack whole folder */
140 app_msgarg(&msgs
, "all");
143 folder
= getfolder (1);
144 maildir
= m_maildir (folder
);
146 if (chdir (maildir
) == NOTOK
)
147 adios (maildir
, "unable to change directory to ");
149 /* read folder and create message structure */
150 if (!(mp
= folder_read (folder
, 1)))
151 die("unable to read folder %s", folder
);
153 /* check for empty folder */
155 die("no messages in %s", folder
);
157 /* parse all the message ranges/sequences and set SELECTED */
158 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
159 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
161 seq_setprev (mp
); /* set the previous-sequence */
163 /* open and lock new maildrop file */
164 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
165 adios (file
, "unable to open");
167 /* copy all the SELECTED messages to the file */
168 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
169 if (is_selected(mp
, msgnum
)) {
170 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
171 admonish (msgnam
, "unable to read message");
175 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
176 adios (file
, "error writing to file");
181 /* close and unlock maildrop file */
182 mbx_close (file
, md
);
184 context_replace (pfolder
, folder
); /* update current folder */
185 if (mp
->hghsel
!= mp
->curmsg
)
186 seq_setcur (mp
, mp
->lowsel
);
188 context_save (); /* save the context file */
189 folder_free (mp
); /* free folder/message structure */
195 mbxclose_done (int status
)
197 mbx_close (file
, md
);