fetch.c 38.6 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 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
/* 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_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_body              __P ((struct fetch_command *, char**));
static int fetch_uid               __P ((struct fetch_command *, char**));

/* Helper functions.  */
static int fetch_envelope0         __P ((message_t));
static int fetch_bodystructure0    __P ((message_t, int));
static int bodystructure           __P ((message_t, int));
static int send_parameter_list     __P ((char *));
static int fetch_operation         __P ((message_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_body_content      __P ((message_t, unsigned long, unsigned long));
static int fetch_io                __P ((stream_t, unsigned long, 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 **));
  message_t msg;
} 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}
};

/* Go through the fetch array sub command and returns the the structure.  */

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;
}

/* The FETCH command retrieves data associated with a message in the
   mailbox.  The data items to be fetched can be either a single atom
   or a parenthesized list.  */
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);
}

/* Where the real implementation is.  It is here since UID command also
   calls FETCH.  */
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;
      message_t msg = NULL;

      msgno = (isuid) ? uid_to_msgno (set[i]) : set[i];
      if (msgno && mailbox_get_message (mbox, msgno, &msg) == 0)
	{
	  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 (isuid)
	    {
	      fcmd = &fetch_command_table[F_UID];
	      fcmd->msg = msg;
	      rc = fetch_uid (fcmd, &items);
	    }
	  /* Get the fetch command names.  */
	  while (*items && *items != ')')
	    {
	      util_token (item, sizeof (item), &items);
	      /* Do not send the UID again.  */
	      if (isuid && 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->msg = msg;
		  rc = fcmd->func (fcmd, &items);
		}
	    }
	  util_send (")\r\n");
	}
      free (p);
    }
  free (set);
  snprintf (resp, resplen, "Completed");
  return RESP_OK;
}

/* ALL:
   Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE)
   Combination of FAST and 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.msg = command->msg;
  fetch_envelope (&c_env, arg);
  return RESP_OK;
}

/* FULL:
   Macro equivalent to: (FLAGS INTERNALDATE
   RFC822.SIZE ENVELOPE BODY).
   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.msg = command->msg;
  fetch_body (&c_body, arg);
  return RESP_OK;
}

/* FAST:
   Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE)
   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.msg = command->msg;
  fetch_flags (&c_flags, arg);
  util_send (" ");
  c_idate.msg = command->msg;
  fetch_internaldate (&c_idate, arg);
  util_send (" ");
  c_rfc.msg = command->msg;
  fetch_rfc822_size (&c_rfc, arg);
  return RESP_OK;
}

/* ENVELOPE:
   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)
{
  int status;
  (void)arg; /* No arguments.  */
  util_send ("%s (", command->name);
  status = fetch_envelope0 (command->msg);
  util_send (")");
  return status;
}

/* FLAGS: The flags that are set for this message.  */
/* FIXME: User flags not done. If enable change the PERMANENTFLAGS in SELECT */
static int
fetch_flags (struct fetch_command *command, char **arg)
{
  attribute_t attr = NULL;
  int space = 0;
  (void)arg; /* No argments.  */
  message_get_attribute (command->msg, &attr);
  util_send ("%s (", command->name);
  if (attribute_is_deleted (attr))
    {
      util_send ("\\Deleted");
      space = 1;
    }
  if (attribute_is_answered (attr))
    {
      if (space)
	util_send (" ");
      util_send ("\\Answered");
      space = 1;
    }
  if (attribute_is_flagged (attr))
    {
      if (space)
	util_send (" ");
      util_send ("\\Flagged");
      space = 1;
    }
  if (attribute_is_seen (attr) && attribute_is_read (attr))
    {
      if (space)
	util_send (" ");
      util_send ("\\Seen");
      space = 1;
    }
  if (attribute_is_draft (attr))
    {
      if (space)
	util_send (" ");
      util_send (" \\Draft");
    }
  util_send (")");
  return RESP_OK;
}

/* INTERNALDATE   The internal date of the message.
   Format:

   date_time       ::= <"> date_day_fixed "-" date_month "-" date_year
   SPACE time SPACE zone <">

   date_day        ::= 1*2digit
   ;; Day of month

   date_day_fixed  ::= (SPACE digit) / 2digit
   ;; Fixed-format version of date_day

   date_month      ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
   "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"

   date_text       ::= date_day "-" date_month "-" date_year

   date_year       ::= 4digit

   time            ::= 2digit ":" 2digit ":" 2digit
   ;; Hours minutes seconds

   zone            ::= ("+" / "-") 4digit
   ;; Signed four-digit value of hhmm representing
   ;; hours and minutes west of Greenwich (that is,
   ;; (the amount that the given time differs from
   ;; Universal Time).  Subtracting the timezone
   ;; from the given time will give the UT form.
   ;; The Universal Time zone is "+0000".  */
static int
fetch_internaldate (struct fetch_command *command, char **arg)
{
  char date[128];
  envelope_t env = NULL;
  struct tm *tptr;
  time_t env_time;

  (void)arg; /* No arguments.  */
  message_get_envelope (command->msg, &env);
  date[0] = '\0';
  envelope_date (env, date, sizeof (date), NULL);
  util_parse_ctime_date (date, &env_time);
  tptr = gmtime(&env_time);
  strftime (date, sizeof (date), "%d-%b-%Y %X", tptr);
  util_send ("%s", command->name);
  util_send (" \"%s +0000\"", date);
  return RESP_OK;
}

/*
  RFC822.HEADER:
  Functionally equivalent to BODY.PEEK[HEADER], differing in the syntax of
  the resulting untagged FETCH data (RFC822.HEADER is returned). */
static int
fetch_rfc822_header (struct fetch_command *command, char **arg)
{
  char buffer[32];
  char *p = buffer;
  (void)arg; /* No arguments.  */
  strcpy (buffer, ".PEEK[HEADER]");
  fetch_body (command, &p);
  return RESP_OK;
}

/* RFC822.TEXT:
   Functionally equivalent to BODY[TEXT], differing in the syntax of the
   resulting untagged FETCH data (RFC822.TEXT is returned). */
static int
fetch_rfc822_text (struct fetch_command *command, char **arg)
{
  char buffer[16];
  char *p = buffer;
  (void)arg; /* No arguments.  */
  strcpy (buffer, "[TEXT]");
  fetch_body (command, &p);
  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;
  (void)arg; /* No arguments.  */
  message_size (command->msg, &size);
  message_lines (command->msg, &lines);
  util_send ("%s %u", command->name, size + lines);
  return RESP_OK;
}

/* RFC822:
   Functionally equivalent to BODY[], differing in the syntax of the
   resulting untagged FETCH data (RFC822 is returned). */
static int
fetch_rfc822 (struct fetch_command *command, char **arg)
{
  if (**arg == '.')
    {
      /* We have to catch the other RFC822.XXX commands here.  This is because
	 util_token() in imap4d_fetch0 will return the RFC822 token only.  */
      if (strncasecmp (*arg, ".SIZE", 5) == 0)
	{
	  struct fetch_command c_rfc= fetch_command_table[F_RFC822_SIZE];
	  c_rfc.msg = command->msg;
	  (*arg) += 5;
	  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.msg = command->msg;
	  (*arg) += 5;
	  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.msg = command->msg;
	  (*arg) += 7;
	  fetch_rfc822_header (&c_rfc, arg);
	}
    }
  else
    {
      char buffer[16];
      char *p = buffer;
      strcpy (buffer, "[]");
      fetch_body (command, &p);
    }
  return RESP_OK;
}

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

/* BODYSTRUCTURE:
   The [MIME-IMB] body structure of the message.  This is computed by the
   server by parsing the [MIME-IMB] header fields in the [RFC-822] header and
   [MIME-IMB] headers.  */
static int
fetch_bodystructure (struct fetch_command *command, char **arg)
{
  (void)arg; /* No arguments.  */
  util_send ("%s (", command->name);
  fetch_bodystructure0 (command->msg, 1); /* 1 means with extension data.  */
  util_send (")");
  return RESP_OK;
}

/* BODY:          Non-extensible form of BODYSTRUCTURE.
   BODY[<section>]<<partial>> :
   The text of a particular body section.  The section specification is a set
   of zero or more part specifiers delimited by periods.  A part specifier
   is either a part number or one of the following: HEADER, HEADER.FIELDS,
   HEADER.FIELDS.NOT, MIME, and TEXT.  An empty section specification refers
   to the entire message, including the header. */
static int
fetch_body (struct fetch_command *command, char **arg)
{
  /* It's body section, set the message as seen  */
  if (**arg == '[')
    {
      attribute_t attr = NULL;
      message_get_attribute (command->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) += 5;
      while (isspace ((unsigned)**arg))
	(*arg)++;
    }
  else if (**arg != '[' && **arg != '.')
    {
      /* Call body structure without the extension.  */
      util_send ("%s (", command->name);
      fetch_bodystructure0 (command->msg, 0);
      util_send (")");
      return RESP_OK;
    }
  util_send ("%s", command->name);
  return fetch_operation (command->msg, arg,
			  strcasecmp (command->name, "BODY"));
}

/* Helper Functions: Where the Beef is.  */

/* ENVELOPE:
   The envelope structure of the message.  This is computed by the server by
   parsing the [RFC-822] header into the component parts, defaulting various
   fields as necessary.  The fields are presented in the order:
   Date, Subject, From, Sender, Reply-To, To, Cc, Bcc, In-Reply-To, Message-ID.
   Any field of an envelope or address structure that is not applicable is
   presented as NIL.  Note that the server MUST default the reply-to and sender
   fields from the from field.  The date, subject, in-reply-to, and message-id
   fields are strings.  The from, sender, reply-to, to, cc, and bcc fields
   are parenthesized lists of address structures.  */
static int
fetch_envelope0 (message_t msg)
{
  char *buffer = NULL;
  char *from = NULL;
  header_t header = NULL;

  message_get_header (msg, &header);

  /* Date:  */
  header_aget_value (header, "Date", &buffer);
  util_send_qstring (buffer);
  free (buffer);
  util_send (" ");

  /* Subject:  */
  header_aget_value (header, "Subject", &buffer);
  util_send_qstring (buffer);
  free (buffer);
  util_send (" ");

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

  /* Sender:  */
  /* 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 (" ");

  /* Reply-To:  */
  /* 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, "Reply-to", &buffer);
  fetch_send_address ((*buffer == '\0') ? from : buffer);
  free (buffer);
  util_send (" ");

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

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

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

  /* In-Reply-To:  */
  header_aget_value (header, "In-Reply-To", &buffer);
  util_send_qstring (buffer);
  free (buffer);
  util_send (" ");

  /* Message-ID:  */
  header_aget_value (header, "Message-ID", &buffer);
  util_send_qstring (buffer);

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

/* The beef BODYSTRUCTURE.
   A parenthesized list that describes the [MIME-IMB] body structure of a
   message. Multiple parts are indicated by parenthesis nesting.  Instead of
   a body type as the first element of the parenthesized list there is a nested
   body.  The second element of the parenthesized list is the multipart
   subtype (mixed, digest, parallel, alternative, etc.).

   The extension data of a multipart body part are in the following order:
   body parameter parenthesized list:
   A parenthesized list of attribute/value pairs [e.g. ("foo" "bar" "baz"
   "rag") where "bar" is the value of "foo" and "rag" is the value of
   "baz"] as defined in [MIME-IMB].

   body disposition:
   A parenthesized list, consisting of a disposition type string followed by a
   parenthesized list of disposition attribute/value pairs.  The disposition
   type and attribute names will be defined in a future standards-track
   revision to [DISPOSITION].

   body language:
   A string or parenthesized list giving the body language value as defined
   in [LANGUAGE-TAGS].  */
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)
    {
      char *buffer = NULL;
      char *s;
      char *sp = NULL;
      header_t header = NULL;

      message_get_num_parts (message, &nparts);

      /* Get all the sub messages.  */
      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 () */

      message_get_header (message, &header);

      /* The subtype.  */
      header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer);
      s = strtok_r (buffer, " \t\r\n;", &sp);
      if (s)
	{
	  s = strchr (s, '/');
	  if (s)
	    *s++ = '\0';
	}
      util_send (" ");
      util_send_qstring (s);

      /* The extension data for multipart. */
      if (extension)
        {
	  while (sp && *sp && isspace ((unsigned)*sp)) sp++;
	  /* body parameter parenthesized list: Content-type parameter list. */
	  if (sp && *sp)
	    {
	      util_send (" (");
	      {
		int space = 0;
		while ((s = strtok_r (NULL, " \t\r\n;", &sp)))
		  {
		    char *p = strchr (s, '=');
		    if (p)
		      *p++ = '\0';
		    if (space)
		      util_send (" ");
		    space = 1;
		    util_send_qstring (s);
		    if (p)
		      {
			util_send (" ");
			util_unquote (&p);
			util_send_qstring (p);
		      }
		  }
	      }
	      util_send (")");
	    }
	  else
	    util_send (" NIL");
          free (buffer);

          /* body disposition: Content-Disposition.  */
          header_aget_value (header, MU_HEADER_CONTENT_DISPOSITION, &buffer);
	  util_send (" ");
	  send_parameter_list (buffer);
          free (buffer);

          /* body language: Content-Language.  */
          header_aget_value (header, MU_HEADER_CONTENT_LANGUAGE, &buffer);
          util_send (" ");
	  send_parameter_list (buffer);
          free (buffer);
        } /* extension */
      else
	free (buffer);
    }
  else
    bodystructure (message, extension);
  return RESP_OK;
}

/* The basic fields of a non-multipart body part are in the following order:
   body type:
   A string giving the content media type name as defined in [MIME-IMB].

   body subtype:
   A string giving the content subtype name as defined in [MIME-IMB].

   body parameter parenthesized list:
   A parenthesized list of attribute/value pairs [e.g. ("foo" "bar" "baz"
   "rag") where "bar" is the value of "foo" and "rag" is the value of "baz"]
   as defined in [MIME-IMB].

   body id:
   A string giving the content id as defined in [MIME-IMB].

   body description:
   A string giving the content description as defined in [MIME-IMB].

   body encoding:
   A string giving the content transfer encoding as defined in [MIME-IMB].

   body size:
   A number giving the size of the body in octets. Note that this size is the
   size in its transfer encoding and not the resulting size after any decoding.

   A body type of type TEXT contains, immediately after the basic fields, the
   size of the body in text lines.

   A body type of type MESSAGE and subtype RFC822 contains, immediately after
   the basic fields, the envelope structure, body structure, and size in text
   lines of the encapsulated message.

   The extension data of a non-multipart body part are in the following order:
   body MD5:
   A string giving the body MD5 value as defined in [MD5].

   body disposition:
   A parenthesized list with the same content and function as the body
   disposition for a multipart body part.

   body language:\
   A string or parenthesized list giving the body language value as defined
   in [LANGUAGE-TAGS].
 */
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);

  /* body type:  Content-Type
     body subtype: */
  header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer);
  s = strtok_r (buffer, " \t\r\n;", &sp);
  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';
      /* MIME media type and subtype  */
      util_send_qstring (s);
      util_send (" ");
      util_unquote (&p);
      util_send_qstring (p);
    }
  else
    {
      /* Default? If Content-Type is not present consider as text/plain.  */
      util_send_qstring ("TEXT");
      util_send (" ");
      util_send_qstring ("PLAIN");
      text_plain = 1;
    }

  while (sp != NULL && *sp && isspace ((unsigned)*sp)) sp++;
  /* body parameter parenthesized list: Content-type attributes */
  if ((sp != NULL && *sp) || text_plain)
    {
      util_send (" (");
      {
	int space = 0;
	int have_charset = 0;
	/* Content-type parameter list. */
	while ((s = strtok_r (NULL, " \t\r\n;", &sp)))
	  {
	    char *p = strchr (s, '=');
	    if (p)
	      *p++ = '\0';
	    if (space)
	      util_send (" ");
	    util_send_qstring (s);
	    util_send (" ");
	    util_unquote (&p);
	    if (strcasecmp (s, "charset") == 0)
	      have_charset = 1;
	    util_send_qstring (p);
	    space = 1;
	  }
	if (!have_charset && text_plain)
	  {
	    if (space)
	      util_send (" ");
	    util_send ("\"CHARSET\" \"US-ASCII\"");
	  }
      }
      util_send (")");
    }
  else
    util_send (" NIL");
  free (buffer);

  /* body id: Content-ID. */
  header_aget_value (header, MU_HEADER_CONTENT_ID, &buffer);
  util_send (" ");
  util_send_qstring (buffer);
  free (buffer);

  /* body description: Content-Description. */
  header_aget_value (header, MU_HEADER_CONTENT_DESCRIPTION, &buffer);
  util_send (" ");
  util_send_qstring (buffer);
  free (buffer);

  /* body encoding: Content-Transfer-Encoding. */
  header_aget_value (header, MU_HEADER_CONTENT_TRANSFER_ENCODING, &buffer);
  util_send (" ");
  util_send_qstring ((*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;
      message_t emsg = NULL;
      message_unencapsulate  (msg, &emsg, NULL);
      /* Add envelope structure of the encapsulated message.  */
      util_send (" (");
      fetch_envelope0 (emsg);
      util_send (")");
      /* Add body structure of the encapsulated message.  */
      util_send ("(");
      bodystructure (emsg, extension);
      util_send (")");
      /* Size in text lines of the encapsulated message.  */
      message_lines (emsg, &lines);
      util_send (" %d", lines);
      message_destroy (&emsg, NULL);
    }

  if (extension)
    {
      /* body MD5: Content-MD5.  */
      header_aget_value (header, MU_HEADER_CONTENT_MD5, &buffer);
      util_send (" ");
      util_send_qstring (buffer);
      free (buffer);

      /* body disposition: Content-Disposition.  */
      header_aget_value (header, MU_HEADER_CONTENT_DISPOSITION, &buffer);
      util_send (" ");
      send_parameter_list (buffer);
      free (buffer);

      /* body language: Content-Language.  */
      header_aget_value (header, MU_HEADER_CONTENT_LANGUAGE, &buffer);
      util_send (" ");
      util_send_qstring (buffer);
      free (buffer);
    }
  return RESP_OK;
}

static int
fetch_operation (message_t msg, char **arg, int silent)
{
  unsigned long start = ULONG_MAX; /* No starting offset.  */
  unsigned long end = ULONG_MAX; /* No limit. */
  char *section; /* Hold the section number string.  */
  char *partial = strchr (*arg, '<');

  /* Check for section specific offset.  */
  if (partial)
    {
      /* NOTE: should 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);
	}
    }

  /* Pass the first bracket '['  */
  (*arg)++;
  section = *arg;

  /* Retreive the section message.  */
  while (isdigit ((unsigned)**arg))
    {
      unsigned long j = strtoul (*arg, arg, 10);
      int status;

      /* Wrong section message number bail out.  */
      if (j == 0 || j == ULONG_MAX) /* Technical: I should check errno too.  */
	break;

      /* If the section message did not exist bail out here.  */
      status = message_get_part (msg, j, &msg);
      if (status != 0)
	{
	  util_send (" \"\"");
	  return RESP_OK;
	}
      if (**arg == '.')
	(*arg)++;
      else
	break;
    }

  /* Did we have a section message?  */
  if (((*arg) - section) > 0)
    {
      char *p = section;
      section = calloc ((*arg) - p + 1, 1);
      if (section)
	memcpy (section, p, (*arg) - p);
    }
  else
    section = calloc (1, 1);

  /* Choose the right fetch attribute.  */
  if (*section == '\0' && **arg == ']')
    {
      if (!silent)
	util_send ("[]");
      (*arg)++;
      fetch_message (msg, start, end);
    }
  else if (strncasecmp (*arg, "HEADER]", 7) == 0)
    {
      if (!silent)
	{
	  /* NOTE: We violate the RFC here: Header can not take a prefix for
	     section messages it only referes to the RFC822 header .. ok
	     see it as an extension. But according to IMAP4 we should
	     have send an empty string: util_send (" \"\"");
	  */
	  if (*section)
	    util_send ("[%sHEADER]", section);
	  else
	    util_send ("[%s", *arg);
	}
      (*arg) += 7;
      fetch_header (msg, start, end);
    }
  else if (strncasecmp (*arg, "MIME]", 5) == 0)
    {
      if (!silent)
	{
	  if (*section)
	    util_send ("[%sMIME]", section);
	  else
	    util_send ("[%s", *arg);
	}
      (*arg) += 5;
      fetch_header (msg, start, end);
    }
  else if (strncasecmp (*arg, "HEADER.FIELDS.NOT", 17) == 0)
    {
      /* NOTE: we should flag an error if section is not empty: accept
	 as an extension for now.  */
      if (*section)
	util_send ("[%s", section);
      else
	util_send ("[");
      (*arg) += 17;
      fetch_header_fields_not (msg, arg, start, end);
    }
  else if (strncasecmp (*arg, "HEADER.FIELDS", 13) == 0)
    {
      /* NOTE: we should flag an error if section is not empty: accept
	 as an extension for now.  */
      if (*section)
	util_send ("[%s", section);
      else
	util_send ("[");
      (*arg) += 13;
      fetch_header_fields (msg, arg, start, end);
    }
  /* Last case check also for section to catch "[1.2.3]" submessages.  */
  else if (strncasecmp (*arg, "TEXT]", 5) == 0 || section)
    {
      if (!silent)
	{
	  if (*section)
	    util_send ("[%s]", section);
	  else
	    util_send ("[TEXT]");
	}
      if (*section)
	(*arg)++;
      else
	(*arg) += 5;
      fetch_body_content (msg, start, end);
    }
  else
    util_send (" \"\"");
  free (section);
  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);
  return fetch_io (stream, start, end, size + lines);
}

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);
  header_get_stream (header, &stream);
  return fetch_io (stream, start, end, size + lines);
}

static int
fetch_body_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);
  body_get_stream (body, &stream);
  return fetch_io (stream, start, end, size + lines);
}

static int
fetch_io (stream_t stream, unsigned long start, unsigned long end,
	  unsigned long max)
{
  stream_t rfc = NULL;
  size_t n = 0;
  off_t offset;

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

  if (start == ULONG_MAX || end == ULONG_MAX)
    {
      char buffer[512];
      offset = 0;
      if (max)
	{
	  util_send (" {%u}\r\n", max);
	  while (stream_read (rfc, buffer, sizeof (buffer) - 1, offset,
			      &n) == 0 && n > 0)
	    {
	      buffer[n] = '\0';
	      util_send ("%s", buffer);
	      offset += n;
	    }
	}
      else
	util_send (" \"\"");
    }
  else
    {
      char *buffer, *p;
      size_t total = 0;
      offset = start;
      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';
      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;
    char *f = *arg;
    /* Find the end of the header.field tag.  */
    field = strchr (f, ']');
    if (field)
      {
	*field++ = '\0';
	*arg = field;
      }
    else
      *arg += strlen (*arg);

    for (;(field = util_getitem (f, " ()]\r\n", &sp)); f = 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 = NULL;
	size_t n = 0;
	header_aget_value (header, array[j], &value);
	if (*value == '\0')
	  {
	    free (value);
	    continue;
	  }
	n = asprintf (&buffer, "%s: %s\n", array[j], value);
	status = stream_write (stream, buffer, n, off, &n);
	off += n;
	/* count the lines.  */
	{
	  char *nl = buffer;
	  for (;(nl = strchr (nl, '\n')); nl++)
	    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. The first braket was already sent.  */
  util_send ("HEADER.FIELDS");
  {
    size_t j;
    util_send (" (");
    for (j = 0; j < array_len; j++)
      {
	util_upper (array[j]);
	if (j)
	  util_send (" ");
	util_send_qstring (array[j]);
      }
    util_send (")");
    util_send ("]");
  }

  fetch_io (stream, start, end, off + lines);
  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;
    char *f = *arg;
    /* Find the end of the header.field.no tag.  */
    field = strchr (f, ']');
    if (field)
      {
	*field++ = '\0';
	*arg = field;
      }
    else
      *arg += strlen (*arg);

    for (;(field = strtok_r (f, " ()\r\n", &sp)); f = 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 = NULL;
	char *value ;
	size_t n = 0;
	size_t ignore = 0;

	/* Get the field name.  */
	status = header_aget_field_name (header, i, &name);
	if (*name == '\0')
	  {
	    free (name);
	    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;
	/* count the lines.  */
	{
	  char *nl = buffer;
	  for (;(nl = strchr (nl, '\n')); nl++)
	    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;
    util_send (" (");
    for (j = 0; j < array_len; j++)
      {
	util_upper (array[j]);
	if (j)
	  util_send (" ");
	util_send_qstring (array[j]);
      }
    util_send (")");
    util_send ("]");
  }

  fetch_io (stream, start, end, off + lines);
  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;

  /* Short circuit.  */
  if (addr == NULL || *addr == '\0')
    {
      util_send ("NIL");
      return RESP_OK;
    }

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

  /* We failed: can't parse.  */
  if (count == 0)
    {
      util_send ("NIL");
      return RESP_OK;
    }

  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);
      util_send_qstring (buf);

      util_send (" ");

      *buf = '\0';
      address_get_route (address, i, buf, sizeof (buf), NULL);
      util_send_qstring (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);
      }
      util_send_qstring (buf);

      util_send (" ");

      *buf = '\0';
      address_get_domain (address, i, buf, sizeof (buf), NULL);
      util_send_qstring (buf);

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

/* Send parameter list for the bodystructure.  */
static int
send_parameter_list (char *buffer)
{
  while (*buffer && isspace ((unsigned)*buffer)) buffer++;
  if (*buffer)
    {
      char *sp = NULL;
      char *s;
      util_send ("(");
      s = strtok_r (buffer, " \t\r\n;", &sp);
      util_send_qstring (s);
      while (sp && *sp && isspace ((unsigned)*sp)) sp++;
      if (sp && *sp)
	{
	  int space = 0;
	  util_send (" (");
	  while ((s = strtok_r (NULL, " \t\r\n;", &sp)))
	    {
	      char *p = strchr (s, '=');
	      if (p)
		*p++ = '\0';
	      if (space)
		util_send (" ");
	      space = 1;
	      util_send_qstring (s);
	      if (p)
		{
		  util_send (" ");
		  util_unquote (&p);
		  util_send_qstring (p);
		}
	    }
	  util_send (")");
	}
      else
	util_send (" NIL");
      util_send (")");
    }
  else
    util_send ("NIL");
  return 0;
}