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