/*
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: t-pm-rcpts.c,v 1.2 2007/02/06 03:29:19 ca Exp $")
#include "sm/error.h"
#include "sm/assert.h"
#include "sm/types.h"
#include "sm/heap.h"
#include "t-pmilter.h"

#if MTA_USE_PMILTER

/*
**  PMR_RCPT_NEW -- add a new recipient to the recipient list
**
**	Parameters:
**		pmt_ctx -- SMTP server context
**		rcpt_pa -- recipient (printable address, RFC 2821)
**		ppmt_rcpt -- new recipient (output)
**
**	Returns:
**		usual return code
*/

sm_ret_T
pmt_rcpt_new(pmt_ctx_P pmt_ctx, const char *rcpt_pa, pmt_rcpt_P *ppmt_rcpt)
{
	pmt_rcpt_P pmt_rcpt;

	SM_REQUIRE(ppmt_rcpt != NULL);
	pmt_rcpt = (pmt_rcpt_P) sm_zalloc(sizeof(**ppmt_rcpt));
	if (pmt_rcpt == NULL)
		goto error;
	pmt_rcpt->pmtr_pa = sm_rpool_strdup(NULL, rcpt_pa);
	if (NULL == pmt_rcpt->pmtr_pa)
		goto error;
	PMT_RCPTS_INSERT_TAIL(&pmt_ctx->pmt_rcpts, pmt_rcpt);
	*ppmt_rcpt = pmt_rcpt;
	return SM_SUCCESS;

  error:
	SM_FREE(*ppmt_rcpt);
	return sm_err_temp(ENOMEM);
}

/*
**  PMR_RCPT_FREE -- free a single recipient address
**
**	Parameters:
**		pmt_ctx -- SMTP server transaction context
**		pmtr_rcpt -- recipient to free
**
**	Returns:
**		usual return code
*/

static sm_ret_T
pmt_rcpt_free(pmt_ctx_P pmt_ctx, pmt_rcpt_P pmt_rcpt)
{
	if (NULL == pmt_rcpt)
		return SM_SUCCESS;
	SM_FREE(pmt_rcpt->pmtr_pa);
	sm_free(pmt_rcpt);
	return SM_SUCCESS;
}

/*
**  PMR_RCPTS_FREE -- free an entire recipient list
**
**	Parameters:
**		pmt_ctx -- SMTP server transaction context
**
**	Returns:
**		usual return code
*/

sm_ret_T
pmt_rcpts_free(pmt_ctx_P pmt_ctx)
{
	pmt_rcpt_P pmt_rcpt, pmt_rcpt_nxt;

	if (PMT_RCPTS_EMPTY(&pmt_ctx->pmt_rcpts))
		return SM_SUCCESS;
	for (pmt_rcpt = PMT_RCPTS_FIRST(&pmt_ctx->pmt_rcpts);
	     pmt_rcpt != PMT_RCPTS_END(&pmt_ctx->pmt_rcpts);
	     pmt_rcpt = pmt_rcpt_nxt)
	{
		pmt_rcpt_nxt = PMT_RCPTS_NEXT(pmt_rcpt);

		/* remove adr from list? use PMT_RCPTS_REMOVE_FREE()? */
		pmt_rcpt_free(pmt_ctx, pmt_rcpt);
	}
	PMT_RCPTS_INIT(&pmt_ctx->pmt_rcpts);
	return SM_SUCCESS;
}


#endif /* MTA_USE_PMILTER */


syntax highlighted by Code2HTML, v. 0.9.1