]>
diplodocus.org Git - nmh/blob - uip/dist.c
30ae67d9c449107e366b33f738ac2099ed5fd93e
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/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/read_switch_multiword.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_setcur.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/showfile.h"
17 #include "sbr/smatch.h"
18 #include "sbr/refile.h"
19 #include "sbr/cpydata.h"
20 #include "sbr/m_draft.h"
21 #include "sbr/m_convert.h"
22 #include "sbr/getfolder.h"
23 #include "sbr/folder_read.h"
24 #include "sbr/context_save.h"
25 #include "sbr/context_replace.h"
26 #include "sbr/context_find.h"
27 #include "sbr/ambigsw.h"
29 #include "sbr/print_version.h"
30 #include "sbr/print_help.h"
31 #include "sbr/error.h"
33 #include "sbr/m_maildir.h"
38 #define DIST_SWITCHES \
39 X("annotate", 0, ANNOSW) \
40 X("noannotate", 0, NANNOSW) \
41 X("draftfolder +folder", 0, DFOLDSW) \
42 X("draftmessage msg", 0, DMSGSW) \
43 X("nodraftfolder", 0, NDFLDSW) \
44 X("editor editor", 0, EDITRSW) \
45 X("noedit", 0, NEDITSW) \
46 X("form formfile", 0, FORMSW) \
47 X("inplace", 0, INPLSW) \
48 X("noinplace", 0, NINPLSW) \
49 X("whatnowproc program", 0, WHATSW) \
50 X("nowhatnowproc", 0, NWHATSW) \
51 X("version", 0, VERSIONSW) \
52 X("help", 0, HELPSW) \
53 X("file file", -4, FILESW) \
54 X("from address", 0, FROMSW) \
55 X("to address", 0, TOSW) \
56 X("cc address", 0, CCSW) \
57 X("fcc mailbox", 0, FCCSW) \
58 X("width columns", 0, WIDTHSW) \
59 X("atfile", 0, ATFILESW) \
60 X("noatfile", 0, NOATFILESW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(DIST
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(DIST
, switches
);
70 #define DISPO_SWITCHES \
72 X("replace", 0, YESW) \
73 X("list", 0, LISTDSW) \
74 X("refile +folder", 0, REFILSW) \
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(DISPO
);
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
86 static struct swit aqrl
[] = {
88 { "replace", 0, YESW
},
89 { "list", 0, LISTDSW
},
90 { "refile +folder", 0, REFILSW
},
96 main (int argc
, char **argv
)
102 int i
, in
, isdf
= 0, out
;
103 int outputlinelen
= OUTPUTLINELEN
;
106 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
107 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
108 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
109 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
110 char **argp
, **arguments
;
111 struct msgs
*mp
= NULL
;
114 if (nmh_init(argv
[0], true, true)) { return 1; }
116 arguments
= getarguments (invo_name
, argc
, argv
, 1);
119 while ((cp
= *argp
++)) {
121 switch (smatch (++cp
, switches
)) {
123 ambigsw (cp
, switches
);
126 die("-%s unknown", cp
);
129 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
131 print_help (buf
, switches
, 1);
134 print_version(invo_name
);
145 if (!(ed
= *argp
++) || *ed
== '-')
146 die("missing argument to %s", argp
[-2]);
154 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
155 die("missing argument to %s", argp
[-2]);
164 die("only one file at a time!");
165 if (!(cp
= *argp
++) || *cp
== '-')
166 die("missing argument to %s", argp
[-2]);
167 file
= path (cp
, TFILE
);
170 if (!(form
= *argp
++) || *form
== '-')
171 die("missing argument to %s", argp
[-2]);
183 die("only one draft folder at a time!");
184 if (!(cp
= *argp
++) || *cp
== '-')
185 die("missing argument to %s", argp
[-2]);
186 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
187 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
191 die("only one draft message at a time!");
192 if (!(dmsg
= *argp
++) || *dmsg
== '-')
193 die("missing argument to %s", argp
[-2]);
201 if (!(cp
= *argp
++) || *cp
== '-')
202 die("missing argument to %s", argp
[-2]);
203 from
= addlist(from
, cp
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 die("missing argument to %s", argp
[-2]);
208 to
= addlist(to
, cp
);
211 if (!(cp
= *argp
++) || *cp
== '-')
212 die("missing argument to %s", argp
[-2]);
213 cc
= addlist(cc
, cp
);
216 if (!(cp
= *argp
++) || *cp
== '-')
217 die("missing argument to %s", argp
[-2]);
218 fcc
= addlist(fcc
, cp
);
222 if (!(cp
= *argp
++) || *cp
== '-')
223 die("missing argument to %s", argp
[-2]);
224 if ((outputlinelen
= atoi(cp
)) < 10)
225 die("impossible width %d", outputlinelen
);
236 if (*cp
== '+' || *cp
== '@') {
238 die("only one folder at a time!");
239 folder
= pluspath (cp
);
242 die("only one message at a time!");
247 cwd
= mh_xstrdup(pwd ());
249 if (!context_find ("path"))
250 free (path ("./", TFOLDER
));
251 if (file
&& (msg
|| folder
))
252 die("can't mix files and folders/msgs");
255 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
257 /* Check if draft already exists */
258 if (stat (drft
, &st
) != NOTOK
) {
259 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
260 for (i
= LISTDSW
; i
!= YESW
;) {
261 if (!(argp
= read_switch_multiword ("\nDisposition? ",
262 isdf
? aqrnl
: aqrl
)))
264 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
273 showfile (++argp
, drft
);
276 if (refile (++argp
, drft
) == 0)
290 anot
= false; /* don't want to annotate a file */
298 folder
= getfolder (1);
299 maildir
= m_maildir (folder
);
301 if (chdir (maildir
) == NOTOK
)
302 adios (maildir
, "unable to change directory to");
304 /* read folder and create message structure */
305 if (!(mp
= folder_read (folder
, 1)))
306 die("unable to read folder %s", folder
);
308 /* check for empty folder */
310 die("no messages in %s", folder
);
312 /* parse the message range/sequence/name and set SELECTED */
313 if (!m_convert (mp
, msg
))
315 seq_setprev (mp
); /* set the previous-sequence */
318 die("only one message at a time!");
321 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
323 dat
[0] = mp
? mp
->lowsel
: 0;
326 dat
[3] = outputlinelen
;
332 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
334 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
335 adios (drft
, "unable to create");
337 cpydata (in
, out
, form
, drft
);
341 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
342 adios (msgnam
, "unable to open message");
345 context_replace (pfolder
, folder
);/* update current folder */
346 seq_setcur (mp
, mp
->lowsel
); /* update current message */
347 seq_save (mp
); /* synchronize sequences */
348 context_save (); /* save the context file */
353 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
354 inplace
, cwd
, atfile
);