]>
diplodocus.org Git - nmh/blob - uip/packf.c
331c4bc0e9e48e1a3111e92951eee6ddfb59bb4f
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/context_find.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
16 #include "h/dropsbr.h"
19 #include "sbr/m_maildir.h"
21 #define PACKF_SWITCHES \
22 X("file name", 0, FILESW) \
23 X("mbox", 0, MBOXSW) \
24 X("mmdf", 0, MMDFSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(PACKF
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
36 static int md
= NOTOK
;
37 static int mbx_style
= MBOX_FORMAT
;
39 static void mbxclose_done(int) NORETURN
;
45 main (int argc
, char **argv
)
48 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
49 char **argp
, **arguments
;
50 struct msgs_array msgs
= { 0, 0, NULL
};
54 if (nmh_init(argv
[0], true, true)) { return 1; }
56 set_done(mbxclose_done
);
58 arguments
= getarguments (invo_name
, argc
, argv
, 1);
64 while ((cp
= *argp
++)) {
66 switch (smatch (++cp
, switches
)) {
68 ambigsw (cp
, switches
);
71 die("-%s unknown", cp
);
74 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
76 print_help (buf
, switches
, 1);
79 print_version(invo_name
);
84 die("only one file at a time!");
85 if (!(file
= *argp
++) || *file
== '-')
86 die("missing argument to %s", argp
[-2]);
90 mbx_style
= MBOX_FORMAT
;
93 mbx_style
= MMDF_FORMAT
;
97 if (*cp
== '+' || *cp
== '@') {
99 die("only one folder at a time!");
100 folder
= pluspath (cp
);
102 app_msgarg(&msgs
, cp
);
107 file
= path (file
, TFILE
);
110 * Check if file to be created (or appended to)
111 * exists. If not, ask for confirmation.
113 if (stat (file
, &st
) == NOTOK
) {
115 adios (file
, "error on file");
116 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
117 if (!read_yes_or_no_if_tty (cp
))
122 if (!context_find ("path"))
123 free (path ("./", TFOLDER
));
125 /* default is to pack whole folder */
127 app_msgarg(&msgs
, "all");
130 folder
= getfolder (1);
131 maildir
= m_maildir (folder
);
133 if (chdir (maildir
) == NOTOK
)
134 adios (maildir
, "unable to change directory to ");
136 /* read folder and create message structure */
137 if (!(mp
= folder_read (folder
, 1)))
138 die("unable to read folder %s", folder
);
140 /* check for empty folder */
142 die("no messages in %s", folder
);
144 /* parse all the message ranges/sequences and set SELECTED */
145 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
146 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
148 seq_setprev (mp
); /* set the previous-sequence */
150 /* open and lock new maildrop file */
151 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
152 adios (file
, "unable to open");
154 /* copy all the SELECTED messages to the file */
155 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
156 if (is_selected(mp
, msgnum
)) {
157 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
158 admonish (msgnam
, "unable to read message");
162 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
163 adios (file
, "error writing to file");
168 /* close and unlock maildrop file */
169 mbx_close (file
, md
);
171 context_replace (pfolder
, folder
); /* update current folder */
172 if (mp
->hghsel
!= mp
->curmsg
)
173 seq_setcur (mp
, mp
->lowsel
);
175 context_save (); /* save the context file */
176 folder_free (mp
); /* free folder/message structure */
182 mbxclose_done (int status
)
184 mbx_close (file
, md
);