1 | |
---|
2 | /* |
---|
3 | * dist.c -- re-distribute a message |
---|
4 | * |
---|
5 | * $Id: dist.c,v 1.1.1.1 1999-02-07 18:14:13 danw Exp $ |
---|
6 | */ |
---|
7 | |
---|
8 | #include <h/mh.h> |
---|
9 | #include <fcntl.h> |
---|
10 | |
---|
11 | static struct swit switches[] = { |
---|
12 | #define ANNOSW 0 |
---|
13 | { "annotate", 0 }, |
---|
14 | #define NANNOSW 1 |
---|
15 | { "noannotate", 0 }, |
---|
16 | #define DFOLDSW 2 |
---|
17 | { "draftfolder +folder", 0 }, |
---|
18 | #define DMSGSW 3 |
---|
19 | { "draftmessage msg", 0 }, |
---|
20 | #define NDFLDSW 4 |
---|
21 | { "nodraftfolder", 0 }, |
---|
22 | #define EDITRSW 5 |
---|
23 | { "editor editor", 0 }, |
---|
24 | #define NEDITSW 6 |
---|
25 | { "noedit", 0 }, |
---|
26 | #define FORMSW 7 |
---|
27 | { "form formfile", 0 }, |
---|
28 | #define INPLSW 8 |
---|
29 | { "inplace", 0 }, |
---|
30 | #define NINPLSW 9 |
---|
31 | { "noinplace", 0 }, |
---|
32 | #define WHATSW 10 |
---|
33 | { "whatnowproc program", 0 }, |
---|
34 | #define NWHATSW 11 |
---|
35 | { "nowhatnowproc", 0 }, |
---|
36 | #define VERSIONSW 12 |
---|
37 | { "version", 0 }, |
---|
38 | #define HELPSW 13 |
---|
39 | { "help", 4 }, |
---|
40 | #define FILESW 14 |
---|
41 | { "file file", -4 }, /* interface from msh */ |
---|
42 | { NULL, 0 } |
---|
43 | }; |
---|
44 | |
---|
45 | static struct swit aqrnl[] = { |
---|
46 | #define NOSW 0 |
---|
47 | { "quit", 0 }, |
---|
48 | #define YESW 1 |
---|
49 | { "replace", 0 }, |
---|
50 | #define LISTDSW 2 |
---|
51 | { "list", 0 }, |
---|
52 | #define REFILSW 3 |
---|
53 | { "refile +folder", 0 }, |
---|
54 | #define NEWSW 4 |
---|
55 | { "new", 0 }, |
---|
56 | { NULL, 0 } |
---|
57 | }; |
---|
58 | |
---|
59 | |
---|
60 | static struct swit aqrl[] = { |
---|
61 | { "quit", 0 }, |
---|
62 | { "replace", 0 }, |
---|
63 | { "list", 0 }, |
---|
64 | { "refile +folder", 0 }, |
---|
65 | { NULL, 0 } |
---|
66 | }; |
---|
67 | |
---|
68 | |
---|
69 | int |
---|
70 | main (int argc, char **argv) |
---|
71 | { |
---|
72 | int anot = 0, inplace = 1, nedit = 0; |
---|
73 | int nwhat = 0, i, in, isdf = 0, out; |
---|
74 | char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL; |
---|
75 | char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL; |
---|
76 | char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ]; |
---|
77 | char **argp, **arguments; |
---|
78 | struct msgs *mp = NULL; |
---|
79 | struct stat st; |
---|
80 | |
---|
81 | #ifdef LOCALE |
---|
82 | setlocale(LC_ALL, ""); |
---|
83 | #endif |
---|
84 | invo_name = r1bindex (argv[0], '/'); |
---|
85 | |
---|
86 | /* read user profile/context */ |
---|
87 | context_read(); |
---|
88 | |
---|
89 | arguments = getarguments (invo_name, argc, argv, 1); |
---|
90 | argp = arguments; |
---|
91 | |
---|
92 | while ((cp = *argp++)) { |
---|
93 | if (*cp == '-') { |
---|
94 | switch (smatch (++cp, switches)) { |
---|
95 | case AMBIGSW: |
---|
96 | ambigsw (cp, switches); |
---|
97 | done (1); |
---|
98 | case UNKWNSW: |
---|
99 | adios (NULL, "-%s unknown", cp); |
---|
100 | |
---|
101 | case HELPSW: |
---|
102 | snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]", |
---|
103 | invo_name); |
---|
104 | print_help (buf, switches, 1); |
---|
105 | done (1); |
---|
106 | case VERSIONSW: |
---|
107 | print_version(invo_name); |
---|
108 | done (1); |
---|
109 | |
---|
110 | case ANNOSW: |
---|
111 | anot++; |
---|
112 | continue; |
---|
113 | case NANNOSW: |
---|
114 | anot = 0; |
---|
115 | continue; |
---|
116 | |
---|
117 | case EDITRSW: |
---|
118 | if (!(ed = *argp++) || *ed == '-') |
---|
119 | adios (NULL, "missing argument to %s", argp[-2]); |
---|
120 | nedit = 0; |
---|
121 | continue; |
---|
122 | case NEDITSW: |
---|
123 | nedit++; |
---|
124 | continue; |
---|
125 | |
---|
126 | case WHATSW: |
---|
127 | if (!(whatnowproc = *argp++) || *whatnowproc == '-') |
---|
128 | adios (NULL, "missing argument to %s", argp[-2]); |
---|
129 | nwhat = 0; |
---|
130 | continue; |
---|
131 | case NWHATSW: |
---|
132 | nwhat++; |
---|
133 | continue; |
---|
134 | |
---|
135 | case FILESW: |
---|
136 | if (file) |
---|
137 | adios (NULL, "only one file at a time!"); |
---|
138 | if (!(cp = *argp++) || *cp == '-') |
---|
139 | adios (NULL, "missing argument to %s", argp[-2]); |
---|
140 | file = path (cp, TFILE); |
---|
141 | continue; |
---|
142 | case FORMSW: |
---|
143 | if (!(form = *argp++) || *form == '-') |
---|
144 | adios (NULL, "missing argument to %s", argp[-2]); |
---|
145 | continue; |
---|
146 | |
---|
147 | case INPLSW: |
---|
148 | inplace++; |
---|
149 | continue; |
---|
150 | case NINPLSW: |
---|
151 | inplace = 0; |
---|
152 | continue; |
---|
153 | |
---|
154 | case DFOLDSW: |
---|
155 | if (dfolder) |
---|
156 | adios (NULL, "only one draft folder at a time!"); |
---|
157 | if (!(cp = *argp++) || *cp == '-') |
---|
158 | adios (NULL, "missing argument to %s", argp[-2]); |
---|
159 | dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp, |
---|
160 | *cp != '@' ? TFOLDER : TSUBCWF); |
---|
161 | continue; |
---|
162 | case DMSGSW: |
---|
163 | if (dmsg) |
---|
164 | adios (NULL, "only one draft message at a time!"); |
---|
165 | if (!(dmsg = *argp++) || *dmsg == '-') |
---|
166 | adios (NULL, "missing argument to %s", argp[-2]); |
---|
167 | continue; |
---|
168 | case NDFLDSW: |
---|
169 | dfolder = NULL; |
---|
170 | isdf = NOTOK; |
---|
171 | continue; |
---|
172 | } |
---|
173 | } |
---|
174 | if (*cp == '+' || *cp == '@') { |
---|
175 | if (folder) |
---|
176 | adios (NULL, "only one folder at a time!"); |
---|
177 | else |
---|
178 | folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF); |
---|
179 | } else { |
---|
180 | if (msg) |
---|
181 | adios (NULL, "only one message at a time!"); |
---|
182 | else |
---|
183 | msg = cp; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | cwd = getcpy (pwd ()); |
---|
188 | |
---|
189 | if (!context_find ("path")) |
---|
190 | free (path ("./", TFOLDER)); |
---|
191 | if (file && (msg || folder)) |
---|
192 | adios (NULL, "can't mix files and folders/msgs"); |
---|
193 | |
---|
194 | if (form) { |
---|
195 | if ((in = open (etcpath (form), O_RDONLY)) == NOTOK) |
---|
196 | adios (form, "unable to open form file"); |
---|
197 | } else { |
---|
198 | if ((in = open (etcpath (distcomps), O_RDONLY)) == NOTOK) |
---|
199 | adios (distcomps, "unable to open default components file"); |
---|
200 | form = distcomps; |
---|
201 | } |
---|
202 | |
---|
203 | try_it_again: |
---|
204 | strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft)); |
---|
205 | |
---|
206 | /* Check if draft already exists */ |
---|
207 | if (stat (drft, &st) != NOTOK) { |
---|
208 | printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size); |
---|
209 | for (i = LISTDSW; i != YESW;) { |
---|
210 | if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl))) |
---|
211 | done (1); |
---|
212 | switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) { |
---|
213 | case NOSW: |
---|
214 | done (0); |
---|
215 | case NEWSW: |
---|
216 | dmsg = NULL; |
---|
217 | goto try_it_again; |
---|
218 | case YESW: |
---|
219 | break; |
---|
220 | case LISTDSW: |
---|
221 | showfile (++argp, drft); |
---|
222 | break; |
---|
223 | case REFILSW: |
---|
224 | if (refile (++argp, drft) == 0) |
---|
225 | i = YESW; |
---|
226 | break; |
---|
227 | default: |
---|
228 | advise (NULL, "say what?"); |
---|
229 | break; |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | if ((out = creat (drft, m_gmprot ())) == NOTOK) |
---|
234 | adios (drft, "unable to create"); |
---|
235 | |
---|
236 | cpydata (in, out, form, drft); |
---|
237 | close (in); |
---|
238 | close (out); |
---|
239 | |
---|
240 | if (file) { |
---|
241 | /* |
---|
242 | * Dist a file |
---|
243 | */ |
---|
244 | anot = 0; /* don't want to annotate a file */ |
---|
245 | } else { |
---|
246 | /* |
---|
247 | * Dist a message |
---|
248 | */ |
---|
249 | if (!msg) |
---|
250 | msg = "cur"; |
---|
251 | if (!folder) |
---|
252 | folder = getfolder (1); |
---|
253 | maildir = m_maildir (folder); |
---|
254 | |
---|
255 | if (chdir (maildir) == NOTOK) |
---|
256 | adios (maildir, "unable to change directory to"); |
---|
257 | |
---|
258 | /* read folder and create message structure */ |
---|
259 | if (!(mp = folder_read (folder))) |
---|
260 | adios (NULL, "unable to read folder %s", folder); |
---|
261 | |
---|
262 | /* check for empty folder */ |
---|
263 | if (mp->nummsg == 0) |
---|
264 | adios (NULL, "no messages in %s", folder); |
---|
265 | |
---|
266 | /* parse the message range/sequence/name and set SELECTED */ |
---|
267 | if (!m_convert (mp, msg)) |
---|
268 | done (1); |
---|
269 | seq_setprev (mp); /* set the previous-sequence */ |
---|
270 | |
---|
271 | if (mp->numsel > 1) |
---|
272 | adios (NULL, "only one message at a time!"); |
---|
273 | } |
---|
274 | |
---|
275 | msgnam = file ? file : getcpy (m_name (mp->lowsel)); |
---|
276 | if ((in = open (msgnam, O_RDONLY)) == NOTOK) |
---|
277 | adios (msgnam, "unable to open message"); |
---|
278 | |
---|
279 | if (!file) { |
---|
280 | context_replace (pfolder, folder);/* update current folder */ |
---|
281 | seq_setcur (mp, mp->lowsel); /* update current message */ |
---|
282 | seq_save (mp); /* synchronize sequences */ |
---|
283 | context_save (); /* save the context file */ |
---|
284 | } |
---|
285 | |
---|
286 | if (nwhat) |
---|
287 | done (0); |
---|
288 | what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp, |
---|
289 | anot ? "Resent" : NULL, inplace, cwd); |
---|
290 | done (1); |
---|
291 | } |
---|