a256f71276cd0edea2ba84c5a67d077f4ddd9e31
[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 = "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:"cur") : "new"), sizeof(drft));
178
179         /*
180         ** Check if we have an existing draft
181         */
182         if ((out = open(drft, O_RDONLY)) != NOTOK) {
183                 i = fdcompare(in, out);
184                 close(out);
185
186                 /*
187                 ** If we have given -use flag, or if the
188                 ** draft is just the same as the components
189                 ** file, then no need to ask any questions.
190                 */
191                 if (use || i)
192                         goto edit_it;
193
194                 if (stat(drft, &st) == NOTOK)
195                         adios(drft, "unable to stat");
196                 printf("Draft \"%s\" exists (%ld bytes).",
197                                 drft, (long) st.st_size);
198                 for (i = LISTDSW; i != YESW;) {
199                         if (!(argp = getans("\nDisposition? ", aqrunl)))
200                                 done(1);
201                         switch (i = smatch(*argp, aqrunl)) {
202                                 case NOSW:
203                                         done(0);
204                                 case NEWSW:
205                                         use = NOUSE;
206                                         goto try_it_again;
207                                 case YESW:
208                                         break;
209                                 case USELSW:
210                                         use++;
211                                         goto edit_it;
212                                 case LISTDSW:
213                                         showfile(++argp, drft);
214                                         break;
215                                 case REFILSW:
216                                         if (refile(++argp, drft) == 0)
217                                                 i = YESW;
218                                         break;
219                                 default:
220                                         advise(NULL, "say what?");
221                                         break;
222                         }
223                 }
224         } else if (use) {
225                 adios(drft, "unable to open");
226         }
227
228         if ((out = creat(drft, m_gmprot())) == NOTOK)
229                 adios(drft, "unable to create");
230         cpydata(in, out, form, drft);
231         close(in);
232         close(out);
233
234 edit_it:
235         context_save();  /* save the context file */
236
237         if (nwhat)
238                 done(0);
239         what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);
240         done(1);
241         return 1;
242 }