45b5e2611aa0b290a46fdfa008a66bf06bc5d593
[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 MIMESW  8
33         { "mime", 0 },
34 #define NMIMESW  9
35         { "nomime", 0 },
36 #define MSGDSW  10
37         { "msgid", 0 },
38 #define NMSGDSW  11
39         { "nomsgid", 0 },
40 #define PUSHSW  12
41         { "push", 0 },
42 #define NPUSHSW  13
43         { "nopush", 0 },
44 #define UNIQSW  14
45         { "unique", -6 },
46 #define NUNIQSW  15
47         { "nounique", -8 },
48 #define VERBSW  16
49         { "verbose", 0 },
50 #define NVERBSW  17
51         { "noverbose", 0 },
52 #define WATCSW  18
53         { "watch", 0 },
54 #define NWATCSW  19
55         { "nowatch", 0 },
56 #define WIDTHSW  20
57         { "width columns", 0 },
58 #define VERSIONSW  21
59         { "version", 0 },
60 #define HELPSW  22
61         { "help", 0 },
62 #define BITSTUFFSW  23
63         { "dashstuffing", -12 },
64 #define NBITSTUFFSW  24
65         { "nodashstuffing", -14 },
66 #define MAILSW  25
67         { "mail", -4 },
68 #define SAMLSW  26
69         { "saml", -4 },
70 #define SENDSW  27
71         { "send", -4 },
72 #define SOMLSW  28
73         { "soml", -4 },
74 #define CLIESW  29
75         { "client host", -6 },
76 #define SERVSW  30
77         { "server host", 6 },
78 #define SNOOPSW  31
79         { "snoop", 5 },
80 #define ATTACHSW  32
81         { "attach", 6 },
82 #define ATTACHFORMATSW  33
83         { "attachformat", 7 },
84 #define PORTSW  34
85         { "port server-port-name/number" , 4 },
86         { NULL, 0 }
87 };
88
89 extern int debugsw;  /* from sendsbr.c */
90 extern int forwsw;
91 extern int inplace;
92 extern int pushsw;
93 extern int unique;
94 extern int verbsw;
95
96 extern char *altmsg;  /*  .. */
97 extern char *annotext;
98 extern char *distfile;
99
100
101 int
102 main (int argc, char **argv)
103 {
104         int msgp = 0, distsw = 0, vecp = 1;
105         int mime = 0;
106         int msgnum, status;
107         char *cp, *maildir = NULL;
108         char buf[BUFSIZ], **ap, **argp, **arguments;
109         char *msgs[MAXARGS], *vec[MAXARGS];
110         struct msgs *mp;
111         struct stat st;
112         char *attach = (char *)0;  /* header field name for attachments */
113         int attachformat = 0; /* mhbuild format specifier for attachments */
114 #ifdef UCI
115         FILE *fp;
116 #endif /* UCI */
117
118 #ifdef LOCALE
119         setlocale(LC_ALL, "");
120 #endif
121         invo_name = r1bindex (argv[0], '/');
122
123         /* read user profile/context */
124         context_read();
125
126         arguments = getarguments (invo_name, argc, argv, 1);
127         argp = arguments;
128
129         vec[vecp++] = "-library";
130         vec[vecp++] = getcpy (m_maildir (""));
131
132         while ((cp = *argp++)) {
133                 if (*cp == '-') {
134                         switch (smatch (++cp, switches)) {
135                                 case AMBIGSW:
136                                         ambigsw (cp, switches);
137                                         done (1);
138                                 case UNKWNSW:
139                                         adios (NULL, "-%s unknown\n", cp);
140
141                                 case HELPSW:
142                                         snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
143                                         print_help (buf, switches, 1);
144                                         done (1);
145                                 case VERSIONSW:
146                                         print_version(invo_name);
147                                         done (1);
148
149                                 case PUSHSW:
150                                         pushsw++;
151                                         continue;
152                                 case NPUSHSW:
153                                         pushsw = 0;
154                                         continue;
155
156                                 case UNIQSW:
157                                         unique++;
158                                         continue;
159                                 case NUNIQSW:
160                                         unique = 0;
161                                         continue;
162
163                                 case FORWSW:
164                                         forwsw++;
165                                         continue;
166                                 case NFORWSW:
167                                         forwsw = 0;
168                                         continue;
169
170                                 case VERBSW:
171                                         verbsw++;
172                                         vec[vecp++] = --cp;
173                                         continue;
174                                 case NVERBSW:
175                                         verbsw = 0;
176                                         vec[vecp++] = --cp;
177                                         continue;
178
179                                 case MIMESW:
180                                         mime++;
181                                         vec[vecp++] = --cp;
182                                         continue;
183                                 case NMIMESW:
184                                         mime = 0;
185                                         vec[vecp++] = --cp;
186                                         continue;
187
188                                 case DEBUGSW:
189                                         debugsw++;  /* fall */
190                                 case NFILTSW:
191                                 case FRMTSW:
192                                 case NFRMTSW:
193                                 case BITSTUFFSW:
194                                 case NBITSTUFFSW:
195                                 case MSGDSW:
196                                 case NMSGDSW:
197                                 case WATCSW:
198                                 case NWATCSW:
199                                 case MAILSW:
200                                 case SAMLSW:
201                                 case SENDSW:
202                                 case SOMLSW:
203                                 case SNOOPSW:
204                                         vec[vecp++] = --cp;
205                                         continue;
206
207                                 case ALIASW:
208                                 case FILTSW:
209                                 case WIDTHSW:
210                                 case CLIESW:
211                                 case SERVSW:
212                                 case PORTSW:
213                                         vec[vecp++] = --cp;
214                                         if (!(cp = *argp++) || *cp == '-')
215                                                 adios (NULL, "missing argument to %s", argp[-2]);
216                                         vec[vecp++] = cp;
217                                         continue;
218
219                                 case ATTACHSW:
220                                         if (!(attach = *argp++) || *attach == '-')
221                                                 adios (NULL, "missing argument to %s", argp[-2]);
222                                         continue;
223
224                                 case ATTACHFORMATSW:
225                                         if (! *argp || **argp == '-')
226                                                 adios (NULL, "missing argument to %s", argp[-1]);
227                                         else {
228                                                 attachformat = atoi (*argp);
229                                                 if (attachformat < 0 ||
230                                                         attachformat > ATTACHFORMATS - 1) {
231                                                         advise (NULL, "unsupported attachformat %d",
232                                                                         attachformat);
233                                                         continue;
234                                                 }
235                                         }
236                                         ++argp;
237                                         continue;
238                         }
239                 } else {
240                         msgs[msgp++] = cp;
241                 }
242         }
243
244         /*
245          * check for "Aliasfile:" profile entry
246          */
247         if ((cp = context_find ("Aliasfile"))) {
248                 char *dp = NULL;
249
250                 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
251                         vec[vecp++] = "-alias";
252                         vec[vecp++] = *ap;
253                 }
254         }
255
256         if (!context_find ("path"))
257                 free (path ("./", TFOLDER));
258
259         if (!msgp)
260                 msgs[msgp++] = "cur";
261         maildir = m_maildir (draftfolder);
262
263         if (chdir (maildir) == NOTOK)
264                 adios (maildir, "unable to change directory to");
265
266         /* read folder and create message structure */
267         if (!(mp = folder_read (draftfolder)))
268                 adios (NULL, "unable to read draft folder %s", draftfolder);
269
270         /* check for empty folder */
271         if (mp->nummsg == 0)
272                 adios (NULL, "no messages in draft folder %s", draftfolder);
273
274         /* parse all the message ranges/sequences and set SELECTED */
275         for (msgnum = 0; msgnum < msgp; msgnum++)
276                 if (!m_convert (mp, msgs[msgnum]))
277                         done (1);
278         seq_setprev (mp);  /* set the previous-sequence */
279
280         for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
281                 if (is_selected (mp, msgnum)) {
282                         msgs[msgp++] = getcpy (m_name (msgnum));
283                         unset_exists (mp, msgnum);
284                 }
285         }
286
287         mp->msgflags |= SEQMOD;
288         seq_save (mp);
289
290         if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
291                 if ((cp = context_find ("signature")) && *cp)
292                         m_putenv ("SIGNATURE", cp);
293 #ifdef UCI
294                 else {
295                         snprintf (buf, sizeof(buf), "%s/.signature", mypath);
296                         if ((fp = fopen (buf, "r")) != NULL
297                                 && fgets (buf, sizeof buf, fp) != NULL) {
298                                         fclose (fp);
299                                         if (cp = strchr (buf, '\n'))
300                                                 *cp = 0;
301                                         m_putenv ("SIGNATURE", buf);
302                         }
303                 }
304 #endif /* UCI */
305
306         for (msgnum = 0; msgnum < msgp; msgnum++)
307                 if (stat (msgs[msgnum], &st) == NOTOK)
308                         adios (msgs[msgnum], "unable to stat draft file");
309
310         if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
311                 annotext = NULL;
312         if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
313                 inplace = atoi (cp);
314         if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
315                 altmsg = NULL;  /* used by dist interface - see below */
316
317         if ((cp = getenv ("mhdist"))
318                         && *cp
319                         && (distsw = atoi (cp))
320                         && altmsg) {
321                 vec[vecp++] = "-dist";
322                 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
323                 if (link (altmsg, distfile) == NOTOK) {
324                         if (errno != EXDEV
325 #ifdef EISREMOTE
326                                         && errno != EISREMOTE
327 #endif /* EISREMOTE */
328                                 )
329                                 adios (distfile, "unable to link %s to", altmsg);
330                         free (distfile);
331                         distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
332                         {
333                                 int in, out;
334                                 struct stat st;
335
336                                 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
337                                         adios (altmsg, "unable to open");
338                                 fstat(in, &st);
339                                 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
340                                         adios (distfile, "unable to write");
341                                 cpydata (in, out, altmsg, distfile);
342                                 close (in);
343                                 close (out);
344                         }
345                 }
346         } else {
347                 distfile = NULL;
348         }
349
350         if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
351                 st.st_mtime = 0;
352                 st.st_dev = 0;
353                 st.st_ino = 0;
354         }
355         if (pushsw)
356                 push ();
357
358         status = 0;
359         vec[0] = r1bindex (postproc, '/');
360         closefds (3);
361
362         for (msgnum = 0; msgnum < msgp; msgnum++) {
363                 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
364                                                  attachformat)) {
365                         case DONE:
366                                 done (++status);
367                         case NOTOK:
368                                 status++;  /* fall */
369                         case OK:
370                                 break;
371                 }
372         }
373
374         context_save ();  /* save the context file */
375         done (status);
376         return 1;
377 }