]> diplodocus.org Git - nmh/blob - uip/packf.c
vector.c: Move interface to own file.
[nmh] / uip / packf.c
1 /* packf.c -- pack a nmh folder into a file
2 *
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.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/path.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
13 #include <fcntl.h>
14 #include "h/dropsbr.h"
15 #include "h/done.h"
16 #include "h/utils.h"
17 #include "sbr/m_maildir.h"
18
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) \
25
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(PACKF);
28 #undef X
29
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(PACKF, switches);
32 #undef X
33
34 static int md = NOTOK;
35 static int mbx_style = MBOX_FORMAT;
36
37 static void mbxclose_done(int) NORETURN;
38
39 char *file = NULL;
40
41
42 int
43 main (int argc, char **argv)
44 {
45 int fd, msgnum;
46 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
47 char **argp, **arguments;
48 struct msgs_array msgs = { 0, 0, NULL };
49 struct msgs *mp;
50 struct stat st;
51
52 if (nmh_init(argv[0], true, true)) { return 1; }
53
54 set_done(mbxclose_done);
55
56 arguments = getarguments (invo_name, argc, argv, 1);
57 argp = arguments;
58
59 /*
60 * Parse arguments
61 */
62 while ((cp = *argp++)) {
63 if (*cp == '-') {
64 switch (smatch (++cp, switches)) {
65 case AMBIGSW:
66 ambigsw (cp, switches);
67 done (1);
68 case UNKWNSW:
69 die("-%s unknown", cp);
70
71 case HELPSW:
72 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
73 invo_name);
74 print_help (buf, switches, 1);
75 done (0);
76 case VERSIONSW:
77 print_version(invo_name);
78 done (0);
79
80 case FILESW:
81 if (file)
82 die("only one file at a time!");
83 if (!(file = *argp++) || *file == '-')
84 die("missing argument to %s", argp[-2]);
85 continue;
86
87 case MBOXSW:
88 mbx_style = MBOX_FORMAT;
89 continue;
90 case MMDFSW:
91 mbx_style = MMDF_FORMAT;
92 continue;
93 }
94 }
95 if (*cp == '+' || *cp == '@') {
96 if (folder)
97 die("only one folder at a time!");
98 folder = pluspath (cp);
99 } else
100 app_msgarg(&msgs, cp);
101 }
102
103 if (!file)
104 file = "./msgbox";
105 file = path (file, TFILE);
106
107 /*
108 * Check if file to be created (or appended to)
109 * exists. If not, ask for confirmation.
110 */
111 if (stat (file, &st) == NOTOK) {
112 if (errno != ENOENT)
113 adios (file, "error on file");
114 cp = concat ("Create file \"", file, "\"? ", NULL);
115 if (!read_yes_or_no_if_tty (cp))
116 done (1);
117 free (cp);
118 }
119
120 if (!context_find ("path"))
121 free (path ("./", TFOLDER));
122
123 /* default is to pack whole folder */
124 if (!msgs.size)
125 app_msgarg(&msgs, "all");
126
127 if (!folder)
128 folder = getfolder (1);
129 maildir = m_maildir (folder);
130
131 if (chdir (maildir) == NOTOK)
132 adios (maildir, "unable to change directory to ");
133
134 /* read folder and create message structure */
135 if (!(mp = folder_read (folder, 1)))
136 die("unable to read folder %s", folder);
137
138 /* check for empty folder */
139 if (mp->nummsg == 0)
140 die("no messages in %s", folder);
141
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]))
145 done (1);
146 seq_setprev (mp); /* set the previous-sequence */
147
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");
151
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");
157 break;
158 }
159
160 if (mbx_copy (file, mbx_style, md, fd, NULL) == NOTOK)
161 adios (file, "error writing to file");
162
163 close (fd);
164 }
165
166 /* close and unlock maildrop file */
167 mbx_close (file, md);
168
169 context_replace (pfolder, folder); /* update current folder */
170 if (mp->hghsel != mp->curmsg)
171 seq_setcur (mp, mp->lowsel);
172 seq_save (mp);
173 context_save (); /* save the context file */
174 folder_free (mp); /* free folder/message structure */
175 done (0);
176 return 1;
177 }
178
179 static void NORETURN
180 mbxclose_done (int status)
181 {
182 mbx_close (file, md);
183 exit (status);
184 }