/*
 * (POP3Lite) Expire - 3lite POP3 Daemon (Message expiration)
 * Copyright (C) 2000, 2001 Gergely Nagy <8@free.bsd.hu>
 *
 * This file is part of POP3Lite.
 *
 * POP3Lite 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.
 *
 * POP3Lite 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
 */

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include <pop3lite.h>
#include <glib.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef DEBUG
#	include <syslog.h>
#endif

#include "mailbox.h"

static const char rcsid[]="$Id: expire.c,v 1.6 2001/01/12 15:58:59 algernon Exp $";

static void expire_hook_mail_parse ( P3LControl *control, char *buffer,
				     MailInfo *minfo );
static CommandResponse *expire_update ( P3LControl *control );

int expire_LTX_module_init ( P3LControl *control );
int expire_LTX_module_done ( P3LControl *control );

static P3LHook_mailbox_mail_parse B_expire_mail_parse;
static P3LControl_update B_expire_update;

static CommandResponse *
expire_update ( P3LControl *control )
{
	time_t curr, mail, max_age;
	MailInfo *minfo;
	unsigned long i;
	char *max_age_s;

#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: UPDATE",
			       __FILE__, __LINE__ );
#endif

	/*
	 * Get the maximum age of a message (in days)
	 */

	max_age_s = P3L_GET_FIRST_OPTION ( "EXPIRE.AGE" );
	if ( ! p3l_is_numeric ( max_age_s ) )
		return (*B_expire_update) ( control );

	max_age = (time_t) atoi ( max_age_s );

	if ( max_age < 1 )
		return (*B_expire_update) ( control );

	curr = time ( NULL );

	/*
	 * Delete all the messages that are too old
	 */

	for ( i = 0; i < g_list_length ( control->msg_info ); i++ )
	{
		minfo = (MailInfo *) g_list_nth_data ( control->msg_info, i );
		mail = (time_t) g_hash_table_lookup ( minfo->driver_data, "DATE" );
		if ( curr - mail > 60*60*24*max_age )
			minfo->deleted = TRUE;
	}

	/*
	 * Do the update!
	 */

	return (*B_expire_update) ( control );
}

static void
expire_hook_mail_parse ( P3LControl *control, char *buffer, MailInfo *minfo )
{
	char *date;
	struct tm tp;

#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: handling MAILBOX-PARSE-MAIL",
			       __FILE__, __LINE__ );
#endif

	/*
	 * Fill the struct with zeros
	 */

	memset ( &tp, 0, sizeof ( tp ) );

	/*
	 * From user@domain.org DOW, DAY MONTH HOUR:MIN:SEC YEAR ZONE
	 * The above thing is parsed for the date, which will be stored
	 * in minfo->driver_data
	 */

	date = strchr ( buffer, ' ' );
	date = strchr ( date + 1, ' ' );
	date = g_strndup ( date + 1, strchr ( date, '\n' ) - date - 1 );

	strptime ( date, "%a %b %d %H:%M:%S %Y %Z", &tp );

	g_hash_table_insert ( minfo->driver_data, "DATE", (gpointer) mktime ( &tp ) );

	if ( B_expire_mail_parse != NULL )
		(*B_expire_mail_parse) ( control, buffer, minfo );
}

int
expire_LTX_module_init ( P3LControl *control )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: init mod-Expire",
			       __FILE__, __LINE__ );
#endif


	if ( control->update != NULL )
	{
		B_expire_mail_parse = (P3LHook_mailbox_mail_parse)
			p3l_command_replace ( control->hooks, "MAILBOX-PARSE-MAIL",
					      (gpointer) expire_hook_mail_parse );
		B_expire_update = control->update;
		control->update = expire_update;
		return 0;
	}

	return -1;
}

int
expire_LTX_module_done ( P3LControl *control )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: done mod-Expire",
			       __FILE__, __LINE__ );
#endif

	control->update = B_expire_update;
	g_hash_table_insert ( control->hooks, "MAILBOX-PARSE-MAIL",
			      (gpointer) B_expire_mail_parse );

	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1