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