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