]>
diplodocus.org Git - nmh/blob - uip/dist.c
373ec1b3764e10de864a7c56304e2391a04054ce
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/seq_setcur.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/showfile.h"
12 #include "sbr/smatch.h"
13 #include "sbr/refile.h"
14 #include "sbr/cpydata.h"
15 #include "sbr/m_draft.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
28 #include "sbr/m_maildir.h"
33 #define DIST_SWITCHES \
34 X("annotate", 0, ANNOSW) \
35 X("noannotate", 0, NANNOSW) \
36 X("draftfolder +folder", 0, DFOLDSW) \
37 X("draftmessage msg", 0, DMSGSW) \
38 X("nodraftfolder", 0, NDFLDSW) \
39 X("editor editor", 0, EDITRSW) \
40 X("noedit", 0, NEDITSW) \
41 X("form formfile", 0, FORMSW) \
42 X("inplace", 0, INPLSW) \
43 X("noinplace", 0, NINPLSW) \
44 X("whatnowproc program", 0, WHATSW) \
45 X("nowhatnowproc", 0, NWHATSW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
48 X("file file", -4, FILESW) \
49 X("from address", 0, FROMSW) \
50 X("to address", 0, TOSW) \
51 X("cc address", 0, CCSW) \
52 X("fcc mailbox", 0, FCCSW) \
53 X("width columns", 0, WIDTHSW) \
54 X("atfile", 0, ATFILESW) \
55 X("noatfile", 0, NOATFILESW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(DIST
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(DIST
, switches
);
65 #define DISPO_SWITCHES \
67 X("replace", 0, YESW) \
68 X("list", 0, LISTDSW) \
69 X("refile +folder", 0, REFILSW) \
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(DISPO
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
81 static struct swit aqrl
[] = {
83 { "replace", 0, YESW
},
84 { "list", 0, LISTDSW
},
85 { "refile +folder", 0, REFILSW
},
91 main (int argc
, char **argv
)
97 int i
, in
, isdf
= 0, out
;
98 int outputlinelen
= OUTPUTLINELEN
;
101 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
102 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
103 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
104 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
105 char **argp
, **arguments
;
106 struct msgs
*mp
= NULL
;
109 if (nmh_init(argv
[0], true, true)) { return 1; }
111 arguments
= getarguments (invo_name
, argc
, argv
, 1);
114 while ((cp
= *argp
++)) {
116 switch (smatch (++cp
, switches
)) {
118 ambigsw (cp
, switches
);
121 die("-%s unknown", cp
);
124 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
126 print_help (buf
, switches
, 1);
129 print_version(invo_name
);
140 if (!(ed
= *argp
++) || *ed
== '-')
141 die("missing argument to %s", argp
[-2]);
149 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
150 die("missing argument to %s", argp
[-2]);
159 die("only one file at a time!");
160 if (!(cp
= *argp
++) || *cp
== '-')
161 die("missing argument to %s", argp
[-2]);
162 file
= path (cp
, TFILE
);
165 if (!(form
= *argp
++) || *form
== '-')
166 die("missing argument to %s", argp
[-2]);
178 die("only one draft folder at a time!");
179 if (!(cp
= *argp
++) || *cp
== '-')
180 die("missing argument to %s", argp
[-2]);
181 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
182 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
186 die("only one draft message at a time!");
187 if (!(dmsg
= *argp
++) || *dmsg
== '-')
188 die("missing argument to %s", argp
[-2]);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
198 from
= addlist(from
, cp
);
201 if (!(cp
= *argp
++) || *cp
== '-')
202 die("missing argument to %s", argp
[-2]);
203 to
= addlist(to
, cp
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 die("missing argument to %s", argp
[-2]);
208 cc
= addlist(cc
, cp
);
211 if (!(cp
= *argp
++) || *cp
== '-')
212 die("missing argument to %s", argp
[-2]);
213 fcc
= addlist(fcc
, cp
);
217 if (!(cp
= *argp
++) || *cp
== '-')
218 die("missing argument to %s", argp
[-2]);
219 if ((outputlinelen
= atoi(cp
)) < 10)
220 die("impossible width %d", outputlinelen
);
231 if (*cp
== '+' || *cp
== '@') {
233 die("only one folder at a time!");
234 folder
= pluspath (cp
);
237 die("only one message at a time!");
242 cwd
= mh_xstrdup(pwd ());
244 if (!context_find ("path"))
245 free (path ("./", TFOLDER
));
246 if (file
&& (msg
|| folder
))
247 die("can't mix files and folders/msgs");
250 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
252 /* Check if draft already exists */
253 if (stat (drft
, &st
) != NOTOK
) {
254 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
255 for (i
= LISTDSW
; i
!= YESW
;) {
256 if (!(argp
= read_switch_multiword ("\nDisposition? ",
257 isdf
? aqrnl
: aqrl
)))
259 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
268 showfile (++argp
, drft
);
271 if (refile (++argp
, drft
) == 0)
285 anot
= false; /* don't want to annotate a file */
293 folder
= getfolder (1);
294 maildir
= m_maildir (folder
);
296 if (chdir (maildir
) == NOTOK
)
297 adios (maildir
, "unable to change directory to");
299 /* read folder and create message structure */
300 if (!(mp
= folder_read (folder
, 1)))
301 die("unable to read folder %s", folder
);
303 /* check for empty folder */
305 die("no messages in %s", folder
);
307 /* parse the message range/sequence/name and set SELECTED */
308 if (!m_convert (mp
, msg
))
310 seq_setprev (mp
); /* set the previous-sequence */
313 die("only one message at a time!");
316 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
318 dat
[0] = mp
? mp
->lowsel
: 0;
321 dat
[3] = outputlinelen
;
327 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
329 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
330 adios (drft
, "unable to create");
332 cpydata (in
, out
, form
, drft
);
336 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
337 adios (msgnam
, "unable to open message");
340 context_replace (pfolder
, folder
);/* update current folder */
341 seq_setcur (mp
, mp
->lowsel
); /* update current message */
342 seq_save (mp
); /* synchronize sequences */
343 context_save (); /* save the context file */
348 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
349 inplace
, cwd
, atfile
);