fetch.c 12 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
/* 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"

/* This will suck, too.
   Alain: Yest 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 ">"]
*/

static int fetch_all (struct imap4d_command *, char*);
static int fetch_full (struct imap4d_command *, char*);
static int fetch_fast (struct imap4d_command *, char*);
static int fetch_envelope (struct imap4d_command *, char*);
static int fetch_flags (struct imap4d_command *, char*);
static int fetch_internaldate (struct imap4d_command *, char*);
static int fetch_rfc822_header (struct imap4d_command *, char*);
static int fetch_rfc822_size (struct imap4d_command *, char*);
static int fetch_rfc822_text (struct imap4d_command *, char*);
static int fetch_rfc822 (struct imap4d_command *, char*);
static int fetch_bodystructure (struct imap4d_command *, char*);
static int fetch_body_peek (struct imap4d_command *, char*);
static int fetch_body (struct imap4d_command *, char*);
static int fetch_uid (struct imap4d_command *, char*);

struct imap4d_command fetch_command_table [] =
{
#define F_ALL 0
  {"ALL", fetch_all},
#define F_FULL 1
  {"FULL", fetch_full},
#define F_FAST 2
  {"FAST", fetch_fast},
#define F_ENVELOPE 3
  {"ENVELOPE", fetch_envelope},
#define F_FLAGS 4
  {"FLAGS", fetch_flags},
#define F_INTERNALDATE 5
  {"INTERNALDATE", fetch_internaldate},
#define F_RFC822_HEADER 6
  {"RFC822.HEADER", fetch_rfc822_header},
#define F_RFC822_SIZE 7
  {"RFC822.SIZE", fetch_rfc822_size},
#define F_RFC822_TEXT 8
  {"RFC822.TEXT", fetch_rfc822_text},
#define F_RFC822 9
  {"RFC822", fetch_rfc822},
#define F_BODYSTRUCTURE 10
  {"BODYSTRUCTURE", fetch_bodystructure},
#define F_BODY_PEEK 12
  {"BODY.PEEK", fetch_body_peek},
#define F_BODY 13
  {"BODY", fetch_body},
#define F_UID 14
  {"UID", fetch_uid},
  { 0, 0},
};

int
imap4d_fetch (struct imap4d_command *command, char *arg)
{
  char *sp = NULL;
  char *msgset;
  int *set = NULL;
  char *item;
  int i, n = 0;
  int rc = RESP_OK;
  int status;
  const char *errmsg = "Completed";
  struct imap4d_command *fcmd;

  msgset = util_getword (arg, &sp);
  if (!msgset)
    return util_finish (command, RESP_BAD, "Too few args");

  item = strchr (sp, '[');
  if (item)
    {
      char *s = alloca (item - sp + 1);
      memcpy (s, sp, item - sp);
      s[item - sp] = '\0';
      arg = item;
      item = s;
    }
  else
      arg = item = sp;

  fcmd = util_getcommand (item, fetch_command_table);
  if (!fcmd)
    return util_finish (command, RESP_BAD, "Command unknown");

  status = util_msgset (msgset, &set, &n, 0);
  if (status != 0)
    return util_finish (command, RESP_BAD, "Bogus number set");

  /* We use the states to hold the msgno/uid, the success to say if we're
     The first.  */
  for (i = 0; i < n; i++)
    {
      fcmd->states = set[i];
      fcmd->tag = command->tag;
      fcmd->success = 1;
      status = fcmd->func (fcmd, sp);
      if (status != 0)
	{
	  rc = RESP_BAD;
	  errmsg = "Bogus attribute";
	  break;
	}
      else
	util_send (")\r\n");
    }
  free (set);
  return util_finish (command, rc, errmsg);
}

/* --------------- Fetch commands definition  ----- */
#define EPILOGUE(command) \
do { \
  if (command->success) \
    util_send ("* FETCH %d (%s", command->states, command->name); \
  else \
    util_send (" %s", command->name); \
} while (0)

static int
fetch_all (struct imap4d_command *command, char *arg)
{
  struct imap4d_command c_env = fetch_command_table[F_ENVELOPE];
  fetch_fast (command, arg);
  c_env.states = command->states;
  c_env.success = 0;
  fetch_envelope (&c_env, arg);
  return 0;
}

static int
fetch_full (struct imap4d_command *command, char *arg)
{
  struct imap4d_command c_body = fetch_command_table[F_BODY];
  fetch_all (command, arg);
  c_body.states = command->states;
  c_body.success = 0;
  fetch_body (&c_body, arg);
  return 0;
}

static int
fetch_fast (struct imap4d_command *command, char *arg)
{
  struct imap4d_command c_idate = fetch_command_table[F_INTERNALDATE];
  struct imap4d_command c_rfc = fetch_command_table[F_RFC822_SIZE];
  struct imap4d_command c_flags = fetch_command_table[F_FLAGS];
  c_flags.states = command->states;
  c_flags.success = command->success;
  fetch_flags (&c_flags, arg);
  command->success = 0;
  c_idate.states = command->states;
  c_idate.success = 0;
  fetch_internaldate (&c_idate, arg);
  c_rfc.states = command->states;
  c_rfc.success = 0;
  fetch_rfc822_size (&c_rfc, arg);
  return 0;
}

/* FIXME, FIXME:
   - incorrect DATE
   - address not the correct format
   - strings change to literals when detecting '"'
*/
static int
fetch_envelope (struct imap4d_command *command, char *arg)
{
  char buffer[512];
  header_t header = NULL;
  message_t msg = NULL;
  int status;
  mailbox_get_message (mbox, command->states, &msg);
  message_get_header (msg, &header);
  EPILOGUE(command);
  status = header_get_value (header, "Date", buffer, sizeof (buffer), NULL);
  util_send (" \"%s\"", buffer);
  status = header_get_value (header, "Subject", buffer, sizeof (buffer), NULL);
  util_send (" \"%s\"", buffer);
  status = header_get_value (header, "From", buffer, sizeof (buffer), NULL);
  util_send (" ((NIL NIL NIL \"%s\"))", buffer);
  status = header_get_value (header, "Sender", buffer, sizeof (buffer), NULL);
  util_send (" ((NIL NIL NIL \"%s\"))", buffer);
  status = header_get_value (header, "Reply-to", buffer, sizeof (buffer), NULL);
  util_send (" ((NIL NIL NIL \"%s\"))", buffer);
  status = header_get_value (header, "To", buffer, sizeof (buffer), NULL);
  util_send (" ((NIL NIL NIL \"%s\"))", buffer);
  status = header_get_value (header, "Cc", buffer, sizeof (buffer), NULL);
  util_send (" ((NIL NIL NIL \"%s\"))", buffer);
  status = header_get_value (header, "Bcc", buffer, sizeof (buffer), NULL);
  util_send (" ((NIL NIL NIL \"%s\"))", buffer);
  status = header_get_value (header, "In-Reply-To", buffer, sizeof (buffer), NULL);
  util_send (" \"%s\"", buffer);
  status = header_get_value (header, "Message-ID", buffer, sizeof (buffer), NULL);
  util_send (" \"%s\"", buffer);
  return 0;
}

static int
fetch_flags (struct imap4d_command *command, char *arg)
{
  attribute_t attr = NULL;
  message_t msg = NULL;
  mailbox_get_message (mbox, command->states, &msg);
  message_get_attribute (msg, &attr);
  EPILOGUE(command);
  util_send (" (");
  if (attribute_is_deleted (attr))
    util_send (" \\Deleted");
  if (attribute_is_read (attr))
    util_send (" \\Read");
  if (attribute_is_seen (attr))
    util_send (" \\Seen");
  if (attribute_is_draft (attr))
    util_send (" \\Draft");
  util_send (" )");
  return 0;
}

static int
fetch_internaldate (struct imap4d_command *command, char *arg)
{
  char date[512];
  envelope_t env = NULL;
  message_t msg = NULL;
  mailbox_get_message (mbox, command->states, &msg);
  message_get_envelope (msg, &env);
  date[0] = '\0';
  envelope_date (env, date, sizeof (date), NULL);
  EPILOGUE(command);
  if (date[strlen (date) - 1] == '\n')
    date[strlen (date) - 1] = '\0';
  util_send (" \"%s\"", date);
  return 0;
}

static int
fetch_rfc822_header (struct imap4d_command *command, char *arg)
{
  char buffer[64];
  struct imap4d_command c_body_p = fetch_command_table[F_BODY_PEEK];
  c_body_p.states = command->states;
  c_body_p.success = command->success;
  strcpy (buffer, "[HEADER]");
  fetch_body_peek (&c_body_p, buffer);
  return 0;
}

static int
fetch_rfc822_size (struct imap4d_command *command, char *arg)
{
  size_t size = 0;
  size_t lines = 0;
  message_t msg = NULL;
  mailbox_get_message (mbox, command->states, &msg);
  message_size (msg, &size);
  message_lines (msg, &lines);
  EPILOGUE(command);
  util_send (" %u", size + lines);
  return 0;
}

static int
fetch_rfc822_text (struct imap4d_command *command, char *arg)
{
  char buffer[64];
  struct imap4d_command c_body = fetch_command_table[F_BODY];
  c_body.states = command->states;
  c_body.success = command->success;
  strcpy (buffer, "[TEXT]");
  fetch_body (&c_body, buffer);
  return 0;
}


static int
fetch_rfc822 (struct imap4d_command *command, char *arg)
{
  char buffer[64];
  struct imap4d_command c_body = fetch_command_table[F_BODY];
  c_body.states = command->states;
  c_body.success = command->success;
  strcpy (buffer, "[]");
  fetch_body (&c_body, buffer);
  return 0;
}

/* FIXME: not implemeted.  */
static int
fetch_bodystructure (struct imap4d_command *command, char *arg)
{
  return 0;
}

static int
fetch_body (struct imap4d_command *command, char *arg)
{
  attribute_t attr = NULL;
  message_t msg = NULL;
  struct imap4d_command c_body_p = fetch_command_table[F_BODY_PEEK];
  mailbox_get_message (mbox, command->states, &msg);
  message_get_attribute (msg, &attr);
  c_body_p.states = command->states;
  c_body_p.success = command->success;
  fetch_body_peek (&c_body_p, arg);
  attribute_set_seen (attr);
  return 0;
}

static int
fetch_uid (struct imap4d_command *command, char *arg)
{
  size_t uid = 0;
  message_t msg = NULL;
  mailbox_get_message (mbox, command->states, &msg);
  message_get_uid (msg, &uid);
  EPILOGUE (command);
  util_send (" %d", uid);
  return 0;
}

static int
fetch_body_peek (struct imap4d_command *command, char *arg)
{
  message_t msg = NULL;
  char *partial = strchr (arg, '<');

  mailbox_get_message (mbox, command->states, &msg);

  EPILOGUE(command);

  if (strncasecmp (arg, "[]", 2) == 0)
    {
      stream_t stream = NULL;
      char buffer[512];
      size_t size = 0, lines = 0, n = 0;
      off_t off = 0;
      message_get_stream (msg, &stream);
      message_size (msg, &size);
      message_size (msg, &lines);
      util_send (" BODY[] {%u}\r\n", size + lines);
      while (stream_readline (stream, buffer, sizeof (buffer), off, &n) == 0
	     && n > 0)
	{
	  /* Nuke the trainline newline.  */
	  if (buffer[n - 1] == '\n')
	    buffer[n - 1] = '\0';
	  util_send ("%s\r\n", buffer);
	  off += n;
	}
    }
  else if (strncasecmp (arg, "[HEADER]", 8) == 0)
    {
      header_t header = NULL;
      stream_t stream = NULL;
      char buffer[512];
      size_t size = 0, lines = 0, n = 0;
      off_t off = 0;
      message_get_header (msg, &header);
      header_size (header, &size);
      header_lines (header, &lines);
      util_send (" BODY[HEADER] {%u}\r\n", size + lines);
      header_get_stream (msg, &stream);
      while (stream_readline (stream, buffer, sizeof (buffer), off, &n) == 0
	     && n > 0)
	{
	  /* Nuke the trainline newline.  */
	  if (buffer[n - 1] == '\n')
	    buffer[n - 1] = '\0';
	  util_send ("%s\r\n", buffer);
	  off += n;
	}
    }
  else if (strncasecmp (arg, "[TEXT]", 6) == 0)
    {
      body_t body = NULL;
      stream_t stream = NULL;
      char buffer[512];
      size_t size = 0, lines = 0, n = 0;
      off_t off = 0;
      message_get_body (msg, &body);
      body_size (body, &size);
      body_lines (body, &lines);
      util_send (" BODY[TEXT] {%u}\r\n", size + lines);
      body_get_stream (msg, &stream);
      while (stream_readline (stream, buffer, sizeof (buffer), off, &n) == 0
	     && n > 0)
	{
	  /* Nuke the trainline newline.  */
	  if (buffer[n - 1] == '\n')
	    buffer[n - 1] = '\0';
	  util_send ("%s\r\n", buffer);
	  off += n;
	}
    }
  else
    util_send (" Not supported");
  return 0;
}