pop.c 14.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010, 2011 Free Software Foundation, Inc.

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Mailutils is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <mailutils/mailutils.h>
#include "mu.h"
#include "argp.h"
#include "xalloc.h"

static char pop_doc[] = N_("mu pop - POP3 client shell.");
char pop_docstring[] = N_("POP3 client shell");
static char pop_args_doc[] = "";

static struct argp_option pop_options[] = {
  { NULL }
};

static error_t
pop_parse_opt (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp pop_argp = {
  pop_options,
  pop_parse_opt,
  pop_args_doc,
  pop_doc,
  NULL,
  NULL,
  NULL
};

/* Global handle for pop3.  */
static mu_pop3_t pop3;

enum pop_session_status
  {
    pop_session_disconnected,
    pop_session_connected,
    pop_session_logged_in
  };

static enum pop_session_status pop_session_status;

static int connect_argc;
static char **connect_argv;

/* Host we are connected to. */
#define host connect_argv[0]
static int port = 110;
static char *username;

const char *
pop_session_str (enum pop_session_status stat)
{
  switch (stat)
    {
    case pop_session_disconnected:
      return "disconnected";
      
    case pop_session_connected:
      return "connected";
      
    case pop_session_logged_in:
      return "logged in";
    }
  return "unknown";
}

static void
pop_prompt_env ()
{
  if (!mutool_prompt_env)
    mutool_prompt_env = xcalloc (2*7 + 1, sizeof(mutool_prompt_env[0]));

  mutool_prompt_env[0] = "user";
  mutool_prompt_env[1] = (pop_session_status == pop_session_logged_in) ?
                           username : "[nouser]";

  mutool_prompt_env[2] = "host"; 
  mutool_prompt_env[3] = (pop_session_status != pop_session_disconnected) ?
                           host : "[nohost]";

  mutool_prompt_env[4] = "program-name";
  mutool_prompt_env[5] = (char*) mu_program_name;

  mutool_prompt_env[6] = "canonical-program-name";
  mutool_prompt_env[7] = "mu";

  mutool_prompt_env[8] = "package";
  mutool_prompt_env[9] = PACKAGE;

  mutool_prompt_env[10] = "version";
  mutool_prompt_env[11] = PACKAGE_VERSION;

  mutool_prompt_env[12] = "status";
  mutool_prompt_env[13] = (char*) pop_session_str (pop_session_status);

  mutool_prompt_env[14] = NULL;
}


static void
pop_set_verbose (void)
{
  if (pop3)
    {
      if (QRY_VERBOSE ())
	mu_pop3_trace (pop3, MU_POP3_TRACE_SET);
      else
	mu_pop3_trace (pop3, MU_POP3_TRACE_CLR);
    }
}

static void
pop_set_verbose_mask (void)
{
  if (pop3)
    {
      mu_pop3_trace_mask (pop3, QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE)
			          ? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
			      MU_XSCRIPT_SECURE);
      mu_pop3_trace_mask (pop3, QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD)
			          ? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
			      MU_XSCRIPT_PAYLOAD);
    }
}

static int
com_verbose (int argc, char **argv)
{
  return shell_verbose (argc, argv,
			pop_set_verbose, pop_set_verbose_mask);
}

static int
com_user (int argc, char **argv)
{
  int status;
  
  status = mu_pop3_user (pop3, argv[1]);
  if (status == 0)
    {
      username = strdup (argv[1]);
      pop_prompt_env ();
    }
  return status;
}

static int
com_apop (int argc, char **argv)
{
  int status;
  char *pwd, *passbuf = NULL;

  if (argc == 3)
    pwd = argv[2];
  else if (!mutool_shell_interactive)
    {
      mu_error (_("apop: password required"));
      return 1;
    }
  else
    {
      status = mu_getpass (mu_strin, mu_strout, "Password:", &passbuf);
      if (status)
	return status;
      pwd = passbuf;
    }
  
  status = mu_pop3_apop (pop3, argv[1], pwd);
  if (status == 0)
    {
      username = strdup (argv[1]);
      pop_session_status = pop_session_logged_in;
    }
  free (passbuf);
  return status;
}

static int
com_capa (int argc, char **argv)
{
  mu_iterator_t iterator = NULL;
  int status = 0;
  int reread = 0;
  int i = 1;
  
  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], "-reread") == 0)
	reread = 1;
      else
	break;
    }

  if (i < argc)
    {
      if (reread)
	{
	  status = mu_pop3_capa (pop3, 1, NULL);
	  if (status)
	    return status;
	}
      for (; i < argc; i++)
	{
	  const char *elt;
	  int rc = mu_pop3_capa_test (pop3, argv[i], &elt);
	  switch (rc)
	    {
	    case 0:
	      if (*elt)
		mu_printf ("%s: %s\n", argv[i], elt);
	      else
		mu_printf ("%s is set\n", argv[i]);
	      break;

	    case MU_ERR_NOENT:
	      mu_printf ("%s is not set\n", argv[i]);
	      break;

	    default:
	      return rc;
	    }
	}
    }
  else
    {
      status = mu_pop3_capa (pop3, reread, &iterator);

      if (status == 0)
	{
	  for (mu_iterator_first (iterator);
	       !mu_iterator_is_done (iterator); mu_iterator_next (iterator))
	    {
	      char *capa = NULL;
	      mu_iterator_current (iterator, (void **) &capa);
	      mu_printf ("CAPA: %s\n", capa ? capa : "");
	    }
	  mu_iterator_destroy (&iterator);
	}
    }
  return status;
}

static int
com_uidl (int argc, char **argv)
{
  int status = 0;
  if (argc == 1)
    {
      mu_stream_t out = mutool_open_pager ();
      mu_iterator_t uidl_iterator = NULL;
      status = mu_pop3_uidl_all (pop3, &uidl_iterator);
      if (status == 0)
	{
	  for (mu_iterator_first (uidl_iterator);
	       !mu_iterator_is_done (uidl_iterator);
	       mu_iterator_next (uidl_iterator))
	    {
	      char *uidl = NULL;
	      mu_iterator_current (uidl_iterator, (void **) &uidl);
	      mu_stream_printf (out, "UIDL: %s\n", uidl ? uidl : "");
	    }
	  mu_iterator_destroy (&uidl_iterator);
	}
      mu_stream_destroy (&out);
    }
  else
    {
      char *uidl = NULL;
      unsigned int msgno = strtoul (argv[1], NULL, 10);
      status = mu_pop3_uidl (pop3, msgno, &uidl);
      if (status == 0)
	mu_printf ("Msg: %d UIDL: %s\n", msgno, uidl ? uidl : "");
      free (uidl);
    }
  return status;
}

static int
com_list (int argc, char **argv)
{
  int status = 0;
  if (argc == 1)
    {
      mu_stream_t out = mutool_open_pager ();
      mu_iterator_t list_iterator;
      status = mu_pop3_list_all (pop3, &list_iterator);
      if (status == 0)
	{
	  for (mu_iterator_first (list_iterator);
	       !mu_iterator_is_done (list_iterator);
	       mu_iterator_next (list_iterator))
	    {
	      char *list = NULL;
	      mu_iterator_current (list_iterator, (void **) &list);
	      mu_stream_printf (out, "LIST: %s\n", (list) ? list : "");
	    }
	  mu_iterator_destroy (&list_iterator);
	  mu_stream_destroy (&out);
	}
    }
  else
    {
      size_t size = 0;
      unsigned int msgno = strtoul (argv[1], NULL, 10);
      status = mu_pop3_list (pop3, msgno, &size);
      if (status == 0)
	mu_printf ("Msg: %u Size: %lu\n", msgno, (unsigned long) size);
    }
  return status;
}

static int
com_noop (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  return mu_pop3_noop (pop3);
}

static int
com_pass (int argc, char **argv)
{
  int status;
  char *pwd, *passbuf = NULL;
  
  if (argc == 1)
    {
      if (!mutool_shell_interactive)
	{
	  mu_error (_("pass: password required"));
	  return 1;
	}
      status = mu_getpass (mu_strin, mu_strout, "Password:", &passbuf);
      if (status)
	return status;
      pwd = passbuf;
    }
  else
    pwd = argv[1];
  status = mu_pop3_pass (pop3, pwd);
  if (status == 0)
    {
      pop_session_status = pop_session_logged_in;
      pop_prompt_env ();
    }
  free (passbuf);
  return status;
}

static int
com_stat (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  size_t count = 0;
  mu_off_t size = 0;
  int status = 0;

  status = mu_pop3_stat (pop3, &count, &size);
  mu_printf ("Mesgs: %lu Size %lu\n",
	     (unsigned long) count, (unsigned long) size);
  return status;
}

static int
com_stls (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  return mu_pop3_stls (pop3);
}

static int
com_dele (int argc, char **argv)
{
  unsigned msgno;
  msgno = strtoul (argv[1], NULL, 10);
  return mu_pop3_dele (pop3, msgno);
}

static int
com_rset (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  return mu_pop3_rset (pop3);
}

static int
com_top (int argc, char **argv)
{
  mu_stream_t stream;
  unsigned int msgno;
  unsigned int lines;
  int status;

  msgno = strtoul (argv[1], NULL, 10);
  if (argc == 3)
    lines = strtoul (argv[2], NULL, 10);
  else
    lines = 5;

  status = mu_pop3_top (pop3, msgno, lines, &stream);

  if (status == 0)
    {
      mu_stream_t out = mutool_open_pager ();
      mu_stream_copy (out, stream, 0, NULL);
      mu_stream_destroy (&out);
      mu_stream_destroy (&stream);
    }
  return status;
}

static int
com_retr (int argc, char **argv)
{
  mu_stream_t stream;
  unsigned int msgno;
  int status;

  msgno = strtoul (argv[1], NULL, 10);
  status = mu_pop3_retr (pop3, msgno, &stream);

  if (status == 0)
    {
      mu_stream_t out = mutool_open_pager ();
      mu_stream_copy (out, stream, 0, NULL);
      mu_stream_destroy (&out);
      mu_stream_destroy (&stream);
    }
  return status;
}

static int
com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  if (pop3)
    {
      mu_pop3_disconnect (pop3);
      mu_pop3_destroy (&pop3);
      pop3 = NULL;
      
      mu_argcv_free (connect_argc, connect_argv);
      connect_argc = 0;
      connect_argv = NULL;
      pop_session_status = pop_session_disconnected;
      pop_prompt_env ();
    }
  return 0;
}

static int
port_from_sa (struct mu_sockaddr *sa)
{
  switch (sa->addr->sa_family)
    {
    case AF_INET:
      return ntohs (((struct sockaddr_in *)sa->addr)->sin_port);

#ifdef MAILUTILS_IPV6
    case AF_INET6:
      return ntohs (((struct sockaddr_in6 *)sa->addr)->sin6_port);
#endif
    }
  return 0;
}

static int
com_connect (int argc, char **argv)
{
  int status;
  int tls = 0;
  int i = 1;
  int n;
  
  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], "-tls") == 0)
	{
	  if (WITH_TLS)
	    tls = 1;
	  else
	    {
	      mu_error ("TLS not supported");
	      return 0;
	    }
	}
      else
	break;
    }

  argc -= i;
  argv += i;
  
  if (pop_session_status != pop_session_disconnected)
    com_disconnect (0, NULL);
  
  status = mu_pop3_create (&pop3);
  if (status == 0)
    {
      mu_stream_t tcp;
      struct mu_sockaddr *sa;
      struct mu_sockaddr_hints hints;

      if (QRY_VERBOSE ())
	{
	  pop_set_verbose ();
	  pop_set_verbose_mask ();
	}

      memset (&hints, 0, sizeof (hints));
      hints.flags = MU_AH_DETECT_FAMILY;
      hints.port = tls ? MU_POP3_DEFAULT_SSL_PORT : MU_POP3_DEFAULT_PORT;
      hints.protocol = IPPROTO_TCP;
      hints.socktype = SOCK_STREAM;
      status = mu_sockaddr_from_node (&sa, argv[0], argv[1], &hints);
      if (status == 0)
	{
	  n = port_from_sa (sa);
	  status = mu_tcp_stream_create_from_sa (&tcp, sa, NULL, 0);
	}
      if (status == 0)
	{
#ifdef WITH_TLS
	  if (tls)
	    {
	      mu_stream_t tlsstream;
	      
	      status = mu_tls_client_stream_create (&tlsstream, tcp, tcp, 0);
	      mu_stream_unref (tcp);
	      if (status)
		{
		  mu_error ("cannot create TLS stream: %s",
			    mu_strerror (status));
		  return 0;
		}
	      tcp = tlsstream;
	    }
#endif
	  mu_pop3_set_carrier (pop3, tcp);
	  status = mu_pop3_connect (pop3);
	}
      else
	{
	  mu_pop3_destroy (&pop3);
	  pop3 = NULL;
	}
    }

  if (status)
    mu_error ("Failed to create pop3: %s", mu_strerror (status));
  else
    {
      connect_argc = argc;
      connect_argv = xcalloc (argc, sizeof (*connect_argv));
      for (i = 0; i < argc; i++)
	connect_argv[i] = xstrdup (argv[i]);
      connect_argv[i] = NULL;
      port = n;
      pop_session_status = pop_session_connected;

      pop_prompt_env ();
    }
  
  return status;
}

static int
com_quit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  int status = 0;
  if (pop3)
    {
      if (mu_pop3_quit (pop3) == 0)
	{
	  status = com_disconnect (0, NULL);
	}
      else
	{
	  mu_printf ("Try 'exit' to leave %s\n", mu_program_name);
	}
    }
  else
    mu_printf ("Try 'exit' to leave %s\n", mu_program_name);
  return status;
}

struct mutool_command pop_comtab[] = {
  { "apop",       2,  3, com_apop,
    N_("USER [PASS]"),
    N_("authenticate with APOP") },
  { "capa",       1, -1, com_capa,
    /* TRANSLATORS: -reread is a keyword; do not translate. */
    N_("[-reread] [NAME...]"),
    N_("list server capabilities") },
  { "disconnect", 1, 1,
    com_disconnect,
    NULL,
    N_("close connection") },
  { "dele",       2, 2, com_dele,
    N_("NUMBER"),
    N_("mark message for deletion") },
  { "list",       1, 2, com_list,
    N_("[NUMBER]"),
    N_("list messages") },
  { "noop",       1, 1, com_noop,
    NULL,
    N_("send a \"no operation\"") },
  { "pass",       1, 2, com_pass,
    N_("[PASSWORD]"),
    N_("send password") },
  { "connect",    1, 4, com_connect,
    /* TRANSLATORS: --tls is a keyword. */
    N_("[-tls] HOSTNAME [PORT]"),
    N_("open connection") },
  { "quit",       1, 1, com_quit,
    NULL,
    N_("quit pop3 session") },
  { "retr",       2, 2, com_retr,
    "NUMBER",
    N_("retrieve a message") },
  { "rset",       1, 1, com_rset,
    NULL,
    N_("remove deletion marks") },
  { "stat",       1, 1, com_stat,
    NULL,
    N_("get the mailbox size and number of messages in it") },
  { "stls",       1, 1, com_stls,
    NULL,
    N_("start TLS negotiation") },
  { "top",        2, 3, com_top,
    "MSGNO [NUMBER]",
    N_("display message headers and first NUMBER (default 5) lines of"
       " its body") },
  { "uidl",       1, 2, com_uidl,
    N_("[NUMBER]"),
    N_("show unique message identifiers") },
  { "user",       2, 2, com_user,
    N_("NAME"),
    N_("send login") },
  { "verbose",    1, 4, com_verbose,
    "[on|off|mask|unmask] [secure [payload]]",
    N_("control the protocol tracing") },
  { NULL }
};


int
mutool_pop (int argc, char **argv)
{
  int index;

  if (argp_parse (&pop_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
    return 1;

  argc -= index;
  argv += index;

  if (argc)
    {
      mu_error (_("too many arguments"));
      return 1;
    }

  /* Command line prompt */
  mutool_shell_prompt = xstrdup ("pop> ");
  pop_prompt_env ();
  mutool_shell ("pop", pop_comtab);
  return 0;
}

/*
  MU Setup: pop
  mu-handler: mutool_pop
  mu-docstring: pop_docstring
  mu-cond: ENABLE_POP
  End MU Setup:
*/