3 * repl.c -- reply to a message
11 static struct swit switches[] = {
21 { "cc all|to|cc|me", 0 },
25 { "draftfolder +folder", 0 },
27 { "draftmessage msg", 0 },
29 { "nodraftfolder", 0 },
31 { "editor editor", 0 },
37 { "filter filterfile", 0 },
39 { "form formfile", 0 },
57 { "whatnowproc program", 0 },
59 { "nowhatnowproc", 0 },
61 { "width columns", 0 },
67 { "file file", -4 }, /* interface from msh */
71 { "build", -5 }, /* interface from mhe */
77 static struct swit ccswitches[] = {
89 static struct swit aqrnl[] = {
97 { "refile +folder", 0 },
103 static struct swit aqrl[] = {
107 { "refile +folder", 0 },
111 short ccto = 1; /* global for replsbr */
116 short outputlinelen = OUTPUTLINELEN;
117 short groupreply = 0; /* Is this a group reply? */
119 int mime = 0; /* include original as MIME part */
120 char *form = NULL; /* form (components) file */
121 char *filter = NULL; /* message filter file */
122 char *fcc = NULL; /* folders to add to Fcc: header */
128 void docc (char *, int);
132 main (int argc, char **argv)
135 int anot = 0, inplace = 1;
136 int nedit = 0, nwhat = 0;
137 char *cp, *cwd, *dp, *maildir, *file = NULL;
138 char *folder = NULL, *msg = NULL, *dfolder = NULL;
139 char *dmsg = NULL, *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
140 char **argp, **arguments;
141 struct msgs *mp = NULL;
150 setlocale(LC_ALL, "");
152 invo_name = r1bindex (argv[0], '/');
154 /* read user profile/context */
157 arguments = getarguments (invo_name, argc, argv, 1);
160 while ((cp = *argp++)) {
162 switch (smatch (++cp, switches)) {
164 ambigsw (cp, switches);
167 adios (NULL, "-%s unknown", cp);
170 snprintf (buf, sizeof(buf), "%s: [+folder] [msg] [switches]",
172 print_help (buf, switches, 1);
175 print_version(invo_name);
193 if (!(cp = *argp++) || *cp == '-')
194 adios (NULL, "missing argument to %s", argp[-2]);
198 if (!(cp = *argp++) || *cp == '-')
199 adios (NULL, "missing argument to %s", argp[-2]);
204 if (!(ed = *argp++) || *ed == '-')
205 adios (NULL, "missing argument to %s", argp[-2]);
213 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
214 adios (NULL, "missing argument to %s", argp[-2]);
219 buildsw++; /* fall... */
226 if (!(cp = *argp++) || *cp == '-')
227 adios (NULL, "missing argument to %s", argp[-2]);
230 cp = dp = path (cp + 1, TSUBCWF);
232 fcc = add (", ", fcc);
240 adios (NULL, "only one file at a time!");
241 if (!(cp = *argp++) || *cp == '-')
242 adios (NULL, "missing argument to %s", argp[-2]);
243 file = path (cp, TFILE);
246 if (!(cp = *argp++) || *cp == '-')
247 adios (NULL, "missing argument to %s", argp[-2]);
248 filter = getcpy (etcpath (cp));
252 if (!(form = *argp++) || *form == '-')
253 adios (NULL, "missing argument to %s", argp[-2]);
257 filter = getcpy (etcpath (mhlreply));
287 if (!(cp = *argp++) || *cp == '-')
288 adios (NULL, "missing argument to %s", argp[-2]);
289 if ((outputlinelen = atoi (cp)) < 10)
290 adios (NULL, "impossible width %d", outputlinelen);
295 adios (NULL, "only one draft folder at a time!");
296 if (!(cp = *argp++) || *cp == '-')
297 adios (NULL, "missing argument to %s", argp[-2]);
298 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
299 *cp != '@' ? TFOLDER : TSUBCWF);
303 adios (NULL, "only one draft message at a time!");
304 if (!(dmsg = *argp++) || *dmsg == '-')
305 adios (NULL, "missing argument to %s", argp[-2]);
313 if (*cp == '+' || *cp == '@') {
315 adios (NULL, "only one folder at a time!");
317 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
320 adios (NULL, "only one message at a time!");
326 cwd = getcpy (pwd ());
328 if (!context_find ("path"))
329 free (path ("./", TFOLDER));
330 if (file && (msg || folder))
331 adios (NULL, "can't mix files and folders/msgs");
336 strncpy (drft, buildsw ? m_maildir ("reply")
337 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
339 /* Check if a draft exists */
340 if (!buildsw && stat (drft, &st) != NOTOK) {
342 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
344 /* Check if a draft exists */
345 if (stat (drft, &st) != NOTOK) {
347 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
348 for (i = LISTDSW; i != YESW;) {
349 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
351 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
360 showfile (++argp, drft);
363 if (refile (++argp, drft) == 0)
367 advise (NULL, "say what?");
375 * We are replying to a file.
377 anot = 0; /* we don't want to annotate a file */
380 * We are replying to a message.
385 folder = getfolder (1);
386 maildir = m_maildir (folder);
388 if (chdir (maildir) == NOTOK)
389 adios (maildir, "unable to change directory to");
391 /* read folder and create message structure */
392 if (!(mp = folder_read (folder)))
393 adios (NULL, "unable to read folder %s", folder);
395 /* check for empty folder */
397 adios (NULL, "no messages in %s", folder);
399 /* parse the message range/sequence/name and set SELECTED */
400 if (!m_convert (mp, msg))
402 seq_setprev (mp); /* set the previous-sequence */
405 adios (NULL, "only one message at a time!");
407 context_replace (pfolder, folder); /* update current folder */
408 seq_setcur (mp, mp->lowsel); /* update current message */
409 seq_save (mp); /* synchronize sequences */
410 context_save (); /* save the context file */
413 msg = file ? file : getcpy (m_name (mp->lowsel));
415 if ((in = fopen (msg, "r")) == NULL)
416 adios (msg, "unable to open");
418 /* find form (components) file */
421 form = etcpath (replgroupcomps);
423 form = etcpath (replcomps);
426 replout (in, msg, drft, mp, outputlinelen, mime, form, filter, fcc);
431 what_now (ed, nedit, NOUSE, drft, msg, 0, mp,
432 anot ? "Replied" : NULL, inplace, cwd);
437 docc (char *cp, int ccflag)
439 switch (smatch (cp, ccswitches)) {
441 ambigsw (cp, ccswitches);
444 adios (NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
459 ccto = cccc = ccme = ccflag;