Reformated comments and long lines
[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),
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++) || *attach == '-')
223                                                 adios (NULL, "missing argument to %s", argp[-2]);
224                                         continue;
225
226                                 case ATTACHFORMATSW:
227                                         if (! *argp || **argp == '-')
228                                                 adios (NULL, "missing argument to %s", argp[-1]);
229                                         else {
230                                                 attachformat = atoi (*argp);
231                                                 if (attachformat < 0 ||
232                                                         attachformat > ATTACHFORMATS - 1) {
233                                                         advise (NULL, "unsupported attachformat %d",
234                                                                         attachformat);
235                                                         continue;
236                                                 }
237                                         }
238                                         ++argp;
239                                         continue;
240                         }
241                 } else {
242                         msgs[msgp++] = cp;
243                 }
244         }
245
246         /*
247         ** check for "Aliasfile:" profile entry
248         */
249         if ((cp = context_find ("Aliasfile"))) {
250                 char *dp = NULL;
251
252                 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap;
253                                 ap++) {
254                         vec[vecp++] = "-alias";
255                         vec[vecp++] = *ap;
256                 }
257         }
258
259         if (!context_find ("path"))
260                 free (path ("./", TFOLDER));
261
262         if (!msgp)
263                 msgs[msgp++] = "cur";
264         maildir = m_maildir (draftfolder);
265
266         if (chdir (maildir) == NOTOK)
267                 adios (maildir, "unable to change directory to");
268
269         /* read folder and create message structure */
270         if (!(mp = folder_read (draftfolder)))
271                 adios (NULL, "unable to read draft folder %s", draftfolder);
272
273         /* check for empty folder */
274         if (mp->nummsg == 0)
275                 adios (NULL, "no messages in draft folder %s", draftfolder);
276
277         /* parse all the message ranges/sequences and set SELECTED */
278         for (msgnum = 0; msgnum < msgp; msgnum++)
279                 if (!m_convert (mp, msgs[msgnum]))
280                         done (1);
281         seq_setprev (mp);  /* set the previous-sequence */
282
283         for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
284                 if (is_selected (mp, msgnum)) {
285                         msgs[msgp++] = getcpy (m_name (msgnum));
286                         unset_exists (mp, msgnum);
287                 }
288         }
289
290         mp->msgflags |= SEQMOD;
291         seq_save (mp);
292
293         if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
294                 if ((cp = context_find ("signature")) && *cp)
295                         m_putenv ("SIGNATURE", cp);
296 #ifdef UCI
297                 else {
298                         snprintf (buf, sizeof(buf), "%s/.signature", mypath);
299                         if ((fp = fopen (buf, "r")) != NULL &&
300                                         fgets(buf, sizeof buf, fp) != NULL) {
301                                         fclose (fp);
302                                         if (cp = strchr (buf, '\n'))
303                                                 *cp = 0;
304                                         m_putenv ("SIGNATURE", buf);
305                         }
306                 }
307 #endif /* UCI */
308
309         for (msgnum = 0; msgnum < msgp; msgnum++)
310                 if (stat (msgs[msgnum], &st) == NOTOK)
311                         adios (msgs[msgnum], "unable to stat draft file");
312
313         if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
314                 annotext = NULL;
315         if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
316                 inplace = atoi (cp);
317         if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
318                 altmsg = NULL;  /* used by dist interface - see below */
319
320         if ((cp = getenv ("mhdist"))
321                         && *cp
322                         && (distsw = atoi (cp))
323                         && altmsg) {
324                 vec[vecp++] = "-dist";
325                 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
326                 if (link (altmsg, distfile) == NOTOK) {
327                         if (errno != EXDEV
328 #ifdef EISREMOTE
329                                         && errno != EISREMOTE
330 #endif /* EISREMOTE */
331                                 )
332                                 adios (distfile, "unable to link %s to",
333                                                 altmsg);
334                         free (distfile);
335                         distfile = getcpy (m_mktemp2(NULL, invo_name,
336                                         NULL, NULL));
337                         {
338                                 int in, out;
339                                 struct stat st;
340
341                                 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
342                                         adios (altmsg, "unable to open");
343                                 fstat(in, &st);
344                                 if ((out = creat (distfile,
345                                                 (int) st.st_mode & 0777))
346                                                 == NOTOK)
347                                         adios (distfile, "unable to write");
348                                 cpydata (in, out, altmsg, distfile);
349                                 close (in);
350                                 close (out);
351                         }
352                 }
353         } else {
354                 distfile = NULL;
355         }
356
357         if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
358                 st.st_mtime = 0;
359                 st.st_dev = 0;
360                 st.st_ino = 0;
361         }
362         if (pushsw)
363                 push ();
364
365         status = 0;
366         vec[0] = r1bindex (postproc, '/');
367         closefds (3);
368
369         for (msgnum = 0; msgnum < msgp; msgnum++) {
370                 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
371                                 attachformat)) {
372                         case DONE:
373                                 done (++status);
374                         case NOTOK:
375                                 status++;  /* fall */
376                         case OK:
377                                 break;
378                 }
379         }
380
381         context_save ();  /* save the context file */
382         done (status);
383         return 1;
384 }