Blame view

libproto/mh/mbox.c 10.7 KB
Sergey Poznyakoff authored
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
   2009, 2010 Free Software Foundation, Inc.
Sergey Poznyakoff authored
4 5 6 7

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
8
   version 3 of the License, or (at your option) any later version.
Sergey Poznyakoff authored
9 10 11 12 13 14 15

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
16 17
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */
Sergey Poznyakoff authored
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

/* First draft by Sergey Poznyakoff */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>

#ifdef WITH_PTHREAD
# ifdef HAVE_PTHREAD_H
#  ifndef _XOPEN_SOURCE
#   define _XOPEN_SOURCE  500
#  endif
#  include <pthread.h>
# endif
#endif

#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#include <mailutils/attribute.h>
#include <mailutils/body.h>
#include <mailutils/debug.h>
#include <mailutils/envelope.h>
#include <mailutils/error.h>
54
#include <mailutils/errno.h>
Sergey Poznyakoff authored
55 56 57
#include <mailutils/header.h>
#include <mailutils/locker.h>
#include <mailutils/message.h>
58
#include <mailutils/util.h>
Sergey Poznyakoff authored
59 60 61 62
#include <mailutils/property.h>
#include <mailutils/stream.h>
#include <mailutils/url.h>
#include <mailutils/observer.h>
63
#include <mailutils/io.h>
64
#include <mailutils/cctype.h>
65
#include <mailutils/mh.h>
66 67 68
#include <mailutils/sys/mailbox.h>
#include <mailutils/sys/registrar.h>
#include <mailutils/sys/amd.h>
Sergey Poznyakoff authored
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

struct _mh_message
{
  struct _amd_message amd_message;
  size_t seq_number;        /* message sequence number */
};

static int
mh_message_cmp (struct _amd_message *a, struct _amd_message *b)
{
  struct _mh_message *ma = (struct _mh_message *) a;
  struct _mh_message *mb = (struct _mh_message *) b;
  if (ma->seq_number < mb->seq_number)
    return -1;
  else if (ma->seq_number > mb->seq_number)
    return 1;
  return 0;
}

static size_t
_mh_next_seq (struct _amd_data *amd)
{
  struct _mh_message *msg = (struct _mh_message *)
                              _amd_get_message (amd, amd->msg_count);
  return (msg ? msg->seq_number : 0) + 1;
}

96
/* Return current filename for the message.
Sergey Poznyakoff authored
97
   NOTE: Allocates memory. */
98 99 100 101 102 103
static int
_mh_cur_message_name (struct _amd_message *amsg, char **pname)
{
  int status = 0;
  struct _mh_message *mhm = (struct _mh_message *) amsg;
  char *filename;
104 105 106 107 108 109 110
  char *pnum;
  size_t len;

  status = mu_asprintf (&pnum, "%lu", (unsigned long) mhm->seq_number);
  if (status)
    return status;
  len = strlen (amsg->amd->name) + 1 + strlen (pnum) + 1;
111 112 113 114 115 116 117 118 119 120
  filename = malloc (len);
  if (filename)
    {
      strcpy (filename, amsg->amd->name);
      strcat (filename, "/");
      strcat (filename, pnum);
      *pname = filename;
    }
  else
    status = ENOMEM;
121
  free (pnum);
122 123 124 125 126 127
  return status;
}

/* Return newfilename for the message.
   NOTE: Allocates memory. */
static int
128 129 130
_mh_new_message_name (struct _amd_message *amsg, int flags,
		      int expunge MU_ARG_UNUSED,
		      char **pname)
Sergey Poznyakoff authored
131
{
132
  int status = 0;
Sergey Poznyakoff authored
133 134
  struct _mh_message *mhm = (struct _mh_message *) amsg;
  char *filename;
135 136 137 138 139 140 141
  char *pnum;
  size_t len;

  status = mu_asprintf (&pnum, "%lu", (unsigned long) mhm->seq_number);
  if (status)
    return status;
  len = strlen (amsg->amd->name) + 1 +
142
               ((flags & MU_ATTRIBUTE_DELETED) ? 1 : 0) + strlen (pnum) + 1;
Sergey Poznyakoff authored
143
  filename = malloc (len);
144 145 146 147 148 149 150 151 152
  if (filename)
    {
      strcpy (filename, amsg->amd->name);
      strcat (filename, "/");
      if (flags & MU_ATTRIBUTE_DELETED)
	strcat (filename, ",");
      strcat (filename, pnum);
      *pname = filename;
    }
Sergey Poznyakoff authored
153
  else
154
    status = ENOMEM;
155
  free (pnum);
156
  return status;
Sergey Poznyakoff authored
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

/* Find the message with the given sequence number */
static struct _mh_message *
_mh_get_message_seq (struct _amd_data *amd, size_t seq)
{
  struct _mh_message msg;
  size_t index;
  
  msg.seq_number = seq;
  if (amd_msg_lookup (amd, (struct _amd_message*) &msg, &index))
    return NULL;

  return (struct _mh_message *) _amd_get_message (amd, index);
}

/* Scan the mailbox */
static int
175
mh_scan0 (mu_mailbox_t mailbox, size_t msgno MU_ARG_UNUSED, size_t *pcount, 
Sergey Poznyakoff authored
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
          int do_notify)
{
  struct _amd_data *amd = mailbox->data;
  struct _mh_message *msg;
  DIR *dir;
  struct dirent *entry;
  int status = 0;
  struct stat st;

  if (amd == NULL)
    return EINVAL;

  dir = opendir (amd->name);
  if (!dir)
    return errno;

  mu_monitor_wrlock (mailbox->monitor);

#ifdef WITH_PTHREAD
  pthread_cleanup_push (amd_cleanup, (void *)mailbox);
#endif

  mu_locker_lock (mailbox->locker);

  /* Do actual work. */

  while ((entry = readdir (dir)))
    {
      char *namep;
      int attr_flags;
      size_t num;

      attr_flags = 0;
      switch (entry->d_name[0])
	{
	case '.':
	  /* FIXME: .mh_sequences */
	  continue;
	case ',':
	  continue;
#if 0
	  attr_flags |= MU_ATTRIBUTE_DELETED;
	  namep = entry->d_name+1;
	  break;
#endif
	case '0':case '1':case '2':case '3':case '4':
	case '5':case '6':case '7':case '8':case '9':
	  namep = entry->d_name;
	  break;
	default:
	  /*FIXME: Invalid entry. Report? */
	  continue;
	}

      num = strtoul (namep, &namep, 10);
      if (namep[0])
	continue;

      msg = _mh_get_message_seq (amd, num);
      if (!msg)
	{
	  msg = calloc (1, sizeof(*msg));

	  msg->seq_number = num;
	  msg->amd_message.attr_flags = attr_flags;
241
	  msg->amd_message.orig_flags = msg->amd_message.attr_flags;
Sergey Poznyakoff authored
242 243 244 245 246 247

	  _amd_message_insert (amd, (struct _amd_message*) msg);
	}
      else
	{
	  msg->amd_message.attr_flags = attr_flags;
248
	  msg->amd_message.orig_flags = msg->amd_message.attr_flags;
Sergey Poznyakoff authored
249 250 251 252 253 254 255 256 257 258 259
	}
    }

  closedir (dir);

  if (do_notify)
    {
      size_t i;

      for (i = 0; i < amd->msg_count; i++)
	{
260
	  DISPATCH_ADD_MSG (mailbox, amd, i);
Sergey Poznyakoff authored
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
	}
    }
  
  if (stat (amd->name, &st) == 0)
    amd->mtime = st.st_mtime;

  if (pcount)
    *pcount = amd->msg_count;

  /* Reset the uidvalidity.  */
  if (amd->msg_count > 0)
    {
      if (amd->uidvalidity == 0)
	{
	  amd->uidvalidity = (unsigned long)time (NULL);
	  /* Tell that we have been modified for expunging.  */
	  if (amd->msg_count)
	    {
	      amd_message_stream_open (amd->msg_array[0]);
	      amd_message_stream_close (amd->msg_array[0]);
	      amd->msg_array[0]->attr_flags |= MU_ATTRIBUTE_MODIFIED;
	    }
	}
    }

  /* Clean up the things */

  amd_cleanup (mailbox);
#ifdef WITH_PTHREAD
  pthread_cleanup_pop (0);
#endif
  return status;
}

295 296 297 298 299 300 301 302 303
static int
mh_qfetch (struct _amd_data *amd, mu_message_qid_t qid)
{
  char *p;
  size_t num = 0;
  int attr_flags = 0;
  struct _mh_message *msg;

  p = qid + strlen (qid) - 1;
304
  if (!mu_isdigit (*p))
305 306
    return EINVAL;
  
307
  for (p--; p >= qid && mu_isdigit (*p); p--)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    ;

  if (p == qid)
    return EINVAL;

  num = strtoul (p + 1, NULL, 10);
  
  if (*p == ',')
    {
      attr_flags |= MU_ATTRIBUTE_DELETED;
      p--;
    }

  if (*p != '/')
    return EINVAL;
  
  msg = calloc (1, sizeof (*msg));
  msg->seq_number = num;
  msg->amd_message.attr_flags = attr_flags;
327
  msg->amd_message.orig_flags = msg->amd_message.attr_flags;
328 329 330 331 332
  _amd_message_insert (amd, (struct _amd_message*) msg);
  return 0;
}


Sergey Poznyakoff authored
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
/* Note: In this particular implementation the message sequence number
   serves also as its UID. This allows to avoid many problems related
   to keeping the uids in the headers of the messages. */

static int
mh_message_uid (mu_message_t msg, size_t *puid)
{
  struct _mh_message *mhm = mu_message_get_owner (msg);
  if (puid)
    *puid = mhm->seq_number;
  return 0;
}

static int
_mh_msg_init (struct _amd_data *amd, struct _amd_message *amm)
{
  struct _mh_message *mhm = (struct _mh_message *) amm;
  mhm->seq_number = _mh_next_seq (amd);
  return 0;
}

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
static int
_mh_msg_delete (struct _amd_data *amd, struct _amd_message *amm)
{
  int rc, status;
  char *name;
  char *argv[3];
  /* FIXME: Cache this value */
  const char *proc = mu_mhprop_get_value (mu_mh_profile, "rmmproc", NULL);
  if (!proc)
    return ENOSYS;

  rc = amd->cur_msg_file_name (amm, &name);
  if (rc)
    return rc;
  
  if (proc[0] == 0)
    {
      rc = unlink (name);
      if (rc)
	rc = errno;
    }
  else
    {
      argv[0] = (char*) proc;
      argv[1] = name;
      argv[2] = NULL;
      rc = mu_spawnvp (proc, argv, &status);
    }
  free (name);
  return rc;
}

Sergey Poznyakoff authored
386

387 388 389 390 391 392 393
static int
mh_remove (struct _amd_data *amd)
{
  return amd_remove_dir (amd->name);
}


394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
static int
mh_get_property (mu_mailbox_t mailbox, mu_property_t *pprop)
{
  struct _amd_data *amd = mailbox->data;
  mu_property_t property = NULL;
  struct mu_mh_prop *mhprop;
  const char *p;
  
  mhprop = calloc (1, sizeof (mhprop[0]));
  if (!mhprop)
    return ENOMEM;
  p = mu_mhprop_get_value (mu_mh_profile, "mh-sequences",
			   MU_MH_SEQUENCES_FILE);
  mhprop->filename = mu_make_file_name (amd->name, p);
  mu_property_create_init (&property, mu_mh_property_init, mhprop);
  mu_mailbox_set_property (mailbox, property);
      
  /*FIXME mu_property_set_value (property, "TYPE", "MH", 1);*/
  *pprop = property;
  return 0;
}

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


static int
mh_translate (mu_mailbox_t mbox, int cmd, size_t from, size_t *to)
{
  struct _amd_data *amd = mbox->data;
  struct _mh_message msg, *mp;
  size_t n;

  /* Make sure the mailbox has been scanned */ 
  mu_mailbox_messages_count (mbox, &n);
    
  switch (cmd)
    {
    case MU_MAILBOX_UID_TO_MSGNO:
      msg.seq_number = from;
      if (amd_msg_lookup (amd, (struct _amd_message*) &msg, &n))
	return MU_ERR_NOENT;
      *to = n;
      break;

    case MU_MAILBOX_MSGNO_TO_UID:
      mp = (struct _mh_message *) _amd_get_message (amd, from);
      if (!mp)
	return MU_ERR_NOENT;
      *to = mp->seq_number;
      break;

    default:
      return ENOSYS;
    }
  return 0;
}
449 450


Sergey Poznyakoff authored
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

int
_mailbox_mh_init (mu_mailbox_t mailbox)
{
  int rc;
  struct _amd_data *amd;

  rc = amd_init_mailbox (mailbox, sizeof (struct _amd_data), &amd);
  if (rc)
    return rc;

  amd->msg_size = sizeof (struct _mh_message);
  amd->msg_free = NULL;
  amd->msg_init_delivery = _mh_msg_init;
  amd->msg_finish_delivery = NULL;
466 467
  amd->cur_msg_file_name = _mh_cur_message_name;
  amd->new_msg_file_name = _mh_new_message_name;
Sergey Poznyakoff authored
468
  amd->scan0 = mh_scan0;
469
  amd->qfetch = mh_qfetch;
Sergey Poznyakoff authored
470 471 472
  amd->msg_cmp = mh_message_cmp;
  amd->message_uid = mh_message_uid;
  amd->next_uid = _mh_next_seq;
473 474
  amd->remove = mh_remove;

475
  mailbox->_get_property = mh_get_property;
476
  mailbox->_translate = mh_translate;
477 478 479 480

  if (mu_mhprop_get_value (mu_mh_profile, "rmmproc", NULL))
    amd->delete_msg = _mh_msg_delete;
    
481
  
Sergey Poznyakoff authored
482 483 484
  return 0;
}

485