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