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