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