]>
diplodocus.org Git - nmh/blob - uip/dist.c
7e1b2de02d3ac591bdfa7731908546f12512f650
1 /* dist.c -- re-distribute a message
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.
9 #include "sbr/read_switch_multiword.h"
10 #include "sbr/seq_setprev.h"
11 #include "sbr/seq_setcur.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/showfile.h"
14 #include "sbr/smatch.h"
15 #include "sbr/refile.h"
16 #include "sbr/cpydata.h"
17 #include "sbr/m_draft.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/folder_read.h"
21 #include "sbr/context_save.h"
22 #include "sbr/context_replace.h"
23 #include "sbr/context_find.h"
24 #include "sbr/ambigsw.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/error.h"
30 #include "sbr/m_maildir.h"
35 #define DIST_SWITCHES \
36 X("annotate", 0, ANNOSW) \
37 X("noannotate", 0, NANNOSW) \
38 X("draftfolder +folder", 0, DFOLDSW) \
39 X("draftmessage msg", 0, DMSGSW) \
40 X("nodraftfolder", 0, NDFLDSW) \
41 X("editor editor", 0, EDITRSW) \
42 X("noedit", 0, NEDITSW) \
43 X("form formfile", 0, FORMSW) \
44 X("inplace", 0, INPLSW) \
45 X("noinplace", 0, NINPLSW) \
46 X("whatnowproc program", 0, WHATSW) \
47 X("nowhatnowproc", 0, NWHATSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
50 X("file file", -4, FILESW) \
51 X("from address", 0, FROMSW) \
52 X("to address", 0, TOSW) \
53 X("cc address", 0, CCSW) \
54 X("fcc mailbox", 0, FCCSW) \
55 X("width columns", 0, WIDTHSW) \
56 X("atfile", 0, ATFILESW) \
57 X("noatfile", 0, NOATFILESW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(DIST
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(DIST
, switches
);
67 #define DISPO_SWITCHES \
69 X("replace", 0, YESW) \
70 X("list", 0, LISTDSW) \
71 X("refile +folder", 0, REFILSW) \
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(DISPO
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
83 static struct swit aqrl
[] = {
85 { "replace", 0, YESW
},
86 { "list", 0, LISTDSW
},
87 { "refile +folder", 0, REFILSW
},
93 main (int argc
, char **argv
)
99 int i
, in
, isdf
= 0, out
;
100 int outputlinelen
= OUTPUTLINELEN
;
103 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
104 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
105 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
106 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
107 char **argp
, **arguments
;
108 struct msgs
*mp
= NULL
;
111 if (nmh_init(argv
[0], true, true)) { return 1; }
113 arguments
= getarguments (invo_name
, argc
, argv
, 1);
116 while ((cp
= *argp
++)) {
118 switch (smatch (++cp
, switches
)) {
120 ambigsw (cp
, switches
);
123 die("-%s unknown", cp
);
126 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
128 print_help (buf
, switches
, 1);
131 print_version(invo_name
);
142 if (!(ed
= *argp
++) || *ed
== '-')
143 die("missing argument to %s", argp
[-2]);
151 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
152 die("missing argument to %s", argp
[-2]);
161 die("only one file at a time!");
162 if (!(cp
= *argp
++) || *cp
== '-')
163 die("missing argument to %s", argp
[-2]);
164 file
= path (cp
, TFILE
);
167 if (!(form
= *argp
++) || *form
== '-')
168 die("missing argument to %s", argp
[-2]);
180 die("only one draft folder at a time!");
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
183 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
184 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
188 die("only one draft message at a time!");
189 if (!(dmsg
= *argp
++) || *dmsg
== '-')
190 die("missing argument to %s", argp
[-2]);
198 if (!(cp
= *argp
++) || *cp
== '-')
199 die("missing argument to %s", argp
[-2]);
200 from
= addlist(from
, cp
);
203 if (!(cp
= *argp
++) || *cp
== '-')
204 die("missing argument to %s", argp
[-2]);
205 to
= addlist(to
, cp
);
208 if (!(cp
= *argp
++) || *cp
== '-')
209 die("missing argument to %s", argp
[-2]);
210 cc
= addlist(cc
, cp
);
213 if (!(cp
= *argp
++) || *cp
== '-')
214 die("missing argument to %s", argp
[-2]);
215 fcc
= addlist(fcc
, cp
);
219 if (!(cp
= *argp
++) || *cp
== '-')
220 die("missing argument to %s", argp
[-2]);
221 if ((outputlinelen
= atoi(cp
)) < 10)
222 die("impossible width %d", outputlinelen
);
233 if (*cp
== '+' || *cp
== '@') {
235 die("only one folder at a time!");
236 folder
= pluspath (cp
);
239 die("only one message at a time!");
244 cwd
= mh_xstrdup(pwd ());
246 if (!context_find ("path"))
247 free (path ("./", TFOLDER
));
248 if (file
&& (msg
|| folder
))
249 die("can't mix files and folders/msgs");
252 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
254 /* Check if draft already exists */
255 if (stat (drft
, &st
) != NOTOK
) {
256 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
257 for (i
= LISTDSW
; i
!= YESW
;) {
258 if (!(argp
= read_switch_multiword ("\nDisposition? ",
259 isdf
? aqrnl
: aqrl
)))
261 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
270 showfile (++argp
, drft
);
273 if (refile (++argp
, drft
) == 0)
287 anot
= false; /* don't want to annotate a file */
295 folder
= getfolder (1);
296 maildir
= m_maildir (folder
);
298 if (chdir (maildir
) == NOTOK
)
299 adios (maildir
, "unable to change directory to");
301 /* read folder and create message structure */
302 if (!(mp
= folder_read (folder
, 1)))
303 die("unable to read folder %s", folder
);
305 /* check for empty folder */
307 die("no messages in %s", folder
);
309 /* parse the message range/sequence/name and set SELECTED */
310 if (!m_convert (mp
, msg
))
312 seq_setprev (mp
); /* set the previous-sequence */
315 die("only one message at a time!");
318 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
320 dat
[0] = mp
? mp
->lowsel
: 0;
323 dat
[3] = outputlinelen
;
329 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
331 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
332 adios (drft
, "unable to create");
334 cpydata (in
, out
, form
, drft
);
338 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
339 adios (msgnam
, "unable to open message");
342 context_replace (pfolder
, folder
);/* update current folder */
343 seq_setcur (mp
, mp
->lowsel
); /* update current message */
344 seq_save (mp
); /* synchronize sequences */
345 context_save (); /* save the context file */
350 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
351 inplace
, cwd
, atfile
);