4c271dffe45e186bcc2064309d350f8a4fb2e54d
[mmh] / uip / rcvpack.c
1 /*
2 ** rcvpack.c -- append message to 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 <h/dropsbr.h>
11 #include <h/rcvmail.h>
12 #include <h/tws.h>
13 #include <unistd.h>
14 #include <locale.h>
15
16 static struct swit switches[] = {
17 #define VERSIONSW  0
18         { "Version", 0 },
19 #define HELPSW  1
20         { "help", 0 },
21         { NULL, 0 }
22 };
23
24
25 int
26 main(int argc, char **argv)
27 {
28         int md;
29         char *cp, *file = NULL, buf[BUFSIZ];
30         char **argp, **arguments;
31
32         setlocale(LC_ALL, "");
33         invo_name = mhbasename(argv[0]);
34
35         /* read user profile/context */
36         context_read();
37
38         arguments = getarguments(invo_name, argc, argv, 1);
39         argp = arguments;
40
41         /* parse arguments */
42         while ((cp = *argp++)) {
43                 if (*cp == '-') {
44                         switch (smatch(++cp, switches)) {
45                         case AMBIGSW:
46                                 ambigsw(cp, switches);
47                                 exit(1);
48                         case UNKWNSW:
49                                 adios(NULL, "-%s unknown", cp);
50
51                         case HELPSW:
52                                 snprintf(buf, sizeof(buf),
53                                                 "%s [switches] file",
54                                                 invo_name);
55                                 print_help(buf, switches, 1);
56                                 exit(0);
57                         case VERSIONSW:
58                                 print_version(invo_name);
59                                 exit(0);
60                         }
61                 }
62                 if (file)
63                         adios(NULL, "only one file at a time!");
64                 else
65                         file = cp;
66         }
67
68         /* copy stdin to stdout, converting rfc822 message to mbox */
69         if (!file) {
70                 if (mbox_copy(fileno(stdout), fileno(stdin)) == NOTOK) {
71                         exit(RCV_MBX);
72                 }
73                 exit(RCV_MOK);
74                 return 1;
75         }
76
77         /* open and lock the file */
78         if ((md = mbox_open(file, getuid(), getgid(), m_gmprot()))
79                         == NOTOK)
80                 exit(RCV_MBX);
81
82         /* append the message */
83         if (mbox_copy(md, fileno(stdin)) == NOTOK) {
84                 mbox_close(file, md);
85                 exit(RCV_MBX);
86         }
87
88         /* close and unlock the file */
89         if (mbox_close(file, md) == NOTOK)
90                 exit(RCV_MBX);
91
92         exit(RCV_MOK);
93         return 1;
94 }