]>
diplodocus.org Git - nmh/blob - uip/dist.c
13a537827e7984a9386869af3bf5dad6e6111796
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/context_save.h"
10 #include "sbr/context_replace.h"
11 #include "sbr/context_find.h"
12 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
18 #include "sbr/m_maildir.h"
23 #define DIST_SWITCHES \
24 X("annotate", 0, ANNOSW) \
25 X("noannotate", 0, NANNOSW) \
26 X("draftfolder +folder", 0, DFOLDSW) \
27 X("draftmessage msg", 0, DMSGSW) \
28 X("nodraftfolder", 0, NDFLDSW) \
29 X("editor editor", 0, EDITRSW) \
30 X("noedit", 0, NEDITSW) \
31 X("form formfile", 0, FORMSW) \
32 X("inplace", 0, INPLSW) \
33 X("noinplace", 0, NINPLSW) \
34 X("whatnowproc program", 0, WHATSW) \
35 X("nowhatnowproc", 0, NWHATSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
38 X("file file", -4, FILESW) \
39 X("from address", 0, FROMSW) \
40 X("to address", 0, TOSW) \
41 X("cc address", 0, CCSW) \
42 X("fcc mailbox", 0, FCCSW) \
43 X("width columns", 0, WIDTHSW) \
44 X("atfile", 0, ATFILESW) \
45 X("noatfile", 0, NOATFILESW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(DIST
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(DIST
, switches
);
55 #define DISPO_SWITCHES \
57 X("replace", 0, YESW) \
58 X("list", 0, LISTDSW) \
59 X("refile +folder", 0, REFILSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(DISPO
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
71 static struct swit aqrl
[] = {
73 { "replace", 0, YESW
},
74 { "list", 0, LISTDSW
},
75 { "refile +folder", 0, REFILSW
},
81 main (int argc
, char **argv
)
87 int i
, in
, isdf
= 0, out
;
88 int outputlinelen
= OUTPUTLINELEN
;
91 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
92 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
93 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
94 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
95 char **argp
, **arguments
;
96 struct msgs
*mp
= NULL
;
99 if (nmh_init(argv
[0], true, true)) { return 1; }
101 arguments
= getarguments (invo_name
, argc
, argv
, 1);
104 while ((cp
= *argp
++)) {
106 switch (smatch (++cp
, switches
)) {
108 ambigsw (cp
, switches
);
111 die("-%s unknown", cp
);
114 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
116 print_help (buf
, switches
, 1);
119 print_version(invo_name
);
130 if (!(ed
= *argp
++) || *ed
== '-')
131 die("missing argument to %s", argp
[-2]);
139 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
140 die("missing argument to %s", argp
[-2]);
149 die("only one file at a time!");
150 if (!(cp
= *argp
++) || *cp
== '-')
151 die("missing argument to %s", argp
[-2]);
152 file
= path (cp
, TFILE
);
155 if (!(form
= *argp
++) || *form
== '-')
156 die("missing argument to %s", argp
[-2]);
168 die("only one draft folder at a time!");
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
171 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
172 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
176 die("only one draft message at a time!");
177 if (!(dmsg
= *argp
++) || *dmsg
== '-')
178 die("missing argument to %s", argp
[-2]);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
188 from
= addlist(from
, cp
);
191 if (!(cp
= *argp
++) || *cp
== '-')
192 die("missing argument to %s", argp
[-2]);
193 to
= addlist(to
, cp
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
198 cc
= addlist(cc
, cp
);
201 if (!(cp
= *argp
++) || *cp
== '-')
202 die("missing argument to %s", argp
[-2]);
203 fcc
= addlist(fcc
, cp
);
207 if (!(cp
= *argp
++) || *cp
== '-')
208 die("missing argument to %s", argp
[-2]);
209 if ((outputlinelen
= atoi(cp
)) < 10)
210 die("impossible width %d", outputlinelen
);
221 if (*cp
== '+' || *cp
== '@') {
223 die("only one folder at a time!");
224 folder
= pluspath (cp
);
227 die("only one message at a time!");
232 cwd
= mh_xstrdup(pwd ());
234 if (!context_find ("path"))
235 free (path ("./", TFOLDER
));
236 if (file
&& (msg
|| folder
))
237 die("can't mix files and folders/msgs");
240 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
242 /* Check if draft already exists */
243 if (stat (drft
, &st
) != NOTOK
) {
244 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
245 for (i
= LISTDSW
; i
!= YESW
;) {
246 if (!(argp
= read_switch_multiword ("\nDisposition? ",
247 isdf
? aqrnl
: aqrl
)))
249 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
258 showfile (++argp
, drft
);
261 if (refile (++argp
, drft
) == 0)
275 anot
= false; /* don't want to annotate a file */
283 folder
= getfolder (1);
284 maildir
= m_maildir (folder
);
286 if (chdir (maildir
) == NOTOK
)
287 adios (maildir
, "unable to change directory to");
289 /* read folder and create message structure */
290 if (!(mp
= folder_read (folder
, 1)))
291 die("unable to read folder %s", folder
);
293 /* check for empty folder */
295 die("no messages in %s", folder
);
297 /* parse the message range/sequence/name and set SELECTED */
298 if (!m_convert (mp
, msg
))
300 seq_setprev (mp
); /* set the previous-sequence */
303 die("only one message at a time!");
306 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
308 dat
[0] = mp
? mp
->lowsel
: 0;
311 dat
[3] = outputlinelen
;
317 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
319 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
320 adios (drft
, "unable to create");
322 cpydata (in
, out
, form
, drft
);
326 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
327 adios (msgnam
, "unable to open message");
330 context_replace (pfolder
, folder
);/* update current folder */
331 seq_setcur (mp
, mp
->lowsel
); /* update current message */
332 seq_save (mp
); /* synchronize sequences */
333 context_save (); /* save the context file */
338 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
339 inplace
, cwd
, atfile
);