]>
diplodocus.org Git - nmh/blob - uip/dist.c
4bfec7d1ec40f7764ad54497b5d58ef29df09cda
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.
11 #include "sbr/m_maildir.h"
16 #define DIST_SWITCHES \
17 X("annotate", 0, ANNOSW) \
18 X("noannotate", 0, NANNOSW) \
19 X("draftfolder +folder", 0, DFOLDSW) \
20 X("draftmessage msg", 0, DMSGSW) \
21 X("nodraftfolder", 0, NDFLDSW) \
22 X("editor editor", 0, EDITRSW) \
23 X("noedit", 0, NEDITSW) \
24 X("form formfile", 0, FORMSW) \
25 X("inplace", 0, INPLSW) \
26 X("noinplace", 0, NINPLSW) \
27 X("whatnowproc program", 0, WHATSW) \
28 X("nowhatnowproc", 0, NWHATSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
31 X("file file", -4, FILESW) \
32 X("from address", 0, FROMSW) \
33 X("to address", 0, TOSW) \
34 X("cc address", 0, CCSW) \
35 X("fcc mailbox", 0, FCCSW) \
36 X("width columns", 0, WIDTHSW) \
37 X("atfile", 0, ATFILESW) \
38 X("noatfile", 0, NOATFILESW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(DIST
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(DIST
, switches
);
48 #define DISPO_SWITCHES \
50 X("replace", 0, YESW) \
51 X("list", 0, LISTDSW) \
52 X("refile +folder", 0, REFILSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(DISPO
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
64 static struct swit aqrl
[] = {
66 { "replace", 0, YESW
},
67 { "list", 0, LISTDSW
},
68 { "refile +folder", 0, REFILSW
},
74 main (int argc
, char **argv
)
80 int i
, in
, isdf
= 0, out
;
81 int outputlinelen
= OUTPUTLINELEN
;
84 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
85 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
86 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
87 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
88 char **argp
, **arguments
;
89 struct msgs
*mp
= NULL
;
92 if (nmh_init(argv
[0], true, true)) { return 1; }
94 arguments
= getarguments (invo_name
, argc
, argv
, 1);
97 while ((cp
= *argp
++)) {
99 switch (smatch (++cp
, switches
)) {
101 ambigsw (cp
, switches
);
104 die("-%s unknown", cp
);
107 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
109 print_help (buf
, switches
, 1);
112 print_version(invo_name
);
123 if (!(ed
= *argp
++) || *ed
== '-')
124 die("missing argument to %s", argp
[-2]);
132 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
133 die("missing argument to %s", argp
[-2]);
142 die("only one file at a time!");
143 if (!(cp
= *argp
++) || *cp
== '-')
144 die("missing argument to %s", argp
[-2]);
145 file
= path (cp
, TFILE
);
148 if (!(form
= *argp
++) || *form
== '-')
149 die("missing argument to %s", argp
[-2]);
161 die("only one draft folder at a time!");
162 if (!(cp
= *argp
++) || *cp
== '-')
163 die("missing argument to %s", argp
[-2]);
164 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
165 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
169 die("only one draft message at a time!");
170 if (!(dmsg
= *argp
++) || *dmsg
== '-')
171 die("missing argument to %s", argp
[-2]);
179 if (!(cp
= *argp
++) || *cp
== '-')
180 die("missing argument to %s", argp
[-2]);
181 from
= addlist(from
, cp
);
184 if (!(cp
= *argp
++) || *cp
== '-')
185 die("missing argument to %s", argp
[-2]);
186 to
= addlist(to
, cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
191 cc
= addlist(cc
, cp
);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
196 fcc
= addlist(fcc
, cp
);
200 if (!(cp
= *argp
++) || *cp
== '-')
201 die("missing argument to %s", argp
[-2]);
202 if ((outputlinelen
= atoi(cp
)) < 10)
203 die("impossible width %d", outputlinelen
);
214 if (*cp
== '+' || *cp
== '@') {
216 die("only one folder at a time!");
217 folder
= pluspath (cp
);
220 die("only one message at a time!");
225 cwd
= mh_xstrdup(pwd ());
227 if (!context_find ("path"))
228 free (path ("./", TFOLDER
));
229 if (file
&& (msg
|| folder
))
230 die("can't mix files and folders/msgs");
233 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
235 /* Check if draft already exists */
236 if (stat (drft
, &st
) != NOTOK
) {
237 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
238 for (i
= LISTDSW
; i
!= YESW
;) {
239 if (!(argp
= read_switch_multiword ("\nDisposition? ",
240 isdf
? aqrnl
: aqrl
)))
242 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
251 showfile (++argp
, drft
);
254 if (refile (++argp
, drft
) == 0)
268 anot
= false; /* don't want to annotate a file */
276 folder
= getfolder (1);
277 maildir
= m_maildir (folder
);
279 if (chdir (maildir
) == NOTOK
)
280 adios (maildir
, "unable to change directory to");
282 /* read folder and create message structure */
283 if (!(mp
= folder_read (folder
, 1)))
284 die("unable to read folder %s", folder
);
286 /* check for empty folder */
288 die("no messages in %s", folder
);
290 /* parse the message range/sequence/name and set SELECTED */
291 if (!m_convert (mp
, msg
))
293 seq_setprev (mp
); /* set the previous-sequence */
296 die("only one message at a time!");
299 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
301 dat
[0] = mp
? mp
->lowsel
: 0;
304 dat
[3] = outputlinelen
;
310 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
312 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
313 adios (drft
, "unable to create");
315 cpydata (in
, out
, form
, drft
);
319 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
320 adios (msgnam
, "unable to open message");
323 context_replace (pfolder
, folder
);/* update current folder */
324 seq_setcur (mp
, mp
->lowsel
); /* update current message */
325 seq_save (mp
); /* synchronize sequences */
326 context_save (); /* save the context file */
331 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
332 inplace
, cwd
, atfile
);