77ea394ae4856d9268776fb52e65b81d32d0b00a
[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 struct msgs *mp;
22         static char buffer[BUFSIZ];
23         char *folder;
24
25         folder = getcpy(toabsdir(draftfolder));
26         create_folder(folder, 0, done);
27         if (!(mp = folder_read(folder))) {
28                 adios(NULL, "unable to read folder %s", folder);
29         }
30
31         /*
32         ** Make sure we have enough message status space for all
33         ** the message numbers from 1 to "new", since we might
34         ** select an empty slot.  If we add more space at the
35         ** end, go ahead and add 10 additional slots.
36         */
37         if (mp->hghmsg >= mp->hghoff) {
38                 if (!(mp = folder_realloc(mp, 1, mp->hghmsg + 10)))
39                         adios(NULL, "unable to allocate folder storage");
40         } else if (mp->lowoff > 1) {
41                 if (!(mp = folder_realloc(mp, 1, mp->hghoff)))
42                         adios(NULL, "unable to allocate folder storage");
43         }
44
45         mp->msgflags |= ALLOW_NEW;  /* allow the "new" sequence */
46
47         /*
48         ** The draft message name to return is defined by `which'.
49         ** Usually it is "cur" (for the current draft) or "new"
50         ** (to start a new draft).
51         */
52         if (!m_convert(mp, which))
53                 done(1);
54         seq_setprev(mp);
55
56         snprintf(buffer, sizeof(buffer), "%s/%s", mp->foldpath,
57                         m_name(mp->lowsel));
58         seq_setcur(mp, mp->lowsel);
59         seq_save(mp);
60         folder_free(mp);
61
62         return buffer;
63 }