4a648047e762e72f46e9c101f9300d763da4904d
[mmh] / sbr / m_draft.c
1 /*
2  * m_draft.c -- construct the name of a draft 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 <errno.h>
12
13
14 /*
15 **  `which' should either be "cur" to use the current draft
16 **  or "new" to start with a new draft.
17 */
18 char *
19 m_draft(char *which)
20 {
21         register char *cp;
22         register struct msgs *mp;
23         static char buffer[BUFSIZ];
24         char *folder;
25
26         cp = draftfolder;
27         folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
28                         *cp != '@' ? TFOLDER : TSUBCWF);
29
30         chdir (m_maildir (""));
31         strncpy (buffer, m_maildir (folder), sizeof(buffer));
32
33         create_folder (buffer, 0, done);
34
35         if (chdir (buffer) == -1)
36                 adios (buffer, "unable to change directory to");
37
38         if (!(mp = folder_read (folder)))
39                 adios (NULL, "unable to read folder %s", folder);
40
41         /*
42          * Make sure we have enough message status space for all
43          * the message numbers from 1 to "new", since we might
44          * select an empty slot.  If we add more space at the
45          * end, go ahead and add 10 additional slots.
46          */
47         if (mp->hghmsg >= mp->hghoff) {
48                 if (!(mp = folder_realloc (mp, 1, mp->hghmsg + 10)))
49                         adios (NULL, "unable to allocate folder storage");
50         } else if (mp->lowoff > 1) {
51                 if (!(mp = folder_realloc (mp, 1, mp->hghoff)))
52                         adios (NULL, "unable to allocate folder storage");
53         }
54
55         mp->msgflags |= ALLOW_NEW;  /* allow the "new" sequence */
56
57         /*
58          * The draft message name to return is defined by `which'.
59          * Usually it is "cur" (for the current draft) or "new"
60          * (to start a new draft).
61          */
62         if (!m_convert (mp, which))
63                 done (1);
64         seq_setprev (mp);
65
66         snprintf (buffer, sizeof(buffer), "%s/%s", mp->foldpath,
67                         m_name (mp->lowsel));
68         seq_setcur (mp, mp->lowsel);
69         seq_save (mp);
70         folder_free (mp);
71
72         return buffer;
73 }