Renamed all standard sequences (e.g. cur->c) and made them globally changeable
[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 = NULL;  /* 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(toabsdir("+"));
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",
218                                                         argp[-2]);
219                                 vec[vecp++] = cp;
220                                 continue;
221
222                         case ATTACHSW:
223                                 if (!(attach = *argp++) || *attach == '-')
224                                         adios(NULL, "missing argument to %s",
225                                                         argp[-2]);
226                                 continue;
227
228                         case ATTACHFORMATSW:
229                                 if (! *argp || **argp == '-')
230                                         adios(NULL, "missing argument to %s",
231                                                         argp[-1]);
232                                 else {
233                                         attachformat = atoi(*argp);
234                                         if (attachformat < 0 || attachformat > ATTACHFORMATS - 1) {
235                                                 advise(NULL, "unsupported attachformat %d", 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 (!msgp)
261                 msgs[msgp++] = seq_cur;
262         maildir = toabsdir(draftfolder);
263
264         if (chdir(maildir) == NOTOK)
265                 adios(maildir, "unable to change directory to");
266
267         /* read folder and create message structure */
268         if (!(mp = folder_read(draftfolder)))
269                 adios(NULL, "unable to read draft folder %s", draftfolder);
270
271         /* check for empty folder */
272         if (mp->nummsg == 0)
273                 adios(NULL, "no messages in draft folder %s", draftfolder);
274
275         /* parse all the message ranges/sequences and set SELECTED */
276         for (msgnum = 0; msgnum < msgp; msgnum++)
277                 if (!m_convert(mp, msgs[msgnum]))
278                         done(1);
279         seq_setprev(mp);  /* set the previous-sequence */
280
281         for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
282                 if (is_selected(mp, msgnum)) {
283                         msgs[msgp++] = getcpy(m_name(msgnum));
284                         unset_exists(mp, msgnum);
285                 }
286         }
287
288         mp->msgflags |= SEQMOD;
289         seq_save(mp);
290
291         if ((cp = getenv("SIGNATURE")) == NULL || *cp == 0)
292                 if ((cp = context_find("signature")) && *cp)
293                         m_putenv("SIGNATURE", cp);
294 #ifdef UCI
295                 else {
296                         snprintf(buf, sizeof(buf), "%s/.signature", mypath);
297                         if ((fp = fopen(buf, "r")) != NULL &&
298                                         fgets(buf, sizeof buf, fp) != NULL) {
299                                         fclose(fp);
300                                         if (cp = strchr(buf, '\n'))
301                                                 *cp = 0;
302                                         m_putenv("SIGNATURE", buf);
303                         }
304                 }
305 #endif /* UCI */
306
307         for (msgnum = 0; msgnum < msgp; msgnum++)
308                 if (stat(msgs[msgnum], &st) == NOTOK)
309                         adios(msgs[msgnum], "unable to stat draft file");
310
311         if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
312                 annotext = NULL;
313         if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
314                 inplace = atoi(cp);
315         if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
316                 altmsg = NULL;  /* used by dist interface - see below */
317
318         if ((cp = getenv("mhdist")) && *cp && (distsw = atoi (cp)) && altmsg) {
319                 vec[vecp++] = "-dist";
320                 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
321                 if (link(altmsg, distfile) == NOTOK) {
322                         if (errno != EXDEV
323 #ifdef EISREMOTE
324                                         && errno != EISREMOTE
325 #endif /* EISREMOTE */
326                                 )
327                                 adios(distfile, "unable to link %s to",
328                                                 altmsg);
329                         free(distfile);
330                         distfile = getcpy(m_mktemp2(NULL, invo_name,
331                                         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,
340                                                 (int) st.st_mode & 0777))
341                                                 == NOTOK)
342                                         adios(distfile, "unable to write");
343                                 cpydata(in, out, altmsg, distfile);
344                                 close(in);
345                                 close(out);
346                         }
347                 }
348         } else {
349                 distfile = NULL;
350         }
351
352         if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
353                 st.st_mtime = 0;
354                 st.st_dev = 0;
355                 st.st_ino = 0;
356         }
357         if (pushsw)
358                 push();
359
360         status = 0;
361         vec[0] = mhbasename(postproc);
362         closefds(3);
363
364         for (msgnum = 0; msgnum < msgp; msgnum++) {
365                 switch (sendsbr(vec, vecp, msgs[msgnum], &st, 1, attach,
366                                 attachformat)) {
367                 case DONE:
368                         done(++status);
369                 case NOTOK:
370                         status++;  /* fall */
371                 case OK:
372                         break;
373                 }
374         }
375
376         context_save();  /* save the context file */
377         done(status);
378         return 1;
379 }