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