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