]>
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_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/read_yes_or_no_if_tty.h"
13 #include "sbr/concat.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_setcur.h"
16 #include "sbr/seq_save.h"
17 #include "sbr/smatch.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/folder_read.h"
21 #include "sbr/folder_free.h"
22 #include "sbr/context_save.h"
23 #include "sbr/context_replace.h"
24 #include "sbr/context_find.h"
25 #include "sbr/ambigsw.h"
27 #include "sbr/print_version.h"
28 #include "sbr/print_help.h"
29 #include "sbr/error.h"
31 #include "h/dropsbr.h"
34 #include "sbr/m_maildir.h"
36 #define PACKF_SWITCHES \
37 X("file name", 0, FILESW) \
38 X("mbox", 0, MBOXSW) \
39 X("mmdf", 0, MMDFSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(PACKF
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
51 static int md
= NOTOK
;
52 static int mbx_style
= MBOX_FORMAT
;
54 static void mbxclose_done(int) NORETURN
;
60 main (int argc
, char **argv
)
63 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
64 char **argp
, **arguments
;
65 struct msgs_array msgs
= { 0, 0, NULL
};
69 if (nmh_init(argv
[0], true, true)) { return 1; }
71 set_done(mbxclose_done
);
73 arguments
= getarguments (invo_name
, argc
, argv
, 1);
79 while ((cp
= *argp
++)) {
81 switch (smatch (++cp
, switches
)) {
83 ambigsw (cp
, switches
);
86 die("-%s unknown", cp
);
89 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
91 print_help (buf
, switches
, 1);
94 print_version(invo_name
);
99 die("only one file at a time!");
100 if (!(file
= *argp
++) || *file
== '-')
101 die("missing argument to %s", argp
[-2]);
105 mbx_style
= MBOX_FORMAT
;
108 mbx_style
= MMDF_FORMAT
;
112 if (*cp
== '+' || *cp
== '@') {
114 die("only one folder at a time!");
115 folder
= pluspath (cp
);
117 app_msgarg(&msgs
, cp
);
122 file
= path (file
, TFILE
);
125 * Check if file to be created (or appended to)
126 * exists. If not, ask for confirmation.
128 if (stat (file
, &st
) == NOTOK
) {
130 adios (file
, "error on file");
131 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
132 if (!read_yes_or_no_if_tty (cp
))
137 if (!context_find ("path"))
138 free (path ("./", TFOLDER
));
140 /* default is to pack whole folder */
142 app_msgarg(&msgs
, "all");
145 folder
= getfolder (1);
146 maildir
= m_maildir (folder
);
148 if (chdir (maildir
) == NOTOK
)
149 adios (maildir
, "unable to change directory to ");
151 /* read folder and create message structure */
152 if (!(mp
= folder_read (folder
, 1)))
153 die("unable to read folder %s", folder
);
155 /* check for empty folder */
157 die("no messages in %s", folder
);
159 /* parse all the message ranges/sequences and set SELECTED */
160 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
161 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
163 seq_setprev (mp
); /* set the previous-sequence */
165 /* open and lock new maildrop file */
166 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
167 adios (file
, "unable to open");
169 /* copy all the SELECTED messages to the file */
170 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
171 if (is_selected(mp
, msgnum
)) {
172 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
173 admonish (msgnam
, "unable to read message");
177 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
178 adios (file
, "error writing to file");
183 /* close and unlock maildrop file */
184 mbx_close (file
, md
);
186 context_replace (pfolder
, folder
); /* update current folder */
187 if (mp
->hghsel
!= mp
->curmsg
)
188 seq_setcur (mp
, mp
->lowsel
);
190 context_save (); /* save the context file */
191 folder_free (mp
); /* free folder/message structure */
197 mbxclose_done (int status
)
199 mbx_close (file
, md
);