The existence of the profile entry Path is already enforced by context_read().
[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 = mhbasename(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]", invo_name);
75                                         print_help(buf, switches, 1);
76                                         done(1);
77                                 case VERSIONSW:
78                                         print_version(invo_name);
79                                         done(1);
80
81                                 case ANNOSW:
82                                         anot++;
83                                         continue;
84                                 case NANNOSW:
85                                         anot = 0;
86                                         continue;
87
88                                 case EDITRSW:
89                                         if (!(ed = *argp++) || *ed == '-')
90                                                 adios(NULL, "missing argument to %s", argp[-2]);
91                                         nedit = 0;
92                                         continue;
93                                 case NEDITSW:
94                                         nedit++;
95                                         continue;
96
97                                 case WHATSW:
98                                         if (!(whatnowproc = *argp++) || *whatnowproc == '-')
99                                                 adios(NULL, "missing argument to %s", argp[-2]);
100                                         nwhat = 0;
101                                         continue;
102                                 case NWHATSW:
103                                         nwhat++;
104                                         continue;
105
106                                 case FILESW:
107                                         if (file)
108                                                 adios(NULL, "only one file at a time!");
109                                         if (!(cp = *argp++) || *cp == '-')
110                                                 adios(NULL, "missing argument to %s", argp[-2]);
111                                         file = path(cp, TFILE);
112                                         continue;
113                                 case FORMSW:
114                                         if (!(form = *argp++) || *form == '-')
115                                                 adios(NULL, "missing argument to %s", argp[-2]);
116                                         continue;
117
118                                 case INPLSW:
119                                         inplace++;
120                                         continue;
121                                 case NINPLSW:
122                                         inplace = 0;
123                                         continue;
124                         }
125                 }
126                 if (*cp == '+' || *cp == '@') {
127                         if (folder)
128                                 adios(NULL, "only one folder at a time!");
129                         else
130                                 folder = pluspath(cp);
131                 } else {
132                         if (msg)
133                                 adios(NULL, "only one message at a time!");
134                         else
135                                 msg = cp;
136                 }
137         }
138
139         cwd = getcpy(pwd());
140
141         if (file && (msg || folder))
142                 adios(NULL, "can't mix files and folders/msgs");
143
144         in = open_form(&form, distcomps);
145
146         strncpy(drft, m_draft("new"), sizeof(drft));
147
148         if ((out = creat(drft, m_gmprot())) == NOTOK)
149                 adios(drft, "unable to create");
150
151         cpydata(in, out, form, drft);
152         close(in);
153         close(out);
154
155         if (file) {
156                 /*
157                 ** Dist a file
158                 */
159                 anot = 0;  /* don't want to annotate a file */
160         } else {
161                 /*
162                 ** Dist a message
163                 */
164                 if (!msg)
165                         msg = "cur";
166                 if (!folder)
167                         folder = getfolder(FCUR);
168                 maildir = m_maildir(folder);
169
170                 if (chdir(maildir) == NOTOK)
171                         adios(maildir, "unable to change directory to");
172
173                 /* read folder and create message structure */
174                 if (!(mp = folder_read(folder)))
175                         adios(NULL, "unable to read folder %s", folder);
176
177                 /* check for empty folder */
178                 if (mp->nummsg == 0)
179                         adios(NULL, "no messages in %s", folder);
180
181                 /* parse the message range/sequence/name and set SELECTED */
182                 if (!m_convert(mp, msg))
183                         done(1);
184                 seq_setprev(mp);  /* set the previous-sequence */
185
186                 if (mp->numsel > 1)
187                         adios(NULL, "only one message at a time!");
188         }
189
190         msgnam = file ? file : getcpy(m_name(mp->lowsel));
191         if ((in = open(msgnam, O_RDONLY)) == NOTOK)
192                 adios(msgnam, "unable to open message");
193
194         if (!file) {
195                 context_replace(pfolder, folder);  /* update current folder */
196                 seq_setcur(mp, mp->lowsel);  /* update current message */
197                 seq_save(mp);  /* synchronize sequences  */
198                 context_save();  /* save the context file  */
199         }
200
201         if (nwhat)
202                 done(0);
203         what_now(ed, nedit, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL,
204                         inplace, cwd);
205         done(1);
206         return 1;
207 }