]>
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 "whatnowproc.h"
10 #include "sbr/m_name.h"
11 #include "sbr/m_gmprot.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/read_switch_multiword.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_setcur.h"
16 #include "sbr/seq_save.h"
17 #include "sbr/showfile.h"
18 #include "sbr/smatch.h"
19 #include "sbr/refile.h"
20 #include "sbr/cpydata.h"
21 #include "sbr/m_draft.h"
22 #include "sbr/m_convert.h"
23 #include "sbr/getfolder.h"
24 #include "sbr/folder_read.h"
25 #include "sbr/context_save.h"
26 #include "sbr/context_replace.h"
27 #include "sbr/context_find.h"
28 #include "sbr/ambigsw.h"
30 #include "sbr/print_version.h"
31 #include "sbr/print_help.h"
32 #include "sbr/error.h"
34 #include "sbr/m_maildir.h"
39 #define DIST_SWITCHES \
40 X("annotate", 0, ANNOSW) \
41 X("noannotate", 0, NANNOSW) \
42 X("draftfolder +folder", 0, DFOLDSW) \
43 X("draftmessage msg", 0, DMSGSW) \
44 X("nodraftfolder", 0, NDFLDSW) \
45 X("editor editor", 0, EDITRSW) \
46 X("noedit", 0, NEDITSW) \
47 X("form formfile", 0, FORMSW) \
48 X("inplace", 0, INPLSW) \
49 X("noinplace", 0, NINPLSW) \
50 X("whatnowproc program", 0, WHATSW) \
51 X("nowhatnowproc", 0, NWHATSW) \
52 X("version", 0, VERSIONSW) \
53 X("help", 0, HELPSW) \
54 X("file file", -4, FILESW) \
55 X("from address", 0, FROMSW) \
56 X("to address", 0, TOSW) \
57 X("cc address", 0, CCSW) \
58 X("fcc mailbox", 0, FCCSW) \
59 X("width columns", 0, WIDTHSW) \
60 X("atfile", 0, ATFILESW) \
61 X("noatfile", 0, NOATFILESW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(DIST
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(DIST
, switches
);
71 #define DISPO_SWITCHES \
73 X("replace", 0, YESW) \
74 X("list", 0, LISTDSW) \
75 X("refile +folder", 0, REFILSW) \
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(DISPO
);
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
87 static struct swit aqrl
[] = {
89 { "replace", 0, YESW
},
90 { "list", 0, LISTDSW
},
91 { "refile +folder", 0, REFILSW
},
97 main (int argc
, char **argv
)
103 int i
, in
, isdf
= 0, out
;
104 int outputlinelen
= OUTPUTLINELEN
;
107 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
108 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
109 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
110 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
111 char **argp
, **arguments
;
112 struct msgs
*mp
= NULL
;
115 if (nmh_init(argv
[0], true, true)) { return 1; }
117 arguments
= getarguments (invo_name
, argc
, argv
, 1);
120 while ((cp
= *argp
++)) {
122 switch (smatch (++cp
, switches
)) {
124 ambigsw (cp
, switches
);
127 die("-%s unknown", cp
);
130 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
132 print_help (buf
, switches
, 1);
135 print_version(invo_name
);
146 if (!(ed
= *argp
++) || *ed
== '-')
147 die("missing argument to %s", argp
[-2]);
155 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
156 die("missing argument to %s", argp
[-2]);
165 die("only one file at a time!");
166 if (!(cp
= *argp
++) || *cp
== '-')
167 die("missing argument to %s", argp
[-2]);
168 file
= path (cp
, TFILE
);
171 if (!(form
= *argp
++) || *form
== '-')
172 die("missing argument to %s", argp
[-2]);
184 die("only one draft folder at a time!");
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
187 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
188 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
192 die("only one draft message at a time!");
193 if (!(dmsg
= *argp
++) || *dmsg
== '-')
194 die("missing argument to %s", argp
[-2]);
202 if (!(cp
= *argp
++) || *cp
== '-')
203 die("missing argument to %s", argp
[-2]);
204 from
= addlist(from
, cp
);
207 if (!(cp
= *argp
++) || *cp
== '-')
208 die("missing argument to %s", argp
[-2]);
209 to
= addlist(to
, cp
);
212 if (!(cp
= *argp
++) || *cp
== '-')
213 die("missing argument to %s", argp
[-2]);
214 cc
= addlist(cc
, cp
);
217 if (!(cp
= *argp
++) || *cp
== '-')
218 die("missing argument to %s", argp
[-2]);
219 fcc
= addlist(fcc
, cp
);
223 if (!(cp
= *argp
++) || *cp
== '-')
224 die("missing argument to %s", argp
[-2]);
225 if ((outputlinelen
= atoi(cp
)) < 10)
226 die("impossible width %d", outputlinelen
);
237 if (*cp
== '+' || *cp
== '@') {
239 die("only one folder at a time!");
240 folder
= pluspath (cp
);
243 die("only one message at a time!");
248 cwd
= mh_xstrdup(pwd ());
250 if (!context_find ("path"))
251 free (path ("./", TFOLDER
));
252 if (file
&& (msg
|| folder
))
253 die("can't mix files and folders/msgs");
256 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
258 /* Check if draft already exists */
259 if (stat (drft
, &st
) != NOTOK
) {
260 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
261 for (i
= LISTDSW
; i
!= YESW
;) {
262 if (!(argp
= read_switch_multiword ("\nDisposition? ",
263 isdf
? aqrnl
: aqrl
)))
265 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
274 showfile (++argp
, drft
);
277 if (refile (++argp
, drft
) == 0)
291 anot
= false; /* don't want to annotate a file */
299 folder
= getfolder (1);
300 maildir
= m_maildir (folder
);
302 if (chdir (maildir
) == NOTOK
)
303 adios (maildir
, "unable to change directory to");
305 /* read folder and create message structure */
306 if (!(mp
= folder_read (folder
, 1)))
307 die("unable to read folder %s", folder
);
309 /* check for empty folder */
311 die("no messages in %s", folder
);
313 /* parse the message range/sequence/name and set SELECTED */
314 if (!m_convert (mp
, msg
))
316 seq_setprev (mp
); /* set the previous-sequence */
319 die("only one message at a time!");
322 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
324 dat
[0] = mp
? mp
->lowsel
: 0;
327 dat
[3] = outputlinelen
;
333 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
335 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
336 adios (drft
, "unable to create");
338 cpydata (in
, out
, form
, drft
);
342 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
343 adios (msgnam
, "unable to open message");
346 context_replace (pfolder
, folder
);/* update current folder */
347 seq_setcur (mp
, mp
->lowsel
); /* update current message */
348 seq_save (mp
); /* synchronize sequences */
349 context_save (); /* save the context file */
354 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
355 inplace
, cwd
, atfile
);