FIX privsep.c, yes, vulnerability (wapiflapi)..
[s-mailx.git] / cmd3.c
blob83a4dbc3da9e055eb1b8cb634b2b47e0b6bbb000
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 if (_bang_buf != NULL) {
125 sz = strlen(_bang_buf);
126 bangbuf = srealloc(bangbuf, bangbufsize += sz);
127 memcpy(bangbuf + j, _bang_buf, sz + 1);
128 j += sz;
130 ++changed;
131 i++;
132 continue;
135 if ((*str)[i] == '\\' && (*str)[i + 1] == '!') {
136 bangbuf[j++] = '!';
137 i += 2;
138 ++changed;
140 bangbuf[j++] = (*str)[i++];
142 bangbuf[j] = '\0';
143 if (changed) {
144 printf("!%s\n", bangbuf);
145 fflush(stdout);
147 sz = j + 1;
148 if (sz > *size)
149 *str = srealloc(*str, *size = sz);
150 memcpy(*str, bangbuf, sz);
151 if (sz > _bang_size)
152 _bang_buf = srealloc(_bang_buf, _bang_size = sz);
153 memcpy(_bang_buf, bangbuf, sz);
154 free(bangbuf);
155 NYD_LEAVE;
158 static void
159 make_ref_and_cs(struct message *mp, struct header *head) /* TODO rewrite FAST */
161 char *oldref, *oldmsgid, *newref, *cp;
162 size_t oldreflen = 0, oldmsgidlen = 0, reflen;
163 unsigned i;
164 struct name *n;
165 NYD_ENTER;
167 oldref = hfield1("references", mp);
168 oldmsgid = hfield1("message-id", mp);
169 if (oldmsgid == NULL || *oldmsgid == '\0') {
170 head->h_ref = NULL;
171 goto jleave;
174 reflen = 1;
175 if (oldref) {
176 oldreflen = strlen(oldref);
177 reflen += oldreflen + 2;
179 if (oldmsgid) {
180 oldmsgidlen = strlen(oldmsgid);
181 reflen += oldmsgidlen;
184 newref = smalloc(reflen);
185 if (oldref != NULL) {
186 memcpy(newref, oldref, oldreflen +1);
187 if (oldmsgid != NULL) {
188 newref[oldreflen++] = ',';
189 newref[oldreflen++] = ' ';
190 memcpy(newref + oldreflen, oldmsgid, oldmsgidlen +1);
192 } else if (oldmsgid)
193 memcpy(newref, oldmsgid, oldmsgidlen +1);
194 n = extract(newref, GREF);
195 free(newref);
197 /* Limit number of references TODO better on parser side */
198 while (n->n_flink != NULL)
199 n = n->n_flink;
200 for (i = 1; i <= REFERENCES_MAX; ++i) {
201 if (n->n_blink != NULL)
202 n = n->n_blink;
203 else
204 break;
206 n->n_blink = NULL;
207 head->h_ref = n;
208 if (ok_blook(reply_in_same_charset) &&
209 (cp = hfield1("content-type", mp)) != NULL)
210 head->h_charset = mime_param_get("charset", cp);
211 jleave:
212 NYD_LEAVE;
215 static int
216 _list_reply(int *msgvec, enum header_flags hf)
218 struct header head;
219 struct message *mp;
220 char const *reply_to, *rcv, *cp;
221 enum gfield gf;
222 struct name *rt, *mft, *np;
223 int *save_msgvec;
224 NYD_ENTER;
226 /* TODO Since we may recur and do stuff with message lists we need to save
227 * TODO away the argument vector as long as that isn't done by machinery */
229 size_t i;
230 for (i = 0; msgvec[i] != 0; ++i)
232 ++i;
233 save_msgvec = ac_alloc(sizeof(*save_msgvec) * i);
234 while (i-- > 0)
235 save_msgvec[i] = msgvec[i];
236 msgvec = save_msgvec;
239 jnext_msg:
240 mp = message + *msgvec - 1;
241 touch(mp);
242 setdot(mp);
244 memset(&head, 0, sizeof head);
245 head.h_flags = hf;
247 head.h_subject = _reedit(hfield1("subject", mp));
248 gf = ok_blook(fullnames) ? GFULL : GSKIN;
249 rt = mft = NULL;
251 rcv = NULL;
252 if ((reply_to = hfield1("reply-to", mp)) != NULL &&
253 (cp = ok_vlook(reply_to_honour)) != NULL &&
254 (rt = checkaddrs(lextract(reply_to, GTO | gf), EACM_STRICT, NULL)
255 ) != NULL) {
256 char const *tr = _("Reply-To \"%s%s\"");
257 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
258 char *sp = salloc(l);
260 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..." : ""));
261 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0)
262 rcv = reply_to;
265 if (rcv == NULL && (rcv = hfield1("from", mp)) == NULL)
266 rcv = nameof(mp, 1);
268 /* Cc: */
269 np = NULL;
270 if (ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
271 np = lextract(cp, GCC | gf);
272 if ((cp = hfield1("cc", mp)) != NULL)
273 np = cat(np, lextract(cp, GCC | gf));
274 if (np != NULL)
275 head.h_cc = delete_alternates(np);
277 /* To: */
278 np = NULL;
279 if (rcv != NULL)
280 np = (rcv == reply_to) ? namelist_dup(rt, GTO | gf)
281 : lextract(rcv, GTO | gf);
282 if (!ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
283 np = cat(np, lextract(cp, GTO | gf));
284 /* Delete my name from reply list, and with it, all my alternate names */
285 np = delete_alternates(np);
286 if (count(np) == 0)
287 np = lextract(rcv, GTO | gf);
288 head.h_to = np;
290 /* The user may have send this to himself, don't ignore that */
291 namelist_vaporise_head(&head, EACM_NORMAL, FAL0, NULL);
292 if (head.h_to == NULL)
293 head.h_to = np;
295 /* Mail-Followup-To: */
296 mft = NULL;
297 if (ok_vlook(followup_to_honour) != NULL &&
298 (cp = hfield1("mail-followup-to", mp)) != NULL &&
299 (mft = np = checkaddrs(lextract(cp, GTO | gf), EACM_STRICT, NULL)
300 ) != NULL) {
301 char const *tr = _("Followup-To \"%s%s\"");
302 size_t l = strlen(tr) + strlen(np->n_name) + 3 +1;
303 char *sp = salloc(l);
305 snprintf(sp, l, tr, np->n_name, (np->n_flink != NULL ? "..." : ""));
306 if (quadify(ok_vlook(followup_to_honour), UIZ_MAX, sp, TRU1) > FAL0) {
307 head.h_cc = NULL;
308 head.h_to = np;
309 head.h_mft =
310 mft = namelist_vaporise_head(&head, EACM_STRICT, FAL0, NULL);
311 } else
312 mft = NULL;
315 /* Special massage for list (follow-up) messages */
316 if (mft != NULL || (hf & HF_LIST_REPLY) || ok_blook(followup_to)) {
317 /* Learn about a possibly sending mailing list; use do for break; */
318 if ((cp = hfield1("list-post", mp)) != NULL) do {
319 struct name *x;
321 if ((x = lextract(cp, GEXTRA | GSKIN)) == NULL || x->n_flink != NULL ||
322 (cp = url_mailto_to_address(x->n_name)) == NULL ||
323 /* XXX terribly wasteful to create a new name, and can't we find
324 * XXX a way to mitigate that?? */
325 is_addr_invalid(x = nalloc(cp, 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 sleep(1);
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));
660 check_folder_hook(FAL0);
662 if (i > 0 && !ok_blook(emptystart)) {
663 i = 1;
664 goto jleave;
666 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
667 i = 0;
668 jleave:
669 NYD2_LEAVE;
670 return i;
673 FL int
674 c_shell(void *v)
676 char const *sh = NULL;
677 char *str = v, *cmd;
678 size_t cmdsize;
679 sigset_t mask;
680 sighandler_type sigint;
681 NYD_ENTER;
683 cmd = smalloc(cmdsize = strlen(str) +1);
684 memcpy(cmd, str, cmdsize);
685 _bangexp(&cmd, &cmdsize);
686 if ((sh = ok_vlook(SHELL)) == NULL)
687 sh = XSHELL;
689 sigint = safe_signal(SIGINT, SIG_IGN);
690 sigemptyset(&mask);
691 run_command(sh, &mask, -1, -1, "-c", cmd, NULL);
692 safe_signal(SIGINT, sigint);
693 printf("!\n");
695 free(cmd);
696 NYD_LEAVE;
697 return 0;
700 FL int
701 c_dosh(void *v)
703 sighandler_type sigint;
704 char const *sh;
705 NYD_ENTER;
706 UNUSED(v);
708 if ((sh = ok_vlook(SHELL)) == NULL)
709 sh = XSHELL;
711 sigint = safe_signal(SIGINT, SIG_IGN);
712 run_command(sh, 0, -1, -1, NULL, NULL, NULL);
713 safe_signal(SIGINT, sigint);
714 putchar('\n');
715 NYD_LEAVE;
716 return 0;
719 FL int
720 c_help(void *v)
722 int ret = 0;
723 char *arg;
724 NYD_ENTER;
726 arg = *(char**)v;
728 if (arg != NULL) {
729 #ifdef HAVE_DOCSTRINGS
730 ret = !print_comm_docstr(arg);
731 if (ret)
732 n_err(_("Unknown command: `%s'\n"), arg);
733 #else
734 ret = c_cmdnotsupp(NULL);
735 #endif
736 goto jleave;
739 /* Very ugly, but take care for compiler supported string lengths :( */
740 fputs(progname, stdout);
741 fputs(_(
742 " commands -- \"<msglist>\" denotes message specifications,\n"
743 "e.g., \"1-5\", \":n\" or \".\", separated by spaces:\n"), stdout);
744 fputs(_(
745 "\n"
746 "type <msglist> type (alias: `print') messages (honour `retain' etc.)\n"
747 "Type <msglist> like `type' but always show all headers\n"
748 "next goto and type next message\n"
749 "from <msglist> print header summary for the given list (\"search\")\n"
750 "headers header summary for messages surrounding \"dot\"\n"
751 "delete <msglist> delete messages (can be `undelete'd)\n"),
752 stdout);
754 fputs(_(
755 "\n"
756 "save <msglist> folder append messages to folder and mark as saved\n"
757 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
758 "write <msglist> file write message contents to file (prompts for parts)\n"
759 "Reply <msglist> reply to message senders only\n"
760 "reply <msglist> like `Reply', but address all recipients\n"
761 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
762 stdout);
764 fputs(_(
765 "\n"
766 "mail <recipients> compose a mail for the given recipients\n"
767 "file folder change to another mailbox\n"
768 "File folder like `file', but open readonly\n"
769 "quit quit and apply changes to the current mailbox\n"
770 "xit or exit like `quit', but discard changes\n"
771 "!shell command shell escape\n"
772 "list list names of all available commands\n"),
773 stdout);
775 jleave:
776 NYD_LEAVE;
777 return ret;
780 FL int
781 c_cwd(void *v)
783 char buf[PATH_MAX]; /* TODO getcwd(3) may return a larger value */
784 NYD_ENTER;
786 if (getcwd(buf, sizeof buf) != NULL) {
787 puts(buf);
788 v = (void*)0x1;
789 } else {
790 n_perr(_("getcwd"), 0);
791 v = NULL;
793 NYD_LEAVE;
794 return (v == NULL);
797 FL int
798 c_chdir(void *v)
800 char **arglist = v;
801 char const *cp;
802 NYD_ENTER;
804 if (*arglist == NULL)
805 cp = homedir;
806 else if ((cp = file_expand(*arglist)) == NULL)
807 goto jleave;
808 if (chdir(cp) == -1) {
809 n_perr(cp, 0);
810 cp = NULL;
812 jleave:
813 NYD_LEAVE;
814 return (cp == NULL);
817 FL int
818 c_reply(void *v)
820 int rv;
821 NYD_ENTER;
823 rv = (*_reply_or_Reply('r'))(v, FAL0);
824 NYD_LEAVE;
825 return rv;
828 FL int
829 c_replyall(void *v)
831 int rv;
832 NYD_ENTER;
834 rv = _reply(v, FAL0);
835 NYD_LEAVE;
836 return rv;
839 FL int
840 c_replysender(void *v)
842 int rv;
843 NYD_ENTER;
845 rv = _Reply(v, FAL0);
846 NYD_LEAVE;
847 return rv;
850 FL int
851 c_Reply(void *v)
853 int rv;
854 NYD_ENTER;
856 rv = (*_reply_or_Reply('R'))(v, FAL0);
857 NYD_LEAVE;
858 return rv;
861 FL int
862 c_Lreply(void *v)
864 int rv;
865 NYD_ENTER;
867 rv = _list_reply(v, HF_LIST_REPLY);
868 NYD_LEAVE;
869 return rv;
872 FL int
873 c_followup(void *v)
875 int rv;
876 NYD_ENTER;
878 rv = (*_reply_or_Reply('r'))(v, TRU1);
879 NYD_LEAVE;
880 return rv;
883 FL int
884 c_followupall(void *v)
886 int rv;
887 NYD_ENTER;
889 rv = _reply(v, TRU1);
890 NYD_LEAVE;
891 return rv;
894 FL int
895 c_followupsender(void *v)
897 int rv;
898 NYD_ENTER;
900 rv = _Reply(v, TRU1);
901 NYD_LEAVE;
902 return rv;
905 FL int
906 c_Followup(void *v)
908 int rv;
909 NYD_ENTER;
911 rv = (*_reply_or_Reply('R'))(v, TRU1);
912 NYD_LEAVE;
913 return rv;
916 FL int
917 c_forward(void *v)
919 int rv;
920 NYD_ENTER;
922 rv = _fwd(v, 0);
923 NYD_LEAVE;
924 return rv;
927 FL int
928 c_Forward(void *v)
930 int rv;
931 NYD_ENTER;
933 rv = _fwd(v, 1);
934 NYD_LEAVE;
935 return rv;
938 FL int
939 c_resend(void *v)
941 int rv;
942 NYD_ENTER;
944 rv = _resend1(v, TRU1);
945 NYD_LEAVE;
946 return rv;
949 FL int
950 c_Resend(void *v)
952 int rv;
953 NYD_ENTER;
955 rv = _resend1(v, FAL0);
956 NYD_LEAVE;
957 return rv;
960 FL int
961 c_preserve(void *v)
963 int *msgvec = v, *ip, mesg, rv = 1;
964 struct message *mp;
965 NYD_ENTER;
967 if (pstate & PS_EDIT) {
968 printf(_("Cannot \"preserve\" in a system mailbox\n"));
969 goto jleave;
972 for (ip = msgvec; *ip != 0; ++ip) {
973 mesg = *ip;
974 mp = message + mesg - 1;
975 mp->m_flag |= MPRESERVE;
976 mp->m_flag &= ~MBOX;
977 setdot(mp);
978 pstate |= PS_DID_PRINT_DOT;
980 rv = 0;
981 jleave:
982 NYD_LEAVE;
983 return rv;
986 FL int
987 c_unread(void *v)
989 int *msgvec = v, *ip;
990 NYD_ENTER;
992 for (ip = msgvec; *ip != 0; ++ip) {
993 setdot(message + *ip - 1);
994 dot->m_flag &= ~(MREAD | MTOUCH);
995 dot->m_flag |= MSTATUS;
996 #ifdef HAVE_IMAP
997 if (mb.mb_type == MB_IMAP || mb.mb_type == MB_CACHE)
998 imap_unread(message + *ip - 1, *ip); /* TODO return? */
999 #endif
1000 pstate |= PS_DID_PRINT_DOT;
1002 NYD_LEAVE;
1003 return 0;
1006 FL int
1007 c_seen(void *v)
1009 int *msgvec = v, *ip;
1010 NYD_ENTER;
1012 for (ip = msgvec; *ip != 0; ++ip) {
1013 struct message *mp = message + *ip - 1;
1014 setdot(mp);
1015 touch(mp);
1017 NYD_LEAVE;
1018 return 0;
1021 FL int
1022 c_messize(void *v)
1024 int *msgvec = v, *ip, mesg;
1025 struct message *mp;
1026 NYD_ENTER;
1028 for (ip = msgvec; *ip != 0; ++ip) {
1029 mesg = *ip;
1030 mp = message + mesg - 1;
1031 printf("%d: ", mesg);
1032 if (mp->m_xlines > 0)
1033 printf("%ld", mp->m_xlines);
1034 else
1035 putchar(' ');
1036 printf("/%lu\n", (ul_i)mp->m_xsize);
1038 NYD_LEAVE;
1039 return 0;
1042 FL int
1043 c_file(void *v)
1045 int rv;
1046 NYD_ENTER;
1048 rv = _c_file(v, FEDIT_NONE);
1049 NYD_LEAVE;
1050 return rv;
1053 FL int
1054 c_File(void *v)
1056 int rv;
1057 NYD_ENTER;
1059 rv = _c_file(v, FEDIT_RDONLY);
1060 NYD_LEAVE;
1061 return rv;
1064 FL int
1065 c_echo(void *v)
1067 char const **argv = v, **ap, *cp;
1068 int c;
1069 NYD_ENTER;
1071 for (ap = argv; *ap != NULL; ++ap) {
1072 cp = *ap;
1073 if ((cp = fexpand(cp, FEXP_NSHORTCUT)) != NULL) {
1074 if (ap != argv)
1075 putchar(' ');
1076 c = 0;
1077 while (*cp != '\0' && (c = expand_shell_escape(&cp, FAL0)) > 0)
1078 putchar(c);
1079 /* \c ends overall processing */
1080 if (c < 0)
1081 goto jleave;
1084 putchar('\n');
1085 jleave:
1086 NYD_LEAVE;
1087 return 0;
1090 FL int
1091 c_newmail(void *v)
1093 int val = 1, mdot;
1094 NYD_ENTER;
1095 UNUSED(v);
1097 if (
1098 #ifdef HAVE_IMAP
1099 (mb.mb_type != MB_IMAP || imap_newmail(1)) &&
1100 #endif
1101 (val = setfile(mailname,
1102 FEDIT_NEWMAIL | ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY))
1103 ) == 0) {
1104 mdot = getmdot(1);
1105 setdot(message + mdot - 1);
1107 NYD_LEAVE;
1108 return val;
1111 FL int
1112 c_flag(void *v)
1114 struct message *m;
1115 int *msgvec = v, *ip;
1116 NYD_ENTER;
1118 for (ip = msgvec; *ip != 0; ++ip) {
1119 m = message + *ip - 1;
1120 setdot(m);
1121 if (!(m->m_flag & (MFLAG | MFLAGGED)))
1122 m->m_flag |= MFLAG | MFLAGGED;
1124 NYD_LEAVE;
1125 return 0;
1128 FL int
1129 c_unflag(void *v)
1131 struct message *m;
1132 int *msgvec = v, *ip;
1133 NYD_ENTER;
1135 for (ip = msgvec; *ip != 0; ++ip) {
1136 m = message + *ip - 1;
1137 setdot(m);
1138 if (m->m_flag & (MFLAG | MFLAGGED)) {
1139 m->m_flag &= ~(MFLAG | MFLAGGED);
1140 m->m_flag |= MUNFLAG;
1143 NYD_LEAVE;
1144 return 0;
1147 FL int
1148 c_answered(void *v)
1150 struct message *m;
1151 int *msgvec = v, *ip;
1152 NYD_ENTER;
1154 for (ip = msgvec; *ip != 0; ++ip) {
1155 m = message + *ip - 1;
1156 setdot(m);
1157 if (!(m->m_flag & (MANSWER | MANSWERED)))
1158 m->m_flag |= MANSWER | MANSWERED;
1160 NYD_LEAVE;
1161 return 0;
1164 FL int
1165 c_unanswered(void *v)
1167 struct message *m;
1168 int *msgvec = v, *ip;
1169 NYD_ENTER;
1171 for (ip = msgvec; *ip != 0; ++ip) {
1172 m = message + *ip - 1;
1173 setdot(m);
1174 if (m->m_flag & (MANSWER | MANSWERED)) {
1175 m->m_flag &= ~(MANSWER | MANSWERED);
1176 m->m_flag |= MUNANSWER;
1179 NYD_LEAVE;
1180 return 0;
1183 FL int
1184 c_draft(void *v)
1186 struct message *m;
1187 int *msgvec = v, *ip;
1188 NYD_ENTER;
1190 for (ip = msgvec; *ip != 0; ++ip) {
1191 m = message + *ip - 1;
1192 setdot(m);
1193 if (!(m->m_flag & (MDRAFT | MDRAFTED)))
1194 m->m_flag |= MDRAFT | MDRAFTED;
1196 NYD_LEAVE;
1197 return 0;
1200 FL int
1201 c_undraft(void *v)
1203 struct message *m;
1204 int *msgvec = v, *ip;
1205 NYD_ENTER;
1207 for (ip = msgvec; *ip != 0; ++ip) {
1208 m = message + *ip - 1;
1209 setdot(m);
1210 if (m->m_flag & (MDRAFT | MDRAFTED)) {
1211 m->m_flag &= ~(MDRAFT | MDRAFTED);
1212 m->m_flag |= MUNDRAFT;
1215 NYD_LEAVE;
1216 return 0;
1219 FL int
1220 c_noop(void *v)
1222 int rv = 0;
1223 NYD_ENTER;
1224 UNUSED(v);
1226 switch (mb.mb_type) {
1227 case MB_IMAP:
1228 #ifdef HAVE_IMAP
1229 imap_noop();
1230 #else
1231 rv = c_cmdnotsupp(NULL);
1232 #endif
1233 break;
1234 case MB_POP3:
1235 #ifdef HAVE_POP3
1236 pop3_noop();
1237 #else
1238 rv = c_cmdnotsupp(NULL);
1239 #endif
1240 break;
1241 default:
1242 break;
1244 NYD_LEAVE;
1245 return rv;
1248 FL int
1249 c_remove(void *v)
1251 char const *fmt;
1252 size_t fmt_len;
1253 char **args = v, *name;
1254 int ec = 0;
1255 NYD_ENTER;
1257 if (*args == NULL) {
1258 n_err(_("Synopsis: remove: <mailbox>...\n"));
1259 ec = 1;
1260 goto jleave;
1263 fmt = _("Remove \"%s\" (y/n) ? ");
1264 fmt_len = strlen(fmt);
1265 do {
1266 if ((name = expand(*args)) == NULL)
1267 continue;
1269 if (!strcmp(name, mailname)) {
1270 n_err(_("Cannot remove current mailbox \"%s\"\n"), name);
1271 ec |= 1;
1272 continue;
1275 size_t vl = strlen(name) + fmt_len +1;
1276 char *vb = ac_alloc(vl);
1277 bool_t asw;
1278 snprintf(vb, vl, fmt, name);
1279 asw = getapproval(vb, TRU1);
1280 ac_free(vb);
1281 if (!asw)
1282 continue;
1285 switch (which_protocol(name)) {
1286 case PROTO_FILE:
1287 if (unlink(name) == -1) { /* TODO do not handle .gz .bz2 .xz.. */
1288 n_perr(name, 0);
1289 ec |= 1;
1291 break;
1292 case PROTO_POP3:
1293 n_err(_("Cannot remove POP3 mailbox \"%s\"\n"),name);
1294 ec |= 1;
1295 break;
1296 case PROTO_IMAP:
1297 #ifdef HAVE_IMAP
1298 if (imap_remove(name) != OKAY)
1299 #endif
1300 ec |= 1;
1301 break;
1302 case PROTO_MAILDIR:
1303 if (maildir_remove(name) != OKAY)
1304 ec |= 1;
1305 break;
1306 case PROTO_UNKNOWN:
1307 n_err(_("Unknown protocol in \"%s\"; not removed\n"), name);
1308 ec |= 1;
1309 break;
1311 } while (*++args != NULL);
1312 jleave:
1313 NYD_LEAVE;
1314 return ec;
1317 FL int
1318 c_rename(void *v)
1320 char **args = v, *old, *new;
1321 enum protocol oldp, newp;
1322 int ec;
1323 NYD_ENTER;
1325 ec = 1;
1327 if (args[0] == NULL || args[1] == NULL || args[2] != NULL) {
1328 n_err(_("Synopsis: rename: <old> <new>\n"));
1329 goto jleave;
1332 if ((old = expand(args[0])) == NULL)
1333 goto jleave;
1334 oldp = which_protocol(old);
1335 if ((new = expand(args[1])) == NULL)
1336 goto jleave;
1337 newp = which_protocol(new);
1339 if (!strcmp(old, mailname) || !strcmp(new, mailname)) {
1340 n_err(_("Cannot rename current mailbox \"%s\"\n"), old);
1341 goto jleave;
1343 if ((oldp == PROTO_IMAP || newp == PROTO_IMAP) && oldp != newp) {
1344 fprintf(stderr, _("Can only rename folders of same type.\n"));
1345 goto jleave;
1348 ec = 0;
1350 if (newp == PROTO_POP3)
1351 goto jnopop3;
1352 switch (oldp) {
1353 case PROTO_FILE:
1354 if (link(old, new) == -1) {
1355 switch (errno) {
1356 case EACCES:
1357 case EEXIST:
1358 case ENAMETOOLONG:
1359 case ENOENT:
1360 case ENOSPC:
1361 case EXDEV:
1362 n_perr(new, 0);
1363 break;
1364 default:
1365 n_perr(old, 0);
1366 break;
1368 ec |= 1;
1369 } else if (unlink(old) == -1) {
1370 n_perr(old, 0);
1371 ec |= 1;
1373 break;
1374 case PROTO_MAILDIR:
1375 if (rename(old, new) == -1) {
1376 n_perr(old, 0);
1377 ec |= 1;
1379 break;
1380 case PROTO_POP3:
1381 jnopop3:
1382 n_err(_("Cannot rename POP3 mailboxes\n"));
1383 ec |= 1;
1384 break;
1385 #ifdef HAVE_IMAP
1386 case PROTO_IMAP:
1387 if (imap_rename(old, new) != OKAY)
1388 ec |= 1;
1389 break;
1390 #endif
1391 case PROTO_UNKNOWN:
1392 default:
1393 n_err(_("Unknown protocol in \"%s\" and \"%s\"; not renamed\n"),
1394 old, new);
1395 ec |= 1;
1396 break;
1398 jleave:
1399 NYD_LEAVE;
1400 return ec;
1403 FL int
1404 c_urlencode(void *v) /* XXX IDNA?? */
1406 char **ap;
1407 NYD_ENTER;
1409 OBSOLETE("`urlencode': please use `urlcodec enc[ode]' instead");
1411 for (ap = v; *ap != NULL; ++ap) {
1412 char *in = *ap, *out = urlxenc(in, FAL0);
1414 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
1415 in, strlen(in), out, strlen(out));
1417 NYD_LEAVE;
1418 return 0;
1421 FL int
1422 c_urldecode(void *v) /* XXX IDNA?? */
1424 char **ap;
1425 NYD_ENTER;
1427 OBSOLETE("`urldecode': please use `urlcodec dec[ode]' instead");
1429 for (ap = v; *ap != NULL; ++ap) {
1430 char *in = *ap, *out = urlxdec(in);
1432 printf(" in: <%s> (%" PRIuZ " bytes)\nout: <%s> (%" PRIuZ " bytes)\n",
1433 in, strlen(in), out, strlen(out));
1435 NYD_LEAVE;
1436 return 0;
1439 /* s-it-mode */