]>
diplodocus.org Git - nmh/blob - uip/packf.c
376ed71d08a75a3fe40b69c8140cc7e95e015dcc
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
13 #include "h/dropsbr.h"
16 #include "sbr/m_maildir.h"
18 #define PACKF_SWITCHES \
19 X("file name", 0, FILESW) \
20 X("mbox", 0, MBOXSW) \
21 X("mmdf", 0, MMDFSW) \
22 X("version", 0, VERSIONSW) \
23 X("help", 0, HELPSW) \
25 #define X(sw, minchars, id) id,
26 DEFINE_SWITCH_ENUM(PACKF
);
29 #define X(sw, minchars, id) { sw, minchars, id },
30 DEFINE_SWITCH_ARRAY(PACKF
, switches
);
33 static int md
= NOTOK
;
34 static int mbx_style
= MBOX_FORMAT
;
36 static void mbxclose_done(int) NORETURN
;
42 main (int argc
, char **argv
)
45 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
46 char **argp
, **arguments
;
47 struct msgs_array msgs
= { 0, 0, NULL
};
51 if (nmh_init(argv
[0], true, true)) { return 1; }
53 set_done(mbxclose_done
);
55 arguments
= getarguments (invo_name
, argc
, argv
, 1);
61 while ((cp
= *argp
++)) {
63 switch (smatch (++cp
, switches
)) {
65 ambigsw (cp
, switches
);
68 die("-%s unknown", cp
);
71 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
73 print_help (buf
, switches
, 1);
76 print_version(invo_name
);
81 die("only one file at a time!");
82 if (!(file
= *argp
++) || *file
== '-')
83 die("missing argument to %s", argp
[-2]);
87 mbx_style
= MBOX_FORMAT
;
90 mbx_style
= MMDF_FORMAT
;
94 if (*cp
== '+' || *cp
== '@') {
96 die("only one folder at a time!");
97 folder
= pluspath (cp
);
99 app_msgarg(&msgs
, cp
);
104 file
= path (file
, TFILE
);
107 * Check if file to be created (or appended to)
108 * exists. If not, ask for confirmation.
110 if (stat (file
, &st
) == NOTOK
) {
112 adios (file
, "error on file");
113 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
114 if (!read_yes_or_no_if_tty (cp
))
119 if (!context_find ("path"))
120 free (path ("./", TFOLDER
));
122 /* default is to pack whole folder */
124 app_msgarg(&msgs
, "all");
127 folder
= getfolder (1);
128 maildir
= m_maildir (folder
);
130 if (chdir (maildir
) == NOTOK
)
131 adios (maildir
, "unable to change directory to ");
133 /* read folder and create message structure */
134 if (!(mp
= folder_read (folder
, 1)))
135 die("unable to read folder %s", folder
);
137 /* check for empty folder */
139 die("no messages in %s", folder
);
141 /* parse all the message ranges/sequences and set SELECTED */
142 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
143 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
145 seq_setprev (mp
); /* set the previous-sequence */
147 /* open and lock new maildrop file */
148 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
149 adios (file
, "unable to open");
151 /* copy all the SELECTED messages to the file */
152 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
153 if (is_selected(mp
, msgnum
)) {
154 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
155 admonish (msgnam
, "unable to read message");
159 if (mbx_copy (file
, mbx_style
, md
, fd
, NULL
) == NOTOK
)
160 adios (file
, "error writing to file");
165 /* close and unlock maildrop file */
166 mbx_close (file
, md
);
168 context_replace (pfolder
, folder
); /* update current folder */
169 if (mp
->hghsel
!= mp
->curmsg
)
170 seq_setcur (mp
, mp
->lowsel
);
172 context_save (); /* save the context file */
173 folder_free (mp
); /* free folder/message structure */
179 mbxclose_done (int status
)
181 mbx_close (file
, md
);