]>
diplodocus.org Git - nmh/blob - uip/dist.c
3 * dist.c -- re-distribute a message
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 #define DIST_SWITCHES \
15 X("annotate", 0, ANNOSW) \
16 X("noannotate", 0, NANNOSW) \
17 X("draftfolder +folder", 0, DFOLDSW) \
18 X("draftmessage msg", 0, DMSGSW) \
19 X("nodraftfolder", 0, NDFLDSW) \
20 X("editor editor", 0, EDITRSW) \
21 X("noedit", 0, NEDITSW) \
22 X("form formfile", 0, FORMSW) \
23 X("inplace", 0, INPLSW) \
24 X("noinplace", 0, NINPLSW) \
25 X("whatnowproc program", 0, WHATSW) \
26 X("nowhatnowproc", 0, NWHATSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
29 X("file file", -4, FILESW) \
30 X("from address", 0, FROMSW) \
31 X("to address", 0, TOSW) \
32 X("cc address", 0, CCSW) \
33 X("fcc mailbox", 0, FCCSW) \
34 X("width columns", 0, WIDTHSW) \
35 X("atfile", 0, ATFILESW) \
36 X("noatfile", 0, NOATFILESW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(DIST
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(DIST
, switches
);
46 #define DISPO_SWITCHES \
48 X("replace", 0, YESW) \
49 X("list", 0, LISTDSW) \
50 X("refile +folder", 0, REFILSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(DISPO
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
62 static struct swit aqrl
[] = {
64 { "replace", 0, YESW
},
65 { "list", 0, LISTDSW
},
66 { "refile +folder", 0, REFILSW
},
72 main (int argc
, char **argv
)
74 int anot
= 0, inplace
= 1, nedit
= 0;
75 int nwhat
= 0, i
, in
, isdf
= 0, out
;
76 int outputlinelen
= OUTPUTLINELEN
;
77 int dat
[5], atfile
= 0;
78 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
79 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
80 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
81 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
82 char **argp
, **arguments
;
83 struct msgs
*mp
= NULL
;
86 if (nmh_init(argv
[0], 1)) { return 1; }
88 arguments
= getarguments (invo_name
, argc
, argv
, 1);
91 while ((cp
= *argp
++)) {
93 switch (smatch (++cp
, switches
)) {
95 ambigsw (cp
, switches
);
98 adios (NULL
, "-%s unknown", cp
);
101 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
103 print_help (buf
, switches
, 1);
106 print_version(invo_name
);
117 if (!(ed
= *argp
++) || *ed
== '-')
118 adios (NULL
, "missing argument to %s", argp
[-2]);
126 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
127 adios (NULL
, "missing argument to %s", argp
[-2]);
136 adios (NULL
, "only one file at a time!");
137 if (!(cp
= *argp
++) || *cp
== '-')
138 adios (NULL
, "missing argument to %s", argp
[-2]);
139 file
= path (cp
, TFILE
);
142 if (!(form
= *argp
++) || *form
== '-')
143 adios (NULL
, "missing argument to %s", argp
[-2]);
155 adios (NULL
, "only one draft folder at a time!");
156 if (!(cp
= *argp
++) || *cp
== '-')
157 adios (NULL
, "missing argument to %s", argp
[-2]);
158 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
159 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
163 adios (NULL
, "only one draft message at a time!");
164 if (!(dmsg
= *argp
++) || *dmsg
== '-')
165 adios (NULL
, "missing argument to %s", argp
[-2]);
173 if (!(cp
= *argp
++) || *cp
== '-')
174 adios (NULL
, "missing argument to %s", argp
[-2]);
175 from
= addlist(from
, cp
);
178 if (!(cp
= *argp
++) || *cp
== '-')
179 adios (NULL
, "missing argument to %s", argp
[-2]);
180 to
= addlist(to
, cp
);
183 if (!(cp
= *argp
++) || *cp
== '-')
184 adios (NULL
, "missing argument to %s", argp
[-2]);
185 cc
= addlist(cc
, cp
);
188 if (!(cp
= *argp
++) || *cp
== '-')
189 adios (NULL
, "missing argument to %s", argp
[-2]);
190 fcc
= addlist(fcc
, cp
);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 adios (NULL
, "missing argument to %s", argp
[-2]);
196 if ((outputlinelen
= atoi(cp
)) < 10)
197 adios (NULL
, "impossible width %d", outputlinelen
);
208 if (*cp
== '+' || *cp
== '@') {
210 adios (NULL
, "only one folder at a time!");
212 folder
= pluspath (cp
);
215 adios (NULL
, "only one message at a time!");
221 cwd
= getcpy (pwd ());
223 if (!context_find ("path"))
224 free (path ("./", TFOLDER
));
225 if (file
&& (msg
|| folder
))
226 adios (NULL
, "can't mix files and folders/msgs");
229 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
231 /* Check if draft already exists */
232 if (stat (drft
, &st
) != NOTOK
) {
233 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
234 for (i
= LISTDSW
; i
!= YESW
;) {
235 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrnl
: aqrl
)))
237 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
246 showfile (++argp
, drft
);
249 if (refile (++argp
, drft
) == 0)
253 advise (NULL
, "say what?");
263 anot
= 0; /* don't want to annotate a file */
271 folder
= getfolder (1);
272 maildir
= m_maildir (folder
);
274 if (chdir (maildir
) == NOTOK
)
275 adios (maildir
, "unable to change directory to");
277 /* read folder and create message structure */
278 if (!(mp
= folder_read (folder
, 1)))
279 adios (NULL
, "unable to read folder %s", folder
);
281 /* check for empty folder */
283 adios (NULL
, "no messages in %s", folder
);
285 /* parse the message range/sequence/name and set SELECTED */
286 if (!m_convert (mp
, msg
))
288 seq_setprev (mp
); /* set the previous-sequence */
291 adios (NULL
, "only one message at a time!");
294 msgnam
= file
? file
: getcpy (m_name (mp
->lowsel
));
296 dat
[0] = mp
? mp
->lowsel
: 0;
299 dat
[3] = outputlinelen
;
305 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
307 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
308 adios (drft
, "unable to create");
310 cpydata (in
, out
, form
, drft
);
314 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
315 adios (msgnam
, "unable to open message");
318 context_replace (pfolder
, folder
);/* update current folder */
319 seq_setcur (mp
, mp
->lowsel
); /* update current message */
320 seq_save (mp
); /* synchronize sequences */
321 context_save (); /* save the context file */
326 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
327 inplace
, cwd
, atfile
);