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