folder_pop.c 3.2 KB
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Library 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 Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

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

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <folder0.h>
#include <registrar0.h>

/* We export url parsing and the initialisation of
   the mailbox, via the register entry/record.  */

static struct _record _pop_record =
{
  MU_POP_SCHEME,
  _url_pop_init, /* Url init.  */
  _mailbox_pop_init, /* Mailbox init.  */
  NULL, /* Mailer init.  */
  _folder_pop_init, /* Folder init.  */
  NULL, /* No need for an back pointer.  */
  NULL, /* _is_scheme method.  */
  NULL, /* _get_url method.  */
  NULL, /* _get_mailbox method.  */
  NULL, /* _get_mailer method.  */
  NULL  /* _get_folder method.  */
};
record_t pop_record = &_pop_record;

static int folder_pop_open  __P ((folder_t, int));
static int folder_pop_close __P ((folder_t));
static int folder_pop_get_authority __P ((folder_t, authority_t *));
extern int _pop_user         __P ((authority_t));

/* XXX: The way, the POP folder is handle is not clean at all.
   the I/O functions should have been here on folder, not in  mbx_pop.c  */
int
_folder_pop_init (folder_t folder)
{
  int status;

  /* Set the authority early:
     (1) so we can check for errors.
     (2) allow the client to get the authority for setting the ticket
     before the open.  */
  status = folder_pop_get_authority (folder, NULL);
  if (status != 0)
    return status;

  folder->_open = folder_pop_open;
  folder->_close = folder_pop_close;
  return 0;
}

static int
folder_pop_open (folder_t folder, int flags)
{
  mailbox_t mbox = folder->data;
  return mailbox_open (mbox, flags);
}

static int
folder_pop_close (folder_t folder)
{
  mailbox_t mbox = folder->data;
  return mailbox_close (mbox);
}

static int
folder_pop_get_authority (folder_t folder, authority_t *pauth)
{
  int status = 0;
  if (folder->authority == NULL)
    {
      char *auth;
      size_t n = 0;

      url_get_auth (folder->url, NULL, 0, &n);
      auth = calloc (n + 1, 1);
      if (auth == NULL)
	return ENOMEM;
      if (strcasecmp (auth, "*") == 0)
	{
	  status = authority_create (&folder->authority, NULL, folder);
	  authority_set_authenticate (folder->authority, _pop_user, folder);
	}
      /*
	else...
	"+apop" could be supported.
	Anything else starting with "+" is an extension mechanism.
	Without a "+" it's a SASL mechanism.
      */
      else
	{
	  status = ENOSYS;
	}
    }
  if (pauth)
    *pauth = folder->authority;
  return status;
}