]>
diplodocus.org Git - nmh/blob - uip/dist.c
552a9a4649f9efeef1e326c2cd739d8564b3cb03
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
13 #include "sbr/m_maildir.h"
18 #define DIST_SWITCHES \
19 X("annotate", 0, ANNOSW) \
20 X("noannotate", 0, NANNOSW) \
21 X("draftfolder +folder", 0, DFOLDSW) \
22 X("draftmessage msg", 0, DMSGSW) \
23 X("nodraftfolder", 0, NDFLDSW) \
24 X("editor editor", 0, EDITRSW) \
25 X("noedit", 0, NEDITSW) \
26 X("form formfile", 0, FORMSW) \
27 X("inplace", 0, INPLSW) \
28 X("noinplace", 0, NINPLSW) \
29 X("whatnowproc program", 0, WHATSW) \
30 X("nowhatnowproc", 0, NWHATSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
33 X("file file", -4, FILESW) \
34 X("from address", 0, FROMSW) \
35 X("to address", 0, TOSW) \
36 X("cc address", 0, CCSW) \
37 X("fcc mailbox", 0, FCCSW) \
38 X("width columns", 0, WIDTHSW) \
39 X("atfile", 0, ATFILESW) \
40 X("noatfile", 0, NOATFILESW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(DIST
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(DIST
, switches
);
50 #define DISPO_SWITCHES \
52 X("replace", 0, YESW) \
53 X("list", 0, LISTDSW) \
54 X("refile +folder", 0, REFILSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(DISPO
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
66 static struct swit aqrl
[] = {
68 { "replace", 0, YESW
},
69 { "list", 0, LISTDSW
},
70 { "refile +folder", 0, REFILSW
},
76 main (int argc
, char **argv
)
82 int i
, in
, isdf
= 0, out
;
83 int outputlinelen
= OUTPUTLINELEN
;
86 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
87 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
88 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
89 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
90 char **argp
, **arguments
;
91 struct msgs
*mp
= NULL
;
94 if (nmh_init(argv
[0], true, true)) { return 1; }
96 arguments
= getarguments (invo_name
, argc
, argv
, 1);
99 while ((cp
= *argp
++)) {
101 switch (smatch (++cp
, switches
)) {
103 ambigsw (cp
, switches
);
106 die("-%s unknown", cp
);
109 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
111 print_help (buf
, switches
, 1);
114 print_version(invo_name
);
125 if (!(ed
= *argp
++) || *ed
== '-')
126 die("missing argument to %s", argp
[-2]);
134 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
135 die("missing argument to %s", argp
[-2]);
144 die("only one file at a time!");
145 if (!(cp
= *argp
++) || *cp
== '-')
146 die("missing argument to %s", argp
[-2]);
147 file
= path (cp
, TFILE
);
150 if (!(form
= *argp
++) || *form
== '-')
151 die("missing argument to %s", argp
[-2]);
163 die("only one draft folder at a time!");
164 if (!(cp
= *argp
++) || *cp
== '-')
165 die("missing argument to %s", argp
[-2]);
166 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
167 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
171 die("only one draft message at a time!");
172 if (!(dmsg
= *argp
++) || *dmsg
== '-')
173 die("missing argument to %s", argp
[-2]);
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
183 from
= addlist(from
, cp
);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
188 to
= addlist(to
, cp
);
191 if (!(cp
= *argp
++) || *cp
== '-')
192 die("missing argument to %s", argp
[-2]);
193 cc
= addlist(cc
, cp
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
198 fcc
= addlist(fcc
, cp
);
202 if (!(cp
= *argp
++) || *cp
== '-')
203 die("missing argument to %s", argp
[-2]);
204 if ((outputlinelen
= atoi(cp
)) < 10)
205 die("impossible width %d", outputlinelen
);
216 if (*cp
== '+' || *cp
== '@') {
218 die("only one folder at a time!");
219 folder
= pluspath (cp
);
222 die("only one message at a time!");
227 cwd
= mh_xstrdup(pwd ());
229 if (!context_find ("path"))
230 free (path ("./", TFOLDER
));
231 if (file
&& (msg
|| folder
))
232 die("can't mix files and folders/msgs");
235 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
237 /* Check if draft already exists */
238 if (stat (drft
, &st
) != NOTOK
) {
239 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
240 for (i
= LISTDSW
; i
!= YESW
;) {
241 if (!(argp
= read_switch_multiword ("\nDisposition? ",
242 isdf
? aqrnl
: aqrl
)))
244 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
253 showfile (++argp
, drft
);
256 if (refile (++argp
, drft
) == 0)
270 anot
= false; /* don't want to annotate a file */
278 folder
= getfolder (1);
279 maildir
= m_maildir (folder
);
281 if (chdir (maildir
) == NOTOK
)
282 adios (maildir
, "unable to change directory to");
284 /* read folder and create message structure */
285 if (!(mp
= folder_read (folder
, 1)))
286 die("unable to read folder %s", folder
);
288 /* check for empty folder */
290 die("no messages in %s", folder
);
292 /* parse the message range/sequence/name and set SELECTED */
293 if (!m_convert (mp
, msg
))
295 seq_setprev (mp
); /* set the previous-sequence */
298 die("only one message at a time!");
301 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
303 dat
[0] = mp
? mp
->lowsel
: 0;
306 dat
[3] = outputlinelen
;
312 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
314 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
315 adios (drft
, "unable to create");
317 cpydata (in
, out
, form
, drft
);
321 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
322 adios (msgnam
, "unable to open message");
325 context_replace (pfolder
, folder
);/* update current folder */
326 seq_setcur (mp
, mp
->lowsel
); /* update current message */
327 seq_save (mp
); /* synchronize sequences */
328 context_save (); /* save the context file */
333 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
334 inplace
, cwd
, atfile
);