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