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