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