]> diplodocus.org Git - nmh/blob - sbr/m_draft.c
forwsbr.c: Move interface declaration to own forwsbr.h.
[nmh] / sbr / m_draft.c
1 /* m_draft.c -- construct the name of a draft message
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10 #include "m_maildir.h"
11 #include "h/done.h"
12
13
14 /* Does not return NULL. */
15 char *
16 m_draft (char *folder, char *msg, int use, int *isdf)
17 {
18 char *cp;
19 struct msgs *mp;
20 static char buffer[BUFSIZ];
21
22 if (*isdf == -1 || folder == NULL || *folder == '\0') {
23 if (*isdf == -1 || (cp = context_find ("Draft-Folder")) == NULL) {
24 *isdf = 0;
25 return m_maildir (msg && *msg ? msg : draft);
26 }
27
28 folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
29 *cp != '@' ? TFOLDER : TSUBCWF);
30 }
31 *isdf = 1;
32
33 if (chdir (m_maildir ("")) < 0) {
34 advise (m_maildir (""), "chdir");
35 }
36 strncpy (buffer, m_maildir (folder), sizeof(buffer));
37
38 create_folder (buffer, 0, done);
39
40 if (chdir (buffer) == -1)
41 adios (buffer, "unable to change directory to");
42
43 if (!(mp = folder_read (folder, 1)))
44 die("unable to read folder %s", folder);
45
46 /*
47 * Make sure we have enough message status space for all
48 * the message numbers from 1 to "new", since we might
49 * select an empty slot. If we add more space at the
50 * end, go ahead and add 10 additional slots.
51 */
52 if (mp->hghmsg >= mp->hghoff) {
53 if (!(mp = folder_realloc (mp, 1, mp->hghmsg + 10)))
54 die("unable to allocate folder storage");
55 } else if (mp->lowoff > 1) {
56 if (!(mp = folder_realloc (mp, 1, mp->hghoff)))
57 die("unable to allocate folder storage");
58 }
59
60 mp->msgflags |= ALLOW_NEW; /* allow the "new" sequence */
61
62 /*
63 * If we have been give a valid message name, then use that.
64 * Else, if we are given the "use" option, then use the
65 * current message. Else, use special sequence "new".
66 */
67 if (!m_convert (mp, msg && *msg ? msg : use ? "cur" : "new"))
68 done (1);
69 seq_setprev (mp);
70
71 if (mp->numsel > 1)
72 die("only one message draft at a time!");
73
74 snprintf (buffer, sizeof(buffer), "%s/%s", mp->foldpath, m_name (mp->lowsel));
75 cp = buffer;
76
77 seq_setcur (mp, mp->lowsel);/* set current message for folder */
78 seq_save (mp); /* synchronize message sequences */
79 folder_free (mp); /* free folder/message structure */
80
81 return cp;
82 }