]>
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/m_maildir.h"
15 #define DIST_SWITCHES \
16 X("annotate", 0, ANNOSW) \
17 X("noannotate", 0, NANNOSW) \
18 X("draftfolder +folder", 0, DFOLDSW) \
19 X("draftmessage msg", 0, DMSGSW) \
20 X("nodraftfolder", 0, NDFLDSW) \
21 X("editor editor", 0, EDITRSW) \
22 X("noedit", 0, NEDITSW) \
23 X("form formfile", 0, FORMSW) \
24 X("inplace", 0, INPLSW) \
25 X("noinplace", 0, NINPLSW) \
26 X("whatnowproc program", 0, WHATSW) \
27 X("nowhatnowproc", 0, NWHATSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
30 X("file file", -4, FILESW) \
31 X("from address", 0, FROMSW) \
32 X("to address", 0, TOSW) \
33 X("cc address", 0, CCSW) \
34 X("fcc mailbox", 0, FCCSW) \
35 X("width columns", 0, WIDTHSW) \
36 X("atfile", 0, ATFILESW) \
37 X("noatfile", 0, NOATFILESW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(DIST
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(DIST
, switches
);
47 #define DISPO_SWITCHES \
49 X("replace", 0, YESW) \
50 X("list", 0, LISTDSW) \
51 X("refile +folder", 0, REFILSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(DISPO
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
63 static struct swit aqrl
[] = {
65 { "replace", 0, YESW
},
66 { "list", 0, LISTDSW
},
67 { "refile +folder", 0, REFILSW
},
73 main (int argc
, char **argv
)
79 int i
, in
, isdf
= 0, out
;
80 int outputlinelen
= OUTPUTLINELEN
;
83 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
84 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
85 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
86 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
87 char **argp
, **arguments
;
88 struct msgs
*mp
= NULL
;
91 if (nmh_init(argv
[0], true, true)) { return 1; }
93 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 die("-%s unknown", cp
);
106 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
122 if (!(ed
= *argp
++) || *ed
== '-')
123 die("missing argument to %s", argp
[-2]);
131 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
132 die("missing argument to %s", argp
[-2]);
141 die("only one file at a time!");
142 if (!(cp
= *argp
++) || *cp
== '-')
143 die("missing argument to %s", argp
[-2]);
144 file
= path (cp
, TFILE
);
147 if (!(form
= *argp
++) || *form
== '-')
148 die("missing argument to %s", argp
[-2]);
160 die("only one draft folder at a time!");
161 if (!(cp
= *argp
++) || *cp
== '-')
162 die("missing argument to %s", argp
[-2]);
163 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
164 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
168 die("only one draft message at a time!");
169 if (!(dmsg
= *argp
++) || *dmsg
== '-')
170 die("missing argument to %s", argp
[-2]);
178 if (!(cp
= *argp
++) || *cp
== '-')
179 die("missing argument to %s", argp
[-2]);
180 from
= addlist(from
, cp
);
183 if (!(cp
= *argp
++) || *cp
== '-')
184 die("missing argument to %s", argp
[-2]);
185 to
= addlist(to
, cp
);
188 if (!(cp
= *argp
++) || *cp
== '-')
189 die("missing argument to %s", argp
[-2]);
190 cc
= addlist(cc
, cp
);
193 if (!(cp
= *argp
++) || *cp
== '-')
194 die("missing argument to %s", argp
[-2]);
195 fcc
= addlist(fcc
, cp
);
199 if (!(cp
= *argp
++) || *cp
== '-')
200 die("missing argument to %s", argp
[-2]);
201 if ((outputlinelen
= atoi(cp
)) < 10)
202 die("impossible width %d", outputlinelen
);
213 if (*cp
== '+' || *cp
== '@') {
215 die("only one folder at a time!");
216 folder
= pluspath (cp
);
219 die("only one message at a time!");
224 cwd
= mh_xstrdup(pwd ());
226 if (!context_find ("path"))
227 free (path ("./", TFOLDER
));
228 if (file
&& (msg
|| folder
))
229 die("can't mix files and folders/msgs");
232 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
234 /* Check if draft already exists */
235 if (stat (drft
, &st
) != NOTOK
) {
236 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
237 for (i
= LISTDSW
; i
!= YESW
;) {
238 if (!(argp
= read_switch_multiword ("\nDisposition? ",
239 isdf
? aqrnl
: aqrl
)))
241 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
250 showfile (++argp
, drft
);
253 if (refile (++argp
, drft
) == 0)
267 anot
= false; /* don't want to annotate a file */
275 folder
= getfolder (1);
276 maildir
= m_maildir (folder
);
278 if (chdir (maildir
) == NOTOK
)
279 adios (maildir
, "unable to change directory to");
281 /* read folder and create message structure */
282 if (!(mp
= folder_read (folder
, 1)))
283 die("unable to read folder %s", folder
);
285 /* check for empty folder */
287 die("no messages in %s", folder
);
289 /* parse the message range/sequence/name and set SELECTED */
290 if (!m_convert (mp
, msg
))
292 seq_setprev (mp
); /* set the previous-sequence */
295 die("only one message at a time!");
298 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
300 dat
[0] = mp
? mp
->lowsel
: 0;
303 dat
[3] = outputlinelen
;
309 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
311 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
312 adios (drft
, "unable to create");
314 cpydata (in
, out
, form
, drft
);
318 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
319 adios (msgnam
, "unable to open message");
322 context_replace (pfolder
, folder
);/* update current folder */
323 seq_setcur (mp
, mp
->lowsel
); /* update current message */
324 seq_save (mp
); /* synchronize sequences */
325 context_save (); /* save the context file */
330 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
331 inplace
, cwd
, atfile
);