faf199c01fe8628c38c5a8bacc38c57e56548d56
[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",
86                                                         argp[-2]);
87                                 continue;
88
89                         case MBOXSW:
90                                 mbx_style = MBOX_FORMAT;
91                                 mapping = 0;
92                                 continue;
93                         case MMDFSW:
94                                 mbx_style = MMDF_FORMAT;
95                                 mapping = 1;
96                                 continue;
97                         }
98                 }
99                 if (*cp == '+' || *cp == '@') {
100                         if (folder)
101                                 adios(NULL, "only one folder at a time!");
102                         folder = getcpy(expandfol(cp));
103                 } else
104                         app_msgarg(&msgs, cp);
105         }
106
107         if (!file)
108                 file = "./msgbox";
109         file = getcpy(expanddir(file));
110
111         /*
112         ** Check if file to be created (or appended to)
113         ** exists.  If not, ask for confirmation.
114         */
115         if (stat (file, &st) == NOTOK) {
116                 if (errno != ENOENT)
117                         adios(file, "error on file");
118                 cp = concat("Create file \"", file, "\"? ", NULL);
119                 if (!getanswer(cp))
120                         done(1);
121                 free(cp);
122         }
123
124         /* default is to pack whole folder */
125         if (!msgs.size)
126                 app_msgarg(&msgs, seq_all);
127
128         if (!folder)
129                 folder = getcurfol();
130         maildir = toabsdir(folder);
131
132         if (chdir(maildir) == NOTOK)
133                 adios(maildir, "unable to change directory to ");
134
135         /* read folder and create message structure */
136         if (!(mp = folder_read(folder)))
137                 adios(NULL, "unable to read folder %s", folder);
138
139         /* check for empty folder */
140         if (mp->nummsg == 0)
141                 adios(NULL, "no messages in %s", folder);
142
143         /* parse all the message ranges/sequences and set SELECTED */
144         for (msgnum = 0; msgnum < msgs.size; msgnum++)
145                 if (!m_convert(mp, msgs.msgs[msgnum]))
146                         done(1);
147         seq_setprev(mp);  /* set the previous-sequence */
148
149         /* open and lock new maildrop file */
150         if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot()))
151                         == NOTOK)
152                 adios(file, "unable to open");
153
154         /* copy all the SELECTED messages to the file */
155         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
156                 if (is_selected(mp, msgnum)) {
157                         if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
158                                         == NOTOK) {
159                                 admonish(msgnam, "unable to read message");
160                                 break;
161                         }
162
163                         if (mbx_copy(file, mbx_style, md, fd, mapping, NULL, 1)
164                                         == NOTOK)
165                                 adios(file, "error writing to file");
166
167                         close(fd);
168                 }
169
170         /* close and unlock maildrop file */
171         mbx_close(file, md);
172
173         context_replace(curfolder, folder);  /* update current folder */
174         if (mp->hghsel != mp->curmsg)
175                 seq_setcur(mp, mp->lowsel);
176         seq_save(mp);
177         context_save();  /* save the context file */
178         folder_free(mp);  /* free folder/message structure */
179         done(0);
180         return 1;
181 }
182
183 static void
184 mbxclose_done(int status)
185 {
186         mbx_close(file, md);
187         exit(status);
188 }