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