b565c164e25cd6d32646d2582d794674972bb03d
[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 VERSIONSW  0
17         { "Version", 0 },
18 #define HELPSW  1
19         { "help", 0 },
20         { NULL, 0 }
21 };
22
23
24 int
25 main(int argc, char **argv)
26 {
27         int fd, msgnum;
28         char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
29         char **argp, **arguments;
30         struct msgs_array msgs = { 0, 0, NULL };
31         struct msgs *mp;
32
33 #ifdef LOCALE
34         setlocale(LC_ALL, "");
35 #endif
36         invo_name = mhbasename(argv[0]);
37
38         /* read user profile/context */
39         context_read();
40
41         arguments = getarguments(invo_name, argc, argv, 1);
42         argp = arguments;
43
44         /*
45         ** Parse arguments
46         */
47         while ((cp = *argp++)) {
48                 if (*cp == '-') {
49                         switch (smatch(++cp, switches)) {
50                         case AMBIGSW:
51                                 ambigsw(cp, switches);
52                                 done(1);
53                         case UNKWNSW:
54                                 adios(NULL, "-%s unknown", cp);
55
56                         case HELPSW:
57                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
58                                 print_help(buf, switches, 1);
59                                 done(1);
60                         case VERSIONSW:
61                                 print_version(invo_name);
62                                 done(1);
63                         }
64                 }
65                 if (*cp == '+' || *cp == '@') {
66                         if (folder)
67                                 adios(NULL, "only one folder at a time!");
68                         folder = getcpy(expandfol(cp));
69                 } else
70                         app_msgarg(&msgs, cp);
71         }
72
73         /* default is to pack whole folder */
74         if (!msgs.size)
75                 app_msgarg(&msgs, seq_all);
76
77         if (!folder)
78                 folder = getcurfol();
79         maildir = toabsdir(folder);
80
81         if (chdir(maildir) == NOTOK)
82                 adios(maildir, "unable to change directory to ");
83
84         /* read folder and create message structure */
85         if (!(mp = folder_read(folder)))
86                 adios(NULL, "unable to read folder %s", folder);
87
88         /* check for empty folder */
89         if (mp->nummsg == 0)
90                 adios(NULL, "no messages in %s", folder);
91
92         /* parse all the message ranges/sequences and set SELECTED */
93         for (msgnum = 0; msgnum < msgs.size; msgnum++)
94                 if (!m_convert(mp, msgs.msgs[msgnum]))
95                         done(1);
96         seq_setprev(mp);  /* set the previous-sequence */
97
98         /* copy all the SELECTED messages to stdout */
99         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
100                 if (is_selected(mp, msgnum)) {
101                         if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
102                                         == NOTOK) {
103                                 admonish(msgnam, "unable to read message");
104                                 break;
105                         }
106                         if (mbox_copy(fileno(stdout), fd) == NOTOK) {
107                                 adios(NULL, "error writing to stdout");
108                         }
109                         close(fd);
110                 }
111         }
112         context_replace(curfolder, folder);
113         if (mp->hghsel != mp->curmsg)
114                 seq_setcur(mp, mp->lowsel);
115         seq_save(mp);
116         context_save();
117         folder_free(mp);
118         done(0);
119         return 1;
120 }