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