1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2003-2021 Free Software Foundation, Inc.
3 
4    GNU Mailutils is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Mailutils is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 /* Functions shared between comp and forw utilities */
18 
19 #include <mh.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 
23 static char *default_format_str =
24 "To:\n"
25 "cc:\n"
26 "Subject:\n"
27 "--------\n";
28 
29 void
mh_comp_draft(const char * formfile,const char * draftfile)30 mh_comp_draft (const char *formfile, const char *draftfile)
31 {
32   char *s;
33 
34   if (mh_find_file (formfile, &s) == 0)
35     {
36       if (mh_file_copy (s, draftfile))
37 	exit (1);
38       free (s);
39     }
40   else
41     {
42       /* I doubt if we need that: */
43       int rc;
44       mu_stream_t stream;
45 
46       if ((rc = mu_file_stream_create (&stream,
47 				       draftfile,
48 				       MU_STREAM_WRITE|MU_STREAM_CREAT)))
49 	{
50 	  mu_error (_("cannot open output file \"%s\": %s"),
51 		    draftfile, mu_strerror (rc));
52 	  exit (1);
53 	}
54 
55       rc = mu_stream_write (stream,
56 			    default_format_str,
57 			    strlen (default_format_str), NULL);
58       mu_stream_close (stream);
59       mu_stream_destroy (&stream);
60 
61       if (rc)
62 	{
63 	  mu_error (_("error writing to \"%s\": %s"),
64 		    draftfile, mu_strerror (rc));
65 	  exit (1);
66 	}
67     }
68 }
69 
70 int
check_draft_disposition(struct mh_whatnow_env * wh,int use_draft)71 check_draft_disposition (struct mh_whatnow_env *wh, int use_draft)
72 {
73   struct stat st;
74   int disp = DISP_REPLACE;
75 
76   if (wh->nowhatnowproc)
77     return disp;
78 
79   /* First check if the draft exists */
80   if (stat (wh->draftfile, &st) == 0)
81     {
82       if (use_draft)
83 	disp = DISP_USE;
84       else
85 	{
86 	  printf (ngettext ("Draft \"%s\" exists (%s byte).\n",
87                             "Draft \"%s\" exists (%s bytes).\n",
88                             (unsigned long) st.st_size),
89 		  wh->draftfile, mu_umaxtostr (0, st.st_size));
90 	  disp = mh_disposition (wh->draftfile);
91 	}
92     }
93 
94   return disp;
95 }
96