Removed the draft message in favor for a consistent draft folder facility
[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 = r1bindex (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]",
86                                                 invo_name);
87                                         print_help (buf, switches, 1);
88                                         done (1);
89                                 case VERSIONSW:
90                                         print_version(invo_name);
91                                         done (1);
92
93                                 case EDITRSW:
94                                         if (!(ed = *argp++) || *ed == '-')
95                                                 adios (NULL, "missing argument to %s", argp[-2]);
96                                         nedit = 0;
97                                         continue;
98                                 case NEDITSW:
99                                         nedit++;
100                                         continue;
101
102                                 case WHATSW:
103                                         if (!(whatnowproc = *argp++) || *whatnowproc == '-')
104                                                 adios (NULL, "missing argument to %s", argp[-2]);
105                                         nwhat = 0;
106                                         continue;
107                                 case NWHATSW:
108                                         nwhat++;
109                                         continue;
110
111                                 case FORMSW:
112                                         if (!(form = *argp++) || *form == '-')
113                                                 adios (NULL, "missing argument to %s", argp[-2]);
114                                         continue;
115
116                                 case USESW:
117                                         use++;
118                                         continue;
119                                 case NUSESW:
120                                         use = NOUSE;
121                                         continue;
122                         }
123                 }
124                 if (*cp == '+' || *cp == '@') {
125                         if (folder)
126                                 adios (NULL, "only one folder at a time!");
127                         else
128                                 folder = pluspath (cp);
129                 } else {
130                         if (msg)
131                                 adios (NULL, "only one message at a time!");
132                         else
133                                 msg = cp;
134                 }
135         }
136
137         cwd = getcpy (pwd ());
138
139         if (!context_find ("path"))
140                 free (path ("./", TFOLDER));
141
142         if (form && (folder || msg))
143                         adios (NULL, "can't mix forms and folders/msgs");
144
145         if (!use && (folder || msg)) {
146                 /*
147                  * Take a message as the "form" for the new message.
148                  */
149                 if (!msg)
150                         msg = "cur";
151                 if (!folder)
152                         folder = getfolder (1);
153                 maildir = m_maildir (folder);
154
155                 if (chdir (maildir) == NOTOK)
156                         adios (maildir, "unable to change directory to");
157
158                 /* read folder and create message structure */
159                 if (!(mp = folder_read (folder)))
160                         adios (NULL, "unable to read folder %s", folder);
161
162                 /* check for empty folder */
163                 if (mp->nummsg == 0)
164                         adios (NULL, "no messages in %s", folder);
165
166                 /* parse the message range/sequence/name and set SELECTED */
167                 if (!m_convert (mp, msg))
168                         done (1);
169                 seq_setprev (mp);  /* set the previous-sequence */
170
171                 if (mp->numsel > 1)
172                         adios (NULL, "only one message at a time!");
173
174                 if ((in = open (form = getcpy (m_name (mp->lowsel)),
175                                 O_RDONLY)) == NOTOK)
176                         adios (form, "unable to open message");
177         } else
178                 in = open_form(&form, components);
179
180 try_it_again:
181         strncpy (drft, m_draft(use ? (msg?msg:"cur") : "new"), sizeof(drft));
182
183         /*
184          * Check if we have an existing draft
185          */
186         if ((out = open (drft, O_RDONLY)) != NOTOK) {
187                 i = fdcompare (in, out);
188                 close (out);
189
190                 /*
191                  * If we have given -use flag, or if the
192                  * draft is just the same as the components
193                  * file, then no need to ask any questions.
194                  */
195                 if (use || i)
196                         goto edit_it;
197
198                 if (stat (drft, &st) == NOTOK)
199                         adios (drft, "unable to stat");
200                 printf ("Draft \"%s\" exists (%ld bytes).", 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 {
228                 if (use)
229                         adios (drft, "unable to open");
230         }
231
232         if ((out = creat (drft, m_gmprot ())) == NOTOK)
233                 adios (drft, "unable to create");
234         cpydata (in, out, form, drft);
235         close (in);
236         close (out);
237
238 edit_it:
239         context_save ();  /* save the context file */
240
241         if (nwhat)
242                 done (0);
243         what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);
244         done (1);
245         return 1;
246 }