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