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