Use the same mh_hostname() function from test/common.h in mhsign(1)
[mmh] / uip / dist.c
1 /*
2 ** dist.c -- re-distribute a message
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/utils.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <locale.h>
14 #include <sysexits.h>
15
16 static struct swit switches[] = {
17 #define ANNOSW  0
18         { "annotate", 0 },
19 #define NANNOSW  1
20         { "noannotate", 2 },
21 #define EDITRSW  2
22         { "editor editor", 0 },
23 #define FORMSW  3
24         { "form formfile", 0 },
25 #define WHATSW  4
26         { "whatnowproc program", 0 },
27 #define VERSIONSW  5
28         { "Version", 0 },
29 #define HELPSW  6
30         { "help", 0 },
31         { NULL, 0 }
32 };
33
34 char *version=VERSION;
35
36 int
37 main(int argc, char **argv)
38 {
39         int anot = 0;
40         int in, out;
41         char *cp, *cwd, *maildir, *msgnam;
42         char *ed = NULL, *folder = NULL;
43         char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
44         char **argp, **arguments;
45         struct msgs *mp = NULL;
46         char *fmtstr;
47
48         setlocale(LC_ALL, "");
49         invo_name = mhbasename(argv[0]);
50
51         /* read user profile/context */
52         context_read();
53
54         arguments = getarguments(invo_name, argc, argv, 1);
55         argp = arguments;
56
57         while ((cp = *argp++)) {
58                 if (*cp == '-') {
59                         switch (smatch(++cp, switches)) {
60                         case AMBIGSW:
61                                 ambigsw(cp, switches);
62                                 exit(EX_USAGE);
63                         case UNKWNSW:
64                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
65
66                         case HELPSW:
67                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
68                                 print_help(buf, switches, 1);
69                                 exit(argc == 2 ? EX_OK : EX_USAGE);
70
71                         case VERSIONSW:
72                                 print_version(invo_name);
73                                 exit(argc == 2 ? EX_OK : EX_USAGE);
74
75                         case ANNOSW:
76                                 anot++;
77                                 continue;
78                         case NANNOSW:
79                                 anot = 0;
80                                 continue;
81
82                         case EDITRSW:
83                                 if (!(ed = *argp++) || *ed == '-')
84                                         adios(EX_USAGE, NULL, "missing argument to %s",
85                                                         argp[-2]);
86                                 continue;
87
88                         case WHATSW:
89                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-') {
90                                         adios(EX_USAGE, NULL, "missing argument to %s",
91                                                         argp[-2]);
92                                 }
93                                 continue;
94
95                         case FORMSW:
96                                 if (!(form = *argp++) || *form == '-') {
97                                         adios(EX_USAGE, NULL, "missing argument to %s",
98                                                         argp[-2]);
99                                 }
100                                 continue;
101                         }
102                 }
103                 if (*cp == '+' || *cp == '@') {
104                         if (folder) {
105                                 adios(EX_USAGE, NULL, "only one folder at a time!");
106                         } else {
107                                 folder = mh_xstrdup(expandfol(cp));
108                         }
109                 } else {
110                         if (msg) {
111                                 adios(EX_USAGE, NULL, "only one message at a time!");
112                         } else {
113                                 msg = cp;
114                         }
115                 }
116         }
117
118         cwd = mh_xstrdup(pwd());
119
120         strncpy(drft, m_draft(seq_beyond), sizeof(drft));
121         if ((out = creat(drft, m_gmprot())) == NOTOK) {
122                 adios(EX_CANTCREAT, drft, "unable to create");
123         }
124
125         fmtstr = new_fs(form, distcomps);
126         if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
127                 adios(EX_IOERR, drft, "error writing");
128         }
129         close(out);
130
131         if (!msg) {
132                 msg = seq_cur;
133         }
134         if (!folder) {
135                 folder = getcurfol();
136         }
137         maildir = toabsdir(folder);
138
139         if (chdir(maildir) == NOTOK) {
140                 adios(EX_OSERR, maildir, "unable to change directory to");
141         }
142
143         if (!(mp = folder_read(folder))) {
144                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
145         }
146
147         /* check for empty folder */
148         if (mp->nummsg == 0) {
149                 adios(EX_NOINPUT, NULL, "no messages in %s", folder);
150         }
151
152         /* parse the message range/sequence/name and set SELECTED */
153         if (!m_convert(mp, msg)) {
154                 exit(EX_USAGE);
155         }
156         seq_setprev(mp);
157
158         if (mp->numsel > 1) {
159                 adios(EX_USAGE, NULL, "only one message at a time!");
160         }
161
162         msgnam = mh_xstrdup(m_name(mp->lowsel));
163         if ((in = open(msgnam, O_RDONLY)) == NOTOK) {
164                 adios(EX_IOERR, msgnam, "unable to open message");
165         }
166
167         context_replace(curfolder, folder);
168         seq_setcur(mp, mp->lowsel);
169         seq_save(mp);  /* synchronize sequences  */
170         context_save();
171
172         what_now(ed, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL, cwd);
173         return EX_SOFTWARE;
174 }