]>
diplodocus.org Git - nmh/blob - uip/dist.c
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/cpydata.h"
10 #include "sbr/m_draft.h"
11 #include "sbr/m_convert.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_replace.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
23 #include "sbr/m_maildir.h"
28 #define DIST_SWITCHES \
29 X("annotate", 0, ANNOSW) \
30 X("noannotate", 0, NANNOSW) \
31 X("draftfolder +folder", 0, DFOLDSW) \
32 X("draftmessage msg", 0, DMSGSW) \
33 X("nodraftfolder", 0, NDFLDSW) \
34 X("editor editor", 0, EDITRSW) \
35 X("noedit", 0, NEDITSW) \
36 X("form formfile", 0, FORMSW) \
37 X("inplace", 0, INPLSW) \
38 X("noinplace", 0, NINPLSW) \
39 X("whatnowproc program", 0, WHATSW) \
40 X("nowhatnowproc", 0, NWHATSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("file file", -4, FILESW) \
44 X("from address", 0, FROMSW) \
45 X("to address", 0, TOSW) \
46 X("cc address", 0, CCSW) \
47 X("fcc mailbox", 0, FCCSW) \
48 X("width columns", 0, WIDTHSW) \
49 X("atfile", 0, ATFILESW) \
50 X("noatfile", 0, NOATFILESW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(DIST
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(DIST
, switches
);
60 #define DISPO_SWITCHES \
62 X("replace", 0, YESW) \
63 X("list", 0, LISTDSW) \
64 X("refile +folder", 0, REFILSW) \
67 #define X(sw, minchars, id) id,
68 DEFINE_SWITCH_ENUM(DISPO
);
71 #define X(sw, minchars, id) { sw, minchars, id },
72 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
76 static struct swit aqrl
[] = {
78 { "replace", 0, YESW
},
79 { "list", 0, LISTDSW
},
80 { "refile +folder", 0, REFILSW
},
86 main (int argc
, char **argv
)
92 int i
, in
, isdf
= 0, out
;
93 int outputlinelen
= OUTPUTLINELEN
;
96 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
97 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
98 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
99 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
100 char **argp
, **arguments
;
101 struct msgs
*mp
= NULL
;
104 if (nmh_init(argv
[0], true, true)) { return 1; }
106 arguments
= getarguments (invo_name
, argc
, argv
, 1);
109 while ((cp
= *argp
++)) {
111 switch (smatch (++cp
, switches
)) {
113 ambigsw (cp
, switches
);
116 die("-%s unknown", cp
);
119 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
121 print_help (buf
, switches
, 1);
124 print_version(invo_name
);
135 if (!(ed
= *argp
++) || *ed
== '-')
136 die("missing argument to %s", argp
[-2]);
144 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
145 die("missing argument to %s", argp
[-2]);
154 die("only one file at a time!");
155 if (!(cp
= *argp
++) || *cp
== '-')
156 die("missing argument to %s", argp
[-2]);
157 file
= path (cp
, TFILE
);
160 if (!(form
= *argp
++) || *form
== '-')
161 die("missing argument to %s", argp
[-2]);
173 die("only one draft folder at a time!");
174 if (!(cp
= *argp
++) || *cp
== '-')
175 die("missing argument to %s", argp
[-2]);
176 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
177 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
181 die("only one draft message at a time!");
182 if (!(dmsg
= *argp
++) || *dmsg
== '-')
183 die("missing argument to %s", argp
[-2]);
191 if (!(cp
= *argp
++) || *cp
== '-')
192 die("missing argument to %s", argp
[-2]);
193 from
= addlist(from
, cp
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
198 to
= addlist(to
, cp
);
201 if (!(cp
= *argp
++) || *cp
== '-')
202 die("missing argument to %s", argp
[-2]);
203 cc
= addlist(cc
, cp
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 die("missing argument to %s", argp
[-2]);
208 fcc
= addlist(fcc
, cp
);
212 if (!(cp
= *argp
++) || *cp
== '-')
213 die("missing argument to %s", argp
[-2]);
214 if ((outputlinelen
= atoi(cp
)) < 10)
215 die("impossible width %d", outputlinelen
);
226 if (*cp
== '+' || *cp
== '@') {
228 die("only one folder at a time!");
229 folder
= pluspath (cp
);
232 die("only one message at a time!");
237 cwd
= mh_xstrdup(pwd ());
239 if (!context_find ("path"))
240 free (path ("./", TFOLDER
));
241 if (file
&& (msg
|| folder
))
242 die("can't mix files and folders/msgs");
245 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
247 /* Check if draft already exists */
248 if (stat (drft
, &st
) != NOTOK
) {
249 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
250 for (i
= LISTDSW
; i
!= YESW
;) {
251 if (!(argp
= read_switch_multiword ("\nDisposition? ",
252 isdf
? aqrnl
: aqrl
)))
254 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
263 showfile (++argp
, drft
);
266 if (refile (++argp
, drft
) == 0)
280 anot
= false; /* don't want to annotate a file */
288 folder
= getfolder (1);
289 maildir
= m_maildir (folder
);
291 if (chdir (maildir
) == NOTOK
)
292 adios (maildir
, "unable to change directory to");
294 /* read folder and create message structure */
295 if (!(mp
= folder_read (folder
, 1)))
296 die("unable to read folder %s", folder
);
298 /* check for empty folder */
300 die("no messages in %s", folder
);
302 /* parse the message range/sequence/name and set SELECTED */
303 if (!m_convert (mp
, msg
))
305 seq_setprev (mp
); /* set the previous-sequence */
308 die("only one message at a time!");
311 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
313 dat
[0] = mp
? mp
->lowsel
: 0;
316 dat
[3] = outputlinelen
;
322 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
324 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
325 adios (drft
, "unable to create");
327 cpydata (in
, out
, form
, drft
);
331 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
332 adios (msgnam
, "unable to open message");
335 context_replace (pfolder
, folder
);/* update current folder */
336 seq_setcur (mp
, mp
->lowsel
); /* update current message */
337 seq_save (mp
); /* synchronize sequences */
338 context_save (); /* save the context file */
343 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
344 inplace
, cwd
, atfile
);