]>
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 "sbr/folder_read.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
19 #include "sbr/m_maildir.h"
24 #define DIST_SWITCHES \
25 X("annotate", 0, ANNOSW) \
26 X("noannotate", 0, NANNOSW) \
27 X("draftfolder +folder", 0, DFOLDSW) \
28 X("draftmessage msg", 0, DMSGSW) \
29 X("nodraftfolder", 0, NDFLDSW) \
30 X("editor editor", 0, EDITRSW) \
31 X("noedit", 0, NEDITSW) \
32 X("form formfile", 0, FORMSW) \
33 X("inplace", 0, INPLSW) \
34 X("noinplace", 0, NINPLSW) \
35 X("whatnowproc program", 0, WHATSW) \
36 X("nowhatnowproc", 0, NWHATSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("file file", -4, FILESW) \
40 X("from address", 0, FROMSW) \
41 X("to address", 0, TOSW) \
42 X("cc address", 0, CCSW) \
43 X("fcc mailbox", 0, FCCSW) \
44 X("width columns", 0, WIDTHSW) \
45 X("atfile", 0, ATFILESW) \
46 X("noatfile", 0, NOATFILESW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(DIST
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(DIST
, switches
);
56 #define DISPO_SWITCHES \
58 X("replace", 0, YESW) \
59 X("list", 0, LISTDSW) \
60 X("refile +folder", 0, REFILSW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(DISPO
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
72 static struct swit aqrl
[] = {
74 { "replace", 0, YESW
},
75 { "list", 0, LISTDSW
},
76 { "refile +folder", 0, REFILSW
},
82 main (int argc
, char **argv
)
88 int i
, in
, isdf
= 0, out
;
89 int outputlinelen
= OUTPUTLINELEN
;
92 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
93 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
94 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
95 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
96 char **argp
, **arguments
;
97 struct msgs
*mp
= NULL
;
100 if (nmh_init(argv
[0], true, true)) { return 1; }
102 arguments
= getarguments (invo_name
, argc
, argv
, 1);
105 while ((cp
= *argp
++)) {
107 switch (smatch (++cp
, switches
)) {
109 ambigsw (cp
, switches
);
112 die("-%s unknown", cp
);
115 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
117 print_help (buf
, switches
, 1);
120 print_version(invo_name
);
131 if (!(ed
= *argp
++) || *ed
== '-')
132 die("missing argument to %s", argp
[-2]);
140 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
141 die("missing argument to %s", argp
[-2]);
150 die("only one file at a time!");
151 if (!(cp
= *argp
++) || *cp
== '-')
152 die("missing argument to %s", argp
[-2]);
153 file
= path (cp
, TFILE
);
156 if (!(form
= *argp
++) || *form
== '-')
157 die("missing argument to %s", argp
[-2]);
169 die("only one draft folder at a time!");
170 if (!(cp
= *argp
++) || *cp
== '-')
171 die("missing argument to %s", argp
[-2]);
172 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
173 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
177 die("only one draft message at a time!");
178 if (!(dmsg
= *argp
++) || *dmsg
== '-')
179 die("missing argument to %s", argp
[-2]);
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 from
= addlist(from
, cp
);
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
194 to
= addlist(to
, cp
);
197 if (!(cp
= *argp
++) || *cp
== '-')
198 die("missing argument to %s", argp
[-2]);
199 cc
= addlist(cc
, cp
);
202 if (!(cp
= *argp
++) || *cp
== '-')
203 die("missing argument to %s", argp
[-2]);
204 fcc
= addlist(fcc
, cp
);
208 if (!(cp
= *argp
++) || *cp
== '-')
209 die("missing argument to %s", argp
[-2]);
210 if ((outputlinelen
= atoi(cp
)) < 10)
211 die("impossible width %d", outputlinelen
);
222 if (*cp
== '+' || *cp
== '@') {
224 die("only one folder at a time!");
225 folder
= pluspath (cp
);
228 die("only one message at a time!");
233 cwd
= mh_xstrdup(pwd ());
235 if (!context_find ("path"))
236 free (path ("./", TFOLDER
));
237 if (file
&& (msg
|| folder
))
238 die("can't mix files and folders/msgs");
241 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
243 /* Check if draft already exists */
244 if (stat (drft
, &st
) != NOTOK
) {
245 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
246 for (i
= LISTDSW
; i
!= YESW
;) {
247 if (!(argp
= read_switch_multiword ("\nDisposition? ",
248 isdf
? aqrnl
: aqrl
)))
250 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
259 showfile (++argp
, drft
);
262 if (refile (++argp
, drft
) == 0)
276 anot
= false; /* don't want to annotate a file */
284 folder
= getfolder (1);
285 maildir
= m_maildir (folder
);
287 if (chdir (maildir
) == NOTOK
)
288 adios (maildir
, "unable to change directory to");
290 /* read folder and create message structure */
291 if (!(mp
= folder_read (folder
, 1)))
292 die("unable to read folder %s", folder
);
294 /* check for empty folder */
296 die("no messages in %s", folder
);
298 /* parse the message range/sequence/name and set SELECTED */
299 if (!m_convert (mp
, msg
))
301 seq_setprev (mp
); /* set the previous-sequence */
304 die("only one message at a time!");
307 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
309 dat
[0] = mp
? mp
->lowsel
: 0;
312 dat
[3] = outputlinelen
;
318 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
320 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
321 adios (drft
, "unable to create");
323 cpydata (in
, out
, form
, drft
);
327 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
328 adios (msgnam
, "unable to open message");
331 context_replace (pfolder
, folder
);/* update current folder */
332 seq_setcur (mp
, mp
->lowsel
); /* update current message */
333 seq_save (mp
); /* synchronize sequences */
334 context_save (); /* save the context file */
339 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
340 inplace
, cwd
, atfile
);