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