]>
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/folder_read.h"
10 #include "sbr/folder_free.h"
11 #include "sbr/context_save.h"
12 #include "sbr/context_replace.h"
13 #include "sbr/context_find.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
20 #include "h/dropsbr.h"
23 #include "sbr/m_maildir.h"
25 #define PACKF_SWITCHES \
26 X("file name", 0, FILESW) \
27 X("mbox", 0, MBOXSW) \
28 X("mmdf", 0, MMDFSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(PACKF
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
40 static int md
= NOTOK
;
41 static int mbx_style
= MBOX_FORMAT
;
43 static void mbxclose_done(int) NORETURN
;
49 main (int argc
, char **argv
)
52 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
53 char **argp
, **arguments
;
54 struct msgs_array msgs
= { 0, 0, NULL
};
58 if (nmh_init(argv
[0], true, true)) { return 1; }
60 set_done(mbxclose_done
);
62 arguments
= getarguments (invo_name
, argc
, argv
, 1);
68 while ((cp
= *argp
++)) {
70 switch (smatch (++cp
, switches
)) {
72 ambigsw (cp
, switches
);
75 die("-%s unknown", cp
);
78 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
80 print_help (buf
, switches
, 1);
83 print_version(invo_name
);
88 die("only one file at a time!");
89 if (!(file
= *argp
++) || *file
== '-')
90 die("missing argument to %s", argp
[-2]);
94 mbx_style
= MBOX_FORMAT
;
97 mbx_style
= MMDF_FORMAT
;
101 if (*cp
== '+' || *cp
== '@') {
103 die("only one folder at a time!");
104 folder
= pluspath (cp
);
106 app_msgarg(&msgs
, cp
);
111 file
= path (file
, TFILE
);
114 * Check if file to be created (or appended to)
115 * exists. If not, ask for confirmation.
117 if (stat (file
, &st
) == NOTOK
) {
119 adios (file
, "error on file");
120 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
121 if (!read_yes_or_no_if_tty (cp
))
126 if (!context_find ("path"))
127 free (path ("./", TFOLDER
));
129 /* default is to pack whole folder */
131 app_msgarg(&msgs
, "all");
134 folder
= getfolder (1);
135 maildir
= m_maildir (folder
);
137 if (chdir (maildir
) == NOTOK
)
138 adios (maildir
, "unable to change directory to ");
140 /* read folder and create message structure */
141 if (!(mp
= folder_read (folder
, 1)))
142 die("unable to read folder %s", folder
);
144 /* check for empty folder */
146 die("no messages in %s", folder
);
148 /* parse all the message ranges/sequences and set SELECTED */
149 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
150 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
152 seq_setprev (mp
); /* set the previous-sequence */
154 /* open and lock new maildrop file */
155 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
156 adios (file
, "unable to open");
158 /* copy all the SELECTED messages to the file */
159 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
160 if (is_selected(mp
, msgnum
)) {
161 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
162 admonish (msgnam
, "unable to read message");
166 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
167 adios (file
, "error writing to file");
172 /* close and unlock maildrop file */
173 mbx_close (file
, md
);
175 context_replace (pfolder
, folder
); /* update current folder */
176 if (mp
->hghsel
!= mp
->curmsg
)
177 seq_setcur (mp
, mp
->lowsel
);
179 context_save (); /* save the context file */
180 folder_free (mp
); /* free folder/message structure */
186 mbxclose_done (int status
)
188 mbx_close (file
, md
);