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