Move socket code to new socket.c
[s-mailx.git] / cmd3.c
blobbae290c734d756885f4ba25c5d2d2ee8e3a4b0c4
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 = lextract(cp, GEXTRA | GSKIN);
318 if (x == NULL || x->n_flink != NULL ||
319 !is_prefix("mailto:", x->n_name) ||
320 /* XXX the mailto: prefix causes failure (":" invalid character)
321 * XXX which is why need to recreate a struct name with an
322 * XXX updated name; this is terribly wasteful and can't we find
323 * XXX a way to mitigate that?? */
324 is_addr_invalid(x = nalloc(x->n_name + sizeof("mailto:") -1,
325 GEXTRA | GSKIN), EACM_STRICT)) {
326 if (options & OPT_D_V)
327 n_err(_("Message contains invalid \"List-Post:\" header\n"));
328 cp = NULL;
329 break;
331 cp = x->n_name;
333 /* A special case has been seen on e.g. ietf-announce@ietf.org:
334 * these usually post to multiple groups, with ietf-announce@
335 * in List-Post:, but with Reply-To: set to ietf@ietf.org (since
336 * -announce@ is only used for announcements, say).
337 * So our desire is to honour this request and actively overwrite
338 * List-Post: for our purpose; but only if its a single address.
339 * However, to avoid ambiguities with users that place themselve in
340 * Reply-To: and mailing lists which don't overwrite this (or only
341 * extend this, shall such exist), only do so if reply_to exists of
342 * a single address which points to the same domain as List-Post: */
343 if (reply_to != NULL && rt->n_flink == NULL &&
344 name_is_same_domain(x, rt))
345 cp = rt->n_name; /* rt is EACM_STRICT tested */
347 /* "Automatically `mlist'" the List-Post: address temporarily */
348 if (is_mlist(cp, FAL0) == MLIST_OTHER)
349 head.h_list_post = cp;
350 else
351 cp = NULL;
352 } while (0);
354 /* In case of list replies we actively sort out any non-list recipient,
355 * but _only_ if we did not honour a MFT:, assuming that members of MFT
356 * were there for a reason; cp is still List-Post:/eqivalent */
357 if ((hf & HF_LIST_REPLY) && mft == NULL) {
358 struct name *nhp = head.h_to;
359 head.h_to = NULL;
360 j_lt_redo:
361 while (nhp != NULL) {
362 np = nhp;
363 nhp = nhp->n_flink;
365 if ((cp != NULL && !asccasecmp(cp, np->n_name)) ||
366 is_mlist(np->n_name, FAL0) != MLIST_OTHER) {
367 np->n_type = (np->n_type & ~GMASK) | GTO;
368 np->n_flink = head.h_to;
369 head.h_to = np;
372 if ((nhp = head.h_cc) != NULL) {
373 head.h_cc = NULL;
374 goto j_lt_redo;
379 make_ref_and_cs(mp, &head);
381 if (ok_blook(quote_as_attachment)) {
382 head.h_attach = csalloc(1, sizeof *head.h_attach);
383 head.h_attach->a_msgno = *msgvec;
384 head.h_attach->a_content_description = _("Original message content");
387 if (mail1(&head, 1, mp, NULL, !!(hf & HF_RECIPIENT_RECORD), 0) == OKAY &&
388 ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
389 mp->m_flag |= MANSWER | MANSWERED;
391 if (*++msgvec != 0) {
392 /* TODO message (error) ring.., less sleep */
393 printf(_("Waiting a second before proceeding to the next message..\n"));
394 fflush(stdout);
395 n_msleep(1000, FAL0);
396 goto jnext_msg;
399 ac_free(save_msgvec);
400 NYD_LEAVE;
401 return 0;
404 static int
405 (*_reply_or_Reply(char c))(int *, bool_t)
407 int (*rv)(int*, bool_t);
408 NYD_ENTER;
410 rv = (ok_blook(flipr) ^ (c == 'R')) ? &_Reply : &_reply;
411 NYD_LEAVE;
412 return rv;
415 static int
416 _reply(int *msgvec, bool_t recipient_record)
418 int rv;
419 NYD_ENTER;
421 rv = _list_reply(msgvec, recipient_record ? HF_RECIPIENT_RECORD : HF_NONE);
422 NYD_LEAVE;
423 return rv;
426 static int
427 _Reply(int *msgvec, bool_t recipient_record)
429 struct header head;
430 struct message *mp;
431 int *ap;
432 char *cp;
433 enum gfield gf;
434 NYD_ENTER;
436 memset(&head, 0, sizeof head);
437 gf = ok_blook(fullnames) ? GFULL : GSKIN;
439 for (ap = msgvec; *ap != 0; ++ap) {
440 char const *rp;
441 struct name *rt;
443 mp = message + *ap - 1;
444 touch(mp);
445 setdot(mp);
447 if ((rp = hfield1("reply-to", mp)) != NULL &&
448 (cp = ok_vlook(reply_to_honour)) != NULL &&
449 (rt = checkaddrs(lextract(rp, GTO | gf), EACM_STRICT, NULL)
450 ) != NULL) {
451 char const *tr = _("Reply-To \"%s%s\"");
452 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
453 char *sp = salloc(l);
455 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..." : ""));
456 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0) {
457 head.h_to = cat(head.h_to, rt);
458 continue;
462 if ((cp = hfield1("from", mp)) == NULL)
463 cp = nameof(mp, 2);
464 head.h_to = cat(head.h_to, lextract(cp, GTO | gf));
466 if (head.h_to == NULL)
467 goto jleave;
469 mp = message + msgvec[0] - 1;
470 head.h_subject = hfield1("subject", mp);
471 head.h_subject = _reedit(head.h_subject);
472 make_ref_and_cs(mp, &head);
474 if (ok_blook(quote_as_attachment)) {
475 head.h_attach = csalloc(1, sizeof *head.h_attach);
476 head.h_attach->a_msgno = *msgvec;
477 head.h_attach->a_content_description = _("Original message content");
480 if (mail1(&head, 1, mp, NULL, recipient_record, 0) == OKAY &&
481 ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
482 mp->m_flag |= MANSWER | MANSWERED;
483 jleave:
484 NYD_LEAVE;
485 return 0;
488 static int
489 _fwd(char *str, int recipient_record)
491 struct header head;
492 int *msgvec, rv = 1;
493 char *recipient;
494 struct message *mp;
495 bool_t f, forward_as_attachment;
496 NYD_ENTER;
498 if ((recipient = laststring(str, &f, TRU1)) == NULL) {
499 puts(_("No recipient specified."));
500 goto jleave;
503 forward_as_attachment = ok_blook(forward_as_attachment);
504 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
506 if (!f) {
507 *msgvec = first(0, MMNORM);
508 if (*msgvec == 0) {
509 if (pstate & PS_HOOK_MASK) {
510 rv = 0;
511 goto jleave;
513 printf(_("No messages to forward.\n"));
514 goto jleave;
516 msgvec[1] = 0;
517 } else if (getmsglist(str, msgvec, 0) < 0)
518 goto jleave;
520 if (*msgvec == 0) {
521 if (pstate & PS_HOOK_MASK) {
522 rv = 0;
523 goto jleave;
525 printf(_("No applicable messages.\n"));
526 goto jleave;
528 if (msgvec[1] != 0) {
529 printf(_("Cannot forward multiple messages at once\n"));
530 goto jleave;
533 memset(&head, 0, sizeof head);
534 if ((head.h_to = lextract(recipient,
535 (GTO | (ok_blook(fullnames) ? GFULL : GSKIN)))) == NULL)
536 goto jleave;
538 mp = message + *msgvec - 1;
540 if (forward_as_attachment) {
541 head.h_attach = csalloc(1, sizeof *head.h_attach);
542 head.h_attach->a_msgno = *msgvec;
543 head.h_attach->a_content_description = _("Forwarded message");
544 } else {
545 touch(mp);
546 setdot(mp);
548 head.h_subject = hfield1("subject", mp);
549 head.h_subject = __fwdedit(head.h_subject);
550 mail1(&head, 1, (forward_as_attachment ? NULL : mp), NULL, recipient_record,
552 rv = 0;
553 jleave:
554 NYD_LEAVE;
555 return rv;
558 static char *
559 __fwdedit(char *subj)
561 struct str in, out;
562 char *newsubj = NULL;
563 NYD_ENTER;
565 if (subj == NULL || *subj == '\0')
566 goto jleave;
568 in.s = subj;
569 in.l = strlen(subj);
570 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
572 newsubj = salloc(out.l + 6);
573 memcpy(newsubj, "Fwd: ", 5); /* XXX localizable */
574 memcpy(newsubj + 5, out.s, out.l +1);
575 free(out.s);
576 jleave:
577 NYD_LEAVE;
578 return newsubj;
581 static int
582 _resend1(void *v, bool_t add_resent)
584 char *name, *str;
585 struct name *to, *sn;
586 int *ip, *msgvec;
587 bool_t f = TRU1;
588 NYD_ENTER;
590 str = v;
591 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
592 name = laststring(str, &f, TRU1);
593 if (name == NULL) {
594 puts(_("No recipient specified."));
595 goto jleave;
598 if (!f) {
599 *msgvec = first(0, MMNORM);
600 if (*msgvec == 0) {
601 if (pstate & PS_HOOK_MASK) {
602 f = FAL0;
603 goto jleave;
605 puts(_("No applicable messages."));
606 goto jleave;
608 msgvec[1] = 0;
609 } else if (getmsglist(str, msgvec, 0) < 0)
610 goto jleave;
612 if (*msgvec == 0) {
613 if (pstate & PS_HOOK_MASK) {
614 f = FAL0;
615 goto jleave;
617 printf("No applicable messages.\n");
618 goto jleave;
621 sn = nalloc(name, GTO | GSKIN);
622 to = usermap(sn, FAL0);
623 for (ip = msgvec; *ip != 0 && UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount);
624 ++ip)
625 if (resend_msg(message + *ip - 1, to, add_resent) != OKAY)
626 goto jleave;
627 f = FAL0;
628 jleave:
629 NYD_LEAVE;
630 return (f != FAL0);
633 static int
634 _c_file(void *v, enum fedit_mode fm)
636 char **argv = v;
637 int i;
638 NYD2_ENTER;
640 if (*argv == NULL) {
641 newfileinfo();
642 i = 0;
643 goto jleave;
646 if (pstate & PS_HOOK_MASK) {
647 n_err(_("Cannot change folder from within a hook\n"));
648 i = 1;
649 goto jleave;
652 save_mbox_for_possible_quitstuff();
654 i = setfile(*argv, fm);
655 if (i < 0) {
656 i = 1;
657 goto jleave;
659 assert(!(fm & FEDIT_NEWMAIL)); /* (Prevent implementation error) */
660 if (pstate & PS_SETFILE_OPENED)
661 check_folder_hook(FAL0);
663 if (i > 0) {
664 i = 1;
665 goto jleave;
667 if (pstate & PS_SETFILE_OPENED)
668 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
669 i = 0;
670 jleave:
671 NYD2_LEAVE;
672 return i;
675 FL int
676 c_shell(void *v)
678 char const *sh = NULL;
679 char *str = v, *cmd;
680 size_t cmdsize;
681 sigset_t mask;
682 sighandler_type sigint;
683 NYD_ENTER;
685 cmd = smalloc(cmdsize = strlen(str) +1);
686 memcpy(cmd, str, cmdsize);
687 _bangexp(&cmd, &cmdsize);
688 if ((sh = ok_vlook(SHELL)) == NULL)
689 sh = XSHELL;
691 sigint = safe_signal(SIGINT, SIG_IGN);
692 sigemptyset(&mask);
693 run_command(sh, &mask, COMMAND_FD_PASS, COMMAND_FD_PASS, "-c", cmd, NULL,
694 NULL);
695 safe_signal(SIGINT, sigint);
696 printf("!\n");
698 free(cmd);
699 NYD_LEAVE;
700 return 0;
703 FL int
704 c_dosh(void *v)
706 sighandler_type sigint;
707 char const *sh;
708 NYD_ENTER;
709 UNUSED(v);
711 if ((sh = ok_vlook(SHELL)) == NULL)
712 sh = XSHELL;
714 sigint = safe_signal(SIGINT, SIG_IGN);
715 run_command(sh, 0, COMMAND_FD_PASS, COMMAND_FD_PASS, NULL, NULL, NULL, NULL);
716 safe_signal(SIGINT, sigint);
717 putchar('\n');
718 NYD_LEAVE;
719 return 0;
722 FL int
723 c_help(void *v)
725 int ret = 0;
726 char *arg;
727 NYD_ENTER;
729 arg = *(char**)v;
731 if (arg != NULL) {
732 #ifdef HAVE_DOCSTRINGS
733 ret = !print_comm_docstr(arg);
734 if (ret)
735 n_err(_("Unknown command: `%s'\n"), arg);
736 #else
737 ret = c_cmdnotsupp(NULL);
738 #endif
739 goto jleave;
742 /* Very ugly, but take care for compiler supported string lengths :( */
743 fputs(progname, stdout);
744 fputs(_(
745 " commands -- \"<msglist>\" denotes message specifications,\n"
746 "e.g., \"1-5\", \":n\" or \".\", separated by spaces:\n"), stdout);
747 fputs(_(
748 "\n"
749 "type <msglist> type (alias: `print') messages (honour `retain' etc.)\n"
750 "Type <msglist> like `type' but always show all headers\n"
751 "next goto and type next message\n"
752 "from <msglist> print header summary for the given list (\"search\")\n"
753 "headers header summary for messages surrounding \"dot\"\n"
754 "delete <msglist> delete messages (can be `undelete'd)\n"),
755 stdout);
757 fputs(_(
758 "\n"
759 "save <msglist> folder append messages to folder and mark as saved\n"
760 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
761 "write <msglist> file write message contents to file (prompts for parts)\n"
762 "Reply <msglist> reply to message senders only\n"
763 "reply <msglist> like `Reply', but address all recipients\n"
764 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
765 stdout);
767 fputs(_(
768 "\n"
769 "mail <recipients> compose a mail for the given recipients\n"
770 "file folder change to another mailbox\n"
771 "File folder like `file', but open readonly\n"
772 "quit quit and apply changes to the current mailbox\n"
773 "xit or exit like `quit', but discard changes\n"
774 "!shell command shell escape\n"
775 "list list names of all available commands\n"),
776 stdout);
778 jleave:
779 NYD_LEAVE;
780 return ret;
783 FL int
784 c_cwd(void *v)
786 char buf[PATH_MAX]; /* TODO getcwd(3) may return a larger value */
787 NYD_ENTER;
789 if (getcwd(buf, sizeof buf) != NULL) {
790 puts(buf);
791 v = (void*)0x1;
792 } else {
793 n_perr(_("getcwd"), 0);
794 v = NULL;
796 NYD_LEAVE;
797 return (v == NULL);
800 FL int
801 c_chdir(void *v)
803 char **arglist = v;
804 char const *cp;
805 NYD_ENTER;
807 if (*arglist == NULL)
808 cp = homedir;
809 else if ((cp = file_expand(*arglist)) == NULL)
810 goto jleave;
811 if (chdir(cp) == -1) {
812 n_perr(cp, 0);
813 cp = NULL;
815 jleave:
816 NYD_LEAVE;
817 return (cp == NULL);
820 FL int
821 c_reply(void *v)
823 int rv;
824 NYD_ENTER;
826 rv = (*_reply_or_Reply('r'))(v, FAL0);
827 NYD_LEAVE;
828 return rv;
831 FL int
832 c_replyall(void *v)
834 int rv;
835 NYD_ENTER;
837 rv = _reply(v, FAL0);
838 NYD_LEAVE;
839 return rv;
842 FL int
843 c_replysender(void *v)
845 int rv;
846 NYD_ENTER;
848 rv = _Reply(v, FAL0);
849 NYD_LEAVE;
850 return rv;
853 FL int
854 c_Reply(void *v)
856 int rv;
857 NYD_ENTER;
859 rv = (*_reply_or_Reply('R'))(v, FAL0);
860 NYD_LEAVE;
861 return rv;
864 FL int
865 c_Lreply(void *v)
867 int rv;
868 NYD_ENTER;
870 rv = _list_reply(v, HF_LIST_REPLY);
871 NYD_LEAVE;
872 return rv;
875 FL int
876 c_followup(void *v)
878 int rv;
879 NYD_ENTER;
881 rv = (*_reply_or_Reply('r'))(v, TRU1);
882 NYD_LEAVE;
883 return rv;
886 FL int
887 c_followupall(void *v)
889 int rv;
890 NYD_ENTER;
892 rv = _reply(v, TRU1);
893 NYD_LEAVE;
894 return rv;
897 FL int
898 c_followupsender(void *v)
900 int rv;
901 NYD_ENTER;
903 rv = _Reply(v, TRU1);
904 NYD_LEAVE;
905 return rv;
908 FL int
909 c_Followup(void *v)
911 int rv;
912 NYD_ENTER;
914 rv = (*_reply_or_Reply('R'))(v, TRU1);
915 NYD_LEAVE;
916 return rv;
919 FL int
920 c_forward(void *v)
922 int rv;
923 NYD_ENTER;
925 rv = _fwd(v, 0);
926 NYD_LEAVE;
927 return rv;
930 FL int
931 c_Forward(void *v)
933 int rv;
934 NYD_ENTER;
936 rv = _fwd(v, 1);
937 NYD_LEAVE;
938 return rv;
941 FL int
942 c_resend(void *v)
944 int rv;
945 NYD_ENTER;
947 rv = _resend1(v, TRU1);
948 NYD_LEAVE;
949 return rv;
952 FL int
953 c_Resend(void *v)
955 int rv;
956 NYD_ENTER;
958 rv = _resend1(v, FAL0);
959 NYD_LEAVE;
960 return rv;
963 FL int
964 c_preserve(void *v)
966 int *msgvec = v, *ip, mesg, rv = 1;
967 struct message *mp;
968 NYD_ENTER;
970 if (pstate & PS_EDIT) {
971 printf(_("Cannot \"preserve\" in a system mailbox\n"));
972 goto jleave;
975 for (ip = msgvec; *ip != 0; ++ip) {
976 mesg = *ip;
977 mp = message + mesg - 1;
978 mp->m_flag |= MPRESERVE;
979 mp->m_flag &= ~MBOX;
980 setdot(mp);
981 pstate |= PS_DID_PRINT_DOT;
983 rv = 0;
984 jleave:
985 NYD_LEAVE;
986 return rv;
989 FL int
990 c_unread(void *v)
992 int *msgvec = v, *ip;
993 NYD_ENTER;
995 for (ip = msgvec; *ip != 0; ++ip) {
996 setdot(message + *ip - 1);
997 dot->m_flag &= ~(MREAD | MTOUCH);
998 dot->m_flag |= MSTATUS;
999 pstate |= PS_DID_PRINT_DOT;
1001 NYD_LEAVE;
1002 return 0;
1005 FL int
1006 c_seen(void *v)
1008 int *msgvec = v, *ip;
1009 NYD_ENTER;
1011 for (ip = msgvec; *ip != 0; ++ip) {
1012 struct message *mp = message + *ip - 1;
1013 setdot(mp);
1014 touch(mp);
1016 NYD_LEAVE;
1017 return 0;
1020 FL int
1021 c_messize(void *v)
1023 int *msgvec = v, *ip, mesg;
1024 struct message *mp;
1025 NYD_ENTER;
1027 for (ip = msgvec; *ip != 0; ++ip) {
1028 mesg = *ip;
1029 mp = message + mesg - 1;
1030 printf("%d: ", mesg);
1031 if (mp->m_xlines > 0)
1032 printf("%ld", mp->m_xlines);
1033 else
1034 putchar(' ');
1035 printf("/%lu\n", (ul_i)mp->m_xsize);
1037 NYD_LEAVE;
1038 return 0;
1041 FL int
1042 c_file(void *v)
1044 int rv;
1045 NYD_ENTER;
1047 rv = _c_file(v, FEDIT_NONE);
1048 NYD_LEAVE;
1049 return rv;
1052 FL int
1053 c_File(void *v)
1055 int rv;
1056 NYD_ENTER;
1058 rv = _c_file(v, FEDIT_RDONLY);
1059 NYD_LEAVE;
1060 return rv;
1063 FL int
1064 c_echo(void *v)
1066 char const **argv = v, **ap, *cp;
1067 int c;
1068 NYD_ENTER;
1070 for (ap = argv; *ap != NULL; ++ap) {
1071 cp = *ap;
1072 if ((cp = fexpand(cp, FEXP_NSHORTCUT | FEXP_NSHELL)) != NULL) {
1073 if (ap != argv)
1074 putchar(' ');
1075 c = 0;
1076 while (*cp != '\0' && (c = n_shell_expand_escape(&cp, FAL0)) > 0)
1077 putchar(c);
1078 /* \c ends overall processing */
1079 if (c < 0)
1080 goto jleave;
1083 putchar('\n');
1084 jleave:
1085 NYD_LEAVE;
1086 return 0;
1089 FL int
1090 c_newmail(void *v)
1092 int val = 1, mdot;
1093 NYD_ENTER;
1094 UNUSED(v);
1096 if ((val = setfile(mailname,
1097 FEDIT_NEWMAIL | ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY))
1098 ) == 0) {
1099 mdot = getmdot(1);
1100 setdot(message + mdot - 1);
1102 NYD_LEAVE;
1103 return val;
1106 FL int
1107 c_flag(void *v)
1109 struct message *m;
1110 int *msgvec = v, *ip;
1111 NYD_ENTER;
1113 for (ip = msgvec; *ip != 0; ++ip) {
1114 m = message + *ip - 1;
1115 setdot(m);
1116 if (!(m->m_flag & (MFLAG | MFLAGGED)))
1117 m->m_flag |= MFLAG | MFLAGGED;
1119 NYD_LEAVE;
1120 return 0;
1123 FL int
1124 c_unflag(void *v)
1126 struct message *m;
1127 int *msgvec = v, *ip;
1128 NYD_ENTER;
1130 for (ip = msgvec; *ip != 0; ++ip) {
1131 m = message + *ip - 1;
1132 setdot(m);
1133 if (m->m_flag & (MFLAG | MFLAGGED)) {
1134 m->m_flag &= ~(MFLAG | MFLAGGED);
1135 m->m_flag |= MUNFLAG;
1138 NYD_LEAVE;
1139 return 0;
1142 FL int
1143 c_answered(void *v)
1145 struct message *m;
1146 int *msgvec = v, *ip;
1147 NYD_ENTER;
1149 for (ip = msgvec; *ip != 0; ++ip) {
1150 m = message + *ip - 1;
1151 setdot(m);
1152 if (!(m->m_flag & (MANSWER | MANSWERED)))
1153 m->m_flag |= MANSWER | MANSWERED;
1155 NYD_LEAVE;
1156 return 0;
1159 FL int
1160 c_unanswered(void *v)
1162 struct message *m;
1163 int *msgvec = v, *ip;
1164 NYD_ENTER;
1166 for (ip = msgvec; *ip != 0; ++ip) {
1167 m = message + *ip - 1;
1168 setdot(m);
1169 if (m->m_flag & (MANSWER | MANSWERED)) {
1170 m->m_flag &= ~(MANSWER | MANSWERED);
1171 m->m_flag |= MUNANSWER;
1174 NYD_LEAVE;
1175 return 0;
1178 FL int
1179 c_draft(void *v)
1181 struct message *m;
1182 int *msgvec = v, *ip;
1183 NYD_ENTER;
1185 for (ip = msgvec; *ip != 0; ++ip) {
1186 m = message + *ip - 1;
1187 setdot(m);
1188 if (!(m->m_flag & (MDRAFT | MDRAFTED)))
1189 m->m_flag |= MDRAFT | MDRAFTED;
1191 NYD_LEAVE;
1192 return 0;
1195 FL int
1196 c_undraft(void *v)
1198 struct message *m;
1199 int *msgvec = v, *ip;
1200 NYD_ENTER;
1202 for (ip = msgvec; *ip != 0; ++ip) {
1203 m = message + *ip - 1;
1204 setdot(m);
1205 if (m->m_flag & (MDRAFT | MDRAFTED)) {
1206 m->m_flag &= ~(MDRAFT | MDRAFTED);
1207 m->m_flag |= MUNDRAFT;
1210 NYD_LEAVE;
1211 return 0;
1214 FL int
1215 c_noop(void *v)
1217 int rv = 0;
1218 NYD_ENTER;
1219 UNUSED(v);
1221 switch (mb.mb_type) {
1222 case MB_POP3:
1223 #ifdef HAVE_POP3
1224 pop3_noop();
1225 #else
1226 rv = c_cmdnotsupp(NULL);
1227 #endif
1228 break;
1229 default:
1230 break;
1232 NYD_LEAVE;
1233 return rv;
1236 FL int
1237 c_remove(void *v)
1239 char const *fmt;
1240 size_t fmt_len;
1241 char **args = v, *name, *ename;
1242 int ec = 0;
1243 NYD_ENTER;
1245 if (*args == NULL) {
1246 n_err(_("Synopsis: remove: <mailbox>...\n"));
1247 ec = 1;
1248 goto jleave;
1251 fmt = _("Remove \"%s\"");
1252 fmt_len = strlen(fmt);
1253 do {
1254 if ((name = expand(*args)) == NULL)
1255 continue;
1256 ename = string_quote(name);
1258 if (!strcmp(name, mailname)) {
1259 n_err(_("Cannot remove current mailbox \"%s\"\n"), ename);
1260 ec |= 1;
1261 continue;
1264 size_t vl = strlen(name) + fmt_len +1;
1265 char *vb = ac_alloc(vl);
1266 bool_t asw;
1267 snprintf(vb, vl, fmt, ename);
1268 asw = getapproval(vb, TRU1);
1269 ac_free(vb);
1270 if (!asw)
1271 continue;
1274 switch (which_protocol(name)) {
1275 case PROTO_FILE:
1276 if (unlink(name) == -1) { /* TODO do not handle .gz .bz2 .xz.. */
1277 n_perr(name, 0);
1278 ec |= 1;
1280 break;
1281 case PROTO_POP3:
1282 n_err(_("Cannot remove POP3 mailbox \"%s\"\n"), ename);
1283 ec |= 1;
1284 break;
1285 case PROTO_MAILDIR:
1286 if (maildir_remove(name) != OKAY)
1287 ec |= 1;
1288 break;
1289 case PROTO_UNKNOWN:
1290 n_err(_("Unknown protocol in \"%s\"; not removed\n"), ename);
1291 ec |= 1;
1292 break;
1294 } while (*++args != NULL);
1295 jleave:
1296 NYD_LEAVE;
1297 return ec;
1300 FL int
1301 c_rename(void *v)
1303 char **args = v, *old, *new;
1304 enum protocol oldp, newp;
1305 int ec;
1306 NYD_ENTER;
1308 ec = 1;
1310 if (args[0] == NULL || args[1] == NULL || args[2] != NULL) {
1311 n_err(_("Synopsis: rename: <old> <new>\n"));
1312 goto jleave;
1315 if ((old = expand(args[0])) == NULL)
1316 goto jleave;
1317 oldp = which_protocol(old);
1318 if ((new = expand(args[1])) == NULL)
1319 goto jleave;
1320 newp = which_protocol(new);
1322 if (!strcmp(old, mailname) || !strcmp(new, mailname)) {
1323 n_err(_("Cannot rename current mailbox \"%s\"\n"), old);
1324 goto jleave;
1327 ec = 0;
1329 if (newp == PROTO_POP3)
1330 goto jnopop3;
1331 switch (oldp) {
1332 case PROTO_FILE:
1333 if (link(old, new) == -1) {
1334 switch (errno) {
1335 case EACCES:
1336 case EEXIST:
1337 case ENAMETOOLONG:
1338 case ENOENT:
1339 case ENOSPC:
1340 case EXDEV:
1341 n_perr(new, 0);
1342 break;
1343 default:
1344 n_perr(old, 0);
1345 break;
1347 ec |= 1;
1348 } else if (unlink(old) == -1) {
1349 n_perr(old, 0);
1350 ec |= 1;
1352 break;
1353 case PROTO_MAILDIR:
1354 if (rename(old, new) == -1) {
1355 n_perr(old, 0);
1356 ec |= 1;
1358 break;
1359 case PROTO_POP3:
1360 jnopop3:
1361 n_err(_("Cannot rename POP3 mailboxes\n"));
1362 ec |= 1;
1363 break;
1364 case PROTO_UNKNOWN:
1365 default:
1366 n_err(_("Unknown protocol in \"%s\" and \"%s\"; not renamed\n"),
1367 old, new);
1368 ec |= 1;
1369 break;
1371 jleave:
1372 NYD_LEAVE;
1373 return ec;
1376 FL int
1377 c_urlencode(void *v) /* XXX IDNA?? */
1379 char **ap;
1380 NYD_ENTER;
1382 for (ap = v; *ap != NULL; ++ap) {
1383 char *in = *ap, *out = urlxenc(in, FAL0);
1385 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
1386 in, strlen(in), out, strlen(out));
1388 NYD_LEAVE;
1389 return 0;
1392 FL int
1393 c_urldecode(void *v) /* XXX IDNA?? */
1395 char **ap;
1396 NYD_ENTER;
1398 for (ap = v; *ap != NULL; ++ap) {
1399 char *in = *ap, *out = urlxdec(in);
1401 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
1402 in, strlen(in), out, strlen(out));
1404 NYD_LEAVE;
1405 return 0;
1408 /* s-it-mode */