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