Commit c4ad70fc c4ad70fcc49fe4d186b97d18de5044c1b2791853 by Sergey Poznyakoff

Handle draft file. Added --use option.

1 parent 4a489905
Showing 1 changed file with 50 additions and 9 deletions
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
18 /* MH reply command */ 18 /* MH reply command */
19 19
20 #include <mh.h> 20 #include <mh.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
21 23
22 const char *argp_program_version = "reply (" PACKAGE_STRING ")"; 24 const char *argp_program_version = "reply (" PACKAGE_STRING ")";
23 static char doc[] = "GNU MH reply"; 25 static char doc[] = "GNU MH reply";
...@@ -62,6 +64,7 @@ static struct argp_option options[] = { ...@@ -62,6 +64,7 @@ static struct argp_option options[] = {
62 {"width", 'w', N_("NUMBER"), 0, N_("Set output width")}, 64 {"width", 'w', N_("NUMBER"), 0, N_("Set output width")},
63 {"whatnowproc", ARG_WHATNOWPROC, N_("PROG"), 0, 65 {"whatnowproc", ARG_WHATNOWPROC, N_("PROG"), 0,
64 N_("Set the replacement for whatnow program")}, 66 N_("Set the replacement for whatnow program")},
67 {"use", 'u', NULL, 0, N_("Use draft file preserved after the last session") },
65 { N_("\nUse -help switch to obtain the list of traditional MH options. "), 0, 0, OPTION_DOC, "" }, 68 { N_("\nUse -help switch to obtain the list of traditional MH options. "), 0, 0, OPTION_DOC, "" },
66 { 0 } 69 { 0 }
67 }; 70 };
...@@ -108,6 +111,7 @@ static mh_msgset_t msgset; ...@@ -108,6 +111,7 @@ static mh_msgset_t msgset;
108 static mailbox_t mbox; 111 static mailbox_t mbox;
109 static int build_only = 0; /* --build flag */ 112 static int build_only = 0; /* --build flag */
110 static int query_mode = 0; /* --query flag */ 113 static int query_mode = 0; /* --query flag */
114 static int use_draft = 0; /* --use flag */
111 115
112 static int 116 static int
113 decode_cc_flag (const char *opt, const char *arg) 117 decode_cc_flag (const char *opt, const char *arg)
...@@ -164,6 +168,10 @@ opt_handler (int key, char *arg, void *unused) ...@@ -164,6 +168,10 @@ opt_handler (int key, char *arg, void *unused)
164 wh_env.draftmessage = arg; 168 wh_env.draftmessage = arg;
165 break; 169 break;
166 170
171 case 'u':
172 use_draft++;
173 break;
174
167 case 'w': 175 case 'w':
168 width = strtoul (arg, NULL, 0); 176 width = strtoul (arg, NULL, 0);
169 if (!width) 177 if (!width)
...@@ -204,19 +212,38 @@ make_draft () ...@@ -204,19 +212,38 @@ make_draft ()
204 { 212 {
205 int rc; 213 int rc;
206 message_t msg; 214 message_t msg;
207 FILE *fp; 215 int disp = DISP_REPLACE;
208 char buffer[1024]; 216 struct stat st;
209 #define bufsize sizeof(buffer)
210 217
211 /* FIXME: first check if the draft exists */ 218 /* First check if the draft exists */
212 fp = fopen (wh_env.file, "w+"); 219 if (stat (wh_env.draftfile, &st) == 0)
213 if (!fp)
214 { 220 {
215 mh_error (_("cannot open draft file %s: %s"), 221 if (use_draft)
216 wh_env.file, strerror (errno)); 222 disp = DISP_USE;
217 exit (1); 223 else
224 {
225 printf (_("Draft \"%s\" exists (%lu bytes).\n"),
226 wh_env.draftfile, (unsigned long) st.st_size);
227 disp = mh_disposition (wh_env.draftfile);
228 }
229 }
230
231 switch (disp)
232 {
233 case DISP_QUIT:
234 exit (0);
235
236 case DISP_USE:
237 unlink (wh_env.file);
238 rename (wh_env.draftfile, wh_env.file);
239 break;
240
241 case DISP_REPLACE:
242 unlink (wh_env.draftfile);
243 break;
218 } 244 }
219 245
246
220 rc = mailbox_get_message (mbox, msgset.list[0], &msg); 247 rc = mailbox_get_message (mbox, msgset.list[0], &msg);
221 if (rc) 248 if (rc)
222 { 249 {
...@@ -226,9 +253,22 @@ make_draft () ...@@ -226,9 +253,22 @@ make_draft ()
226 exit (1); 253 exit (1);
227 } 254 }
228 255
256 if (disp == DISP_REPLACE)
257 {
258 FILE *fp = fopen (wh_env.file, "w+");
259 char buffer[1024];
260 #define bufsize sizeof(buffer)
261
262 if (!fp)
263 {
264 mh_error (_("cannot open draft file %s: %s"),
265 wh_env.file, strerror (errno));
266 exit (1);
267 }
229 mh_format (&format, msg, msgset.list[0], buffer, bufsize); 268 mh_format (&format, msg, msgset.list[0], buffer, bufsize);
230 fprintf (fp, "%s", buffer); 269 fprintf (fp, "%s", buffer);
231 fclose (fp); 270 fclose (fp);
271 }
232 272
233 { 273 {
234 url_t url; 274 url_t url;
...@@ -278,6 +318,7 @@ main (int argc, char **argv) ...@@ -278,6 +318,7 @@ main (int argc, char **argv)
278 } 318 }
279 319
280 wh_env.file = mh_expand_name (wh_env.draftfolder, "reply", 0); 320 wh_env.file = mh_expand_name (wh_env.draftfolder, "reply", 0);
321 wh_env.draftfile = mh_expand_name (wh_env.draftfolder, "draft", 0);
281 322
282 make_draft (); 323 make_draft ();
283 324
......