]> diplodocus.org Git - nmh/blob - uip/packf.c
* uip/sendsbr.c: with attachformat values of 1 or 2, only
[nmh] / uip / packf.c
1
2 /*
3 * packf.c -- pack a nmh folder into a file
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14 #include <h/dropsbr.h>
15 #include <h/utils.h>
16 #include <errno.h>
17
18 /*
19 * We allocate space for messages (msgs array)
20 * this number of elements at a time.
21 */
22 #define MAXMSGS 256
23
24
25 static struct swit switches[] = {
26 #define FILESW 0
27 { "file name", 0 },
28 #define MBOXSW 1
29 { "mbox", 0 },
30 #define MMDFSW 2
31 { "mmdf", 0 },
32 #define VERSIONSW 3
33 { "version", 0 },
34 #define HELPSW 4
35 { "help", 0 },
36 { NULL, 0 }
37 };
38
39 static int md = NOTOK;
40 static int mbx_style = MBOX_FORMAT;
41 static int mapping = 0;
42
43 char *file = NULL;
44
45
46 int
47 main (int argc, char **argv)
48 {
49 int nummsgs, maxmsgs, fd, msgnum;
50 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
51 char **argp, **arguments, **msgs;
52 struct msgs *mp;
53 struct stat st;
54
55 #ifdef LOCALE
56 setlocale(LC_ALL, "");
57 #endif
58 invo_name = r1bindex (argv[0], '/');
59
60 /* read user profile/context */
61 context_read();
62
63 arguments = getarguments (invo_name, argc, argv, 1);
64 argp = arguments;
65
66 /* Allocate the initial space to record message
67 * names and ranges.
68 */
69 nummsgs = 0;
70 maxmsgs = MAXMSGS;
71 msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
72
73 /*
74 * Parse arguments
75 */
76 while ((cp = *argp++)) {
77 if (*cp == '-') {
78 switch (smatch (++cp, switches)) {
79 case AMBIGSW:
80 ambigsw (cp, switches);
81 done (1);
82 case UNKWNSW:
83 adios (NULL, "-%s unknown", cp);
84
85 case HELPSW:
86 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
87 invo_name);
88 print_help (buf, switches, 1);
89 done (1);
90 case VERSIONSW:
91 print_version(invo_name);
92 done (1);
93
94 case FILESW:
95 if (file)
96 adios (NULL, "only one file at a time!");
97 if (!(file = *argp++) || *file == '-')
98 adios (NULL, "missing argument to %s", argp[-2]);
99 continue;
100
101 case MBOXSW:
102 mbx_style = MBOX_FORMAT;
103 mapping = 0;
104 continue;
105 case MMDFSW:
106 mbx_style = MMDF_FORMAT;
107 mapping = 1;
108 continue;
109 }
110 }
111 if (*cp == '+' || *cp == '@') {
112 if (folder)
113 adios (NULL, "only one folder at a time!");
114 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
115 } else {
116 /*
117 * Check if we need to allocate more space
118 * for message name/ranges.
119 */
120 if (nummsgs >= maxmsgs) {
121 maxmsgs += MAXMSGS;
122 msgs = (char **) mh_xrealloc (msgs,
123 (size_t) (maxmsgs * sizeof(*msgs)));
124 }
125 msgs[nummsgs++] = cp;
126 }
127 }
128
129 if (!file)
130 file = "./msgbox";
131 file = path (file, TFILE);
132
133 /*
134 * Check if file to be created (or appended to)
135 * exists. If not, ask for confirmation.
136 */
137 if (stat (file, &st) == NOTOK) {
138 if (errno != ENOENT)
139 adios (file, "error on file");
140 cp = concat ("Create file \"", file, "\"? ", NULL);
141 if (!getanswer (cp))
142 done (1);
143 free (cp);
144 }
145
146 if (!context_find ("path"))
147 free (path ("./", TFOLDER));
148
149 /* default is to pack whole folder */
150 if (!nummsgs)
151 msgs[nummsgs++] = "all";
152
153 if (!folder)
154 folder = getfolder (1);
155 maildir = m_maildir (folder);
156
157 if (chdir (maildir) == NOTOK)
158 adios (maildir, "unable to change directory to ");
159
160 /* read folder and create message structure */
161 if (!(mp = folder_read (folder)))
162 adios (NULL, "unable to read folder %s", folder);
163
164 /* check for empty folder */
165 if (mp->nummsg == 0)
166 adios (NULL, "no messages in %s", folder);
167
168 /* parse all the message ranges/sequences and set SELECTED */
169 for (msgnum = 0; msgnum < nummsgs; msgnum++)
170 if (!m_convert (mp, msgs[msgnum]))
171 done (1);
172 seq_setprev (mp); /* set the previous-sequence */
173
174 /* open and lock new maildrop file */
175 if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
176 adios (file, "unable to open");
177
178 /* copy all the SELECTED messages to the file */
179 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
180 if (is_selected(mp, msgnum)) {
181 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
182 admonish (msgnam, "unable to read message");
183 break;
184 }
185
186 if (mbx_copy (file, mbx_style, md, fd, mapping, NULL, 1) == NOTOK)
187 adios (file, "error writing to file");
188
189 close (fd);
190 }
191
192 /* close and unlock maildrop file */
193 mbx_close (file, md);
194
195 context_replace (pfolder, folder); /* update current folder */
196 if (mp->hghsel != mp->curmsg)
197 seq_setcur (mp, mp->lowsel);
198 seq_save (mp);
199 context_save (); /* save the context file */
200 folder_free (mp); /* free folder/message structure */
201 return done (0);
202 }
203
204 int
205 done (int status)
206 {
207 mbx_close (file, md);
208 exit (status);
209 return 1; /* dead code to satisfy the compiler */
210 }