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