Removed -nowhatnowproc switch as it does nothing useful.
[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 NEDITSW  3
21         { "noedit", 0 },
22 #define FORMSW  4
23         { "form formfile", 0 },
24 #define WHATSW  5
25         { "whatnowproc program", 0 },
26 #define VERSIONSW  6
27         { "version", 0 },
28 #define HELPSW  7
29         { "help", 0 },
30         { NULL, 0 }
31 };
32
33
34 int
35 main(int argc, char **argv)
36 {
37         int anot = 0, nedit = 0;
38         int in, out;
39         char *cp, *cwd, *maildir, *msgnam;
40         char *ed = NULL, *folder = NULL;
41         char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
42         char **argp, **arguments;
43         struct msgs *mp = NULL;
44         char *fmtstr;
45
46 #ifdef LOCALE
47         setlocale(LC_ALL, "");
48 #endif
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                                 done(1);
63                         case UNKWNSW:
64                                 adios(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                                 done(1);
70                         case VERSIONSW:
71                                 print_version(invo_name);
72                                 done(1);
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(NULL, "missing argument to %s",
84                                                         argp[-2]);
85                                 nedit = 0;
86                                 continue;
87                         case NEDITSW:
88                                 nedit++;
89                                 continue;
90
91                         case WHATSW:
92                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
93                                         adios(NULL, "missing argument to %s",
94                                                         argp[-2]);
95                                 continue;
96
97                         case FORMSW:
98                                 if (!(form = *argp++) || *form == '-')
99                                         adios(NULL, "missing argument to %s",
100                                                         argp[-2]);
101                                 continue;
102                         }
103                 }
104                 if (*cp == '+' || *cp == '@') {
105                         if (folder)
106                                 adios(NULL, "only one folder at a time!");
107                         else
108                                 folder = getcpy(expandfol(cp));
109                 } else {
110                         if (msg)
111                                 adios(NULL, "only one message at a time!");
112                         else
113                                 msg = cp;
114                 }
115         }
116
117         cwd = getcpy(pwd());
118
119         strncpy(drft, m_draft(seq_beyond), sizeof(drft));
120         if ((out = creat(drft, m_gmprot())) == NOTOK)
121                 adios(drft, "unable to create");
122
123         fmtstr = new_fs(form, distcomps);
124         if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
125                 adios(drft, "error writing");
126         }
127         close(out);
128
129         if (!msg)
130                 msg = seq_cur;
131         if (!folder)
132                 folder = getcurfol();
133         maildir = toabsdir(folder);
134
135         if (chdir(maildir) == NOTOK)
136                 adios(maildir, "unable to change directory to");
137
138         if (!(mp = folder_read(folder)))
139                 adios(NULL, "unable to read folder %s", folder);
140
141         /* check for empty folder */
142         if (mp->nummsg == 0)
143                 adios(NULL, "no messages in %s", folder);
144
145         /* parse the message range/sequence/name and set SELECTED */
146         if (!m_convert(mp, msg))
147                 done(1);
148         seq_setprev(mp);
149
150         if (mp->numsel > 1)
151                 adios(NULL, "only one message at a time!");
152
153         msgnam = getcpy(m_name(mp->lowsel));
154         if ((in = open(msgnam, O_RDONLY)) == NOTOK)
155                 adios(msgnam, "unable to open message");
156
157         context_replace(curfolder, folder);
158         seq_setcur(mp, mp->lowsel);
159         seq_save(mp);  /* synchronize sequences  */
160         context_save();
161
162         what_now(ed, nedit, NOUSE, drft, msgnam, 1, mp,
163                         anot ? "Resent" : NULL, cwd);
164         done(1);
165         return 1;
166 }