]>
diplodocus.org Git - nmh/blob - uip/packf.c
855228ef79cad058d4d8bfc480f3f6d2d959aa04
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/smatch.h"
10 #include "sbr/m_convert.h"
11 #include "sbr/getfolder.h"
12 #include "sbr/folder_read.h"
13 #include "sbr/folder_free.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_replace.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
23 #include "h/dropsbr.h"
26 #include "sbr/m_maildir.h"
28 #define PACKF_SWITCHES \
29 X("file name", 0, FILESW) \
30 X("mbox", 0, MBOXSW) \
31 X("mmdf", 0, MMDFSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(PACKF
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
43 static int md
= NOTOK
;
44 static int mbx_style
= MBOX_FORMAT
;
46 static void mbxclose_done(int) NORETURN
;
52 main (int argc
, char **argv
)
55 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
56 char **argp
, **arguments
;
57 struct msgs_array msgs
= { 0, 0, NULL
};
61 if (nmh_init(argv
[0], true, true)) { return 1; }
63 set_done(mbxclose_done
);
65 arguments
= getarguments (invo_name
, argc
, argv
, 1);
71 while ((cp
= *argp
++)) {
73 switch (smatch (++cp
, switches
)) {
75 ambigsw (cp
, switches
);
78 die("-%s unknown", cp
);
81 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
83 print_help (buf
, switches
, 1);
86 print_version(invo_name
);
91 die("only one file at a time!");
92 if (!(file
= *argp
++) || *file
== '-')
93 die("missing argument to %s", argp
[-2]);
97 mbx_style
= MBOX_FORMAT
;
100 mbx_style
= MMDF_FORMAT
;
104 if (*cp
== '+' || *cp
== '@') {
106 die("only one folder at a time!");
107 folder
= pluspath (cp
);
109 app_msgarg(&msgs
, cp
);
114 file
= path (file
, TFILE
);
117 * Check if file to be created (or appended to)
118 * exists. If not, ask for confirmation.
120 if (stat (file
, &st
) == NOTOK
) {
122 adios (file
, "error on file");
123 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
124 if (!read_yes_or_no_if_tty (cp
))
129 if (!context_find ("path"))
130 free (path ("./", TFOLDER
));
132 /* default is to pack whole folder */
134 app_msgarg(&msgs
, "all");
137 folder
= getfolder (1);
138 maildir
= m_maildir (folder
);
140 if (chdir (maildir
) == NOTOK
)
141 adios (maildir
, "unable to change directory to ");
143 /* read folder and create message structure */
144 if (!(mp
= folder_read (folder
, 1)))
145 die("unable to read folder %s", folder
);
147 /* check for empty folder */
149 die("no messages in %s", folder
);
151 /* parse all the message ranges/sequences and set SELECTED */
152 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
153 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
155 seq_setprev (mp
); /* set the previous-sequence */
157 /* open and lock new maildrop file */
158 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
159 adios (file
, "unable to open");
161 /* copy all the SELECTED messages to the file */
162 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
163 if (is_selected(mp
, msgnum
)) {
164 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
165 admonish (msgnam
, "unable to read message");
169 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
170 adios (file
, "error writing to file");
175 /* close and unlock maildrop file */
176 mbx_close (file
, md
);
178 context_replace (pfolder
, folder
); /* update current folder */
179 if (mp
->hghsel
!= mp
->curmsg
)
180 seq_setcur (mp
, mp
->lowsel
);
182 context_save (); /* save the context file */
183 folder_free (mp
); /* free folder/message structure */
189 mbxclose_done (int status
)
191 mbx_close (file
, md
);