]>
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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
14 #include "sbr/m_maildir.h"
19 #define DIST_SWITCHES \
20 X("annotate", 0, ANNOSW) \
21 X("noannotate", 0, NANNOSW) \
22 X("draftfolder +folder", 0, DFOLDSW) \
23 X("draftmessage msg", 0, DMSGSW) \
24 X("nodraftfolder", 0, NDFLDSW) \
25 X("editor editor", 0, EDITRSW) \
26 X("noedit", 0, NEDITSW) \
27 X("form formfile", 0, FORMSW) \
28 X("inplace", 0, INPLSW) \
29 X("noinplace", 0, NINPLSW) \
30 X("whatnowproc program", 0, WHATSW) \
31 X("nowhatnowproc", 0, NWHATSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
34 X("file file", -4, FILESW) \
35 X("from address", 0, FROMSW) \
36 X("to address", 0, TOSW) \
37 X("cc address", 0, CCSW) \
38 X("fcc mailbox", 0, FCCSW) \
39 X("width columns", 0, WIDTHSW) \
40 X("atfile", 0, ATFILESW) \
41 X("noatfile", 0, NOATFILESW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(DIST
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(DIST
, switches
);
51 #define DISPO_SWITCHES \
53 X("replace", 0, YESW) \
54 X("list", 0, LISTDSW) \
55 X("refile +folder", 0, REFILSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(DISPO
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
67 static struct swit aqrl
[] = {
69 { "replace", 0, YESW
},
70 { "list", 0, LISTDSW
},
71 { "refile +folder", 0, REFILSW
},
77 main (int argc
, char **argv
)
83 int i
, in
, isdf
= 0, out
;
84 int outputlinelen
= OUTPUTLINELEN
;
87 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
88 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
89 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
90 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
91 char **argp
, **arguments
;
92 struct msgs
*mp
= NULL
;
95 if (nmh_init(argv
[0], true, true)) { return 1; }
97 arguments
= getarguments (invo_name
, argc
, argv
, 1);
100 while ((cp
= *argp
++)) {
102 switch (smatch (++cp
, switches
)) {
104 ambigsw (cp
, switches
);
107 die("-%s unknown", cp
);
110 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
112 print_help (buf
, switches
, 1);
115 print_version(invo_name
);
126 if (!(ed
= *argp
++) || *ed
== '-')
127 die("missing argument to %s", argp
[-2]);
135 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
136 die("missing argument to %s", argp
[-2]);
145 die("only one file at a time!");
146 if (!(cp
= *argp
++) || *cp
== '-')
147 die("missing argument to %s", argp
[-2]);
148 file
= path (cp
, TFILE
);
151 if (!(form
= *argp
++) || *form
== '-')
152 die("missing argument to %s", argp
[-2]);
164 die("only one draft folder at a time!");
165 if (!(cp
= *argp
++) || *cp
== '-')
166 die("missing argument to %s", argp
[-2]);
167 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
168 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
172 die("only one draft message at a time!");
173 if (!(dmsg
= *argp
++) || *dmsg
== '-')
174 die("missing argument to %s", argp
[-2]);
182 if (!(cp
= *argp
++) || *cp
== '-')
183 die("missing argument to %s", argp
[-2]);
184 from
= addlist(from
, cp
);
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 to
= addlist(to
, cp
);
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
194 cc
= addlist(cc
, cp
);
197 if (!(cp
= *argp
++) || *cp
== '-')
198 die("missing argument to %s", argp
[-2]);
199 fcc
= addlist(fcc
, cp
);
203 if (!(cp
= *argp
++) || *cp
== '-')
204 die("missing argument to %s", argp
[-2]);
205 if ((outputlinelen
= atoi(cp
)) < 10)
206 die("impossible width %d", outputlinelen
);
217 if (*cp
== '+' || *cp
== '@') {
219 die("only one folder at a time!");
220 folder
= pluspath (cp
);
223 die("only one message at a time!");
228 cwd
= mh_xstrdup(pwd ());
230 if (!context_find ("path"))
231 free (path ("./", TFOLDER
));
232 if (file
&& (msg
|| folder
))
233 die("can't mix files and folders/msgs");
236 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
238 /* Check if draft already exists */
239 if (stat (drft
, &st
) != NOTOK
) {
240 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
241 for (i
= LISTDSW
; i
!= YESW
;) {
242 if (!(argp
= read_switch_multiword ("\nDisposition? ",
243 isdf
? aqrnl
: aqrl
)))
245 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
254 showfile (++argp
, drft
);
257 if (refile (++argp
, drft
) == 0)
271 anot
= false; /* don't want to annotate a file */
279 folder
= getfolder (1);
280 maildir
= m_maildir (folder
);
282 if (chdir (maildir
) == NOTOK
)
283 adios (maildir
, "unable to change directory to");
285 /* read folder and create message structure */
286 if (!(mp
= folder_read (folder
, 1)))
287 die("unable to read folder %s", folder
);
289 /* check for empty folder */
291 die("no messages in %s", folder
);
293 /* parse the message range/sequence/name and set SELECTED */
294 if (!m_convert (mp
, msg
))
296 seq_setprev (mp
); /* set the previous-sequence */
299 die("only one message at a time!");
302 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
304 dat
[0] = mp
? mp
->lowsel
: 0;
307 dat
[3] = outputlinelen
;
313 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
315 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
316 adios (drft
, "unable to create");
318 cpydata (in
, out
, form
, drft
);
322 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
323 adios (msgnam
, "unable to open message");
326 context_replace (pfolder
, folder
);/* update current folder */
327 seq_setcur (mp
, mp
->lowsel
); /* update current message */
328 seq_save (mp
); /* synchronize sequences */
329 context_save (); /* save the context file */
334 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
335 inplace
, cwd
, atfile
);