imap.c 30.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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010-2012, 2014 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 <termios.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <mailutils/mailutils.h>
#include <mailutils/imap.h>
#include <mailutils/imapio.h>
#include <mailutils/imaputil.h>
#include <mailutils/msgset.h>
#include "mu.h"
#include "argp.h"

static char imap_doc[] = N_("mu imap - IMAP4 client shell.");
char imap_docstring[] = N_("IMAP4 client shell");
static char imap_args_doc[] = "";

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

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

static struct argp imap_argp = {
  imap_options,
  imap_parse_opt,
  imap_args_doc,
  imap_doc,
  NULL,
  NULL,
  NULL
};


static mu_imap_t imap;
static int uid_mode;

static enum mu_imap_session_state
current_imap_state ()
{
  int state;
  if (imap == NULL)
    state = MU_IMAP_SESSION_INIT;
  else
    state = mu_imap_session_state (imap);
  return state;
}


static void
imap_set_verbose ()
{
  if (imap)
    {
      if (QRY_VERBOSE ())
	mu_imap_trace (imap, MU_IMAP_TRACE_SET);
      else
	mu_imap_trace (imap, MU_IMAP_TRACE_CLR);
    }
}

void
imap_set_verbose_mask ()
{
  if (imap)
    {
      mu_imap_trace_mask (imap, QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE)
			          ? MU_IMAP_TRACE_SET : MU_IMAP_TRACE_CLR,
			      MU_XSCRIPT_SECURE);
      mu_imap_trace_mask (imap, QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD)
			          ? MU_IMAP_TRACE_SET : MU_IMAP_TRACE_CLR,
			      MU_XSCRIPT_PAYLOAD);
    }
}

static int
com_verbose (int argc, char **argv)
{
  return shell_verbose (argc, argv,
			imap_set_verbose, imap_set_verbose_mask);
  
}

static mu_msgset_t
parse_msgset (const char *arg)
{
  int status;
  mu_msgset_t msgset;
  char *p;
  
  status = mu_msgset_create (&msgset, NULL, MU_MSGSET_NUM);
  if (status)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_create", NULL, status);
      return NULL;
    }
  status = mu_msgset_parse_imap (msgset, MU_MSGSET_NUM, arg, &p);
  if (status)
    {
      mu_error (_("failed to parse message set near \"%s\": %s"),
		p, mu_strerror (status));
      mu_msgset_destroy (&msgset);
    }
  return msgset;
}

static void
report_failure (const char *what, int status)
{
  const char *str;
      
  mu_error (_("%s failed: %s"), what, mu_strerror (status));
  if (mu_imap_strerror (imap, &str) == 0)
    mu_error (_("server reply: %s"), str);
}

static int connect_argc;
static char **connect_argv;
#define host connect_argv[0]

static char *username;

static void
imap_prompt_env ()
{
  enum mu_imap_session_state state = current_imap_state ();
  if (!mutool_prompt_env)
    mutool_prompt_env = mu_calloc (2*7 + 1, sizeof(mutool_prompt_env[0]));

  mutool_prompt_env[0] = "user";
  mutool_prompt_env[1] = (state >= MU_IMAP_SESSION_AUTH && username) ?
                           username : "[nouser]";

  mutool_prompt_env[2] = "host"; 
  mutool_prompt_env[3] = connect_argv ? 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";
  if (mu_imap_session_state_str (state,
				 (const char **) &mutool_prompt_env[13]))
    mutool_prompt_env[12] = NULL;

  mutool_prompt_env[14] = NULL;
}

/* Callbacks */
static void
imap_preauth_callback (void *data, int code, size_t sdat, void *pdat)
{
  const char *text = pdat;
  if (text)
    mu_diag_output (MU_DIAG_INFO, _("session authenticated: %s"), text);
  else
    mu_diag_output (MU_DIAG_INFO, _("session authenticated"));
}

static void
imap_bye_callback (void *data, int code, size_t sdat, void *pdat)
{
  const char *text = pdat;
  if (text)
    mu_diag_output (MU_DIAG_INFO, _("server is closing connection: %s"), text);
  else
    mu_diag_output (MU_DIAG_INFO, _("server is closing connection"));
}

static void
imap_bad_callback (void *data, int code, size_t sdat, void *pdat)
{
  const char *text = pdat;
  mu_diag_output (MU_DIAG_CRIT, "SERVER ALERT: %s", text);
}

/* Fetch callback */
static void
format_email (mu_stream_t str, const char *name, mu_address_t addr)
{
  mu_stream_printf (str, "  %s = ", name);
  if (!addr)
    mu_stream_printf (str, "NIL");
  else
    mu_stream_format_address (str, addr);
  mu_stream_printf (str, "\n");
}

static void
format_date (mu_stream_t str, char *name,
	     struct tm *tm, struct mu_timezone *tz)
{
  char date[128];
  
  strftime (date, sizeof (date), "%a %b %e %H:%M:%S", tm);
  mu_stream_printf (str, "  %s = %s", name, date);
  if (tz->tz_name)
    mu_stream_printf (str, " %s", tz->tz_name);
  else
    {
      int off = tz->utc_offset;
      if (off < 0)
	{
	  mu_stream_printf (str, " -");
	  off = - off;
	}
      else
	mu_stream_printf (str, " +");
      off /= 60;
      mu_stream_printf (str, "%02d%02d", off / 60, off % 60);
    }
  mu_stream_printf (str, "\n");
}

#define S(str) ((str) ? (str) : "")

static void
print_param (mu_stream_t ostr, const char *prefix, mu_assoc_t assoc,
	     int indent)
{
  mu_iterator_t itr;
  int i;

  mu_stream_printf (ostr, "%*s%s:\n", indent, "", prefix);
  indent += 4;
  if (mu_assoc_get_iterator (assoc, &itr))
    return;
  for (i = 0, mu_iterator_first (itr);
       !mu_iterator_is_done (itr);
       i++, mu_iterator_next (itr))
    {
      const char *name;
      struct mu_mime_param *p;
      
      mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
      mu_stream_printf (ostr, "%*s%d: %s=%s\n", indent, "", i, name, p->value);
    }
  mu_iterator_destroy (&itr);
}

struct print_data
{
  mu_stream_t ostr;
  int num;
  int level;
};

static void print_bs (mu_stream_t ostr,
		      struct mu_bodystructure *bs, int level);

static int
print_item (void *item, void *data)
{
  struct mu_bodystructure *bs = item;
  struct print_data *pd = data;
  mu_stream_printf (pd->ostr, "%*sPart #%d\n", (pd->level-1) << 2, "",
		    pd->num);
  print_bs (pd->ostr, bs, pd->level);
  ++pd->num;
  return 0;
}

static void
print_address (mu_stream_t ostr, const char *title, mu_address_t addr,
	       int indent)
{
  mu_stream_printf (ostr, "%*s%s: ", indent, "", title);
  mu_stream_format_address (mu_strout, addr);
  mu_stream_printf (ostr, "\n");
}

static void
print_imapenvelope (mu_stream_t ostr, struct mu_imapenvelope *env, int level)
{
  int indent = (level << 2);

  mu_stream_printf (ostr, "%*sEnvelope:\n", indent, "");
  indent += 4;
  mu_stream_printf (ostr, "%*sTime: ", indent, "");
  mu_c_streamftime (mu_strout, "%c%n", &env->date, &env->tz);
  mu_stream_printf (ostr, "%*sSubject: %s\n", indent, "", S(env->subject));
  print_address (ostr, "From", env->from, indent);
  print_address (ostr, "Sender", env->sender, indent);
  print_address (ostr, "Reply-to", env->reply_to, indent);
  print_address (ostr, "To", env->to, indent);
  print_address (ostr, "Cc", env->cc, indent);
  print_address (ostr, "Bcc", env->bcc, indent);
  mu_stream_printf (ostr, "%*sIn-Reply-To: %s\n", indent, "",
		    S(env->in_reply_to));
  mu_stream_printf (ostr, "%*sMessage-ID: %s\n", indent, "",
		    S(env->message_id));
}

static void
print_bs (mu_stream_t ostr, struct mu_bodystructure *bs, int level)
{
  int indent = level << 2;
  mu_stream_printf (ostr, "%*sbody_type=%s\n", indent, "", S(bs->body_type));
  mu_stream_printf (ostr, "%*sbody_subtype=%s\n", indent, "",
		    S(bs->body_subtype));
  print_param (ostr, "Parameters", bs->body_param, indent);
  mu_stream_printf (ostr, "%*sbody_id=%s\n", indent, "", S(bs->body_id));
  mu_stream_printf (ostr, "%*sbody_descr=%s\n", indent, "", S(bs->body_descr));
  mu_stream_printf (ostr, "%*sbody_encoding=%s\n", indent, "",
		    S(bs->body_encoding));
  mu_stream_printf (ostr, "%*sbody_size=%lu\n", indent, "",
		    (unsigned long) bs->body_size);
  /* Optional */
  mu_stream_printf (ostr, "%*sbody_md5=%s\n", indent, "", S(bs->body_md5));
  mu_stream_printf (ostr, "%*sbody_disposition=%s\n", indent, "",
		    S(bs->body_disposition));
  print_param (ostr, "Disposition Parameters", bs->body_disp_param, indent);
  mu_stream_printf (ostr, "%*sbody_language=%s\n", indent, "",
		    S(bs->body_language));
  mu_stream_printf (ostr, "%*sbody_location=%s\n", indent, "",
		    S(bs->body_location));

  mu_stream_printf (ostr, "%*sType ", indent, "");
  switch (bs->body_message_type)
    {
    case mu_message_other:
      mu_stream_printf (ostr, "mu_message_other\n");
      break;
      
    case mu_message_text:
      mu_stream_printf (ostr, "mu_message_text:\n%*sbody_lines=%lu\n",
			indent + 4, "",
			(unsigned long) bs->v.text.body_lines);
      break;
      
    case mu_message_rfc822:
      mu_stream_printf (ostr, "mu_message_rfc822:\n%*sbody_lines=%lu\n",
			indent + 4, "",
		 (unsigned long) bs->v.rfc822.body_lines);
      print_imapenvelope (ostr, bs->v.rfc822.body_env, level + 1);
      print_bs (ostr, bs->v.rfc822.body_struct, level + 1);
      break;
      
    case mu_message_multipart:
      {
	struct print_data pd;
	pd.ostr = ostr;
	pd.num = 0;
	pd.level = level + 1;
	mu_stream_printf (ostr, "mu_message_multipart:\n");
	mu_list_foreach (bs->v.multipart.body_parts, print_item, &pd);
      }
    }
}

static int
fetch_response_printer (void *item, void *data)
{
  union mu_imap_fetch_response *resp = item;
  mu_stream_t str = data;

  switch (resp->type)
    {
    case MU_IMAP_FETCH_BODY:
      mu_stream_printf (str, "BODY [");
      if (resp->body.partv)
	{
	  size_t i;
	  
	  for (i = 0; i < resp->body.partc; i++)
	    mu_stream_printf (str, "%lu.",
			      (unsigned long) resp->body.partv[i]);
	}
      if (resp->body.section)
	mu_stream_printf (str, "%s", resp->body.section);
      mu_stream_printf (str, "]");
      mu_stream_printf (str, "\nBEGIN\n%s\nEND\n", resp->body.text);
      break;
      
    case MU_IMAP_FETCH_BODYSTRUCTURE:
      /* FIXME */
      mu_stream_printf (str, "BODYSTRUCTURE:\nBEGIN\n");
      print_bs (str, resp->bodystructure.bs, 0);
      mu_stream_printf (str, "END\n");
      break;
      
    case MU_IMAP_FETCH_ENVELOPE:
      {
	struct mu_imapenvelope *env = resp->envelope.imapenvelope;
	
	mu_stream_printf (str, "ENVELOPE:\n");
	
	format_date (str, "date", &env->date, &env->tz);
	mu_stream_printf (str, "  subject = %s\n",
			  env->subject ?
			   env->subject : "NIL");

	format_email (str, "from", env->from);
	format_email (str, "sender", env->sender);
	format_email (str, "reply-to", env->reply_to);
	format_email (str, "to", env->to);
	format_email (str, "cc", env->cc);
	format_email (str, "bcc", env->bcc);

	mu_stream_printf (str, "  in-reply-to = %s\n",
			  env->in_reply_to ?
			   env->in_reply_to : "NIL");
	mu_stream_printf (str, "  message-id = %s\n",
			  env->message_id ?
			   env->message_id : "NIL");
      }
      break;
      
    case MU_IMAP_FETCH_FLAGS:
      mu_stream_printf (str, "  flags = ");
      mu_imap_format_flags (str, resp->flags.flags, 1);
      mu_stream_printf (str, "\n");
      break;
      
    case MU_IMAP_FETCH_INTERNALDATE:
      format_date (str, "internaldate", &resp->internaldate.tm,
		   &resp->internaldate.tz);
      break;
      
    case MU_IMAP_FETCH_RFC822_SIZE:
      mu_stream_printf (str, "  size = %lu\n",
			(unsigned long) resp->rfc822_size.size);
      break;
      
    case MU_IMAP_FETCH_UID:
      mu_stream_printf (str, "  UID = %lu\n",
			(unsigned long) resp->uid.uid);
    }
  return 0;
}

static void
imap_fetch_callback (void *data, int code, size_t sdat, void *pdat)
{
  mu_stream_t str = data;
  mu_list_t list = pdat;

  mu_stream_printf (str, "Message #%lu:\n", (unsigned long) sdat);
  mu_list_foreach (list, fetch_response_printer, str);
  mu_stream_printf (str, "\n\n");
}

static int
com_uid (int argc, char **argv)
{
  int bv;
  
  if (argc == 1)
    mu_printf ("%s\n", uid_mode ? _("UID is on") : _("UID is off"));
  else if (get_bool (argv[1], &bv) == 0)
    uid_mode = bv;
  else
    mu_error (_("invalid boolean value"));
  return 0;
}

static int
com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  if (imap)
    {
      mu_imap_disconnect (imap);
      mu_imap_destroy (&imap);
      
      mu_argcv_free (connect_argc, connect_argv);
      connect_argc = 0;
      connect_argv = NULL;
      imap_prompt_env ();
    }
  return 0;
}

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

  argc -= i;
  argv += i;
  
  state = current_imap_state ();
  
  if (state != MU_IMAP_SESSION_INIT)
    com_disconnect (0, NULL);
  
  status = mu_imap_create (&imap);
  if (status == 0)
    {
      mu_stream_t tcp;
      struct mu_sockaddr *sa;
      struct mu_sockaddr_hints hints;

      memset (&hints, 0, sizeof (hints));
      hints.flags = MU_AH_DETECT_FAMILY;
      hints.port = tls ? MU_IMAP_DEFAULT_SSL_PORT : MU_IMAP_DEFAULT_PORT;
      hints.protocol = IPPROTO_TCP;
      hints.socktype = SOCK_STREAM;
      status = mu_sockaddr_from_node (&sa, argv[0], argv[1], &hints);
      if (status == 0)
        {
	  status = mu_tcp_stream_create_from_sa (&tcp, sa, NULL, 0);
          if (status)
            mu_sockaddr_free (sa);
        }
      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_imap_set_carrier (imap, tcp);

	  if (QRY_VERBOSE ())
	    {
	      imap_set_verbose ();
	      imap_set_verbose_mask ();
	    }

	  /* Set callbacks */
	  mu_imap_register_callback_function (imap, MU_IMAP_CB_PREAUTH,
					      imap_preauth_callback,
					      NULL);
	  mu_imap_register_callback_function (imap, MU_IMAP_CB_BYE,
					      imap_bye_callback,
					      NULL);
	  mu_imap_register_callback_function (imap, MU_IMAP_CB_BAD,
					      imap_bad_callback,
					      NULL);
	  mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
					      imap_fetch_callback,
					      mu_strout);
	  
	  status = mu_imap_connect (imap);
	  if (status)
	    {
	      const char *err;

	      mu_error ("Failed to connect: %s", mu_strerror (status));
	      if (mu_imap_strerror (imap, &err))
		mu_error ("server response: %s", err);
	      mu_imap_destroy (&imap);
	    }
	}
      else
	mu_imap_destroy (&imap);
    }
  else
    mu_error (_("Failed to create imap connection: %s"), mu_strerror (status));
  
  if (!status)
    {
      connect_argc = argc;
      connect_argv = mu_calloc (argc, sizeof (*connect_argv));
      for (i = 0; i < argc; i++)
	connect_argv[i] = mu_strdup (argv[i]);
      connect_argv[i] = NULL;
    
      imap_prompt_env ();
    }
  
  return status;
}

static int
com_starttls (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  int status = mu_imap_starttls (imap);
  if (status)
    report_failure ("starttls", status);
  return 0;
}

static int
com_logout (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  int status = 0;
  if (imap)
    {
      if (mu_imap_logout (imap) == 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;
}

static int
com_capability (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_imap_capability (imap, 1, NULL);
	  if (status)
	    return status;
	}
      for (; i < argc; i++)
	{
	  const char *elt;
	  int rc = mu_imap_capability_test (imap, 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_imap_capability (imap, 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_login (int argc, char **argv)
{
  int status;
  char *pwd, *passbuf = NULL;

  if (!imap)
    {
      mu_error (_("you need to connect first"));
      return 0;
    }
  
  if (argc == 2)
    {
      if (!mutool_shell_interactive)
	{
	  mu_error (_("login: password required"));
	  return 1;
	}
      status = mu_getpass (mu_strin, mu_strout, _("Password:"), &passbuf);
      if (status)
	return status;
      pwd = passbuf;
    }
  else
    pwd = argv[2];

  status = mu_imap_login (imap, argv[1], pwd);
  memset (pwd, 0, strlen (pwd));
  free (passbuf);
  if (status == 0)
    imap_prompt_env ();
  else
    report_failure ("login", status);
  return 0;
}

static int
com_id (int argc, char **argv)
{
  mu_assoc_t assoc;
  char *test = NULL;
  int status;

  argv++;
  if (argv[0] && strcmp (argv[0], "-test") == 0)
    {
      argv++;
      if (argv[0] == NULL)
	{
	  mu_error (_("id -test requires an argument"));
	  return 0;
	}
      test = argv[0];
      argv++;
    }
  
  status = mu_imap_id (imap, argv + 1, &assoc);
  if (status == 0)
    {
      if (test)
	{
	  void *res = mu_assoc_ref (assoc, test);
	  if (res)
	    {
	      mu_printf ("%s: %s\n", test, *(char **)res);
	    }
	  else
	    mu_printf (_("%s is not set\n"), test);
	}
      else
	{
	  mu_iterator_t itr;
	  
	  mu_assoc_get_iterator (assoc, &itr);
	  for (mu_iterator_first (itr);
	       !mu_iterator_is_done (itr); mu_iterator_next (itr))
	    {
	      char *key;
	      void *val;

	      mu_iterator_current_kv (itr, (const void**)&key, &val);
	      mu_printf ("ID: %s %s\n", key, *(char**)val);
	    }
	  mu_iterator_destroy (&itr);
	}
      mu_assoc_destroy (&assoc);
    }
  return status;
}

static void
print_imap_stats (struct mu_imap_stat *st)
{
  if (st->flags & MU_IMAP_STAT_DEFINED_FLAGS)
    {
      mu_printf (_("Flags defined: "));
      mu_imap_format_flags (mu_strout, st->defined_flags, 0);
      mu_printf ("\n");
    }
  if (st->flags & MU_IMAP_STAT_PERMANENT_FLAGS)
    {
      mu_printf (_("Flags permanent: "));
      mu_imap_format_flags (mu_strout, st->permanent_flags, 0);
      mu_printf ("\n");
    }

  if (st->flags & MU_IMAP_STAT_MESSAGE_COUNT)
    mu_printf (_("Total messages: %lu\n"), (unsigned long) st->message_count);
  if (st->flags & MU_IMAP_STAT_RECENT_COUNT)
    mu_printf (_("Recent messages: %lu\n"), (unsigned long) st->recent_count);
  if (st->flags & MU_IMAP_STAT_FIRST_UNSEEN)
    mu_printf (_("First unseen message: %lu\n"),
	       (unsigned long) st->first_unseen);
  if (st->flags & MU_IMAP_STAT_UIDNEXT)
    mu_printf (_("Next UID: %lu\n"), (unsigned long) st->uidnext);
  if (st->flags & MU_IMAP_STAT_UIDVALIDITY)
    mu_printf (_("UID validity: %lu\n"), st->uidvalidity);
}


static int
select_mbox (int argc, char **argv, int writable)
{
  int status;
  struct mu_imap_stat st;

  status = mu_imap_select (imap, argv[1], writable, &st);
  if (status == 0)
    {
      print_imap_stats (&st);
      imap_prompt_env ();
    }
  else
    report_failure ("select", status);
  return 0;
}

static int
com_select (int argc, char **argv)
{
  return select_mbox (argc, argv, 1);
}

static int
com_examine (int argc, char **argv)
{
  return select_mbox (argc, argv, 0);
}

static int
com_status (int argc, char **argv)
{
  struct mu_imap_stat st;
  int i, flag;
  int status;
    
  st.flags = 0;
  for (i = 2; i < argc; i++)
    {
      if (mu_kwd_xlat_name_ci (_mu_imap_status_name_table, argv[i], &flag))
	{
	  mu_error (_("unknown data item: %s"), argv[i]);
	  return 0;
	}
      st.flags |= flag;
    }

  status = mu_imap_status (imap, argv[1], &st);
  if (status == 0)
    {
      print_imap_stats (&st);
    }
  else
    report_failure ("status", status);
  return 0;
}

static int
com_noop (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  int status = mu_imap_noop (imap);
  if (status)
    report_failure ("noop", status);
  return 0;
}

static int
com_check (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  int status = mu_imap_check (imap);
  if (status)
    report_failure ("check", status);
  return 0;
}

static int
com_fetch (int argc, char **argv)
{
  mu_stream_t out = mutool_open_pager ();
  mu_msgset_t msgset = parse_msgset (argv[1]);

  if (msgset)
    {
      int status;

      mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
					  imap_fetch_callback,
					  out);
      status = mu_imap_fetch (imap, uid_mode, msgset, argv[2]);
      mu_stream_destroy (&out);
      mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
					  imap_fetch_callback,
					  mu_strout);
      mu_msgset_free (msgset);
      if (status)
	report_failure ("fetch", status);
    }
  return 0;  
}

static int
com_store (int argc, char **argv)
{
  mu_msgset_t msgset = parse_msgset (argv[1]);

  if (msgset)
    {
      int status = mu_imap_store (imap, uid_mode, msgset, argv[2]);
      if (status)
	report_failure ("store", status);
    }
  return 0;
}

static int
com_close (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  int status = mu_imap_close (imap);
  if (status)
    report_failure ("close", status);
  return 0;
}

static int
com_unselect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  int status = mu_imap_noop (imap);
  if (status)
    report_failure ("unselect", status);
  return 0;
}

static int
com_delete (int argc, char **argv)
{
  int status = mu_imap_delete (imap, argv[1]);
  if (status)
    report_failure ("delete", status);
  return 0;
}

static int
com_rename (int argc, char **argv)
{
  int status = mu_imap_rename (imap, argv[1], argv[2]);
  if (status)
    report_failure ("rename", status);
  return 0;
}

static int
com_expunge (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
  int status = mu_imap_expunge (imap);
  if (status)
    report_failure ("expunge", status);
  return 0;
}

static int
com_create (int argc, char **argv)
{
  int status = mu_imap_mailbox_create (imap, argv[1]);
  if (status)
    report_failure ("create", status);
  return 0;
}

static int
com_subscribe (int argc, char **argv)
{
  int status = mu_imap_subscribe (imap, argv[1]);
  if (status)
    report_failure ("subscribe", status);
  return 0;
}

static int
com_unsubscribe (int argc, char **argv)
{
  int status = mu_imap_unsubscribe (imap, argv[1]);
  if (status)
    report_failure ("unsubscribe", status);
  return 0;
}

static int
com_append (int argc, char **argv)
{
  struct tm tmbuf, *tm = NULL;
  struct mu_timezone tzbuf, *tz = NULL;
  int flags = 0;
  mu_stream_t stream;
  int rc, i;
  
  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], "-time") == 0)
	{
	  char *p;
	  
	  if (++i == argc)
	    {
	      mu_error (_("-time requires argument"));
	      return 0;
	    }
	  rc = mu_scan_datetime (argv[i], MU_DATETIME_INTERNALDATE,
				 &tmbuf, &tzbuf, &p);
	  if (rc || *p)
	    {
	      mu_error (_("cannot parse time"));
	      return 0;
	    }
	  tm = &tmbuf;
	  tz = &tzbuf;
	}
      else if (strcmp (argv[i], "-flag") == 0)
	{
	  if (++i == argc)
	    {
	      mu_error (_("-flag requires argument"));
	      return 0;
	    }
	  if (mu_imap_flag_to_attribute (argv[i], &flags))
	    {
	      mu_error (_("unrecognized flag: %s"), argv[i]);
	      return 0;
	    }
	}
      else if (strcmp (argv[i], "--") == 0)
	{
	  i++;
	  break;
	}
      else if (argv[i][0] == '-')
	{
	  mu_error (_("unrecognized option: %s"), argv[i]);
	  return 0;
	}
      else
	break;
    }

  if (argc - i != 2)
    {
      mu_error (_("wrong number of arguments"));
      return 0;
    }

  rc = mu_file_stream_create (&stream, argv[i + 1],
			      MU_STREAM_READ|MU_STREAM_SEEK);
  if (rc)
    {
      mu_error (_("cannot open file %s: %s"), argv[i + 1], mu_strerror (rc));
      return 0;
    }

  rc = mu_imap_append_stream (imap, argv[i], flags, tm, tz, stream);
  mu_stream_unref (stream);
  if (rc)
    report_failure ("append", rc);
  return 0;
}

static int
com_copy (int argc, char **argv)
{
  mu_msgset_t msgset = parse_msgset (argv[1]);
  if (msgset)
    {
      int status = mu_imap_copy (imap, uid_mode, msgset, argv[2]);
      mu_msgset_free (msgset);
      if (status)
	report_failure ("copy", status);
    }
  return 0;
}

static int
com_search (int argc, char **argv)
{
  mu_msgset_t mset;
  size_t count;
  int rc;
  
  rc = mu_imap_search (imap, uid_mode, argv[1], &mset);
  if (rc)
    {
      report_failure ("search", rc);
      return 0;
    }
	
  rc = mu_msgset_count (mset, &count);
  if (rc == EINVAL || count == 0)
    {
      mu_printf (_("no matches"));
      return 0;
    }
  mu_printf ("%lu matches:", (unsigned long) count);
  mu_msgset_print (mu_strout, mset);
  mu_printf ("\n");
  mu_msgset_free (mset);
  
  return 0;
}


static int
print_list_item (void *item, void *data)
{
  struct mu_list_response *resp = item;
  mu_stream_t out = data;

  mu_stream_printf (out,
		    "%c%c %c %4d %s\n",
		    (resp->type & MU_FOLDER_ATTRIBUTE_DIRECTORY) ? 'd' : '-',
		    (resp->type & MU_FOLDER_ATTRIBUTE_FILE) ? 'f' : '-',
		    resp->separator ?
		        resp->separator : MU_HIERARCHY_DELIMITER,
		    resp->level,
		    resp->name);
  return 0;
}

static int
com_list (int argc, char **argv)
{
  mu_list_t list;
  int rc;
  mu_stream_t out;
  
  rc = mu_imap_list_new (imap, argv[1], argv[2], &list);
  if (rc)
    {
      report_failure ("list", rc);
      return 0;
    }

  out = mutool_open_pager ();
  mu_list_foreach (list, print_list_item, out);
  mu_stream_unref (out);
  return 0;
}

static int
com_lsub (int argc, char **argv)
{
  mu_list_t list;
  int rc;
  mu_stream_t out;
  
  rc = mu_imap_lsub_new (imap, argv[1], argv[2], &list);
  if (rc)
    {
      report_failure ("lsub", rc);
      return 0;
    }

  out = mutool_open_pager ();
  mu_list_foreach (list, print_list_item, out);
  mu_stream_unref (out);
  return 0;
}

struct mutool_command imap_comtab[] = {
  { "capability",   1, -1, 0,
    com_capability,
    /* TRANSLATORS: -reread is a keyword; do not translate. */
    N_("[-reread] [NAME...]"),
    N_("list server capabilities") },
  { "verbose",      1, 4, 0,
    com_verbose,
    "[on|off|mask|unmask] [secure [payload]]",
    N_("control the protocol tracing") },
  { "connect",      1, 4, 0,
    com_connect,
    /* TRANSLATORS: -tls is a keyword. */
    N_("[-tls] HOSTNAME [PORT]"),
    N_("open connection") },
  { "disconnect",   1, 1, 0,
    com_disconnect,
    NULL,
    N_("close connection") },
  { "starttls",     1, 1, 0,
    com_starttls,
    NULL,
    N_("Establish TLS encrypted channel") },
  { "login",        2, 3, 0,
    com_login,
    N_("USER [PASS]"),
    N_("login to the server") },
  { "logout",       1, 1, 0,
    com_logout,
    NULL,
    N_("quit imap session") },
  { "id",           1, -1, 0,
    com_id,
    N_("[-test KW] [ARG [ARG...]]"),
    N_("send ID command") },
  { "noop",         1, 1, 0,
    com_noop,
    NULL,
    N_("no operation (keepalive)") },
  { "check",        1, 1, 0,
    com_check,
    NULL,
    N_("request a server checkpoint") },
  { "select",       1, 2, 0,
    com_select,
    N_("[MBOX]"),
    N_("select a mailbox") },
  { "examine",      1, 2, 0,
    com_examine,
    N_("[MBOX]"),
    N_("examine a mailbox") },
  { "status",       3, -1, 0,
    com_status,
    N_("MBOX KW [KW...]"),
    N_("get mailbox status") },
  { "fetch",        3, 3, CMD_COALESCE_EXTRA_ARGS,
    com_fetch,
    N_("MSGSET ITEMS"),
    N_("fetch message data") },
  { "store",        3, 3, CMD_COALESCE_EXTRA_ARGS,
    com_store,
    N_("MSGSET ITEMS"),
    N_("alter mailbox data") },
  { "close",        1, 1, 0,
    com_close,
    NULL,
    N_("close the mailbox (with expunge)") },
  { "unselect",     1, 1, 0,
    com_unselect,
    NULL,
    N_("close the mailbox (without expunge)") },
  { "delete",       2, 2, 0,
    com_delete,
    N_("MAILBOX"),
    N_("delete the mailbox") },
  { "rename",       3, 3, 0,
    com_rename,
    N_("OLD-NAME NEW-NAME"),
    N_("rename existing mailbox") },
  { "expunge",      1, 1, 0,
    com_expunge,
    NULL,
    N_("permanently remove messages marked for deletion") },
  { "create",       2, 2, 0,
    com_create,
    N_("MAILBOX"),
    N_("create new mailbox") },
  { "append",       3, -1, 0,
    com_append,
    N_("[-time DATETIME] [-flag FLAG] MAILBOX FILE"),
    N_("append message text from FILE to MAILBOX") },
  { "copy",         3, 3, 0,
    com_copy,
    N_("MSGSET MAILBOX"),
    N_("Copy messages from MSGSET to MAILBOX") },
  { "list",         3, 3, 0,
    com_list,
    N_("REF MBOX"),
    N_("List matching mailboxes") },
  { "lsub",         3, 3, 0,
    com_lsub,
    N_("REF MBOX"),
    N_("List subscribed mailboxes") },
  { "subscribe",    2, 2, 0,
    com_subscribe,
    N_("MBOX"),
    N_("Subscribe to a mailbox") },
  { "unsubscribe",    2, 2, 0,
    com_unsubscribe,
    N_("MBOX"),
    N_("Remove mailbox from subscription list") },
  { "uid",          1, 2, 0,
    com_uid,
    N_("[on|off]"),
    N_("control UID mode") },
  { "search",       2, 2, CMD_COALESCE_EXTRA_ARGS,
    com_search,
    N_("args..."),
    N_("search the mailbox") },
  { "quit",         1, 1, 0,
    com_logout,
    NULL,
    N_("same as `logout'") },
  { NULL }
};

int
mutool_imap (int argc, char **argv)
{
  int index;

  if (argp_parse (&imap_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 = mu_strdup ("imap> ");
  imap_prompt_env ();
  mutool_shell ("imap", imap_comtab);
  return 0;
}

/*
  MU Setup: imap
  mu-handler: mutool_imap
  mu-docstring: imap_docstring
  mu-cond: ENABLE_IMAP
  End MU Setup:
*/