9f71b927df4151dc1cd6c4e8d642edbd4838c7d9
[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 <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                 if (!(msgs = (char **) realloc (msgs,
123                         (size_t) (maxmsgs * sizeof(*msgs)))))
124                     adios (NULL, "unable to reallocate msgs storage");
125             }
126             msgs[nummsgs++] = cp;
127         }
128     }
129
130     if (!file)
131         file = "./msgbox";
132     file = path (file, TFILE);
133
134     /*
135      * Check if file to be created (or appended to)
136      * exists.  If not, ask for confirmation.
137      */
138     if (stat (file, &st) == NOTOK) {
139         if (errno != ENOENT)
140             adios (file, "error on file");
141         cp = concat ("Create file \"", file, "\"? ", NULL);
142         if (!getanswer (cp))
143             done (1);
144         free (cp);
145     }
146
147     if (!context_find ("path"))
148         free (path ("./", TFOLDER));
149
150     /* default is to pack whole folder */
151     if (!nummsgs)
152         msgs[nummsgs++] = "all";
153
154     if (!folder)
155         folder = getfolder (1);
156     maildir = m_maildir (folder);
157
158     if (chdir (maildir) == NOTOK)
159         adios (maildir, "unable to change directory to ");
160
161     /* read folder and create message structure */
162     if (!(mp = folder_read (folder)))
163         adios (NULL, "unable to read folder %s", folder);
164
165     /* check for empty folder */
166     if (mp->nummsg == 0)
167         adios (NULL, "no messages in %s", folder);
168
169     /* parse all the message ranges/sequences and set SELECTED */
170     for (msgnum = 0; msgnum < nummsgs; msgnum++)
171         if (!m_convert (mp, msgs[msgnum]))
172             done (1);
173     seq_setprev (mp);   /* set the previous-sequence */
174
175     /* open and lock new maildrop file */
176     if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
177         adios (file, "unable to open");
178
179     /* copy all the SELECTED messages to the file */
180     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
181         if (is_selected(mp, msgnum)) {
182             if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
183                 admonish (msgnam, "unable to read message");
184                 break;
185             }
186
187             if (mbx_copy (file, mbx_style, md, fd, mapping, NULL, 1) == NOTOK)
188                 adios (file, "error writing to file");
189
190             close (fd);
191         }
192
193     /* close and unlock maildrop file */
194     mbx_close (file, md);
195
196     context_replace (pfolder, folder);  /* update current folder         */
197     if (mp->hghsel != mp->curmsg)
198         seq_setcur (mp, mp->lowsel);
199     seq_save (mp);
200     context_save ();                    /* save the context file         */
201     folder_free (mp);                   /* free folder/message structure */
202     return done (0);
203 }
204
205 int
206 done (int status)
207 {
208     mbx_close (file, md);
209     exit (status);
210     return 1;  /* dead code to satisfy the compiler */
211 }