]>
diplodocus.org Git - nmh/blob - uip/packf.c
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/m_convert.h"
10 #include "sbr/getfolder.h"
11 #include "sbr/folder_read.h"
12 #include "sbr/folder_free.h"
13 #include "sbr/context_save.h"
14 #include "sbr/context_replace.h"
15 #include "sbr/context_find.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/error.h"
22 #include "h/dropsbr.h"
25 #include "sbr/m_maildir.h"
27 #define PACKF_SWITCHES \
28 X("file name", 0, FILESW) \
29 X("mbox", 0, MBOXSW) \
30 X("mmdf", 0, MMDFSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(PACKF
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
42 static int md
= NOTOK
;
43 static int mbx_style
= MBOX_FORMAT
;
45 static void mbxclose_done(int) NORETURN
;
51 main (int argc
, char **argv
)
54 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
55 char **argp
, **arguments
;
56 struct msgs_array msgs
= { 0, 0, NULL
};
60 if (nmh_init(argv
[0], true, true)) { return 1; }
62 set_done(mbxclose_done
);
64 arguments
= getarguments (invo_name
, argc
, argv
, 1);
70 while ((cp
= *argp
++)) {
72 switch (smatch (++cp
, switches
)) {
74 ambigsw (cp
, switches
);
77 die("-%s unknown", cp
);
80 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
82 print_help (buf
, switches
, 1);
85 print_version(invo_name
);
90 die("only one file at a time!");
91 if (!(file
= *argp
++) || *file
== '-')
92 die("missing argument to %s", argp
[-2]);
96 mbx_style
= MBOX_FORMAT
;
99 mbx_style
= MMDF_FORMAT
;
103 if (*cp
== '+' || *cp
== '@') {
105 die("only one folder at a time!");
106 folder
= pluspath (cp
);
108 app_msgarg(&msgs
, cp
);
113 file
= path (file
, TFILE
);
116 * Check if file to be created (or appended to)
117 * exists. If not, ask for confirmation.
119 if (stat (file
, &st
) == NOTOK
) {
121 adios (file
, "error on file");
122 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
123 if (!read_yes_or_no_if_tty (cp
))
128 if (!context_find ("path"))
129 free (path ("./", TFOLDER
));
131 /* default is to pack whole folder */
133 app_msgarg(&msgs
, "all");
136 folder
= getfolder (1);
137 maildir
= m_maildir (folder
);
139 if (chdir (maildir
) == NOTOK
)
140 adios (maildir
, "unable to change directory to ");
142 /* read folder and create message structure */
143 if (!(mp
= folder_read (folder
, 1)))
144 die("unable to read folder %s", folder
);
146 /* check for empty folder */
148 die("no messages in %s", folder
);
150 /* parse all the message ranges/sequences and set SELECTED */
151 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
152 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
154 seq_setprev (mp
); /* set the previous-sequence */
156 /* open and lock new maildrop file */
157 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
158 adios (file
, "unable to open");
160 /* copy all the SELECTED messages to the file */
161 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
162 if (is_selected(mp
, msgnum
)) {
163 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
164 admonish (msgnam
, "unable to read message");
168 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
169 adios (file
, "error writing to file");
174 /* close and unlock maildrop file */
175 mbx_close (file
, md
);
177 context_replace (pfolder
, folder
); /* update current folder */
178 if (mp
->hghsel
!= mp
->curmsg
)
179 seq_setcur (mp
, mp
->lowsel
);
181 context_save (); /* save the context file */
182 folder_free (mp
); /* free folder/message structure */
188 mbxclose_done (int status
)
190 mbx_close (file
, md
);