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