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