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