eb140c9fa6b99750a031b2f1132e0851cd06f0e0
[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 PORTSW  32
81         { "port server-port-name/number" , 4 },
82         { NULL, 0 }
83 };
84
85 extern int debugsw;  /* from sendsbr.c */
86 extern int forwsw;
87 extern int inplace;
88 extern int pushsw;
89 extern int unique;
90 extern int verbsw;
91
92 extern char *altmsg;  /*  .. */
93 extern char *annotext;
94 extern char *distfile;
95
96
97 int
98 main(int argc, char **argv)
99 {
100         int msgp = 0, distsw = 0, vecp = 1;
101         int mime = 0;
102         int msgnum, status;
103         char *cp, *maildir = NULL;
104         char buf[BUFSIZ], **ap, **argp, **arguments;
105         char *msgs[MAXARGS], *vec[MAXARGS];
106         struct msgs *mp;
107         struct stat st;
108 #ifdef UCI
109         FILE *fp;
110 #endif /* UCI */
111
112 #ifdef LOCALE
113         setlocale(LC_ALL, "");
114 #endif
115         invo_name = mhbasename(argv[0]);
116
117         /* read user profile/context */
118         context_read();
119
120         arguments = getarguments(invo_name, argc, argv, 1);
121         argp = arguments;
122
123         vec[vecp++] = "-library";
124         vec[vecp++] = getcpy(toabsdir("+"));
125
126         while ((cp = *argp++)) {
127                 if (*cp == '-') {
128                         switch (smatch(++cp, switches)) {
129                         case AMBIGSW:
130                                 ambigsw(cp, switches);
131                                 done(1);
132                         case UNKWNSW:
133                                 adios(NULL, "-%s unknown\n", cp);
134
135                         case HELPSW:
136                                 snprintf(buf, sizeof(buf),
137                                                 "%s [file] [switches]",
138                                                 invo_name);
139                                 print_help(buf, switches, 1);
140                                 done(1);
141                         case VERSIONSW:
142                                 print_version(invo_name);
143                                 done(1);
144
145                         case PUSHSW:
146                                 pushsw++;
147                                 continue;
148                         case NPUSHSW:
149                                 pushsw = 0;
150                                 continue;
151
152                         case UNIQSW:
153                                 unique++;
154                                 continue;
155                         case NUNIQSW:
156                                 unique = 0;
157                                 continue;
158
159                         case FORWSW:
160                                 forwsw++;
161                                 continue;
162                         case NFORWSW:
163                                 forwsw = 0;
164                                 continue;
165
166                         case VERBSW:
167                                 verbsw++;
168                                 vec[vecp++] = --cp;
169                                 continue;
170                         case NVERBSW:
171                                 verbsw = 0;
172                                 vec[vecp++] = --cp;
173                                 continue;
174
175                         case MIMESW:
176                                 mime++;
177                                 vec[vecp++] = --cp;
178                                 continue;
179                         case NMIMESW:
180                                 mime = 0;
181                                 vec[vecp++] = --cp;
182                                 continue;
183
184                         case DEBUGSW:
185                                 debugsw++;  /* fall */
186                         case NFILTSW:
187                         case FRMTSW:
188                         case NFRMTSW:
189                         case BITSTUFFSW:
190                         case NBITSTUFFSW:
191                         case MSGDSW:
192                         case NMSGDSW:
193                         case WATCSW:
194                         case NWATCSW:
195                         case MAILSW:
196                         case SAMLSW:
197                         case SENDSW:
198                         case SOMLSW:
199                         case SNOOPSW:
200                                 vec[vecp++] = --cp;
201                                 continue;
202
203                         case ALIASW:
204                         case FILTSW:
205                         case WIDTHSW:
206                         case CLIESW:
207                         case SERVSW:
208                         case PORTSW:
209                                 vec[vecp++] = --cp;
210                                 if (!(cp = *argp++) || *cp == '-')
211                                         adios(NULL, "missing argument to %s",
212                                                         argp[-2]);
213                                 vec[vecp++] = cp;
214                                 continue;
215
216                         }
217                 } else {
218                         msgs[msgp++] = cp;
219                 }
220         }
221
222         /*
223         ** check for "Aliasfile:" profile entry
224         */
225         if ((cp = context_find("Aliasfile"))) {
226                 char *dp = NULL;
227
228                 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap;
229                                 ap++) {
230                         vec[vecp++] = "-alias";
231                         vec[vecp++] = *ap;
232                 }
233         }
234
235         if (!msgp)
236                 msgs[msgp++] = seq_cur;
237         maildir = toabsdir(draftfolder);
238
239         if (chdir(maildir) == NOTOK)
240                 adios(maildir, "unable to change directory to");
241
242         /* read folder and create message structure */
243         if (!(mp = folder_read(draftfolder)))
244                 adios(NULL, "unable to read draft folder %s", draftfolder);
245
246         /* check for empty folder */
247         if (mp->nummsg == 0)
248                 adios(NULL, "no messages in draft folder %s", draftfolder);
249
250         /* parse all the message ranges/sequences and set SELECTED */
251         for (msgnum = 0; msgnum < msgp; msgnum++)
252                 if (!m_convert(mp, msgs[msgnum]))
253                         done(1);
254         seq_setprev(mp);  /* set the previous-sequence */
255
256         for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
257                 if (is_selected(mp, msgnum)) {
258                         msgs[msgp++] = getcpy(m_name(msgnum));
259                         unset_exists(mp, msgnum);
260                 }
261         }
262
263         mp->msgflags |= SEQMOD;
264         seq_save(mp);
265
266         if ((cp = getenv("SIGNATURE")) == NULL || *cp == 0)
267                 if ((cp = context_find("signature")) && *cp)
268                         m_putenv("SIGNATURE", cp);
269 #ifdef UCI
270                 else {
271                         snprintf(buf, sizeof(buf), "%s/.signature", mypath);
272                         if ((fp = fopen(buf, "r")) != NULL &&
273                                         fgets(buf, sizeof buf, fp) != NULL) {
274                                         fclose(fp);
275                                         if (cp = strchr(buf, '\n'))
276                                                 *cp = 0;
277                                         m_putenv("SIGNATURE", buf);
278                         }
279                 }
280 #endif /* UCI */
281
282         for (msgnum = 0; msgnum < msgp; msgnum++)
283                 if (stat(msgs[msgnum], &st) == NOTOK)
284                         adios(msgs[msgnum], "unable to stat draft file");
285
286         if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
287                 annotext = NULL;
288         if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
289                 inplace = atoi(cp);
290         if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
291                 altmsg = NULL;  /* used by dist interface - see below */
292
293         if ((cp = getenv("mhdist")) && *cp && (distsw = atoi (cp)) && altmsg) {
294                 vec[vecp++] = "-dist";
295                 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
296                 if (link(altmsg, distfile) == NOTOK) {
297                         if (errno != EXDEV
298 #ifdef EISREMOTE
299                                         && errno != EISREMOTE
300 #endif /* EISREMOTE */
301                                 )
302                                 adios(distfile, "unable to link %s to",
303                                                 altmsg);
304                         free(distfile);
305                         distfile = getcpy(m_mktemp2(NULL, invo_name,
306                                         NULL, NULL));
307                         {
308                                 int in, out;
309                                 struct stat st;
310
311                                 if ((in = open(altmsg, O_RDONLY)) == NOTOK)
312                                         adios(altmsg, "unable to open");
313                                 fstat(in, &st);
314                                 if ((out = creat(distfile,
315                                                 (int) st.st_mode & 0777))
316                                                 == NOTOK)
317                                         adios(distfile, "unable to write");
318                                 cpydata(in, out, altmsg, distfile);
319                                 close(in);
320                                 close(out);
321                         }
322                 }
323         } else {
324                 distfile = NULL;
325         }
326
327         if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
328                 st.st_mtime = 0;
329                 st.st_dev = 0;
330                 st.st_ino = 0;
331         }
332         if (pushsw)
333                 push();
334
335         status = 0;
336         vec[0] = mhbasename(postproc);
337         closefds(3);
338
339         for (msgnum = 0; msgnum < msgp; msgnum++) {
340                 switch (sendsbr(vec, vecp, msgs[msgnum], &st, 1)) {
341                 case DONE:
342                         done(++status);
343                 case NOTOK:
344                         status++;  /* fall */
345                 case OK:
346                         break;
347                 }
348         }
349
350         context_save();  /* save the context file */
351         done(status);
352         return 1;
353 }