]>
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.
12 #define DIST_SWITCHES \
13 X("annotate", 0, ANNOSW) \
14 X("noannotate", 0, NANNOSW) \
15 X("draftfolder +folder", 0, DFOLDSW) \
16 X("draftmessage msg", 0, DMSGSW) \
17 X("nodraftfolder", 0, NDFLDSW) \
18 X("editor editor", 0, EDITRSW) \
19 X("noedit", 0, NEDITSW) \
20 X("form formfile", 0, FORMSW) \
21 X("inplace", 0, INPLSW) \
22 X("noinplace", 0, NINPLSW) \
23 X("whatnowproc program", 0, WHATSW) \
24 X("nowhatnowproc", 0, NWHATSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
27 X("file file", -4, FILESW) \
28 X("from address", 0, FROMSW) \
29 X("to address", 0, TOSW) \
30 X("cc address", 0, CCSW) \
31 X("fcc mailbox", 0, FCCSW) \
32 X("width columns", 0, WIDTHSW) \
33 X("atfile", 0, ATFILESW) \
34 X("noatfile", 0, NOATFILESW) \
36 #define X(sw, minchars, id) id,
37 DEFINE_SWITCH_ENUM(DIST
);
40 #define X(sw, minchars, id) { sw, minchars, id },
41 DEFINE_SWITCH_ARRAY(DIST
, switches
);
44 #define DISPO_SWITCHES \
46 X("replace", 0, YESW) \
47 X("list", 0, LISTDSW) \
48 X("refile +folder", 0, REFILSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(DISPO
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
60 static struct swit aqrl
[] = {
62 { "replace", 0, YESW
},
63 { "list", 0, LISTDSW
},
64 { "refile +folder", 0, REFILSW
},
70 main (int argc
, char **argv
)
72 int anot
= 0, inplace
= 1, nedit
= 0;
73 int nwhat
= 0, i
, in
, isdf
= 0, out
;
74 int outputlinelen
= OUTPUTLINELEN
;
75 int dat
[5], atfile
= 0;
76 char *cp
, *cwd
, *maildir
, *msgnam
, *dfolder
= NULL
;
77 char *dmsg
= NULL
, *ed
= NULL
, *file
= NULL
, *folder
= NULL
;
78 char *form
= NULL
, *msg
= NULL
, buf
[BUFSIZ
], drft
[BUFSIZ
];
79 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *fcc
= NULL
;
80 char **argp
, **arguments
;
81 struct msgs
*mp
= NULL
;
84 if (nmh_init(argv
[0], 1)) { return 1; }
86 arguments
= getarguments (invo_name
, argc
, argv
, 1);
89 while ((cp
= *argp
++)) {
91 switch (smatch (++cp
, switches
)) {
93 ambigsw (cp
, switches
);
96 adios (NULL
, "-%s unknown", cp
);
99 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
101 print_help (buf
, switches
, 1);
104 print_version(invo_name
);
115 if (!(ed
= *argp
++) || *ed
== '-')
116 adios (NULL
, "missing argument to %s", argp
[-2]);
124 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
125 adios (NULL
, "missing argument to %s", argp
[-2]);
134 adios (NULL
, "only one file at a time!");
135 if (!(cp
= *argp
++) || *cp
== '-')
136 adios (NULL
, "missing argument to %s", argp
[-2]);
137 file
= path (cp
, TFILE
);
140 if (!(form
= *argp
++) || *form
== '-')
141 adios (NULL
, "missing argument to %s", argp
[-2]);
153 adios (NULL
, "only one draft folder at a time!");
154 if (!(cp
= *argp
++) || *cp
== '-')
155 adios (NULL
, "missing argument to %s", argp
[-2]);
156 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
157 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
161 adios (NULL
, "only one draft message at a time!");
162 if (!(dmsg
= *argp
++) || *dmsg
== '-')
163 adios (NULL
, "missing argument to %s", argp
[-2]);
171 if (!(cp
= *argp
++) || *cp
== '-')
172 adios (NULL
, "missing argument to %s", argp
[-2]);
173 from
= addlist(from
, cp
);
176 if (!(cp
= *argp
++) || *cp
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
178 to
= addlist(to
, cp
);
181 if (!(cp
= *argp
++) || *cp
== '-')
182 adios (NULL
, "missing argument to %s", argp
[-2]);
183 cc
= addlist(cc
, cp
);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
188 fcc
= addlist(fcc
, cp
);
192 if (!(cp
= *argp
++) || *cp
== '-')
193 adios (NULL
, "missing argument to %s", argp
[-2]);
194 if ((outputlinelen
= atoi(cp
)) < 10)
195 adios (NULL
, "impossible width %d", outputlinelen
);
206 if (*cp
== '+' || *cp
== '@') {
208 adios (NULL
, "only one folder at a time!");
210 folder
= pluspath (cp
);
213 adios (NULL
, "only one message at a time!");
219 cwd
= mh_xstrdup(pwd ());
221 if (!context_find ("path"))
222 free (path ("./", TFOLDER
));
223 if (file
&& (msg
|| folder
))
224 adios (NULL
, "can't mix files and folders/msgs");
227 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
229 /* Check if draft already exists */
230 if (stat (drft
, &st
) != NOTOK
) {
231 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
232 for (i
= LISTDSW
; i
!= YESW
;) {
233 if (!(argp
= read_switch_multiword ("\nDisposition? ",
234 isdf
? aqrnl
: aqrl
)))
236 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
245 showfile (++argp
, drft
);
248 if (refile (++argp
, drft
) == 0)
262 anot
= 0; /* don't want to annotate a file */
270 folder
= getfolder (1);
271 maildir
= m_maildir (folder
);
273 if (chdir (maildir
) == NOTOK
)
274 adios (maildir
, "unable to change directory to");
276 /* read folder and create message structure */
277 if (!(mp
= folder_read (folder
, 1)))
278 adios (NULL
, "unable to read folder %s", folder
);
280 /* check for empty folder */
282 adios (NULL
, "no messages in %s", folder
);
284 /* parse the message range/sequence/name and set SELECTED */
285 if (!m_convert (mp
, msg
))
287 seq_setprev (mp
); /* set the previous-sequence */
290 adios (NULL
, "only one message at a time!");
293 msgnam
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
295 dat
[0] = mp
? mp
->lowsel
: 0;
298 dat
[3] = outputlinelen
;
304 in
= build_form(form
, NULL
, dat
, from
, to
, cc
, fcc
, NULL
, msgnam
);
306 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
307 adios (drft
, "unable to create");
309 cpydata (in
, out
, form
, drft
);
313 if ((in
= open (msgnam
, O_RDONLY
)) == NOTOK
)
314 adios (msgnam
, "unable to open message");
317 context_replace (pfolder
, folder
);/* update current folder */
318 seq_setcur (mp
, mp
->lowsel
); /* update current message */
319 seq_save (mp
); /* synchronize sequences */
320 context_save (); /* save the context file */
325 what_now (ed
, nedit
, NOUSE
, drft
, msgnam
, 1, mp
, anot
? "Resent" : NULL
,
326 inplace
, cwd
, atfile
);