7eee3f7e9f8c19ac57b7588902e58b4b39a8dda1
[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 static struct swit aqrunl[] = {
36 #define NOSW  0
37         { "quit", 0 },
38 #define YESW  1
39         { "replace", 0 },
40 #define USELSW  2
41         { "use", 0 },
42 #define LISTDSW  3
43         { "list", 0 },
44 #define REFILSW  4
45         { "refile +folder", 0 },
46 #define NEWSW  5
47         { "new", 0 },
48         { NULL, 0 }
49 };
50
51
52 int
53 main(int argc, char **argv)
54 {
55         int use = NOUSE, nedit = 0, nwhat = 0;
56         int i, in, out;
57         char *cp, *cwd, *maildir;
58         char *ed = NULL, *form = NULL;
59         char *folder = NULL, *msg = NULL, buf[BUFSIZ];
60         char drft[BUFSIZ], **argp, **arguments;
61         struct msgs *mp = NULL;
62         struct stat st;
63
64 #ifdef LOCALE
65         setlocale(LC_ALL, "");
66 #endif
67         invo_name = mhbasename(argv[0]);
68
69         /* read user profile/context */
70         context_read();
71
72         arguments = getarguments(invo_name, argc, argv, 1);
73         argp = arguments;
74
75         while ((cp = *argp++)) {
76                 if (*cp == '-') {
77                         switch (smatch(++cp, switches)) {
78                         case AMBIGSW:
79                                 ambigsw(cp, switches);
80                                 done(1);
81                         case UNKWNSW:
82                                 adios(NULL, "-%s unknown", cp);
83
84                         case HELPSW:
85                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
86                                 print_help(buf, switches, 1);
87                                 done(1);
88                         case VERSIONSW:
89                                 print_version(invo_name);
90                                 done(1);
91
92                         case EDITRSW:
93                                 if (!(ed = *argp++) || *ed == '-')
94                                         adios(NULL, "missing argument to %s", argp[-2]);
95                                 nedit = 0;
96                                 continue;
97                         case NEDITSW:
98                                 nedit++;
99                                 continue;
100
101                         case WHATSW:
102                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
103                                         adios(NULL, "missing argument to %s", argp[-2]);
104                                 nwhat = 0;
105                                 continue;
106                         case NWHATSW:
107                                 nwhat++;
108                                 continue;
109
110                         case FORMSW:
111                                 if (!(form = *argp++) || *form == '-')
112                                         adios(NULL, "missing argument to %s", argp[-2]);
113                                 continue;
114
115                         case USESW:
116                                 use++;
117                                 continue;
118                         case NUSESW:
119                                 use = NOUSE;
120                                 continue;
121                         }
122                 }
123                 if (*cp == '+' || *cp == '@') {
124                         if (folder)
125                                 adios(NULL, "only one folder at a time!");
126                         else
127                                 folder = getcpy(expandfol(cp));
128                 } else {
129                         if (msg)
130                                 adios(NULL, "only one message at a time!");
131                         else
132                                 msg = cp;
133                 }
134         }
135
136         cwd = getcpy(pwd());
137
138         if (form && (folder || msg))
139                 adios(NULL, "can't mix forms and folders/msgs");
140
141         if (!use && (folder || msg)) {
142                 /*
143                 ** Take a message as the "form" for the new message.
144                 */
145                 if (!msg)
146                         msg = seq_cur;
147                 if (!folder)
148                         folder = getcurfol();
149                 maildir = toabsdir(folder);
150
151                 if (chdir(maildir) == NOTOK)
152                         adios(maildir, "unable to change directory to");
153
154                 /* read folder and create message structure */
155                 if (!(mp = folder_read(folder)))
156                         adios(NULL, "unable to read folder %s", folder);
157
158                 /* check for empty folder */
159                 if (mp->nummsg == 0)
160                         adios(NULL, "no messages in %s", folder);
161
162                 /* parse the message range/sequence/name and set SELECTED */
163                 if (!m_convert(mp, msg))
164                         done(1);
165                 seq_setprev(mp);  /* set the previous-sequence */
166
167                 if (mp->numsel > 1)
168                         adios(NULL, "only one message at a time!");
169
170                 if ((in = open(form = getcpy(m_name(mp->lowsel)),
171                                 O_RDONLY)) == NOTOK)
172                         adios(form, "unable to open message");
173         } else
174                 in = open_form(&form, components);
175
176 try_it_again:
177         strncpy(drft, m_draft(use ? (msg?msg:seq_cur) : seq_beyond),
178                         sizeof(drft));
179
180         /*
181         ** Check if we have an existing draft
182         */
183         if ((out = open(drft, O_RDONLY)) != NOTOK) {
184                 i = fdcompare(in, out);
185                 close(out);
186
187                 /*
188                 ** If we have given -use flag, or if the
189                 ** draft is just the same as the components
190                 ** file, then no need to ask any questions.
191                 */
192                 if (use || i)
193                         goto edit_it;
194
195                 if (stat(drft, &st) == NOTOK)
196                         adios(drft, "unable to stat");
197                 printf("Draft \"%s\" exists (%ld bytes).",
198                                 drft, (long) st.st_size);
199                 for (i = LISTDSW; i != YESW;) {
200                         if (!(argp = getans("\nDisposition? ", aqrunl)))
201                                 done(1);
202                         switch (i = smatch(*argp, aqrunl)) {
203                         case NOSW:
204                                 done(0);
205                         case NEWSW:
206                                 use = NOUSE;
207                                 goto try_it_again;
208                         case YESW:
209                                 break;
210                         case USELSW:
211                                 use++;
212                                 goto edit_it;
213                         case LISTDSW:
214                                 showfile(++argp, drft);
215                                 break;
216                         case REFILSW:
217                                 if (refile(++argp, drft) == 0)
218                                         i = YESW;
219                                 break;
220                         default:
221                                 advise(NULL, "say what?");
222                                 break;
223                         }
224                 }
225         } else if (use) {
226                 adios(drft, "unable to open");
227         }
228
229         if ((out = creat(drft, m_gmprot())) == NOTOK)
230                 adios(drft, "unable to create");
231         cpydata(in, out, form, drft);
232         close(in);
233         close(out);
234
235 edit_it:
236         context_save();  /* save the context file */
237
238         if (nwhat)
239                 done(0);
240         what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);
241         done(1);
242         return 1;
243 }