]>
diplodocus.org Git - nmh/blob - uip/dist.c
1b5bca89ec39008c68babb57befea71ee88936bd
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/context_find.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
16 #include "sbr/m_maildir.h"
21 #define DIST_SWITCHES \
22 X("annotate", 0, ANNOSW) \
23 X("noannotate", 0, NANNOSW) \
24 X("draftfolder +folder", 0, DFOLDSW) \
25 X("draftmessage msg", 0, DMSGSW) \
26 X("nodraftfolder", 0, NDFLDSW) \
27 X("editor editor", 0, EDITRSW) \
28 X("noedit", 0, NEDITSW) \
29 X("form formfile", 0, FORMSW) \
30 X("inplace", 0, INPLSW) \
31 X("noinplace", 0, NINPLSW) \
32 X("whatnowproc program", 0, WHATSW) \
33 X("nowhatnowproc", 0, NWHATSW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
36 X("file file", -4, FILESW) \
37 X("from address", 0, FROMSW) \
38 X("to address", 0, TOSW) \
39 X("cc address", 0, CCSW) \
40 X("fcc mailbox", 0, FCCSW) \
41 X("width columns", 0, WIDTHSW) \
42 X("atfile", 0, ATFILESW) \
43 X("noatfile", 0, NOATFILESW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(DIST
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(DIST
, switches
);
53 #define DISPO_SWITCHES \
55 X("replace", 0, YESW) \
56 X("list", 0, LISTDSW) \
57 X("refile +folder", 0, REFILSW) \
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(DISPO
);
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
69 static struct swit aqrl
[] = {
71 { "replace", 0, YESW
},
72 { "list", 0, LISTDSW
},
73 { "refile +folder", 0, REFILSW
},
79 main (int argc
, char **argv
)
85 int i
, in
, isdf
= 0, out
;
86 int outputlinelen
= OUTPUTLINELEN
;
89 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
90 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
91 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
92 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
93 char **argp
, **arguments
;
94 struct msgs
*mp
= NULL
;
97 if (nmh_init(argv
[0], true, true)) { return 1; }
99 arguments
= getarguments (invo_name
, argc
, argv
, 1);
102 while ((cp
= *argp
++)) {
104 switch (smatch (++cp
, switches
)) {
106 ambigsw (cp
, switches
);
109 die("-%s unknown", cp
);
112 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
114 print_help (buf
, switches
, 1);
117 print_version(invo_name
);
128 if (!(ed
= *argp
++) || *ed
== '-')
129 die("missing argument to %s", argp
[-2]);
137 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
138 die("missing argument to %s", argp
[-2]);
147 die("only one file at a time!");
148 if (!(cp
= *argp
++) || *cp
== '-')
149 die("missing argument to %s", argp
[-2]);
150 file
= path (cp
, TFILE
);
153 if (!(form
= *argp
++) || *form
== '-')
154 die("missing argument to %s", argp
[-2]);
166 die("only one draft folder at a time!");
167 if (!(cp
= *argp
++) || *cp
== '-')
168 die("missing argument to %s", argp
[-2]);
169 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
170 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
174 die("only one draft message at a time!");
175 if (!(dmsg
= *argp
++) || *dmsg
== '-')
176 die("missing argument to %s", argp
[-2]);
184 if (!(cp
= *argp
++) || *cp
== '-')
185 die("missing argument to %s", argp
[-2]);
186 from
= addlist(from
, cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
191 to
= addlist(to
, cp
);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
196 cc
= addlist(cc
, cp
);
199 if (!(cp
= *argp
++) || *cp
== '-')
200 die("missing argument to %s", argp
[-2]);
201 fcc
= addlist(fcc
, cp
);
205 if (!(cp
= *argp
++) || *cp
== '-')
206 die("missing argument to %s", argp
[-2]);
207 if ((outputlinelen
= atoi(cp
)) < 10)
208 die("impossible width %d", outputlinelen
);
219 if (*cp
== '+' || *cp
== '@') {
221 die("only one folder at a time!");
222 folder
= pluspath (cp
);
225 die("only one message at a time!");
230 cwd
= mh_xstrdup(pwd ());
232 if (!context_find ("path"))
233 free (path ("./", TFOLDER
));
234 if (file
&& (msg
|| folder
))
235 die("can't mix files and folders/msgs");
238 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
240 /* Check if draft already exists */
241 if (stat (drft
, &st
) != NOTOK
) {
242 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
243 for (i
= LISTDSW
; i
!= YESW
;) {
244 if (!(argp
= read_switch_multiword ("\nDisposition? ",
245 isdf
? aqrnl
: aqrl
)))
247 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
256 showfile (++argp
, drft
);
259 if (refile (++argp
, drft
) == 0)
273 anot
= false; /* don't want to annotate a file */
281 folder
= getfolder (1);
282 maildir
= m_maildir (folder
);
284 if (chdir (maildir
) == NOTOK
)
285 adios (maildir
, "unable to change directory to");
287 /* read folder and create message structure */
288 if (!(mp
= folder_read (folder
, 1)))
289 die("unable to read folder %s", folder
);
291 /* check for empty folder */
293 die("no messages in %s", folder
);
295 /* parse the message range/sequence/name and set SELECTED */
296 if (!m_convert (mp
, msg
))
298 seq_setprev (mp
); /* set the previous-sequence */
301 die("only one message at a time!");
304 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
306 dat
[0] = mp
? mp
->lowsel
: 0;
309 dat
[3] = outputlinelen
;
315 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
317 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
318 adios (drft
, "unable to create");
320 cpydata (in
, out
, form
, drft
);
324 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
325 adios (msgnam
, "unable to open message");
328 context_replace (pfolder
, folder
);/* update current folder */
329 seq_setcur (mp
, mp
->lowsel
); /* update current message */
330 seq_save (mp
); /* synchronize sequences */
331 context_save (); /* save the context file */
336 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
337 inplace
, cwd
, atfile
);