Renamed r1bindex() to mhbasename(), to make its function becomes clear.
[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 = mhbasename(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),
143                                                         "%s [file] [switches]",
144                                                         invo_name);
145                                         print_help(buf, switches, 1);
146                                         done(1);
147                                 case VERSIONSW:
148                                         print_version(invo_name);
149                                         done(1);
150
151                                 case PUSHSW:
152                                         pushsw++;
153                                         continue;
154                                 case NPUSHSW:
155                                         pushsw = 0;
156                                         continue;
157
158                                 case UNIQSW:
159                                         unique++;
160                                         continue;
161                                 case NUNIQSW:
162                                         unique = 0;
163                                         continue;
164
165                                 case FORWSW:
166                                         forwsw++;
167                                         continue;
168                                 case NFORWSW:
169                                         forwsw = 0;
170                                         continue;
171
172                                 case VERBSW:
173                                         verbsw++;
174                                         vec[vecp++] = --cp;
175                                         continue;
176                                 case NVERBSW:
177                                         verbsw = 0;
178                                         vec[vecp++] = --cp;
179                                         continue;
180
181                                 case MIMESW:
182                                         mime++;
183                                         vec[vecp++] = --cp;
184                                         continue;
185                                 case NMIMESW:
186                                         mime = 0;
187                                         vec[vecp++] = --cp;
188                                         continue;
189
190                                 case DEBUGSW:
191                                         debugsw++;  /* fall */
192                                 case NFILTSW:
193                                 case FRMTSW:
194                                 case NFRMTSW:
195                                 case BITSTUFFSW:
196                                 case NBITSTUFFSW:
197                                 case MSGDSW:
198                                 case NMSGDSW:
199                                 case WATCSW:
200                                 case NWATCSW:
201                                 case MAILSW:
202                                 case SAMLSW:
203                                 case SENDSW:
204                                 case SOMLSW:
205                                 case SNOOPSW:
206                                         vec[vecp++] = --cp;
207                                         continue;
208
209                                 case ALIASW:
210                                 case FILTSW:
211                                 case WIDTHSW:
212                                 case CLIESW:
213                                 case SERVSW:
214                                 case PORTSW:
215                                         vec[vecp++] = --cp;
216                                         if (!(cp = *argp++) || *cp == '-')
217                                                 adios(NULL, "missing argument to %s", argp[-2]);
218                                         vec[vecp++] = cp;
219                                         continue;
220
221                                 case ATTACHSW:
222                                         if (!(attach = *argp++) ||
223                                                         *attach == '-')
224                                                 adios(NULL, "missing argument to %s", argp[-2]);
225                                         continue;
226
227                                 case ATTACHFORMATSW:
228                                         if (! *argp || **argp == '-')
229                                                 adios(NULL, "missing argument to %s", argp[-1]);
230                                         else {
231                                                 attachformat = atoi(*argp);
232                                                 if (attachformat < 0 ||
233                                                         attachformat > ATTACHFORMATS - 1) {
234                                                         advise(NULL, "unsupported attachformat %d",
235                                                                         attachformat);
236                                                         continue;
237                                                 }
238                                         }
239                                         ++argp;
240                                         continue;
241                         }
242                 } else {
243                         msgs[msgp++] = cp;
244                 }
245         }
246
247         /*
248         ** check for "Aliasfile:" profile entry
249         */
250         if ((cp = context_find("Aliasfile"))) {
251                 char *dp = NULL;
252
253                 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap;
254                                 ap++) {
255                         vec[vecp++] = "-alias";
256                         vec[vecp++] = *ap;
257                 }
258         }
259
260         if (!context_find("path"))
261                 free(path("./", TFOLDER));
262
263         if (!msgp)
264                 msgs[msgp++] = "cur";
265         maildir = m_maildir(draftfolder);
266
267         if (chdir(maildir) == NOTOK)
268                 adios(maildir, "unable to change directory to");
269
270         /* read folder and create message structure */
271         if (!(mp = folder_read(draftfolder)))
272                 adios(NULL, "unable to read draft folder %s", draftfolder);
273
274         /* check for empty folder */
275         if (mp->nummsg == 0)
276                 adios(NULL, "no messages in draft folder %s", draftfolder);
277
278         /* parse all the message ranges/sequences and set SELECTED */
279         for (msgnum = 0; msgnum < msgp; msgnum++)
280                 if (!m_convert(mp, msgs[msgnum]))
281                         done(1);
282         seq_setprev(mp);  /* set the previous-sequence */
283
284         for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
285                 if (is_selected(mp, msgnum)) {
286                         msgs[msgp++] = getcpy(m_name(msgnum));
287                         unset_exists(mp, msgnum);
288                 }
289         }
290
291         mp->msgflags |= SEQMOD;
292         seq_save(mp);
293
294         if ((cp = getenv("SIGNATURE")) == NULL || *cp == 0)
295                 if ((cp = context_find("signature")) && *cp)
296                         m_putenv("SIGNATURE", cp);
297 #ifdef UCI
298                 else {
299                         snprintf(buf, sizeof(buf), "%s/.signature", mypath);
300                         if ((fp = fopen(buf, "r")) != NULL &&
301                                         fgets(buf, sizeof buf, fp) != NULL) {
302                                         fclose(fp);
303                                         if (cp = strchr(buf, '\n'))
304                                                 *cp = 0;
305                                         m_putenv("SIGNATURE", buf);
306                         }
307                 }
308 #endif /* UCI */
309
310         for (msgnum = 0; msgnum < msgp; msgnum++)
311                 if (stat(msgs[msgnum], &st) == NOTOK)
312                         adios(msgs[msgnum], "unable to stat draft file");
313
314         if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
315                 annotext = NULL;
316         if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
317                 inplace = atoi(cp);
318         if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
319                 altmsg = NULL;  /* used by dist interface - see below */
320
321         if ((cp = getenv("mhdist")) && *cp && (distsw = atoi (cp)) && altmsg) {
322                 vec[vecp++] = "-dist";
323                 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
324                 if (link(altmsg, distfile) == NOTOK) {
325                         if (errno != EXDEV
326 #ifdef EISREMOTE
327                                         && errno != EISREMOTE
328 #endif /* EISREMOTE */
329                                 )
330                                 adios(distfile, "unable to link %s to",
331                                                 altmsg);
332                         free(distfile);
333                         distfile = getcpy(m_mktemp2(NULL, invo_name,
334                                         NULL, NULL));
335                         {
336                                 int in, out;
337                                 struct stat st;
338
339                                 if ((in = open(altmsg, O_RDONLY)) == NOTOK)
340                                         adios(altmsg, "unable to open");
341                                 fstat(in, &st);
342                                 if ((out = creat(distfile,
343                                                 (int) st.st_mode & 0777))
344                                                 == NOTOK)
345                                         adios(distfile, "unable to write");
346                                 cpydata(in, out, altmsg, distfile);
347                                 close(in);
348                                 close(out);
349                         }
350                 }
351         } else {
352                 distfile = NULL;
353         }
354
355         if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
356                 st.st_mtime = 0;
357                 st.st_dev = 0;
358                 st.st_ino = 0;
359         }
360         if (pushsw)
361                 push();
362
363         status = 0;
364         vec[0] = mhbasename(postproc);
365         closefds(3);
366
367         for (msgnum = 0; msgnum < msgp; msgnum++) {
368                 switch (sendsbr(vec, vecp, msgs[msgnum], &st, 1, attach,
369                                 attachformat)) {
370                         case DONE:
371                                 done(++status);
372                         case NOTOK:
373                                 status++;  /* fall */
374                         case OK:
375                                 break;
376                 }
377         }
378
379         context_save();  /* save the context file */
380         done(status);
381         return 1;
382 }