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