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