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