/*****************************************************************************

  POPular -- A POP3 server and proxy for large mail systems

  $Id: libpdm_any.c,v 1.1 2001/04/08 18:23:06 sqrt Exp $

  http://www.remote.org/jochen/mail/popular/

******************************************************************************

  Copyright (C) 1999-2001  Jochen Topf <jochen@remote.org>

  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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA

*****************************************************************************/

#include <popular.h>


static struct pdm_mvar *mv;

struct pdm_ctx_any {
  char namespace[MAXLEN_ID];
  char backend[MAXLEN_ID];
};


/*****************************************************************************
 
  pdm_init()
 
*****************************************************************************/
char *
pdm_init(int argc, char *argv[], struct pdm_mvar *mvar, void **pdmctx)
{
  struct pdm_ctx_any *ctx;

  mv = mvar;

  PDM_DEBUG0("pdm_init", "start");

  if (argc < 3 || argc > 4) {
    PDM_DEBUG0("pdm_init", "need one or two args");
    return("not enough or too many arguments");
  }

  ctx = malloc(sizeof(struct pdm_ctx_any));
  if (! ctx) {
    PDM_DEBUG0("pdm_init", "out of memory");
    return("out of memory");
  }

  strlcpy(ctx->namespace, argv[2], sizeof(ctx->backend));
  if (argc == 4) {
    strlcpy(ctx->backend, argv[3], sizeof(ctx->backend));
  } else {
    ctx->backend[0] = '\0';
  }

  *pdmctx = ctx;
  return NULL;
}


/*****************************************************************************
 
  pdm_close()
 
*****************************************************************************/
int
pdm_close(void *pdmctx)
{
  PDM_DEBUG0("pdm_close", "start");

  free(pdmctx);

  return 0;
}


/*****************************************************************************
 
  pdm_args()

  This should return a string with the arguments that were used to
  configure this module.

*****************************************************************************/
char *
pdm_args(void *pdmctx)
{
  static char buf[sizeof(struct pdm_ctx_any)];
  struct pdm_ctx_any *ctx = pdmctx;

  PDM_DEBUG0("pdm_args", "start");

  strlcpy(buf, ctx->namespace, sizeof(buf));
  strlcat(buf, " ", sizeof(buf));
  strlcat(buf, ctx->backend, sizeof(buf));

  return buf;
}


/*****************************************************************************
 
  pdm_reload()

  This function can be used to reinitialize a module. Return 1, if this
  worked, or 0 if it didn't.

*****************************************************************************/
int
pdm_reload(void *pdmctx)
{
  PDM_DEBUG0("pdm_reload", "start");
  return 1;
}


/*****************************************************************************
 
  pdm_check()
 
*****************************************************************************/
pdm_result_t
pdm_check(void *pdmctx, const struct pdm_request *pdmr, struct pdm_data *pdmd)
{
  struct pdm_ctx_any *ctx = pdmctx;

  PDM_DEBUG0("pdm_check", "start");

  if (strcmp(ctx->namespace, pdmr->namespace)) {
    return pdmUnknown;
  }

  strlcpy(pdmd->user, pdmr->user, sizeof(pdmd->user));
  strlcpy(pdmd->pass, pdmr->pass, sizeof(pdmd->pass));

  if (ctx->backend[0]) {
    strlcpy(pdmd->backend, ctx->backend, sizeof(pdmd->backend));
  } else {
    strlcpy(pdmd->backend, pdmr->pass, sizeof(pdmd->backend));
  }

  return pdmAccept;
}


/*****************************************************************************
 
  pdm_auth()
 
*****************************************************************************/
pdm_result_t
pdm_auth(void *pdmctx, const struct pdm_request *pdmr, struct pdm_data *pdmd)
{
  struct pdm_ctx_any *ctx = pdmctx;

  PDM_DEBUG0("pdm_auth", "start");

  if (strcmp(ctx->namespace, pdmr->namespace)) {
    return pdmFail;
  }

  return pdmAccept;
}


/** THE END *****************************************************************/


syntax highlighted by Code2HTML, v. 0.9.1