Bump S-nail v14.8.14.ar, 2016-10-20
[s-mailx.git] / cmd3.c
blobd56d66216b10d1e6e0bb8d4fdcf1a43364b1d6de
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Still more user commands.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE cmd3
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 static char *_bang_buf;
43 static size_t _bang_size;
45 /* Modify subject we reply to to begin with Re: if it does not already */
46 static char * _reedit(char *subj);
48 /* Expand the shell escape by expanding unescaped !'s into the last issued
49 * command where possible */
50 static void _bangexp(char **str, size_t *size);
52 static void make_ref_and_cs(struct message *mp, struct header *head);
54 /* `reply' and `Lreply' workhorse */
55 static int _list_reply(int *msgvec, enum header_flags hf);
57 /* Get PTF to implementation of command c (i.e., take care for *flipr*) */
58 static int (* _reply_or_Reply(char c))(int *, bool_t);
60 /* Reply to a single message. Extract each name from the message header and
61 * send them off to mail1() */
62 static int _reply(int *msgvec, bool_t recipient_record);
64 /* Reply to a series of messages by simply mailing to the senders and not
65 * messing around with the To: and Cc: lists as in normal reply */
66 static int _Reply(int *msgvec, bool_t recipient_record);
68 /* Forward a message to a new recipient, in the sense of RFC 2822 */
69 static int _fwd(char *str, int recipient_record);
71 /* Modify the subject we are replying to to begin with Fwd: */
72 static char * __fwdedit(char *subj);
74 /* Do the real work of resending */
75 static int _resend1(void *v, bool_t add_resent);
77 /* c_file, c_File */
78 static int _c_file(void *v, enum fedit_mode fm);
80 static char *
81 _reedit(char *subj)
83 struct str in, out;
84 char *newsubj = NULL;
85 NYD_ENTER;
87 if (subj == NULL || *subj == '\0')
88 goto jleave;
90 in.s = subj;
91 in.l = strlen(subj);
92 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
94 if ((newsubj = subject_re_trim(out.s)) != out.s)
95 newsubj = savestr(out.s);
96 else {
97 /* RFC mandates english "Re: " */
98 newsubj = salloc(out.l + 4 +1);
99 sstpcpy(sstpcpy(newsubj, "Re: "), out.s);
102 free(out.s);
103 jleave:
104 NYD_LEAVE;
105 return newsubj;
108 static void
109 _bangexp(char **str, size_t *size)
111 char *bangbuf;
112 int changed = 0;
113 bool_t dobang;
114 size_t sz, i, j, bangbufsize;
115 NYD_ENTER;
117 dobang = ok_blook(bang);
119 bangbuf = smalloc(bangbufsize = *size);
120 i = j = 0;
121 while ((*str)[i]) {
122 if (dobang) {
123 if ((*str)[i] == '!') {
124 sz = strlen(_bang_buf);
125 bangbuf = srealloc(bangbuf, bangbufsize += sz);
126 ++changed;
127 memcpy(bangbuf + j, _bang_buf, sz + 1);
128 j += sz;
129 i++;
130 continue;
133 if ((*str)[i] == '\\' && (*str)[i + 1] == '!') {
134 bangbuf[j++] = '!';
135 i += 2;
136 ++changed;
138 bangbuf[j++] = (*str)[i++];
140 bangbuf[j] = '\0';
141 if (changed) {
142 printf("!%s\n", bangbuf);
143 fflush(stdout);
145 sz = j + 1;
146 if (sz > *size)
147 *str = srealloc(*str, *size = sz);
148 memcpy(*str, bangbuf, sz);
149 if (sz > _bang_size)
150 _bang_buf = srealloc(_bang_buf, _bang_size = sz);
151 memcpy(_bang_buf, bangbuf, sz);
152 free(bangbuf);
153 NYD_LEAVE;
156 static void
157 make_ref_and_cs(struct message *mp, struct header *head) /* TODO rewrite FAST */
159 char *oldref, *oldmsgid, *newref, *cp;
160 size_t oldreflen = 0, oldmsgidlen = 0, reflen;
161 unsigned i;
162 struct name *n;
163 NYD_ENTER;
165 oldref = hfield1("references", mp);
166 oldmsgid = hfield1("message-id", mp);
167 if (oldmsgid == NULL || *oldmsgid == '\0') {
168 head->h_ref = NULL;
169 goto jleave;
172 reflen = 1;
173 if (oldref) {
174 oldreflen = strlen(oldref);
175 reflen += oldreflen + 2;
177 if (oldmsgid) {
178 oldmsgidlen = strlen(oldmsgid);
179 reflen += oldmsgidlen;
182 newref = smalloc(reflen);
183 if (oldref != NULL) {
184 memcpy(newref, oldref, oldreflen +1);
185 if (oldmsgid != NULL) {
186 newref[oldreflen++] = ',';
187 newref[oldreflen++] = ' ';
188 memcpy(newref + oldreflen, oldmsgid, oldmsgidlen +1);
190 } else if (oldmsgid)
191 memcpy(newref, oldmsgid, oldmsgidlen +1);
192 n = extract(newref, GREF);
193 free(newref);
195 /* Limit number of references TODO better on parser side */
196 while (n->n_flink != NULL)
197 n = n->n_flink;
198 for (i = 1; i <= REFERENCES_MAX; ++i) {
199 if (n->n_blink != NULL)
200 n = n->n_blink;
201 else
202 break;
204 n->n_blink = NULL;
205 head->h_ref = n;
206 if (ok_blook(reply_in_same_charset) &&
207 (cp = hfield1("content-type", mp)) != NULL)
208 head->h_charset = mime_param_get("charset", cp);
209 jleave:
210 NYD_LEAVE;
213 static int
214 _list_reply(int *msgvec, enum header_flags hf)
216 struct header head;
217 struct message *mp;
218 char const *reply_to, *rcv, *cp;
219 enum gfield gf;
220 struct name *rt, *mft, *np;
221 int *save_msgvec;
222 NYD_ENTER;
224 /* TODO Since we may recur and do stuff with message lists we need to save
225 * TODO away the argument vector as long as that isn't done by machinery */
227 size_t i;
228 for (i = 0; msgvec[i] != 0; ++i)
230 ++i;
231 save_msgvec = ac_alloc(sizeof(*save_msgvec) * i);
232 while (i-- > 0)
233 save_msgvec[i] = msgvec[i];
234 msgvec = save_msgvec;
237 jnext_msg:
238 mp = message + *msgvec - 1;
239 touch(mp);
240 setdot(mp);
242 memset(&head, 0, sizeof head);
243 head.h_flags = hf;
245 head.h_subject = _reedit(hfield1("subject", mp));
246 gf = ok_blook(fullnames) ? GFULL : GSKIN;
247 rt = mft = NULL;
249 rcv = NULL;
250 if ((reply_to = hfield1("reply-to", mp)) != NULL &&
251 (cp = ok_vlook(reply_to_honour)) != NULL &&
252 (rt = checkaddrs(lextract(reply_to, GTO | gf), EACM_STRICT, NULL)
253 ) != NULL) {
254 char const *tr = _("Reply-To \"%s%s\"");
255 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
256 char *sp = salloc(l);
258 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..." : ""));
259 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0)
260 rcv = reply_to;
263 if (rcv == NULL && (rcv = hfield1("from", mp)) == NULL)
264 rcv = nameof(mp, 1);
266 /* Cc: */
267 np = NULL;
268 if (ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
269 np = lextract(cp, GCC | gf);
270 if ((cp = hfield1("cc", mp)) != NULL)
271 np = cat(np, lextract(cp, GCC | gf));
272 if (np != NULL)
273 head.h_cc = delete_alternates(np);
275 /* To: */
276 np = NULL;
277 if (rcv != NULL)
278 np = (rcv == reply_to) ? namelist_dup(rt, GTO | gf)
279 : lextract(rcv, GTO | gf);
280 if (!ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
281 np = cat(np, lextract(cp, GTO | gf));
282 /* Delete my name from reply list, and with it, all my alternate names */
283 np = delete_alternates(np);
284 if (count(np) == 0)
285 np = lextract(rcv, GTO | gf);
286 head.h_to = np;
288 /* The user may have send this to himself, don't ignore that */
289 namelist_vaporise_head(&head, EACM_NORMAL, FAL0, NULL);
290 if (head.h_to == NULL)
291 head.h_to = np;
293 /* Mail-Followup-To: */
294 mft = NULL;
295 if (ok_vlook(followup_to_honour) != NULL &&
296 (cp = hfield1("mail-followup-to", mp)) != NULL &&
297 (mft = np = checkaddrs(lextract(cp, GTO | gf), EACM_STRICT, NULL)
298 ) != NULL) {
299 char const *tr = _("Followup-To \"%s%s\"");
300 size_t l = strlen(tr) + strlen(np->n_name) + 3 +1;
301 char *sp = salloc(l);
303 snprintf(sp, l, tr, np->n_name, (np->n_flink != NULL ? "..." : ""));
304 if (quadify(ok_vlook(followup_to_honour), UIZ_MAX, sp, TRU1) > FAL0) {
305 head.h_cc = NULL;
306 head.h_to = np;
307 head.h_mft =
308 mft = namelist_vaporise_head(&head, EACM_STRICT, FAL0, NULL);
309 } else
310 mft = NULL;
313 /* Special massage for list (follow-up) messages */
314 if (mft != NULL || (hf & HF_LIST_REPLY) || ok_blook(followup_to)) {
315 /* Learn about a possibly sending mailing list; use do for break; */
316 if ((cp = hfield1("list-post", mp)) != NULL) do {
317 struct name *x;
319 if ((x = lextract(cp, GEXTRA | GSKIN)) == NULL || x->n_flink != NULL ||
320 (cp = url_mailto_to_address(x->n_name)) == NULL ||
321 /* XXX terribly wasteful to create a new name, and can't we find
322 * XXX a way to mitigate that?? */
323 is_addr_invalid(x = nalloc(cp, GEXTRA | GSKIN), EACM_STRICT)) {
324 if (options & OPT_D_V)
325 n_err(_("Message contains invalid \"List-Post:\" header\n"));
326 cp = NULL;
327 break;
329 cp = x->n_name;
331 /* A special case has been seen on e.g. ietf-announce@ietf.org:
332 * these usually post to multiple groups, with ietf-announce@
333 * in List-Post:, but with Reply-To: set to ietf@ietf.org (since
334 * -announce@ is only used for announcements, say).
335 * So our desire is to honour this request and actively overwrite
336 * List-Post: for our purpose; but only if its a single address.
337 * However, to avoid ambiguities with users that place themselve in
338 * Reply-To: and mailing lists which don't overwrite this (or only
339 * extend this, shall such exist), only do so if reply_to exists of
340 * a single address which points to the same domain as List-Post: */
341 if (reply_to != NULL && rt->n_flink == NULL &&
342 name_is_same_domain(x, rt))
343 cp = rt->n_name; /* rt is EACM_STRICT tested */
345 /* "Automatically `mlist'" the List-Post: address temporarily */
346 if (is_mlist(cp, FAL0) == MLIST_OTHER)
347 head.h_list_post = cp;
348 else
349 cp = NULL;
350 } while (0);
352 /* In case of list replies we actively sort out any non-list recipient,
353 * but _only_ if we did not honour a MFT:, assuming that members of MFT
354 * were there for a reason; cp is still List-Post:/eqivalent */
355 if ((hf & HF_LIST_REPLY) && mft == NULL) {
356 struct name *nhp = head.h_to;
357 head.h_to = NULL;
358 j_lt_redo:
359 while (nhp != NULL) {
360 np = nhp;
361 nhp = nhp->n_flink;
363 if ((cp != NULL && !asccasecmp(cp, np->n_name)) ||
364 is_mlist(np->n_name, FAL0) != MLIST_OTHER) {
365 np->n_type = (np->n_type & ~GMASK) | GTO;
366 np->n_flink = head.h_to;
367 head.h_to = np;
370 if ((nhp = head.h_cc) != NULL) {
371 head.h_cc = NULL;
372 goto j_lt_redo;
377 make_ref_and_cs(mp, &head);
379 if (ok_blook(quote_as_attachment)) {
380 head.h_attach = csalloc(1, sizeof *head.h_attach);
381 head.h_attach->a_msgno = *msgvec;
382 head.h_attach->a_content_description = _("Original message content");
385 if (mail1(&head, 1, mp, NULL, !!(hf & HF_RECIPIENT_RECORD), 0) == OKAY &&
386 ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
387 mp->m_flag |= MANSWER | MANSWERED;
389 if (*++msgvec != 0) {
390 /* TODO message (error) ring.., less sleep */
391 printf(_("Waiting a second before proceeding to the next message..\n"));
392 fflush(stdout);
393 sleep(1);
394 goto jnext_msg;
397 ac_free(save_msgvec);
398 NYD_LEAVE;
399 return 0;
402 static int
403 (*_reply_or_Reply(char c))(int *, bool_t)
405 int (*rv)(int*, bool_t);
406 NYD_ENTER;
408 rv = (ok_blook(flipr) ^ (c == 'R')) ? &_Reply : &_reply;
409 NYD_LEAVE;
410 return rv;
413 static int
414 _reply(int *msgvec, bool_t recipient_record)
416 int rv;
417 NYD_ENTER;
419 rv = _list_reply(msgvec, recipient_record ? HF_RECIPIENT_RECORD : HF_NONE);
420 NYD_LEAVE;
421 return rv;
424 static int
425 _Reply(int *msgvec, bool_t recipient_record)
427 struct header head;
428 struct message *mp;
429 int *ap;
430 char *cp;
431 enum gfield gf;
432 NYD_ENTER;
434 memset(&head, 0, sizeof head);
435 gf = ok_blook(fullnames) ? GFULL : GSKIN;
437 for (ap = msgvec; *ap != 0; ++ap) {
438 char const *rp;
439 struct name *rt;
441 mp = message + *ap - 1;
442 touch(mp);
443 setdot(mp);
445 if ((rp = hfield1("reply-to", mp)) != NULL &&
446 (cp = ok_vlook(reply_to_honour)) != NULL &&
447 (rt = checkaddrs(lextract(rp, GTO | gf), EACM_STRICT, NULL)
448 ) != NULL) {
449 char const *tr = _("Reply-To \"%s%s\"");
450 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
451 char *sp = salloc(l);
453 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..." : ""));
454 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0) {
455 head.h_to = cat(head.h_to, rt);
456 continue;
460 if ((cp = hfield1("from", mp)) == NULL)
461 cp = nameof(mp, 2);
462 head.h_to = cat(head.h_to, lextract(cp, GTO | gf));
464 if (head.h_to == NULL)
465 goto jleave;
467 mp = message + msgvec[0] - 1;
468 head.h_subject = hfield1("subject", mp);
469 head.h_subject = _reedit(head.h_subject);
470 make_ref_and_cs(mp, &head);
472 if (ok_blook(quote_as_attachment)) {
473 head.h_attach = csalloc(1, sizeof *head.h_attach);
474 head.h_attach->a_msgno = *msgvec;
475 head.h_attach->a_content_description = _("Original message content");
478 if (mail1(&head, 1, mp, NULL, recipient_record, 0) == OKAY &&
479 ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
480 mp->m_flag |= MANSWER | MANSWERED;
481 jleave:
482 NYD_LEAVE;
483 return 0;
486 static int
487 _fwd(char *str, int recipient_record)
489 struct header head;
490 int *msgvec, rv = 1;
491 char *recipient;
492 struct message *mp;
493 bool_t f, forward_as_attachment;
494 NYD_ENTER;
496 if ((recipient = laststring(str, &f, TRU1)) == NULL) {
497 puts(_("No recipient specified."));
498 goto jleave;
501 forward_as_attachment = ok_blook(forward_as_attachment);
502 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
504 if (!f) {
505 *msgvec = first(0, MMNORM);
506 if (*msgvec == 0) {
507 if (pstate & PS_HOOK_MASK) {
508 rv = 0;
509 goto jleave;
511 printf(_("No messages to forward.\n"));
512 goto jleave;
514 msgvec[1] = 0;
515 } else if (getmsglist(str, msgvec, 0) < 0)
516 goto jleave;
518 if (*msgvec == 0) {
519 if (pstate & PS_HOOK_MASK) {
520 rv = 0;
521 goto jleave;
523 printf(_("No applicable messages.\n"));
524 goto jleave;
526 if (msgvec[1] != 0) {
527 printf(_("Cannot forward multiple messages at once\n"));
528 goto jleave;
531 memset(&head, 0, sizeof head);
532 if ((head.h_to = lextract(recipient,
533 (GTO | (ok_blook(fullnames) ? GFULL : GSKIN)))) == NULL)
534 goto jleave;
536 mp = message + *msgvec - 1;
538 if (forward_as_attachment) {
539 head.h_attach = csalloc(1, sizeof *head.h_attach);
540 head.h_attach->a_msgno = *msgvec;
541 head.h_attach->a_content_description = _("Forwarded message");
542 } else {
543 touch(mp);
544 setdot(mp);
546 head.h_subject = hfield1("subject", mp);
547 head.h_subject = __fwdedit(head.h_subject);
548 mail1(&head, 1, (forward_as_attachment ? NULL : mp), NULL, recipient_record,
550 rv = 0;
551 jleave:
552 NYD_LEAVE;
553 return rv;
556 static char *
557 __fwdedit(char *subj)
559 struct str in, out;
560 char *newsubj = NULL;
561 NYD_ENTER;
563 if (subj == NULL || *subj == '\0')
564 goto jleave;
566 in.s = subj;
567 in.l = strlen(subj);
568 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
570 newsubj = salloc(out.l + 6);
571 memcpy(newsubj, "Fwd: ", 5); /* XXX localizable */
572 memcpy(newsubj + 5, out.s, out.l +1);
573 free(out.s);
574 jleave:
575 NYD_LEAVE;
576 return newsubj;
579 static int
580 _resend1(void *v, bool_t add_resent)
582 char *name, *str;
583 struct name *to, *sn;
584 int *ip, *msgvec;
585 bool_t f = TRU1;
586 NYD_ENTER;
588 str = v;
589 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
590 name = laststring(str, &f, TRU1);
591 if (name == NULL) {
592 puts(_("No recipient specified."));
593 goto jleave;
596 if (!f) {
597 *msgvec = first(0, MMNORM);
598 if (*msgvec == 0) {
599 if (pstate & PS_HOOK_MASK) {
600 f = FAL0;
601 goto jleave;
603 puts(_("No applicable messages."));
604 goto jleave;
606 msgvec[1] = 0;
607 } else if (getmsglist(str, msgvec, 0) < 0)
608 goto jleave;
610 if (*msgvec == 0) {
611 if (pstate & PS_HOOK_MASK) {
612 f = FAL0;
613 goto jleave;
615 printf("No applicable messages.\n");
616 goto jleave;
619 sn = nalloc(name, GTO | GSKIN);
620 to = usermap(sn, FAL0);
621 for (ip = msgvec; *ip != 0 && UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount);
622 ++ip)
623 if (resend_msg(message + *ip - 1, to, add_resent) != OKAY)
624 goto jleave;
625 f = FAL0;
626 jleave:
627 NYD_LEAVE;
628 return (f != FAL0);
631 static int
632 _c_file(void *v, enum fedit_mode fm)
634 char **argv = v;
635 int i;
636 NYD2_ENTER;
638 if (*argv == NULL) {
639 newfileinfo();
640 i = 0;
641 goto jleave;
644 if (pstate & PS_HOOK_MASK) {
645 n_err(_("Cannot change folder from within a hook\n"));
646 i = 1;
647 goto jleave;
650 save_mbox_for_possible_quitstuff();
652 i = setfile(*argv, fm);
653 if (i < 0) {
654 i = 1;
655 goto jleave;
657 assert(!(fm & FEDIT_NEWMAIL));
658 check_folder_hook(FAL0);
660 if (i > 0 && !ok_blook(emptystart)) {
661 i = 1;
662 goto jleave;
664 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
665 i = 0;
666 jleave:
667 NYD2_LEAVE;
668 return i;
671 FL int
672 c_shell(void *v)
674 char const *sh = NULL;
675 char *str = v, *cmd;
676 size_t cmdsize;
677 sigset_t mask;
678 sighandler_type sigint;
679 NYD_ENTER;
681 cmd = smalloc(cmdsize = strlen(str) +1);
682 memcpy(cmd, str, cmdsize);
683 _bangexp(&cmd, &cmdsize);
684 if ((sh = ok_vlook(SHELL)) == NULL)
685 sh = XSHELL;
687 sigint = safe_signal(SIGINT, SIG_IGN);
688 sigemptyset(&mask);
689 run_command(sh, &mask, -1, -1, "-c", cmd, NULL);
690 safe_signal(SIGINT, sigint);
691 printf("!\n");
693 free(cmd);
694 NYD_LEAVE;
695 return 0;
698 FL int
699 c_dosh(void *v)
701 sighandler_type sigint;
702 char const *sh;
703 NYD_ENTER;
704 UNUSED(v);
706 if ((sh = ok_vlook(SHELL)) == NULL)
707 sh = XSHELL;
709 sigint = safe_signal(SIGINT, SIG_IGN);
710 run_command(sh, 0, -1, -1, NULL, NULL, NULL);
711 safe_signal(SIGINT, sigint);
712 putchar('\n');
713 NYD_LEAVE;
714 return 0;
717 FL int
718 c_help(void *v)
720 int ret = 0;
721 char *arg;
722 NYD_ENTER;
724 arg = *(char**)v;
726 if (arg != NULL) {
727 #ifdef HAVE_DOCSTRINGS
728 ret = !print_comm_docstr(arg);
729 if (ret)
730 n_err(_("Unknown command: `%s'\n"), arg);
731 #else
732 ret = c_cmdnotsupp(NULL);
733 #endif
734 goto jleave;
737 /* Very ugly, but take care for compiler supported string lengths :( */
738 fputs(progname, stdout);
739 fputs(_(
740 " commands -- \"<msglist>\" denotes message specifications,\n"
741 "e.g., \"1-5\", \":n\" or \".\", separated by spaces:\n"), stdout);
742 fputs(_(
743 "\n"
744 "type <msglist> type (alias: `print') messages (honour `retain' etc.)\n"
745 "Type <msglist> like `type' but always show all headers\n"
746 "next goto and type next message\n"
747 "from <msglist> print header summary for the given list (\"search\")\n"
748 "headers header summary for messages surrounding \"dot\"\n"
749 "delete <msglist> delete messages (can be `undelete'd)\n"),
750 stdout);
752 fputs(_(
753 "\n"
754 "save <msglist> folder append messages to folder and mark as saved\n"
755 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
756 "write <msglist> file write message contents to file (prompts for parts)\n"
757 "Reply <msglist> reply to message senders only\n"
758 "reply <msglist> like `Reply', but address all recipients\n"
759 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
760 stdout);
762 fputs(_(
763 "\n"
764 "mail <recipients> compose a mail for the given recipients\n"
765 "file folder change to another mailbox\n"
766 "File folder like `file', but open readonly\n"
767 "quit quit and apply changes to the current mailbox\n"
768 "xit or exit like `quit', but discard changes\n"
769 "!shell command shell escape\n"
770 "list list names of all available commands\n"),
771 stdout);
773 jleave:
774 NYD_LEAVE;
775 return ret;
778 FL int
779 c_cwd(void *v)
781 char buf[PATH_MAX]; /* TODO getcwd(3) may return a larger value */
782 NYD_ENTER;
784 if (getcwd(buf, sizeof buf) != NULL) {
785 puts(buf);
786 v = (void*)0x1;
787 } else {
788 n_perr(_("getcwd"), 0);
789 v = NULL;
791 NYD_LEAVE;
792 return (v == NULL);
795 FL int
796 c_chdir(void *v)
798 char **arglist = v;
799 char const *cp;
800 NYD_ENTER;
802 if (*arglist == NULL)
803 cp = homedir;
804 else if ((cp = file_expand(*arglist)) == NULL)
805 goto jleave;
806 if (chdir(cp) == -1) {
807 n_perr(cp, 0);
808 cp = NULL;
810 jleave:
811 NYD_LEAVE;
812 return (cp == NULL);
815 FL int
816 c_reply(void *v)
818 int rv;
819 NYD_ENTER;
821 rv = (*_reply_or_Reply('r'))(v, FAL0);
822 NYD_LEAVE;
823 return rv;
826 FL int
827 c_replyall(void *v)
829 int rv;
830 NYD_ENTER;
832 rv = _reply(v, FAL0);
833 NYD_LEAVE;
834 return rv;
837 FL int
838 c_replysender(void *v)
840 int rv;
841 NYD_ENTER;
843 rv = _Reply(v, FAL0);
844 NYD_LEAVE;
845 return rv;
848 FL int
849 c_Reply(void *v)
851 int rv;
852 NYD_ENTER;
854 rv = (*_reply_or_Reply('R'))(v, FAL0);
855 NYD_LEAVE;
856 return rv;
859 FL int
860 c_Lreply(void *v)
862 int rv;
863 NYD_ENTER;
865 rv = _list_reply(v, HF_LIST_REPLY);
866 NYD_LEAVE;
867 return rv;
870 FL int
871 c_followup(void *v)
873 int rv;
874 NYD_ENTER;
876 rv = (*_reply_or_Reply('r'))(v, TRU1);
877 NYD_LEAVE;
878 return rv;
881 FL int
882 c_followupall(void *v)
884 int rv;
885 NYD_ENTER;
887 rv = _reply(v, TRU1);
888 NYD_LEAVE;
889 return rv;
892 FL int
893 c_followupsender(void *v)
895 int rv;
896 NYD_ENTER;
898 rv = _Reply(v, TRU1);
899 NYD_LEAVE;
900 return rv;
903 FL int
904 c_Followup(void *v)
906 int rv;
907 NYD_ENTER;
909 rv = (*_reply_or_Reply('R'))(v, TRU1);
910 NYD_LEAVE;
911 return rv;
914 FL int
915 c_forward(void *v)
917 int rv;
918 NYD_ENTER;
920 rv = _fwd(v, 0);
921 NYD_LEAVE;
922 return rv;
925 FL int
926 c_Forward(void *v)
928 int rv;
929 NYD_ENTER;
931 rv = _fwd(v, 1);
932 NYD_LEAVE;
933 return rv;
936 FL int
937 c_resend(void *v)
939 int rv;
940 NYD_ENTER;
942 rv = _resend1(v, TRU1);
943 NYD_LEAVE;
944 return rv;
947 FL int
948 c_Resend(void *v)
950 int rv;
951 NYD_ENTER;
953 rv = _resend1(v, FAL0);
954 NYD_LEAVE;
955 return rv;
958 FL int
959 c_preserve(void *v)
961 int *msgvec = v, *ip, mesg, rv = 1;
962 struct message *mp;
963 NYD_ENTER;
965 if (pstate & PS_EDIT) {
966 printf(_("Cannot \"preserve\" in a system mailbox\n"));
967 goto jleave;
970 for (ip = msgvec; *ip != 0; ++ip) {
971 mesg = *ip;
972 mp = message + mesg - 1;
973 mp->m_flag |= MPRESERVE;
974 mp->m_flag &= ~MBOX;
975 setdot(mp);
976 pstate |= PS_DID_PRINT_DOT;
978 rv = 0;
979 jleave:
980 NYD_LEAVE;
981 return rv;
984 FL int
985 c_unread(void *v)
987 int *msgvec = v, *ip;
988 NYD_ENTER;
990 for (ip = msgvec; *ip != 0; ++ip) {
991 setdot(message + *ip - 1);
992 dot->m_flag &= ~(MREAD | MTOUCH);
993 dot->m_flag |= MSTATUS;
994 #ifdef HAVE_IMAP
995 if (mb.mb_type == MB_IMAP || mb.mb_type == MB_CACHE)
996 imap_unread(message + *ip - 1, *ip); /* TODO return? */
997 #endif
998 pstate |= PS_DID_PRINT_DOT;
1000 NYD_LEAVE;
1001 return 0;
1004 FL int
1005 c_seen(void *v)
1007 int *msgvec = v, *ip;
1008 NYD_ENTER;
1010 for (ip = msgvec; *ip != 0; ++ip) {
1011 struct message *mp = message + *ip - 1;
1012 setdot(mp);
1013 touch(mp);
1015 NYD_LEAVE;
1016 return 0;
1019 FL int
1020 c_messize(void *v)
1022 int *msgvec = v, *ip, mesg;
1023 struct message *mp;
1024 NYD_ENTER;
1026 for (ip = msgvec; *ip != 0; ++ip) {
1027 mesg = *ip;
1028 mp = message + mesg - 1;
1029 printf("%d: ", mesg);
1030 if (mp->m_xlines > 0)
1031 printf("%ld", mp->m_xlines);
1032 else
1033 putchar(' ');
1034 printf("/%lu\n", (ul_i)mp->m_xsize);
1036 NYD_LEAVE;
1037 return 0;
1040 FL int
1041 c_file(void *v)
1043 int rv;
1044 NYD_ENTER;
1046 rv = _c_file(v, FEDIT_NONE);
1047 NYD_LEAVE;
1048 return rv;
1051 FL int
1052 c_File(void *v)
1054 int rv;
1055 NYD_ENTER;
1057 rv = _c_file(v, FEDIT_RDONLY);
1058 NYD_LEAVE;
1059 return rv;
1062 FL int
1063 c_echo(void *v)
1065 char const **argv = v, **ap, *cp;
1066 int c;
1067 NYD_ENTER;
1069 for (ap = argv; *ap != NULL; ++ap) {
1070 cp = *ap;
1071 if ((cp = fexpand(cp, FEXP_NSHORTCUT)) != NULL) {
1072 if (ap != argv)
1073 putchar(' ');
1074 c = 0;
1075 while (*cp != '\0' && (c = expand_shell_escape(&cp, FAL0)) > 0)
1076 putchar(c);
1077 /* \c ends overall processing */
1078 if (c < 0)
1079 goto jleave;
1082 putchar('\n');
1083 jleave:
1084 NYD_LEAVE;
1085 return 0;
1088 FL int
1089 c_newmail(void *v)
1091 int val = 1, mdot;
1092 NYD_ENTER;
1093 UNUSED(v);
1095 if (
1096 #ifdef HAVE_IMAP
1097 (mb.mb_type != MB_IMAP || imap_newmail(1)) &&
1098 #endif
1099 (val = setfile(mailname,
1100 FEDIT_NEWMAIL | ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY))
1101 ) == 0) {
1102 mdot = getmdot(1);
1103 setdot(message + mdot - 1);
1105 NYD_LEAVE;
1106 return val;
1109 FL int
1110 c_flag(void *v)
1112 struct message *m;
1113 int *msgvec = v, *ip;
1114 NYD_ENTER;
1116 for (ip = msgvec; *ip != 0; ++ip) {
1117 m = message + *ip - 1;
1118 setdot(m);
1119 if (!(m->m_flag & (MFLAG | MFLAGGED)))
1120 m->m_flag |= MFLAG | MFLAGGED;
1122 NYD_LEAVE;
1123 return 0;
1126 FL int
1127 c_unflag(void *v)
1129 struct message *m;
1130 int *msgvec = v, *ip;
1131 NYD_ENTER;
1133 for (ip = msgvec; *ip != 0; ++ip) {
1134 m = message + *ip - 1;
1135 setdot(m);
1136 if (m->m_flag & (MFLAG | MFLAGGED)) {
1137 m->m_flag &= ~(MFLAG | MFLAGGED);
1138 m->m_flag |= MUNFLAG;
1141 NYD_LEAVE;
1142 return 0;
1145 FL int
1146 c_answered(void *v)
1148 struct message *m;
1149 int *msgvec = v, *ip;
1150 NYD_ENTER;
1152 for (ip = msgvec; *ip != 0; ++ip) {
1153 m = message + *ip - 1;
1154 setdot(m);
1155 if (!(m->m_flag & (MANSWER | MANSWERED)))
1156 m->m_flag |= MANSWER | MANSWERED;
1158 NYD_LEAVE;
1159 return 0;
1162 FL int
1163 c_unanswered(void *v)
1165 struct message *m;
1166 int *msgvec = v, *ip;
1167 NYD_ENTER;
1169 for (ip = msgvec; *ip != 0; ++ip) {
1170 m = message + *ip - 1;
1171 setdot(m);
1172 if (m->m_flag & (MANSWER | MANSWERED)) {
1173 m->m_flag &= ~(MANSWER | MANSWERED);
1174 m->m_flag |= MUNANSWER;
1177 NYD_LEAVE;
1178 return 0;
1181 FL int
1182 c_draft(void *v)
1184 struct message *m;
1185 int *msgvec = v, *ip;
1186 NYD_ENTER;
1188 for (ip = msgvec; *ip != 0; ++ip) {
1189 m = message + *ip - 1;
1190 setdot(m);
1191 if (!(m->m_flag & (MDRAFT | MDRAFTED)))
1192 m->m_flag |= MDRAFT | MDRAFTED;
1194 NYD_LEAVE;
1195 return 0;
1198 FL int
1199 c_undraft(void *v)
1201 struct message *m;
1202 int *msgvec = v, *ip;
1203 NYD_ENTER;
1205 for (ip = msgvec; *ip != 0; ++ip) {
1206 m = message + *ip - 1;
1207 setdot(m);
1208 if (m->m_flag & (MDRAFT | MDRAFTED)) {
1209 m->m_flag &= ~(MDRAFT | MDRAFTED);
1210 m->m_flag |= MUNDRAFT;
1213 NYD_LEAVE;
1214 return 0;
1217 FL int
1218 c_noop(void *v)
1220 int rv = 0;
1221 NYD_ENTER;
1222 UNUSED(v);
1224 switch (mb.mb_type) {
1225 case MB_IMAP:
1226 #ifdef HAVE_IMAP
1227 imap_noop();
1228 #else
1229 rv = c_cmdnotsupp(NULL);
1230 #endif
1231 break;
1232 case MB_POP3:
1233 #ifdef HAVE_POP3
1234 pop3_noop();
1235 #else
1236 rv = c_cmdnotsupp(NULL);
1237 #endif
1238 break;
1239 default:
1240 break;
1242 NYD_LEAVE;
1243 return rv;
1246 FL int
1247 c_remove(void *v)
1249 char const *fmt;
1250 size_t fmt_len;
1251 char **args = v, *name;
1252 int ec = 0;
1253 NYD_ENTER;
1255 if (*args == NULL) {
1256 n_err(_("Synopsis: remove: <mailbox>...\n"));
1257 ec = 1;
1258 goto jleave;
1261 fmt = _("Remove \"%s\" (y/n) ? ");
1262 fmt_len = strlen(fmt);
1263 do {
1264 if ((name = expand(*args)) == NULL)
1265 continue;
1267 if (!strcmp(name, mailname)) {
1268 n_err(_("Cannot remove current mailbox \"%s\"\n"), name);
1269 ec |= 1;
1270 continue;
1273 size_t vl = strlen(name) + fmt_len +1;
1274 char *vb = ac_alloc(vl);
1275 bool_t asw;
1276 snprintf(vb, vl, fmt, name);
1277 asw = getapproval(vb, TRU1);
1278 ac_free(vb);
1279 if (!asw)
1280 continue;
1283 switch (which_protocol(name)) {
1284 case PROTO_FILE:
1285 if (unlink(name) == -1) { /* TODO do not handle .gz .bz2 .xz.. */
1286 n_perr(name, 0);
1287 ec |= 1;
1289 break;
1290 case PROTO_POP3:
1291 n_err(_("Cannot remove POP3 mailbox \"%s\"\n"),name);
1292 ec |= 1;
1293 break;
1294 case PROTO_IMAP:
1295 #ifdef HAVE_IMAP
1296 if (imap_remove(name) != OKAY)
1297 #endif
1298 ec |= 1;
1299 break;
1300 case PROTO_MAILDIR:
1301 if (maildir_remove(name) != OKAY)
1302 ec |= 1;
1303 break;
1304 case PROTO_UNKNOWN:
1305 n_err(_("Unknown protocol in \"%s\"; not removed\n"), name);
1306 ec |= 1;
1307 break;
1309 } while (*++args != NULL);
1310 jleave:
1311 NYD_LEAVE;
1312 return ec;
1315 FL int
1316 c_rename(void *v)
1318 char **args = v, *old, *new;
1319 enum protocol oldp, newp;
1320 int ec;
1321 NYD_ENTER;
1323 ec = 1;
1325 if (args[0] == NULL || args[1] == NULL || args[2] != NULL) {
1326 n_err(_("Synopsis: rename: <old> <new>\n"));
1327 goto jleave;
1330 if ((old = expand(args[0])) == NULL)
1331 goto jleave;
1332 oldp = which_protocol(old);
1333 if ((new = expand(args[1])) == NULL)
1334 goto jleave;
1335 newp = which_protocol(new);
1337 if (!strcmp(old, mailname) || !strcmp(new, mailname)) {
1338 n_err(_("Cannot rename current mailbox \"%s\"\n"), old);
1339 goto jleave;
1341 if ((oldp == PROTO_IMAP || newp == PROTO_IMAP) && oldp != newp) {
1342 fprintf(stderr, _("Can only rename folders of same type.\n"));
1343 goto jleave;
1346 ec = 0;
1348 if (newp == PROTO_POP3)
1349 goto jnopop3;
1350 switch (oldp) {
1351 case PROTO_FILE:
1352 if (link(old, new) == -1) {
1353 switch (errno) {
1354 case EACCES:
1355 case EEXIST:
1356 case ENAMETOOLONG:
1357 case ENOENT:
1358 case ENOSPC:
1359 case EXDEV:
1360 n_perr(new, 0);
1361 break;
1362 default:
1363 n_perr(old, 0);
1364 break;
1366 ec |= 1;
1367 } else if (unlink(old) == -1) {
1368 n_perr(old, 0);
1369 ec |= 1;
1371 break;
1372 case PROTO_MAILDIR:
1373 if (rename(old, new) == -1) {
1374 n_perr(old, 0);
1375 ec |= 1;
1377 break;
1378 case PROTO_POP3:
1379 jnopop3:
1380 n_err(_("Cannot rename POP3 mailboxes\n"));
1381 ec |= 1;
1382 break;
1383 #ifdef HAVE_IMAP
1384 case PROTO_IMAP:
1385 if (imap_rename(old, new) != OKAY)
1386 ec |= 1;
1387 break;
1388 #endif
1389 case PROTO_UNKNOWN:
1390 default:
1391 n_err(_("Unknown protocol in \"%s\" and \"%s\"; not renamed\n"),
1392 old, new);
1393 ec |= 1;
1394 break;
1396 jleave:
1397 NYD_LEAVE;
1398 return ec;
1401 FL int
1402 c_urlencode(void *v) /* XXX IDNA?? */
1404 char **ap;
1405 NYD_ENTER;
1407 OBSOLETE("`urlencode': please use `urlcodec enc[ode]' instead");
1409 for (ap = v; *ap != NULL; ++ap) {
1410 char *in = *ap, *out = urlxenc(in, FAL0);
1412 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
1413 in, strlen(in), out, strlen(out));
1415 NYD_LEAVE;
1416 return 0;
1419 FL int
1420 c_urldecode(void *v) /* XXX IDNA?? */
1422 char **ap;
1423 NYD_ENTER;
1425 OBSOLETE("`urldecode': please use `urlcodec dec[ode]' instead");
1427 for (ap = v; *ap != NULL; ++ap) {
1428 char *in = *ap, *out = urlxdec(in);
1430 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
1431 in, strlen(in), out, strlen(out));
1433 NYD_LEAVE;
1434 return 0;
1437 /* s-it-mode */