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