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