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