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