Removed unneeded options from send.
[mmh] / uip / send.c
1 /*
2 ** send.c -- send a composed 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 <fcntl.h>
11 #include <errno.h>
12 #include <signal.h>
13
14
15 static struct swit switches[] = {
16 #define ALIASW  0
17         { "alias aliasfile", 0 },
18 #define DEBUGSW  1
19         { "debug", -5 },
20 #define FILTSW  2
21         { "filter filterfile", 0 },
22 #define NFILTSW  3
23         { "nofilter", 0 },
24 #define FRMTSW  4
25         { "format", 0 },
26 #define NFRMTSW  5
27         { "noformat", 0 },
28 #define FORWSW  6
29         { "forward", 0 },
30 #define NFORWSW  7
31         { "noforward", 0 },
32 #define PUSHSW  8
33         { "push", 0 },
34 #define NPUSHSW  9
35         { "nopush", 0 },
36 #define UNIQSW  10
37         { "unique", -6 },
38 #define NUNIQSW  11
39         { "nounique", -8 },
40 #define VERBSW  12
41         { "verbose", 0 },
42 #define NVERBSW  13
43         { "noverbose", 0 },
44 #define WATCSW  14
45         { "watch", 0 },
46 #define NWATCSW  15
47         { "nowatch", 0 },
48 #define WIDTHSW  16
49         { "width columns", 0 },
50 #define VERSIONSW  17
51         { "version", 0 },
52 #define HELPSW  18
53         { "help", 0 },
54         { NULL, 0 }
55 };
56
57 extern int debugsw;  /* from sendsbr.c */
58 extern int forwsw;
59 extern int inplace;
60 extern int pushsw;
61 extern int unique;
62 extern int verbsw;
63
64 extern char *altmsg;  /*  .. */
65 extern char *annotext;
66 extern char *distfile;
67
68
69 int
70 main(int argc, char **argv)
71 {
72         int msgp = 0, distsw = 0, vecp = 1;
73         int msgnum, status;
74         char *cp, *maildir = NULL;
75         char buf[BUFSIZ], **ap, **argp, **arguments;
76         char *msgs[MAXARGS], *vec[MAXARGS];
77         struct msgs *mp;
78         struct stat st;
79
80 #ifdef LOCALE
81         setlocale(LC_ALL, "");
82 #endif
83         invo_name = mhbasename(argv[0]);
84
85         /* read user profile/context */
86         context_read();
87
88         arguments = getarguments(invo_name, argc, argv, 1);
89         argp = arguments;
90
91         vec[vecp++] = "-library";
92         vec[vecp++] = getcpy(toabsdir("+"));
93
94         while ((cp = *argp++)) {
95                 if (*cp == '-') {
96                         switch (smatch(++cp, switches)) {
97                         case AMBIGSW:
98                                 ambigsw(cp, switches);
99                                 done(1);
100                         case UNKWNSW:
101                                 adios(NULL, "-%s unknown\n", cp);
102
103                         case HELPSW:
104                                 snprintf(buf, sizeof(buf),
105                                                 "%s [file] [switches]",
106                                                 invo_name);
107                                 print_help(buf, switches, 1);
108                                 done(1);
109                         case VERSIONSW:
110                                 print_version(invo_name);
111                                 done(1);
112
113                         case PUSHSW:
114                                 pushsw++;
115                                 continue;
116                         case NPUSHSW:
117                                 pushsw = 0;
118                                 continue;
119
120                         case UNIQSW:
121                                 unique++;
122                                 continue;
123                         case NUNIQSW:
124                                 unique = 0;
125                                 continue;
126
127                         case FORWSW:
128                                 forwsw++;
129                                 continue;
130                         case NFORWSW:
131                                 forwsw = 0;
132                                 continue;
133
134                         case VERBSW:
135                                 verbsw++;
136                                 vec[vecp++] = --cp;
137                                 continue;
138                         case NVERBSW:
139                                 verbsw = 0;
140                                 vec[vecp++] = --cp;
141                                 continue;
142
143                         case DEBUGSW:
144                                 debugsw++;  /* fall */
145                         case NFILTSW:
146                         case FRMTSW:
147                         case NFRMTSW:
148                         case WATCSW:
149                         case NWATCSW:
150                                 vec[vecp++] = --cp;
151                                 continue;
152
153                         case ALIASW:
154                         case FILTSW:
155                         case WIDTHSW:
156                                 vec[vecp++] = --cp;
157                                 if (!(cp = *argp++) || *cp == '-')
158                                         adios(NULL, "missing argument to %s",
159                                                         argp[-2]);
160                                 vec[vecp++] = cp;
161                                 continue;
162
163                         }
164                 } else {
165                         msgs[msgp++] = cp;
166                 }
167         }
168
169         /*
170         ** check for "Aliasfile:" profile entry
171         */
172         if ((cp = context_find("Aliasfile"))) {
173                 char *dp = NULL;
174
175                 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap;
176                                 ap++) {
177                         vec[vecp++] = "-alias";
178                         vec[vecp++] = *ap;
179                 }
180         }
181
182         if (!msgp)
183                 msgs[msgp++] = seq_cur;
184         maildir = toabsdir(draftfolder);
185
186         if (chdir(maildir) == NOTOK)
187                 adios(maildir, "unable to change directory to");
188
189         /* read folder and create message structure */
190         if (!(mp = folder_read(draftfolder)))
191                 adios(NULL, "unable to read draft folder %s", draftfolder);
192
193         /* check for empty folder */
194         if (mp->nummsg == 0)
195                 adios(NULL, "no messages in draft folder %s", draftfolder);
196
197         /* parse all the message ranges/sequences and set SELECTED */
198         for (msgnum = 0; msgnum < msgp; msgnum++)
199                 if (!m_convert(mp, msgs[msgnum]))
200                         done(1);
201         seq_setprev(mp);  /* set the previous-sequence */
202
203         for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
204                 if (is_selected(mp, msgnum)) {
205                         msgs[msgp++] = getcpy(m_name(msgnum));
206                         unset_exists(mp, msgnum);
207                 }
208         }
209
210         mp->msgflags |= SEQMOD;
211         seq_save(mp);
212
213         if (!(cp = getenv("SIGNATURE")) || !*cp)
214                 if ((cp = context_find("signature")) && *cp)
215                         m_putenv("SIGNATURE", cp);
216
217         for (msgnum = 0; msgnum < msgp; msgnum++)
218                 if (stat(msgs[msgnum], &st) == NOTOK)
219                         adios(msgs[msgnum], "unable to stat draft file");
220
221         if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
222                 annotext = NULL;
223         if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
224                 inplace = atoi(cp);
225         if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
226                 altmsg = NULL;  /* used by dist interface - see below */
227
228         if ((cp = getenv("mhdist")) && *cp && (distsw = atoi (cp)) && altmsg) {
229                 vec[vecp++] = "-dist";
230                 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
231                 if (link(altmsg, distfile) == NOTOK) {
232                         if (errno != EXDEV
233 #ifdef EISREMOTE
234                                         && errno != EISREMOTE
235 #endif /* EISREMOTE */
236                                 )
237                                 adios(distfile, "unable to link %s to",
238                                                 altmsg);
239                         free(distfile);
240                         distfile = getcpy(m_mktemp2(NULL, invo_name,
241                                         NULL, NULL));
242                         {
243                                 int in, out;
244                                 struct stat st;
245
246                                 if ((in = open(altmsg, O_RDONLY)) == NOTOK)
247                                         adios(altmsg, "unable to open");
248                                 fstat(in, &st);
249                                 if ((out = creat(distfile,
250                                                 (int) st.st_mode & 0777))
251                                                 == NOTOK)
252                                         adios(distfile, "unable to write");
253                                 cpydata(in, out, altmsg, distfile);
254                                 close(in);
255                                 close(out);
256                         }
257                 }
258         } else {
259                 distfile = NULL;
260         }
261
262         if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
263                 st.st_mtime = 0;
264                 st.st_dev = 0;
265                 st.st_ino = 0;
266         }
267         if (pushsw)
268                 push();
269
270         status = 0;
271         vec[0] = mhbasename(postproc);
272         closefds(3);
273
274         for (msgnum = 0; msgnum < msgp; msgnum++) {
275                 switch (sendsbr(vec, vecp, msgs[msgnum], &st, 1)) {
276                 case DONE:
277                         done(++status);
278                 case NOTOK:
279                         status++;  /* fall */
280                 case OK:
281                         break;
282                 }
283         }
284
285         context_save();  /* save the context file */
286         done(status);
287         return 1;
288 }