]>
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
;
87 setlocale(LC_ALL
, "");
89 invo_name
= r1bindex (argv
[0], '/');
91 /* read user profile/context */
94 arguments
= getarguments (invo_name
, argc
, argv
, 1);
97 while ((cp
= *argp
++)) {
99 switch (smatch (++cp
, switches
)) {
101 ambigsw (cp
, switches
);
104 adios (NULL
, "-%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 adios (NULL
, "missing argument to %s", argp
[-2]);
132 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
133 adios (NULL
, "missing argument to %s", argp
[-2]);
142 adios (NULL
, "only one file at a time!");
143 if (!(cp
= *argp
++) || *cp
== '-')
144 adios (NULL
, "missing argument to %s", argp
[-2]);
145 file
= path (cp
, TFILE
);
148 if (!(form
= *argp
++) || *form
== '-')
149 adios (NULL
, "missing argument to %s", argp
[-2]);
161 adios (NULL
, "only one draft folder at a time!");
162 if (!(cp
= *argp
++) || *cp
== '-')
163 adios (NULL
, "missing argument to %s", argp
[-2]);
164 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
165 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
169 adios (NULL
, "only one draft message at a time!");
170 if (!(dmsg
= *argp
++) || *dmsg
== '-')
171 adios (NULL
, "missing argument to %s", argp
[-2]);
179 if (!(cp
= *argp
++) || *cp
== '-')
180 adios (NULL
, "missing argument to %s", argp
[-2]);
181 from
= addlist(from
, cp
);
184 if (!(cp
= *argp
++) || *cp
== '-')
185 adios (NULL
, "missing argument to %s", argp
[-2]);
186 to
= addlist(to
, cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 adios (NULL
, "missing argument to %s", argp
[-2]);
191 cc
= addlist(cc
, cp
);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 adios (NULL
, "missing argument to %s", argp
[-2]);
196 fcc
= addlist(fcc
, cp
);
200 if (!(cp
= *argp
++) || *cp
== '-')
201 adios (NULL
, "missing argument to %s", argp
[-2]);
202 if ((outputlinelen
= atoi(cp
)) < 10)
203 adios (NULL
, "impossible width %d", outputlinelen
);
214 if (*cp
== '+' || *cp
== '@') {
216 adios (NULL
, "only one folder at a time!");
218 folder
= pluspath (cp
);
221 adios (NULL
, "only one message at a time!");
227 cwd
= getcpy (pwd ());
229 if (!context_find ("path"))
230 free (path ("./", TFOLDER
));
231 if (file
&& (msg
|| folder
))
232 adios (NULL
, "can't mix files and folders/msgs");
235 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
237 /* Check if draft already exists */
238 if (stat (drft
, &st
) != NOTOK
) {
239 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
240 for (i
= LISTDSW
; i
!= YESW
;) {
241 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrnl
: aqrl
)))
243 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
252 showfile (++argp
, drft
);
255 if (refile (++argp
, drft
) == 0)
259 advise (NULL
, "say what?");
269 anot
= 0; /* don't want to annotate a file */
277 folder
= getfolder (1);
278 maildir
= m_maildir (folder
);
280 if (chdir (maildir
) == NOTOK
)
281 adios (maildir
, "unable to change directory to");
283 /* read folder and create message structure */
284 if (!(mp
= folder_read (folder
)))
285 adios (NULL
, "unable to read folder %s", folder
);
287 /* check for empty folder */
289 adios (NULL
, "no messages in %s", folder
);
291 /* parse the message range/sequence/name and set SELECTED */
292 if (!m_convert (mp
, msg
))
294 seq_setprev (mp
); /* set the previous-sequence */
297 adios (NULL
, "only one message at a time!");
300 msgnam
= file
? file
: getcpy (m_name (mp
->lowsel
));
302 dat
[0] = mp
? mp
->lowsel
: 0;
305 dat
[3] = outputlinelen
;
311 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
313 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
314 adios (drft
, "unable to create");
316 cpydata (in
, out
, form
, drft
);
320 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
321 adios (msgnam
, "unable to open message");
324 context_replace (pfolder
, folder
);/* update current folder */
325 seq_setcur (mp
, mp
->lowsel
); /* update current message */
326 seq_save (mp
); /* synchronize sequences */
327 context_save (); /* save the context file */
332 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
333 inplace
, cwd
, atfile
);