ce76cd856d8bac8789968d07c67fb2f0dd959d71
[mmh] / uip / comp.c
1 /*
2 ** comp.c -- compose a 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 <h/utils.h>
11 #include <fcntl.h>
12
13 static struct swit switches[] = {
14 #define EDITRSW  0
15         { "editor editor", 0 },
16 #define NEDITSW  1
17         { "noedit", 0 },
18 #define FORMSW  2
19         { "form formfile", 0 },
20 #define USESW  3
21         { "use", 0 },
22 #define NUSESW  4
23         { "nouse", 0 },
24 #define WHATSW  5
25         { "whatnowproc program", 0 },
26 #define NWHATSW  6
27         { "nowhatnowproc", 0 },
28 #define VERSIONSW  7
29         { "version", 0 },
30 #define HELPSW  8
31         { "help", 0 },
32         { NULL, 0 }
33 };
34
35
36 int
37 main(int argc, char **argv)
38 {
39         int use = NOUSE, nedit = 0, nwhat = 0;
40         int in, out;
41         char *cp, *cwd, *maildir;
42         char *ed = NULL, *form = NULL;
43         char *folder = NULL, *msg = NULL, buf[BUFSIZ];
44         char drft[BUFSIZ], **argp, **arguments;
45         struct msgs *mp = NULL;
46
47 #ifdef LOCALE
48         setlocale(LC_ALL, "");
49 #endif
50         invo_name = mhbasename(argv[0]);
51
52         /* read user profile/context */
53         context_read();
54
55         arguments = getarguments(invo_name, argc, argv, 1);
56         argp = arguments;
57
58         while ((cp = *argp++)) {
59                 if (*cp == '-') {
60                         switch (smatch(++cp, switches)) {
61                         case AMBIGSW:
62                                 ambigsw(cp, switches);
63                                 done(1);
64                         case UNKWNSW:
65                                 adios(NULL, "-%s unknown", cp);
66
67                         case HELPSW:
68                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
69                                 print_help(buf, switches, 1);
70                                 done(1);
71                         case VERSIONSW:
72                                 print_version(invo_name);
73                                 done(1);
74
75                         case EDITRSW:
76                                 if (!(ed = *argp++) || *ed == '-')
77                                         adios(NULL, "missing argument to %s", argp[-2]);
78                                 nedit = 0;
79                                 continue;
80                         case NEDITSW:
81                                 nedit++;
82                                 continue;
83
84                         case WHATSW:
85                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
86                                         adios(NULL, "missing argument to %s", argp[-2]);
87                                 nwhat = 0;
88                                 continue;
89                         case NWHATSW:
90                                 nwhat++;
91                                 continue;
92
93                         case FORMSW:
94                                 if (!(form = *argp++) || *form == '-')
95                                         adios(NULL, "missing argument to %s", argp[-2]);
96                                 continue;
97
98                         case USESW:
99                                 use++;
100                                 continue;
101                         case NUSESW:
102                                 use = NOUSE;
103                                 continue;
104                         }
105                 }
106                 if (*cp == '+' || *cp == '@') {
107                         if (folder)
108                                 adios(NULL, "only one folder at a time!");
109                         else
110                                 folder = getcpy(expandfol(cp));
111                 } else {
112                         if (msg)
113                                 adios(NULL, "only one message at a time!");
114                         else
115                                 msg = cp;
116                 }
117         }
118
119         cwd = getcpy(pwd());
120
121         if (form && (folder || msg))
122                 adios(NULL, "can't mix forms and folders/msgs");
123
124         if (use && folder)
125                 adios(NULL, "can't mix -use and +folder");
126
127         if (use) {
128                 /* Don't copy; the draft shall get removed in the end. */
129                 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
130
131         } else if (folder || msg) {
132                 /* Take a message as the "form" for the new message. */
133                 if (!msg)
134                         msg = seq_cur;
135                 if (!folder)
136                         folder = getcurfol();
137                 maildir = toabsdir(folder);
138                 if (chdir(maildir) == NOTOK)
139                         adios(maildir, "unable to change directory to");
140                 /* read folder and create message structure */
141                 if (!(mp = folder_read(folder)))
142                         adios(NULL, "unable to read folder %s", folder);
143                 /* check for empty folder */
144                 if (mp->nummsg == 0)
145                         adios(NULL, "no messages in %s", folder);
146                 /* parse the message range/sequence/name and set SELECTED */
147                 if (!m_convert(mp, msg))
148                         done(1);
149                 seq_setprev(mp);  /* set the previous-sequence */
150                 if (mp->numsel > 1)
151                         adios(NULL, "only one message at a time!");
152                 if ((in = open(form = getcpy(m_name(mp->lowsel)),
153                                 O_RDONLY)) == NOTOK)
154                         adios(form, "unable to open message");
155
156                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
157                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
158                         adios(drft, "unable to create");
159                 }
160                 cpydata(in, out, form, drft);
161                 close(in);
162                 close(out);
163
164         } else {
165                 in = open_form(&form, components);
166
167                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
168                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
169                         adios(drft, "unable to create");
170                 }
171                 cpydata(in, out, form, drft);
172                 close(in);
173                 close(out);
174         }
175
176         context_save();
177
178         if (nwhat) {
179                 done(0);
180         }
181         what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, cwd);
182         done(1);
183         return 1;
184 }