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