]>
diplodocus.org Git - nmh/blob - uip/packf.c
4420b9cb7bf33a5de31895ba630653a4befdfb79
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/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
15 #include "h/dropsbr.h"
18 #include "sbr/m_maildir.h"
20 #define PACKF_SWITCHES \
21 X("file name", 0, FILESW) \
22 X("mbox", 0, MBOXSW) \
23 X("mmdf", 0, MMDFSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(PACKF
);
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
35 static int md
= NOTOK
;
36 static int mbx_style
= MBOX_FORMAT
;
38 static void mbxclose_done(int) NORETURN
;
44 main (int argc
, char **argv
)
47 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
48 char **argp
, **arguments
;
49 struct msgs_array msgs
= { 0, 0, NULL
};
53 if (nmh_init(argv
[0], true, true)) { return 1; }
55 set_done(mbxclose_done
);
57 arguments
= getarguments (invo_name
, argc
, argv
, 1);
63 while ((cp
= *argp
++)) {
65 switch (smatch (++cp
, switches
)) {
67 ambigsw (cp
, switches
);
70 die("-%s unknown", cp
);
73 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
75 print_help (buf
, switches
, 1);
78 print_version(invo_name
);
83 die("only one file at a time!");
84 if (!(file
= *argp
++) || *file
== '-')
85 die("missing argument to %s", argp
[-2]);
89 mbx_style
= MBOX_FORMAT
;
92 mbx_style
= MMDF_FORMAT
;
96 if (*cp
== '+' || *cp
== '@') {
98 die("only one folder at a time!");
99 folder
= pluspath (cp
);
101 app_msgarg(&msgs
, cp
);
106 file
= path (file
, TFILE
);
109 * Check if file to be created (or appended to)
110 * exists. If not, ask for confirmation.
112 if (stat (file
, &st
) == NOTOK
) {
114 adios (file
, "error on file");
115 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
116 if (!read_yes_or_no_if_tty (cp
))
121 if (!context_find ("path"))
122 free (path ("./", TFOLDER
));
124 /* default is to pack whole folder */
126 app_msgarg(&msgs
, "all");
129 folder
= getfolder (1);
130 maildir
= m_maildir (folder
);
132 if (chdir (maildir
) == NOTOK
)
133 adios (maildir
, "unable to change directory to ");
135 /* read folder and create message structure */
136 if (!(mp
= folder_read (folder
, 1)))
137 die("unable to read folder %s", folder
);
139 /* check for empty folder */
141 die("no messages in %s", folder
);
143 /* parse all the message ranges/sequences and set SELECTED */
144 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
145 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
147 seq_setprev (mp
); /* set the previous-sequence */
149 /* open and lock new maildrop file */
150 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
151 adios (file
, "unable to open");
153 /* copy all the SELECTED messages to the file */
154 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
155 if (is_selected(mp
, msgnum
)) {
156 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
157 admonish (msgnam
, "unable to read message");
161 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
162 adios (file
, "error writing to file");
167 /* close and unlock maildrop file */
168 mbx_close (file
, md
);
170 context_replace (pfolder
, folder
); /* update current folder */
171 if (mp
->hghsel
!= mp
->curmsg
)
172 seq_setcur (mp
, mp
->lowsel
);
174 context_save (); /* save the context file */
175 folder_free (mp
); /* free folder/message structure */
181 mbxclose_done (int status
)
183 mbx_close (file
, md
);