fetch.c 29.3 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
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2001 Free Software Foundation, Inc.

   This program 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 2, or (at your option)
   any later version.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "imap4d.h"
#include <ctype.h>

/* This will suck, too.
   Alain: Yes it does.  */

/*  Taken from RFC2060
    fetch           ::= "FETCH" SPACE set SPACE ("ALL" / "FULL" /
    "FAST" / fetch_att / "(" 1#fetch_att ")")

    fetch_att       ::= "ENVELOPE" / "FLAGS" / "INTERNALDATE" /
    "RFC822" [".HEADER" / ".SIZE" / ".TEXT"] /
    "BODY" ["STRUCTURE"] / "UID" /
    "BODY" [".PEEK"] section
    ["<" number "." nz_number ">"]
*/

struct fetch_command;

static int fetch_all               __P ((struct fetch_command *, char*));
static int fetch_full              __P ((struct fetch_command *, char*));
static int fetch_fast              __P ((struct fetch_command *, char*));
static int fetch_envelope          __P ((struct fetch_command *, char*));
static int fetch_envelope0         __P ((message_t));
static int fetch_flags             __P ((struct fetch_command *, char*));
static int fetch_internaldate      __P ((struct fetch_command *, char*));
static int fetch_rfc822_header     __P ((struct fetch_command *, char*));
static int fetch_rfc822_size       __P ((struct fetch_command *, char*));
static int fetch_rfc822_text       __P ((struct fetch_command *, char*));
static int fetch_rfc822            __P ((struct fetch_command *, char*));
static int fetch_bodystructure     __P ((struct fetch_command *, char*));
static int fetch_bodystructure0    __P ((message_t, int));
static int bodystructure           __P ((message_t, int));
static int fetch_body              __P ((struct fetch_command *, char*));
static int fetch_uid               __P ((struct fetch_command *, char*));

static int fetch_operation         __P ((size_t, char *, int));
static int fetch_message           __P ((message_t, unsigned long, unsigned long));
static int fetch_header            __P ((message_t, unsigned long, unsigned long));
static int fetch_content           __P ((message_t, unsigned long, unsigned long));
static int fetch_io                __P ((stream_t, unsigned long, unsigned long));
static int fetch_header_fields     __P ((message_t, char *, unsigned long, unsigned long));
static int fetch_header_fields_not __P ((message_t, char *, unsigned long, unsigned long));
static int fetch_send_address      __P ((char *));

static struct fetch_command* fetch_getcommand __P ((char *, struct fetch_command[]));

struct fetch_command
{
  const char *name;
  int (*func) __P ((struct fetch_command *, char *));
  size_t msgno;
} fetch_command_table [] =
{
#define F_ALL 0
  {"ALL", fetch_all, 0},
#define F_FULL 1
  {"FULL", fetch_full, 0},
#define F_FAST 2
  {"FAST", fetch_fast, 0},
#define F_ENVELOPE 3
  {"ENVELOPE", fetch_envelope, 0},
#define F_FLAGS 4
  {"FLAGS", fetch_flags, 0},
#define F_INTERNALDATE 5
  {"INTERNALDATE", fetch_internaldate, 0},
#define F_RFC822_HEADER 6
   {"RFC822.HEADER", fetch_rfc822_header, 0},
#define F_RFC822_SIZE 7
  {"RFC822.SIZE", fetch_rfc822_size, 0},
#define F_RFC822_TEXT 8
  {"RFC822.TEXT", fetch_rfc822_text, 0},
#define F_RFC822 9
  {"RFC822", fetch_rfc822, 0},
#define F_BODYSTRUCTURE 10
  {"BODYSTRUCTURE", fetch_bodystructure, 0},
#define F_BODY 11
  {"BODY", fetch_body, 0},
#define F_UID 12
  {"UID", fetch_uid, 0},
  { NULL, 0, 0}
};

int
imap4d_fetch (struct imap4d_command *command, char *arg)
{
  int rc;
  char buffer[64];

  if (! (command->states & state))
    return util_finish (command, RESP_BAD, "Wrong state");

  rc = imap4d_fetch0 (arg, 0, buffer, sizeof buffer);
  return util_finish (command, rc, buffer);
}

static struct fetch_command *
fetch_getcommand (char *cmd, struct fetch_command command_table[])
{
  size_t i, len = strlen (cmd);

  for (i = 0; command_table[i].name != 0; i++)
    {
      if (strlen (command_table[i].name) == len &&
          !strcasecmp (command_table[i].name, cmd))
        return &command_table[i];
    }
  return NULL;
}

int
imap4d_fetch0 (char *arg, int isuid, char *resp, size_t resplen)
{
  struct fetch_command *fcmd = NULL;
  int rc = RESP_NO;
  char *sp = NULL;
  char *msgset;
  size_t *set = NULL;
  int n = 0;
  int i;
  int status;

  msgset = util_getword (arg, &sp);
  if (!msgset || !sp || *sp == '\0')
    {
      snprintf (resp, resplen, "Too few args");
      return RESP_BAD;
    }

  /* Get the message numbers in set[].  */
  status = util_msgset (msgset, &set, &n, isuid);
  if (status != 0)
    {
      snprintf (resp, resplen, "Bogus number set");
      return RESP_BAD;
    }

  for (i = 0; i < n; i++)
    {
      char item[32];
      char *items = strdup (sp);
      char *p = items;
      size_t msgno;
      int space = 0;
      int uid_sent = !isuid; /* Pretend we sent the uid if via fetch.  */

      msgno = (isuid) ? uid_to_msgno (set[i]) : set[i];
      if (msgno)
	{
	  fcmd = NULL;
	  util_send ("* %d FETCH (", msgno);
	  item[0] = '\0';
	  /* Server implementations MUST implicitly
	     include the UID message data item as part of any FETCH response
	     caused by a UID command, regardless of whether a UID was specified
	     as a message data item to the FETCH. */
	  if (!uid_sent)
	    {
	      fcmd = &fetch_command_table[F_UID];
	      fcmd->msgno = msgno;
	      rc = fetch_uid (fcmd, p);
	      uid_sent = 1;
	    }
	  /* Get the fetch command names.  */
	  while (*items && *items != ')')
	    {
	      util_token (item, sizeof (item), &items);
	      if (uid_sent && strcasecmp (item, "UID") == 0)
		  continue;
	      if (fcmd)
		space = 1;
	      /* Search in the table.  */
	      fcmd = fetch_getcommand (item, fetch_command_table);
	      if (fcmd)
		{
		  if (space)
		    {
		      util_send (" ");
		      space = 0;
		    }
		  fcmd->msgno = msgno;
		  rc = fcmd->func (fcmd, items);
		}
	    }
	  util_send (")\r\n");
	}
      free (p);
    }
  free (set);
  snprintf (resp, resplen, "Completed");
  return rc;
}

/* The Fetch comand retireves data associated with a message in the
   mailbox,  The data items to be fetched can be either a single  atom
   or a parenthesized list.  */

/* Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE)
   or (FAST ENVELOPE) */
static int
fetch_all (struct fetch_command *command, char *arg)
{
  struct fetch_command c_env = fetch_command_table[F_ENVELOPE];
  fetch_fast (command, arg);
  util_send (" ");
  c_env.msgno = command->msgno;
  fetch_envelope (&c_env, arg);
  return RESP_OK;
}

/* Combination of (ALL BODY).  */
static int
fetch_full (struct fetch_command *command, char *arg)
{
  struct fetch_command c_body = fetch_command_table[F_BODY];
  fetch_all (command, arg);
  util_send (" ");
  c_body.msgno = command->msgno;
  fetch_body (&c_body, arg);
  return RESP_OK;
}

/* Combination of (FLAGS INTERNALDATE RFC822.SIZE).  */
static int
fetch_fast (struct fetch_command *command, char *arg)
{
  struct fetch_command c_idate = fetch_command_table[F_INTERNALDATE];
  struct fetch_command c_rfc = fetch_command_table[F_RFC822_SIZE];
  struct fetch_command c_flags = fetch_command_table[F_FLAGS];
  c_flags.msgno = command->msgno;
  fetch_flags (&c_flags, arg);
  util_send (" ");
  c_idate.msgno = command->msgno;
  fetch_internaldate (&c_idate, arg);
  util_send (" ");
  c_rfc.msgno = command->msgno;
  fetch_rfc822_size (&c_rfc, arg);
  return RESP_OK;
}

/* Header: Date, Subject, From, Sender, Reply-To, To, Cc, Bcc, In-Reply-To,
   and Message-Id.  */
static int
fetch_envelope (struct fetch_command *command, char *arg)
{
  message_t msg = NULL;
  int status;
  mailbox_get_message (mbox, command->msgno, &msg);
  util_send ("%s (", command->name);
  status = fetch_envelope0 (msg);
  util_send (")");
  return status;
}

/* FIXME: - strings change to literals when detecting '"'  */
static int
fetch_envelope0 (message_t msg)
{
  char *buffer = NULL;
  char *from = NULL;
  header_t header = NULL;

  message_get_header (msg, &header);

  /* FIXME: Incorrect Date.  */
  header_aget_value (header, "Date", &buffer);
  if (*buffer == '\0')
    util_send ("NIL");
  else
    util_send ("\"%s\"", buffer);
  free (buffer);
  util_send (" ");

  /* Subject.  */
  header_aget_value (header, "Subject", &buffer);
  if (*buffer == '\0')
    util_send ("NIL");
  else
    util_send ("\"%s\"", buffer);
  free (buffer);
  util_send (" ");

  /* From.  */
  header_aget_value (header, "From", &from);
  fetch_send_address (from);
  util_send (" ");

  /* Note that the server MUST default the reply-to and sender fields from
     the From field; a client is not expected to know to do this. */
  header_aget_value (header, "Sender", &buffer);
  fetch_send_address ((*buffer == '\0') ? from : buffer);
  free (buffer);
  util_send (" ");

  header_aget_value (header, "Reply-to", &buffer);
  fetch_send_address ((*buffer == '\0') ? from : buffer);
  free (buffer);
  util_send (" ");

  header_aget_value (header, "To", &buffer);
  fetch_send_address (buffer);
  free (buffer);
  util_send (" ");

  header_aget_value (header, "Cc", &buffer);
  fetch_send_address (buffer);
  free (buffer);
  util_send (" ");

  header_aget_value (header, "Bcc", &buffer);
  fetch_send_address (buffer);
  free (buffer);
  util_send (" ");

  header_aget_value (header, "In-Reply-To", &buffer);
  fetch_send_address (buffer);
  free (buffer);
  util_send (" ");

  header_aget_value (header, "Message-ID", &buffer);
  if (*buffer == '\0')
    util_send ("NIL");
  else
    util_send ("\"%s\"", buffer);

  free (buffer);
  free (from);
  return RESP_OK;
}

/* The flags that are set for this message.  */
/* FIXME: User flags not done.  */
static int
fetch_flags (struct fetch_command *command, char *arg)
{
  attribute_t attr = NULL;
  message_t msg = NULL;
  (void)arg;
  mailbox_get_message (mbox, command->msgno, &msg);
  message_get_attribute (msg, &attr);
  util_send ("%s (", command->name);
  if (attribute_is_deleted (attr))
    util_send (" \\Deleted");
  if (attribute_is_answered (attr))
    util_send (" \\Answered");
  if (attribute_is_flagged (attr))
    util_send (" \\Flagged");
  if (attribute_is_seen (attr) && attribute_is_read (attr))
    util_send (" \\Seen");
  if (attribute_is_draft (attr))
    util_send (" \\Draft");
  util_send (" )");
  return RESP_OK;
}

/* The internal date of the message.  */
/* FIXME: Wrong format?  */
static int
fetch_internaldate (struct fetch_command *command, char *arg)
{
  char date[512];
  envelope_t env = NULL;
  message_t msg = NULL;
  mailbox_get_message (mbox, command->msgno, &msg);
  message_get_envelope (msg, &env);
  date[0] = '\0';
  envelope_date (env, date, sizeof (date), NULL);
  util_send ("%s", command->name);
  if (date[strlen (date) - 1] == '\n')
    date[strlen (date) - 1] = '\0';
  util_send (" \"%s\"", date);
  return RESP_OK;
}

/* Equivalent to BODY.PEEK[HEADER].  */
static int
fetch_rfc822_header (struct fetch_command *command, char *arg)
{
  char buffer[16];
  (void)arg;
  util_send ("%s ", command->name);
  strcpy (buffer, "[HEADER]");
  fetch_operation (command->msgno, buffer, 1);
  return RESP_OK;
}

/* Equivalent to BODY[TEXT].  */
static int
fetch_rfc822_text (struct fetch_command *command, char *arg)
{
  char buffer[16];
  attribute_t attr = NULL;
  message_t msg = NULL;
  (void)arg;
  mailbox_get_message (mbox, command->msgno, &msg);
  message_get_attribute (msg, &attr);
  if (!attribute_is_seen (attr) && !attribute_is_read (attr))
    {
      util_send ("FLAGS (\\Seen) ");
      attribute_set_seen (attr);
      attribute_set_read (attr);
    }
  util_send ("%s ", command->name);
  strcpy (buffer, "[TEXT]");
  fetch_operation (command->msgno, buffer, 1);
  return RESP_OK;
}

/* The [RFC-822] size of the message.  */
static int
fetch_rfc822_size (struct fetch_command *command, char *arg)
{
  size_t size = 0;
  size_t lines = 0;
  message_t msg = NULL;
  (void)arg;
  mailbox_get_message (mbox, command->msgno, &msg);
  message_size (msg, &size);
  message_lines (msg, &lines);
  util_send ("%s %u", command->name, size + lines);
  return RESP_OK;
}

/* Equivalent to BODY[].  */
static int
fetch_rfc822 (struct fetch_command *command, char *arg)
{
  if (*arg == '.')
    {
      if (strncasecmp (arg, ".SIZE", 5) == 0)
	{
	  struct fetch_command c_rfc= fetch_command_table[F_RFC822_SIZE];
	  c_rfc.msgno = command->msgno;
	  fetch_rfc822_size (&c_rfc, arg);
	}
      else if (strncasecmp (arg, ".TEXT", 5) == 0)
	{
	  struct fetch_command c_rfc = fetch_command_table[F_RFC822_TEXT];
	  c_rfc.msgno = command->msgno;
	  fetch_rfc822_text (&c_rfc, arg);
	}
      else if (strncasecmp (arg, ".HEADER", 7) == 0)
	{
	  struct fetch_command c_rfc = fetch_command_table[F_RFC822_HEADER];
	  c_rfc.msgno = command->msgno;
	  fetch_rfc822_header (&c_rfc, arg);
	}
    }
  else
    {
      char buffer[16];
      attribute_t attr = NULL;
      message_t msg = NULL;
      mailbox_get_message (mbox, command->msgno, &msg);
      message_get_attribute (msg, &attr);
      if (!attribute_is_seen (attr) && !attribute_is_read (attr))
	{
	  util_send ("FLAGS (\\Seen) ");
	  attribute_set_seen (attr);
	  attribute_set_read (attr);
	}
      util_send ("%s ", command->name);
      strcpy (buffer, "[]");
      fetch_operation (command->msgno, buffer, 1);
    }
  return RESP_OK;
}

/* The unique identifier for the message.  */
static int
fetch_uid (struct fetch_command *command, char *arg)
{
  size_t uid = 0;
  message_t msg = NULL;
  (void)arg;
  mailbox_get_message (mbox, command->msgno, &msg);
  message_get_uid (msg, &uid);
  util_send ("%s %d", command->name, uid);
  return RESP_OK;
}

static int
fetch_bodystructure (struct fetch_command *command, char *arg)
{
  message_t message = NULL;
  (void)arg;
  util_send ("%s (", command->name);
  mailbox_get_message (mbox, command->msgno, &message);
  fetch_bodystructure0 (message, 1);
  util_send (")");
  return RESP_OK;
}

static int
fetch_bodystructure0 (message_t message, int extension)
{
  size_t nparts = 1;
  size_t i;
  int is_multipart = 0;
  message_is_multipart (message, &is_multipart);
  if (is_multipart)
    {
      message_get_num_parts (message, &nparts);
      for (i = 1; i <= nparts; i++)
	{
	  message_t msg = NULL;
	  message_get_part (message, i, &msg);
	  util_send ("(");
	  fetch_bodystructure0 (msg, extension);
	  util_send (")");
	} /* for () */
      /* The extension data for multipart. */
      if (extension)
	{
	  header_t header = NULL;
	  char *buffer = NULL;
	  char *sp = NULL;
	  char *s;
	      /* The subtype.  */
	  message_get_header (message, &header);
	  header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer);
	  s = strtok_r (buffer, " \t\r\n;", &sp);
	  s = strchr (buffer, '/');
	  if (s)
	    {
	      s++;
	      util_send (" \"%s\"", s);
	    }
	  else
	    util_send (" NIL");
	  /* Content-type parameter list. */
	  util_send (" (");
	  {
	    while ((s = strtok_r (NULL, " \t\r\n;", &sp)))
	      {
		char *p = strchr (s, '=');
		if (p)
		  *p++ = '\0';
		util_send ("\"%s\"", s);
		util_send (" \"%s\"", (p) ? p : "NIL");
	      }
	  }
	  free (buffer);
	  /* Content-Disposition.  */
	  header_aget_value (header, MU_HEADER_CONTENT_DISPOSITION, &buffer);
	  if (*buffer)
	    {
	      util_send (" (");
	      while ((s = strtok_r (buffer, " \t\r\n;", &sp)))
		{
		  char *p = strchr (s, '=');
		  if (p)
		    *p++ = '\0';
		  util_send ("\"%s\"", s);
		  util_send (" \"%s\"", (p) ? p : "NIL");
		}
	    }
	  else
	    util_send (" NIL");
	  free (buffer);
	  /* Content-Language.  */
	  header_aget_value (header, MU_HEADER_CONTENT_LANGUAGE, &buffer);
	  if (*buffer)
	    util_send (" \"%s\"", buffer);
	  else
	    util_send (" NIL");
	} /* extension */
    }
  else
    bodystructure (message, extension);
  return RESP_OK;
}

static int
bodystructure (message_t msg, int extension)
{
  header_t header = NULL;
  char *sp = NULL;
  char *buffer = NULL;
  char *s;
  size_t blines = 0;
  int message_rfc822 = 0;
  int text_plain = 0;

  message_get_header (msg, &header);

  /* MIME:  */
  header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer);
  s = strtok_r (buffer, " \t\r\n;", &sp);
  /* MIME media type and subtype  */
  if (s)
    {
      char *p = strchr (s, '/');
      if (strcasecmp (s, "MESSAGE/RFC822") == 0)
	message_rfc822 = 1;
      if (strcasecmp (s, "TEXT/PLAIN") == 0)
	text_plain = 1;
      if (p)
	*p++ = '\0';
      util_send ("\"%s\"", s);
      util_send (" \"%s\"", (p) ? p : "NIL");
    }
  else
    {
      /* Default?  */
      util_send ("TEXT");
      util_send (" PLAIN");
    }
  /* Content-type parameter list. */
  util_send (" (");
    {
      int have_charset = 0;
      while ((s = strtok_r (NULL, " \t\r\n;", &sp)))
	{
	  char *p = strchr (s, '=');
	  if (p)
	    *p++ = '\0';
	  util_send ("\"%s\"", s);
	  util_send (" \"%s\"", (p) ? p : "NIL");
	  if (strcasecmp (s, "charset") == 0)
	    have_charset = 1;
	}
      /* Default.  */
      if (!have_charset)
	{
	  util_send ("\"CHARSET\"");
	  util_send (" \"US-ASCII\"");
	}
    }
  util_send (")");
  free (buffer);

  /* Content-ID. */
  header_aget_value (header, MU_HEADER_CONTENT_ID, &buffer);
  if (*buffer)
    util_send (" \"%s\"", buffer);
  else
    util_send (" NIL");
  free (buffer);

  /* Content-Description. */
  header_aget_value (header, MU_HEADER_CONTENT_DESCRIPTION, &buffer);
  if (*buffer)
    util_send (" \"%s\"", buffer);
  else
    util_send (" NIL");
  free (buffer);

  /* Content-Transfer-Encoding. */
  header_aget_value (header, MU_HEADER_CONTENT_TRANSFER_ENCODING, &buffer);
  util_send (" \"%s\"", (*buffer) ? buffer : "7bit");
  free (buffer);

  /* Body size RFC822 format.  */
  {
    size_t size = 0;
    body_t body = NULL;
    message_get_body (msg, &body);
    body_size (body, &size);
    body_lines (body, &blines);
    util_send (" %d", size + blines);
  }

  /* If the mime type was text.  */
  if (text_plain)
    {
      /* Add the line number of the body.  */
      util_send (" %d", blines);
    }
  else if (message_rfc822)
    {
      size_t lines = 0;
      /* Add envelope structure */
      util_send ("(");
      fetch_envelope0 (msg);
      util_send (")");
      /* Add body structure */
      util_send ("(");
      bodystructure (msg, 1);
      util_send (")");
      /* size in text lines of the encapsulated message.  */
      message_lines (msg, &lines);
      util_send (" %d", lines);
    }

  if (extension)
    {
      /* Content-MD5.  */
      header_aget_value (header, MU_HEADER_CONTENT_MD5, &buffer);
      if (*buffer)
	util_send (" \"%s\"", buffer);
      else
	util_send (" NIL");
      free (buffer);
      /* Content-Disposition.  */
      header_aget_value (header, MU_HEADER_CONTENT_DISPOSITION, &buffer);
      if (*buffer)
	{
	  util_send (" (");
	  while ((s = strtok_r (buffer, " \t\r\n;", &sp)))
	    {
	      char *p = strchr (s, '=');
	      if (p)
		*p++ = '\0';
	      util_send ("\"%s\"", s);
	      util_send (" \"%s\"", (p) ? p : "NIL");
	    }
	}
      else
	util_send (" NIL");
      free (buffer);
      /* Content-Language.  */
      header_aget_value (header, MU_HEADER_CONTENT_LANGUAGE, &buffer);
      if (*buffer)
	util_send (" \"%s\"", buffer);
      else
	util_send (" NIL");
      free (buffer);
    }
  return RESP_OK;
}

/* An alternate form of BODY that does not implicitly set the \Seen flag.  */
static int
fetch_body (struct fetch_command *command, char *arg)
{
  /* It's BODY set the message as seen  */
  if (*arg == '[')
    {
      message_t msg = NULL;
      attribute_t attr = NULL;
      mailbox_get_message (mbox, command->msgno, &msg);
      message_get_attribute (msg, &attr);
      if (!attribute_is_seen (attr) && !attribute_is_read (attr))
	{
	  util_send ("FLAGS (\\Seen) ");
	  attribute_set_seen (attr);
	  attribute_set_read (attr);
	}
    }
  else if (strncasecmp (arg,".PEEK", 5) == 0)
    {
      /* Move pass the .peek  */
      arg += strlen (".PEEK");
      while (isspace ((unsigned)*arg))
	arg++;
    }
  else if (*arg != '[' && *arg != '.')
    {
      message_t message = NULL;
      mailbox_get_message (mbox, command->msgno, &message);
      /* Call body structure without the extension.  */
      util_send ("%s (", command->name);
      fetch_bodystructure0 (message, 0);
      util_send (")");
      return RESP_OK;
    }
  util_send ("%s", command->name);
  return fetch_operation (command->msgno, arg, 0);
}

static int
fetch_operation (size_t msgno, char *arg, int silent)
{
  unsigned long start = ULONG_MAX;
  unsigned long end = ULONG_MAX; /* No limit. */
  message_t msg = NULL;
  char *partial = strchr (arg, '<');

  /* Check for section specific offset.  */
  if (partial)
    {
      /* FIXME: This should be move in imap4d_fetch() and have a more
	 draconian check.  */
      *partial = '\0';
      partial++;
      start = strtoul (partial, &partial, 10);
      if (*partial == '.')
	{
	  partial++;
	  end = strtoul (partial, NULL, 10);
	}
    }

  mailbox_get_message (mbox, msgno, &msg);

  /* Retreive the sub message.  */
  for (arg++; isdigit ((unsigned)*arg) || *arg == '.'; arg++)
    {
      unsigned long j = strtoul (arg, &arg, 10);
      int status;
      /* Technicaly I should check errno too.  */
      if (j == 0 || j == ULONG_MAX)
	break;
      status = message_get_part (msg, j, &msg);
      if (status != 0)
	{
	  util_send ("\"\"");
	  return RESP_OK;
	}
    }

  /* Choose the right fetch attribute.  */
  if (*arg == ']')
    {
      if (!silent)
	util_send ("[]");
      fetch_message (msg, start, end);
    }
  else if (strncasecmp (arg, "HEADER]", 7) == 0
	   || strncasecmp (arg, "MIME]", 5) == 0)
    {
      if (!silent)
	{
	  util_upper (arg);
	  util_send ("[%s", arg);
	}
      fetch_header (msg, start, end);
    }
  else if (strncasecmp (arg, "TEXT]", 5) == 0)
    {
      if (!silent)
	util_send ("[TEXT]");
      return fetch_content (msg, start, end);
    }
  else if (strncasecmp (arg, "HEADER.FIELDS.NOT", 17) == 0)
    {
      arg += 17;
      fetch_header_fields_not (msg, arg, start, end);
    }
  else if (strncasecmp (arg, "HEADER.FIELDS", 13) == 0)
    {
      arg += 13;
      fetch_header_fields (msg, arg, start, end);
    }
  else
    util_send ("\"\"");
  return RESP_OK;
}

static int
fetch_message (message_t msg, unsigned long start, unsigned long end)
{
  stream_t stream = NULL;
  size_t size = 0, lines = 0;
  message_get_stream (msg, &stream);
  message_size (msg, &size);
  message_lines (msg, &lines);
  if ((size + lines) < end || end == ULONG_MAX)
    end = size + lines;
  return fetch_io (stream, start, end);
}

static int
fetch_header (message_t msg, unsigned long start, unsigned long end)
{
  header_t header = NULL;
  stream_t stream = NULL;
  size_t size = 0, lines = 0;
  message_get_header (msg, &header);
  header_size (header, &size);
  header_lines (header, &lines);
  if ((size + lines) < end || end == ULONG_MAX)
    end = size + lines;
  header_get_stream (header, &stream);
  return fetch_io (stream, start, end);
}

static int
fetch_content (message_t msg, unsigned long start, unsigned long end)
{
  body_t body = NULL;
  stream_t stream = NULL;
  size_t size = 0, lines = 0;
  message_get_body (msg, &body);
  body_size (body, &size);
  body_lines (body, &lines);
  if ((size + lines) < end || end == ULONG_MAX)
    end = size + lines;
  body_get_stream (body, &stream);
  return fetch_io (stream, start, end);
}

static int
fetch_io (stream_t stream, unsigned long start, unsigned long end)
{
  stream_t rfc = NULL;
  char *buffer, *p;
  size_t n = 0;
  size_t total = 0;
  off_t offset;

  offset = (start == ULONG_MAX) ? 0 : start;

  filter_create (&rfc, stream, "rfc822", MU_FILTER_ENCODE, MU_STREAM_READ);

  p = buffer = calloc (end + 2, 1);
  while (end > 0 && stream_read (rfc, buffer, end + 1, offset, &n) == 0 && n > 0)
    {
      offset += n;
      total += n;
      end -= n;
      buffer += n;
    }
  /* Make sure we null terminate.  */
  *buffer = '\0';

  if (start != ULONG_MAX)
    util_send ("<%lu>", start);

  if (total)
    {
      util_send (" {%u}\r\n", total);
      util_send ("%s", p);
    }
  else
    util_send (" \"\"");
  free (p);
  return RESP_OK;
}

static int
fetch_header_fields (message_t msg, char *arg, unsigned long start,
		     unsigned long end)
{
  char *buffer = NULL;
  char **array = NULL;
  size_t array_len = 0;
  size_t off = 0;
  size_t lines = 0;
  stream_t stream = NULL;
  int status;

  status = memory_stream_create (&stream);
  if (status != 0)
    imap4d_bye (ERR_NO_MEM);

  /* Save the fields in an array.  */
  {
    char *field;
    char *sp = NULL;
    for (;(field = strtok_r (arg, " ()]\r\n", &sp));
	 arg = NULL, array_len++)
      {
	array = realloc (array, (array_len + 1) * sizeof (*array));
	if (!array)
	  imap4d_bye (ERR_NO_MEM);
	array[array_len] = field;
      }
  }

  /* Get the header values.  */
  {
    size_t j;
    header_t header = NULL;
    message_get_header (msg, &header);
    for (j = 0; j < array_len; j++)
      {
	char *value;
	size_t n = 0;
	header_aget_value (header, array[j], &value);
	if (*value == '\0')
	  continue;
	n = asprintf (&buffer, "%s: %s\n", array[j], value);
	status = stream_write (stream, buffer, n, off, &n);
	off += n;
	lines++;
	free (value);
	free (buffer);
	buffer = NULL;
	if (status != 0)
	  {
	    free (array);
	    imap4d_bye (ERR_NO_MEM);
	  }
      }
  }
  /* Headers are always sent with the NL separator.  */
  stream_write (stream, "\n", 1, off, NULL);
  off++;
  lines++;

  /* Send the command back.  */
  util_send ("[HEADER.FIELDS");
  {
    size_t j;
    for (j = 0; j < array_len; j++)
      {
	util_upper (array[j]);
	util_send (" \"%s\"", array[j]);
      }
    util_send ("]");
  }

  if ((off + lines) < end || end == ULONG_MAX)
    end = off + lines;
  fetch_io (stream, start, end);
  if (array)
    free (array);
  return RESP_OK;
}

static int
fetch_header_fields_not (message_t msg, char *arg, unsigned long start,
			 unsigned long end)
{
  char **array = NULL;
  size_t array_len = 0;
  char *buffer = NULL;
  size_t off = 0;
  size_t lines = 0;
  stream_t stream = NULL;
  int status;

  status = memory_stream_create (&stream);
  if (status)
    imap4d_bye (ERR_NO_MEM);

  /* Save the field we want to ignore.  */
  {
    char *field;
    char *sp = NULL;
    for (;(field = strtok_r (arg, " ()]\r\n", &sp));
	 arg = NULL, array_len++)
      {
	array = realloc (array, (array_len + 1) * sizeof (*array));
	if (!array)
	  imap4d_bye (ERR_NO_MEM);
	array[array_len] = field;
      }
  }

  /* Build the memory buffer.  */
  {
    size_t i;
    header_t header = NULL;
    size_t count = 0;
    message_get_header (msg, &header);
    header_get_field_count (header, &count);
    for (i = 1; i <= count; i++)
      {
	char *name;
	char *value ;
	size_t n = 0;
	size_t ignore = 0;

	/* Get the field name.  */
	status = header_aget_field_name (header, i, &name);
	if (status != 0 || *name == '\0')
	  continue;

	/* Should we ignore the field?  */
	{
	  size_t j;
	  for (j = 0; j < array_len; j++)
	    {
	      if (strcasecmp (array[j], name) == 0)
		{
		  ignore = 1;
		  break;
		}
	    }
	  if (ignore)
	    {
	      free (name);
	      continue;
	    }
	}

	header_aget_field_value (header, i, &value);
	/* Save the field.  */
	n = asprintf (&buffer, "%s: %s\n", name, value);
	status = stream_write (stream, buffer, n, off, &n);
        off += n;
	lines++;
        free (value);
        free (name);
        free (buffer);
        buffer = NULL;
        if (status != 0)
	  {
	    free (array);
	    imap4d_bye (ERR_NO_MEM);
	  }
      }
  }
  /* Headers are always sent with a NL separator.  */
  stream_write (stream, "\n", 1, off, NULL);
  off++;
  lines++;

  util_send ("[HEADER.FIELDS.NOT");
  {
    size_t j;
    for (j = 0; j < array_len; j++)
      {
	util_upper (array[j]);
	util_send (" \"%s\"", array[j]);
      }
    util_send ("]");
  }

  if ((off + lines) < end || end == ULONG_MAX)
    end = off + lines;
  fetch_io (stream, start, end);
  if (array)
    free (array);
  return RESP_OK;
}

/* FIXME: The address is limit by a buffer of 128, no good.  We should
   allocate the buffer.  */
static int
fetch_send_address (char *addr)
{
  address_t address;
  size_t i, count = 0;

  if (*addr == '\0')
    {
      util_send ("NIL");
      return RESP_OK;
    }

  address_create (&address, addr);
  address_get_count (address, &count);

  util_send ("(", count);
  for (i = 1; i <= count; i++)
    {
      char buf[128];

      util_send ("(");

      *buf = '\0';
      address_get_personal (address, i, buf, sizeof (buf), NULL);
      if (*buf == '\0')
	util_send ("NIL");
      else
	util_send ("\"%s\"", buf);

      util_send (" ");

      *buf = '\0';
      address_get_route (address, i, buf, sizeof (buf), NULL);
      if (*buf == '\0')
	util_send ("NIL");
      else
	util_send ("\"%s\"", buf);

      util_send (" ");

      *buf = '\0';
      {
	int is_group = 0;

	address_is_group(address, i, &is_group);
	if(is_group)
	  address_get_personal (address, i, buf, sizeof (buf), NULL);
	else
	  address_get_local_part (address, i, buf, sizeof (buf), NULL);
      }
      if (*buf == '\0')
	util_send ("NIL");
      else
	util_send ("\"%s\"", buf);

      util_send (" ");

      *buf = '\0';
      address_get_domain (address, i, buf, sizeof (buf), NULL);
      if (*buf == '\0')
	util_send ("NIL");
      else
	util_send ("\"%s\"", buf);

      util_send (")");
    }
  util_send (")");
  return RESP_OK;
}