Introduced FDEF and FCUR for speaking arguments to getfolder().
[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 = pluspath(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 (!context_find("path"))
139                 free(path("./", TFOLDER));
140
141         if (form && (folder || msg))
142                         adios(NULL, "can't mix forms and folders/msgs");
143
144         if (!use && (folder || msg)) {
145                 /*
146                 ** Take a message as the "form" for the new message.
147                 */
148                 if (!msg)
149                         msg = "cur";
150                 if (!folder)
151                         folder = getfolder(FCUR);
152                 maildir = m_maildir(folder);
153
154                 if (chdir(maildir) == NOTOK)
155                         adios(maildir, "unable to change directory to");
156
157                 /* read folder and create message structure */
158                 if (!(mp = folder_read(folder)))
159                         adios(NULL, "unable to read folder %s", folder);
160
161                 /* check for empty folder */
162                 if (mp->nummsg == 0)
163                         adios(NULL, "no messages in %s", folder);
164
165                 /* parse the message range/sequence/name and set SELECTED */
166                 if (!m_convert(mp, msg))
167                         done(1);
168                 seq_setprev(mp);  /* set the previous-sequence */
169
170                 if (mp->numsel > 1)
171                         adios(NULL, "only one message at a time!");
172
173                 if ((in = open(form = getcpy(m_name(mp->lowsel)),
174                                 O_RDONLY)) == NOTOK)
175                         adios(form, "unable to open message");
176         } else
177                 in = open_form(&form, components);
178
179 try_it_again:
180         strncpy(drft, m_draft(use ? (msg?msg:"cur") : "new"), sizeof(drft));
181
182         /*
183         ** Check if we have an existing draft
184         */
185         if ((out = open(drft, O_RDONLY)) != NOTOK) {
186                 i = fdcompare(in, out);
187                 close(out);
188
189                 /*
190                 ** If we have given -use flag, or if the
191                 ** draft is just the same as the components
192                 ** file, then no need to ask any questions.
193                 */
194                 if (use || i)
195                         goto edit_it;
196
197                 if (stat(drft, &st) == NOTOK)
198                         adios(drft, "unable to stat");
199                 printf("Draft \"%s\" exists (%ld bytes).",
200                                 drft, (long) st.st_size);
201                 for (i = LISTDSW; i != YESW;) {
202                         if (!(argp = getans("\nDisposition? ", aqrunl)))
203                                 done(1);
204                         switch (i = smatch(*argp, aqrunl)) {
205                                 case NOSW:
206                                         done(0);
207                                 case NEWSW:
208                                         use = NOUSE;
209                                         goto try_it_again;
210                                 case YESW:
211                                         break;
212                                 case USELSW:
213                                         use++;
214                                         goto edit_it;
215                                 case LISTDSW:
216                                         showfile(++argp, drft);
217                                         break;
218                                 case REFILSW:
219                                         if (refile(++argp, drft) == 0)
220                                                 i = YESW;
221                                         break;
222                                 default:
223                                         advise(NULL, "say what?");
224                                         break;
225                         }
226                 }
227         } else if (use) {
228                 adios(drft, "unable to open");
229         }
230
231         if ((out = creat(drft, m_gmprot())) == NOTOK)
232                 adios(drft, "unable to create");
233         cpydata(in, out, form, drft);
234         close(in);
235         close(out);
236
237 edit_it:
238         context_save();  /* save the context file */
239
240         if (nwhat)
241                 done(0);
242         what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);
243         done(1);
244         return 1;
245 }