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