]>
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"
13 #define DIST_SWITCHES \
14 X("annotate", 0, ANNOSW) \
15 X("noannotate", 0, NANNOSW) \
16 X("draftfolder +folder", 0, DFOLDSW) \
17 X("draftmessage msg", 0, DMSGSW) \
18 X("nodraftfolder", 0, NDFLDSW) \
19 X("editor editor", 0, EDITRSW) \
20 X("noedit", 0, NEDITSW) \
21 X("form formfile", 0, FORMSW) \
22 X("inplace", 0, INPLSW) \
23 X("noinplace", 0, NINPLSW) \
24 X("whatnowproc program", 0, WHATSW) \
25 X("nowhatnowproc", 0, NWHATSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
28 X("file file", -4, FILESW) \
29 X("from address", 0, FROMSW) \
30 X("to address", 0, TOSW) \
31 X("cc address", 0, CCSW) \
32 X("fcc mailbox", 0, FCCSW) \
33 X("width columns", 0, WIDTHSW) \
34 X("atfile", 0, ATFILESW) \
35 X("noatfile", 0, NOATFILESW) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(DIST
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(DIST
, switches
);
45 #define DISPO_SWITCHES \
47 X("replace", 0, YESW) \
48 X("list", 0, LISTDSW) \
49 X("refile +folder", 0, REFILSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(DISPO
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
61 static struct swit aqrl
[] = {
63 { "replace", 0, YESW
},
64 { "list", 0, LISTDSW
},
65 { "refile +folder", 0, REFILSW
},
71 main (int argc
, char **argv
)
73 int anot
= 0, inplace
= 1, nedit
= 0;
74 int nwhat
= 0, i
, in
, isdf
= 0, out
;
75 int outputlinelen
= OUTPUTLINELEN
;
76 int dat
[5], atfile
= 0;
77 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
78 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
79 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
80 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
81 char **argp
, **arguments
;
82 struct msgs
*mp
= NULL
;
85 if (nmh_init(argv
[0], 1)) { return 1; }
87 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++)) {
92 switch (smatch (++cp
, switches
)) {
94 ambigsw (cp
, switches
);
97 adios (NULL
, "-%s unknown", cp
);
100 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
102 print_help (buf
, switches
, 1);
105 print_version(invo_name
);
116 if (!(ed
= *argp
++) || *ed
== '-')
117 adios (NULL
, "missing argument to %s", argp
[-2]);
125 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
126 adios (NULL
, "missing argument to %s", argp
[-2]);
135 adios (NULL
, "only one file at a time!");
136 if (!(cp
= *argp
++) || *cp
== '-')
137 adios (NULL
, "missing argument to %s", argp
[-2]);
138 file
= path (cp
, TFILE
);
141 if (!(form
= *argp
++) || *form
== '-')
142 adios (NULL
, "missing argument to %s", argp
[-2]);
154 adios (NULL
, "only one draft folder at a time!");
155 if (!(cp
= *argp
++) || *cp
== '-')
156 adios (NULL
, "missing argument to %s", argp
[-2]);
157 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
158 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
162 adios (NULL
, "only one draft message at a time!");
163 if (!(dmsg
= *argp
++) || *dmsg
== '-')
164 adios (NULL
, "missing argument to %s", argp
[-2]);
172 if (!(cp
= *argp
++) || *cp
== '-')
173 adios (NULL
, "missing argument to %s", argp
[-2]);
174 from
= addlist(from
, cp
);
177 if (!(cp
= *argp
++) || *cp
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
179 to
= addlist(to
, cp
);
182 if (!(cp
= *argp
++) || *cp
== '-')
183 adios (NULL
, "missing argument to %s", argp
[-2]);
184 cc
= addlist(cc
, cp
);
187 if (!(cp
= *argp
++) || *cp
== '-')
188 adios (NULL
, "missing argument to %s", argp
[-2]);
189 fcc
= addlist(fcc
, cp
);
193 if (!(cp
= *argp
++) || *cp
== '-')
194 adios (NULL
, "missing argument to %s", argp
[-2]);
195 if ((outputlinelen
= atoi(cp
)) < 10)
196 adios (NULL
, "impossible width %d", outputlinelen
);
207 if (*cp
== '+' || *cp
== '@') {
209 adios (NULL
, "only one folder at a time!");
210 folder
= pluspath (cp
);
213 adios (NULL
, "only one message at a time!");
218 cwd
= mh_xstrdup(pwd ());
220 if (!context_find ("path"))
221 free (path ("./", TFOLDER
));
222 if (file
&& (msg
|| folder
))
223 adios (NULL
, "can't mix files and folders/msgs");
226 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
228 /* Check if draft already exists */
229 if (stat (drft
, &st
) != NOTOK
) {
230 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
231 for (i
= LISTDSW
; i
!= YESW
;) {
232 if (!(argp
= read_switch_multiword ("\nDisposition? ",
233 isdf
? aqrnl
: aqrl
)))
235 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
244 showfile (++argp
, drft
);
247 if (refile (++argp
, drft
) == 0)
261 anot
= 0; /* don't want to annotate a file */
269 folder
= getfolder (1);
270 maildir
= m_maildir (folder
);
272 if (chdir (maildir
) == NOTOK
)
273 adios (maildir
, "unable to change directory to");
275 /* read folder and create message structure */
276 if (!(mp
= folder_read (folder
, 1)))
277 adios (NULL
, "unable to read folder %s", folder
);
279 /* check for empty folder */
281 adios (NULL
, "no messages in %s", folder
);
283 /* parse the message range/sequence/name and set SELECTED */
284 if (!m_convert (mp
, msg
))
286 seq_setprev (mp
); /* set the previous-sequence */
289 adios (NULL
, "only one message at a time!");
292 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
294 dat
[0] = mp
? mp
->lowsel
: 0;
297 dat
[3] = outputlinelen
;
303 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
305 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
306 adios (drft
, "unable to create");
308 cpydata (in
, out
, form
, drft
);
312 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
313 adios (msgnam
, "unable to open message");
316 context_replace (pfolder
, folder
);/* update current folder */
317 seq_setcur (mp
, mp
->lowsel
); /* update current message */
318 seq_save (mp
); /* synchronize sequences */
319 context_save (); /* save the context file */
324 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
325 inplace
, cwd
, atfile
);