]>
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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
14 #include "h/dropsbr.h"
17 #include "sbr/m_maildir.h"
19 #define PACKF_SWITCHES \
20 X("file name", 0, FILESW) \
21 X("mbox", 0, MBOXSW) \
22 X("mmdf", 0, MMDFSW) \
23 X("version", 0, VERSIONSW) \
24 X("help", 0, HELPSW) \
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(PACKF
);
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
34 static int md
= NOTOK
;
35 static int mbx_style
= MBOX_FORMAT
;
37 static void mbxclose_done(int) NORETURN
;
43 main (int argc
, char **argv
)
46 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
47 char **argp
, **arguments
;
48 struct msgs_array msgs
= { 0, 0, NULL
};
52 if (nmh_init(argv
[0], true, true)) { return 1; }
54 set_done(mbxclose_done
);
56 arguments
= getarguments (invo_name
, argc
, argv
, 1);
62 while ((cp
= *argp
++)) {
64 switch (smatch (++cp
, switches
)) {
66 ambigsw (cp
, switches
);
69 die("-%s unknown", cp
);
72 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
74 print_help (buf
, switches
, 1);
77 print_version(invo_name
);
82 die("only one file at a time!");
83 if (!(file
= *argp
++) || *file
== '-')
84 die("missing argument to %s", argp
[-2]);
88 mbx_style
= MBOX_FORMAT
;
91 mbx_style
= MMDF_FORMAT
;
95 if (*cp
== '+' || *cp
== '@') {
97 die("only one folder at a time!");
98 folder
= pluspath (cp
);
100 app_msgarg(&msgs
, cp
);
105 file
= path (file
, TFILE
);
108 * Check if file to be created (or appended to)
109 * exists. If not, ask for confirmation.
111 if (stat (file
, &st
) == NOTOK
) {
113 adios (file
, "error on file");
114 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
115 if (!read_yes_or_no_if_tty (cp
))
120 if (!context_find ("path"))
121 free (path ("./", TFOLDER
));
123 /* default is to pack whole folder */
125 app_msgarg(&msgs
, "all");
128 folder
= getfolder (1);
129 maildir
= m_maildir (folder
);
131 if (chdir (maildir
) == NOTOK
)
132 adios (maildir
, "unable to change directory to ");
134 /* read folder and create message structure */
135 if (!(mp
= folder_read (folder
, 1)))
136 die("unable to read folder %s", folder
);
138 /* check for empty folder */
140 die("no messages in %s", folder
);
142 /* parse all the message ranges/sequences and set SELECTED */
143 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
144 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
146 seq_setprev (mp
); /* set the previous-sequence */
148 /* open and lock new maildrop file */
149 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
150 adios (file
, "unable to open");
152 /* copy all the SELECTED messages to the file */
153 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
154 if (is_selected(mp
, msgnum
)) {
155 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
156 admonish (msgnam
, "unable to read message");
160 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
161 adios (file
, "error writing to file");
166 /* close and unlock maildrop file */
167 mbx_close (file
, md
);
169 context_replace (pfolder
, folder
); /* update current folder */
170 if (mp
->hghsel
!= mp
->curmsg
)
171 seq_setcur (mp
, mp
->lowsel
);
173 context_save (); /* save the context file */
174 folder_free (mp
); /* free folder/message structure */
180 mbxclose_done (int status
)
182 mbx_close (file
, md
);