Removed the draft message in favor for a consistent draft folder facility
[mmh] / uip / dist.c
1 /*
2  * dist.c -- re-distribute a message
3  *
4  * This code is Copyright (c) 2002, by the authors of nmh.  See the
5  * COPYRIGHT file in the root directory of the nmh distribution for
6  * complete copyright information.
7  */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <fcntl.h>
12
13 static struct swit switches[] = {
14 #define ANNOSW  0
15         { "annotate", 0 },
16 #define NANNOSW  1
17         { "noannotate", 0 },
18 #define EDITRSW  2
19         { "editor editor", 0 },
20 #define NEDITSW  3
21         { "noedit", 0 },
22 #define FORMSW  4
23         { "form formfile", 0 },
24 #define INPLSW  5
25         { "inplace", 0 },
26 #define NINPLSW  6
27         { "noinplace", 0 },
28 #define WHATSW  7
29         { "whatnowproc program", 0 },
30 #define NWHATSW  8
31         { "nowhatnowproc", 0 },
32 #define VERSIONSW  9
33         { "version", 0 },
34 #define HELPSW  10
35         { "help", 0 },
36 #define FILESW  11
37         { "file file", -4 },  /* interface from msh */
38         { NULL, 0 }
39 };
40
41
42 int
43 main (int argc, char **argv)
44 {
45         int anot = 0, inplace = 1, nedit = 0;
46         int nwhat = 0, in, out;
47         char *cp, *cwd, *maildir, *msgnam;
48         char *ed = NULL, *file = NULL, *folder = NULL;
49         char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
50         char **argp, **arguments;
51         struct msgs *mp = NULL;
52
53 #ifdef LOCALE
54         setlocale(LC_ALL, "");
55 #endif
56         invo_name = r1bindex (argv[0], '/');
57
58         /* read user profile/context */
59         context_read();
60
61         arguments = getarguments (invo_name, argc, argv, 1);
62         argp = arguments;
63
64         while ((cp = *argp++)) {
65                 if (*cp == '-') {
66                         switch (smatch (++cp, switches)) {
67                                 case AMBIGSW:
68                                         ambigsw (cp, switches);
69                                         done (1);
70                                 case UNKWNSW:
71                                         adios (NULL, "-%s unknown", cp);
72
73                                 case HELPSW:
74                                         snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
75                                                 invo_name);
76                                         print_help (buf, switches, 1);
77                                         done (1);
78                                 case VERSIONSW:
79                                         print_version(invo_name);
80                                         done (1);
81
82                                 case ANNOSW:
83                                         anot++;
84                                         continue;
85                                 case NANNOSW:
86                                         anot = 0;
87                                         continue;
88
89                                 case EDITRSW:
90                                         if (!(ed = *argp++) || *ed == '-')
91                                                 adios (NULL, "missing argument to %s", argp[-2]);
92                                         nedit = 0;
93                                         continue;
94                                 case NEDITSW:
95                                         nedit++;
96                                         continue;
97
98                                 case WHATSW:
99                                         if (!(whatnowproc = *argp++) || *whatnowproc == '-')
100                                                 adios (NULL, "missing argument to %s", argp[-2]);
101                                         nwhat = 0;
102                                         continue;
103                                 case NWHATSW:
104                                         nwhat++;
105                                         continue;
106
107                                 case FILESW:
108                                         if (file)
109                                                 adios (NULL, "only one file at a time!");
110                                         if (!(cp = *argp++) || *cp == '-')
111                                                 adios (NULL, "missing argument to %s", argp[-2]);
112                                         file = path (cp, TFILE);
113                                         continue;
114                                 case FORMSW:
115                                         if (!(form = *argp++) || *form == '-')
116                                                 adios (NULL, "missing argument to %s", argp[-2]);
117                                         continue;
118
119                                 case INPLSW:
120                                         inplace++;
121                                         continue;
122                                 case NINPLSW:
123                                         inplace = 0;
124                                         continue;
125                         }
126                 }
127                 if (*cp == '+' || *cp == '@') {
128                         if (folder)
129                                 adios (NULL, "only one folder at a time!");
130                         else
131                                 folder = pluspath (cp);
132                 } else {
133                         if (msg)
134                                 adios (NULL, "only one message at a time!");
135                         else
136                                 msg = cp;
137                 }
138         }
139
140         cwd = getcpy (pwd ());
141
142         if (!context_find ("path"))
143                 free (path ("./", TFOLDER));
144         if (file && (msg || folder))
145                 adios (NULL, "can't mix files and folders/msgs");
146
147         in = open_form(&form, distcomps);
148
149         strncpy (drft, m_draft("new"), sizeof(drft));
150
151         if ((out = creat (drft, m_gmprot ())) == NOTOK)
152                 adios (drft, "unable to create");
153
154         cpydata (in, out, form, drft);
155         close (in);
156         close (out);
157
158         if (file) {
159                 /*
160                  * Dist a file
161                  */
162                 anot = 0;  /* don't want to annotate a file */
163         } else {
164                 /*
165                  * Dist a message
166                  */
167                 if (!msg)
168                         msg = "cur";
169                 if (!folder)
170                         folder = getfolder (1);
171                 maildir = m_maildir (folder);
172
173                 if (chdir (maildir) == NOTOK)
174                         adios (maildir, "unable to change directory to");
175
176                 /* read folder and create message structure */
177                 if (!(mp = folder_read (folder)))
178                         adios (NULL, "unable to read folder %s", folder);
179
180                 /* check for empty folder */
181                 if (mp->nummsg == 0)
182                         adios (NULL, "no messages in %s", folder);
183
184                 /* parse the message range/sequence/name and set SELECTED */
185                 if (!m_convert (mp, msg))
186                         done (1);
187                 seq_setprev (mp);  /* set the previous-sequence */
188
189                 if (mp->numsel > 1)
190                         adios (NULL, "only one message at a time!");
191         }
192
193         msgnam = file ? file : getcpy (m_name (mp->lowsel));
194         if ((in = open (msgnam, O_RDONLY)) == NOTOK)
195                 adios (msgnam, "unable to open message");
196
197         if (!file) {
198                 context_replace (pfolder, folder);  /* update current folder */
199                 seq_setcur (mp, mp->lowsel);  /* update current message */
200                 seq_save (mp);  /* synchronize sequences  */
201                 context_save ();  /* save the context file  */
202         }
203
204         if (nwhat)
205                 done (0);
206         what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
207                 anot ? "Resent" : NULL, inplace, cwd);
208         done (1);
209         return 1;
210 }