Removed the -width switch from send, spost and repl.
[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 PUSHSW  8
33         { "push", 0 },
34 #define NPUSHSW  9
35         { "nopush", 0 },
36 #define UNIQSW  10
37         { "unique", -6 },
38 #define NUNIQSW  11
39         { "nounique", -8 },
40 #define VERBSW  12
41         { "verbose", 0 },
42 #define NVERBSW  13
43         { "noverbose", 0 },
44 #define WATCSW  14
45         { "watch", 0 },
46 #define NWATCSW  15
47         { "nowatch", 0 },
48 #define VERSIONSW  16
49         { "version", 0 },
50 #define HELPSW  17
51         { "help", 0 },
52         { NULL, 0 }
53 };
54
55 extern int debugsw;  /* from sendsbr.c */
56 extern int forwsw;
57 extern int inplace;
58 extern int pushsw;
59 extern int unique;
60 extern int verbsw;
61
62 extern char *altmsg;  /*  .. */
63 extern char *annotext;
64 extern char *distfile;
65
66
67 int
68 main(int argc, char **argv)
69 {
70         int msgp = 0, distsw = 0, vecp = 1;
71         int msgnum, status;
72         char *cp, *maildir = NULL;
73         char buf[BUFSIZ], **ap, **argp, **arguments;
74         char *msgs[MAXARGS], *vec[MAXARGS];
75         struct msgs *mp;
76         struct stat st;
77
78 #ifdef LOCALE
79         setlocale(LC_ALL, "");
80 #endif
81         invo_name = mhbasename(argv[0]);
82
83         /* read user profile/context */
84         context_read();
85
86         arguments = getarguments(invo_name, argc, argv, 1);
87         argp = arguments;
88
89         vec[vecp++] = "-library";
90         vec[vecp++] = getcpy(toabsdir("+"));
91
92         while ((cp = *argp++)) {
93                 if (*cp == '-') {
94                         switch (smatch(++cp, switches)) {
95                         case AMBIGSW:
96                                 ambigsw(cp, switches);
97                                 done(1);
98                         case UNKWNSW:
99                                 adios(NULL, "-%s unknown\n", cp);
100
101                         case HELPSW:
102                                 snprintf(buf, sizeof(buf),
103                                                 "%s [file] [switches]",
104                                                 invo_name);
105                                 print_help(buf, switches, 1);
106                                 done(1);
107                         case VERSIONSW:
108                                 print_version(invo_name);
109                                 done(1);
110
111                         case PUSHSW:
112                                 pushsw++;
113                                 continue;
114                         case NPUSHSW:
115                                 pushsw = 0;
116                                 continue;
117
118                         case UNIQSW:
119                                 unique++;
120                                 continue;
121                         case NUNIQSW:
122                                 unique = 0;
123                                 continue;
124
125                         case FORWSW:
126                                 forwsw++;
127                                 continue;
128                         case NFORWSW:
129                                 forwsw = 0;
130                                 continue;
131
132                         case VERBSW:
133                                 verbsw++;
134                                 vec[vecp++] = --cp;
135                                 continue;
136                         case NVERBSW:
137                                 verbsw = 0;
138                                 vec[vecp++] = --cp;
139                                 continue;
140
141                         case DEBUGSW:
142                                 debugsw++;  /* fall */
143                         case NFILTSW:
144                         case FRMTSW:
145                         case NFRMTSW:
146                         case WATCSW:
147                         case NWATCSW:
148                                 vec[vecp++] = --cp;
149                                 continue;
150
151                         case ALIASW:
152                         case FILTSW:
153                                 vec[vecp++] = --cp;
154                                 if (!(cp = *argp++) || *cp == '-')
155                                         adios(NULL, "missing argument to %s",
156                                                         argp[-2]);
157                                 vec[vecp++] = cp;
158                                 continue;
159
160                         }
161                 } else {
162                         msgs[msgp++] = cp;
163                 }
164         }
165
166         /*
167         ** check for "Aliasfile:" profile entry
168         */
169         if ((cp = context_find("Aliasfile"))) {
170                 char *dp = NULL;
171
172                 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap;
173                                 ap++) {
174                         vec[vecp++] = "-alias";
175                         vec[vecp++] = *ap;
176                 }
177         }
178
179         if (!msgp)
180                 msgs[msgp++] = seq_cur;
181         maildir = toabsdir(draftfolder);
182
183         if (chdir(maildir) == NOTOK)
184                 adios(maildir, "unable to change directory to");
185
186         /* read folder and create message structure */
187         if (!(mp = folder_read(draftfolder)))
188                 adios(NULL, "unable to read draft folder %s", draftfolder);
189
190         /* check for empty folder */
191         if (mp->nummsg == 0)
192                 adios(NULL, "no messages in draft folder %s", draftfolder);
193
194         /* parse all the message ranges/sequences and set SELECTED */
195         for (msgnum = 0; msgnum < msgp; msgnum++)
196                 if (!m_convert(mp, msgs[msgnum]))
197                         done(1);
198         seq_setprev(mp);  /* set the previous-sequence */
199
200         for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
201                 if (is_selected(mp, msgnum)) {
202                         msgs[msgp++] = getcpy(m_name(msgnum));
203                         unset_exists(mp, msgnum);
204                 }
205         }
206
207         mp->msgflags |= SEQMOD;
208         seq_save(mp);
209
210         if (!(cp = getenv("SIGNATURE")) || !*cp)
211                 if ((cp = context_find("signature")) && *cp)
212                         m_putenv("SIGNATURE", cp);
213
214         for (msgnum = 0; msgnum < msgp; msgnum++)
215                 if (stat(msgs[msgnum], &st) == NOTOK)
216                         adios(msgs[msgnum], "unable to stat draft file");
217
218         if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
219                 annotext = NULL;
220         if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
221                 inplace = atoi(cp);
222         if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
223                 altmsg = NULL;  /* used by dist interface - see below */
224
225         if ((cp = getenv("mhdist")) && *cp && (distsw = atoi (cp)) && altmsg) {
226                 vec[vecp++] = "-dist";
227                 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
228                 if (link(altmsg, distfile) == NOTOK) {
229                         if (errno != EXDEV
230 #ifdef EISREMOTE
231                                         && errno != EISREMOTE
232 #endif /* EISREMOTE */
233                                 )
234                                 adios(distfile, "unable to link %s to",
235                                                 altmsg);
236                         free(distfile);
237                         distfile = getcpy(m_mktemp2(NULL, invo_name,
238                                         NULL, NULL));
239                         {
240                                 int in, out;
241                                 struct stat st;
242
243                                 if ((in = open(altmsg, O_RDONLY)) == NOTOK)
244                                         adios(altmsg, "unable to open");
245                                 fstat(in, &st);
246                                 if ((out = creat(distfile,
247                                                 (int) st.st_mode & 0777))
248                                                 == NOTOK)
249                                         adios(distfile, "unable to write");
250                                 cpydata(in, out, altmsg, distfile);
251                                 close(in);
252                                 close(out);
253                         }
254                 }
255         } else {
256                 distfile = NULL;
257         }
258
259         if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
260                 st.st_mtime = 0;
261                 st.st_dev = 0;
262                 st.st_ino = 0;
263         }
264         if (pushsw)
265                 push();
266
267         status = 0;
268         vec[0] = mhbasename(postproc);
269         closefds(3);
270
271         for (msgnum = 0; msgnum < msgp; msgnum++) {
272                 switch (sendsbr(vec, vecp, msgs[msgnum], &st, 1)) {
273                 case DONE:
274                         done(++status);
275                 case NOTOK:
276                         status++;  /* fall */
277                 case OK:
278                         break;
279                 }
280         }
281
282         context_save();  /* save the context file */
283         done(status);
284         return 1;
285 }