/*
 * POP3Lite - 3lite POP3 Daemon
 * 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 <stdlib.h>
#include <string.h>
#include <syslog.h>

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

/*
 * FORWARD DECLARATIONS
 */
static void TRANS_clear_dflag ( gpointer data, gpointer user_data );
static CommandResponse *TRANS_status ( P3LControl *control,
				       const char *args );

CommandResponse *
default_cmd_trans_rset ( P3LControl *control, const char *args )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: handling RSET (%s)",
			       __FILE__, __LINE__, P3L_GET_DATA ( "USER" ) );
#endif
	g_list_foreach ( control->msg_info, (GFunc) TRANS_clear_dflag, NULL );
	return TRANS_status ( control, NULL );
}

CommandResponse *
default_cmd_trans_stat ( P3LControl *control, const char *args )
{
	unsigned long count=0, i;
	size_t size=0;
	MailInfo *minfo;

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

	for ( i=0; i < g_list_length ( control->msg_info ); i++ )
	{
		minfo = (MailInfo *)g_list_nth_data (control->msg_info, i);
		if ( minfo->deleted == FALSE )
		{
			size+=minfo->virtual_size;
			count++;
		}
	}

	return p3l_respond ( POP3_OK, g_strdup_printf ("%lu %d", count, size ) );
}

CommandResponse *
default_cmd_trans_list ( P3LControl *control, const char *args )
{
	unsigned long i, msg;
	MailInfo *minfo;
	CommandResponse *res;

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


	if ( args != NULL )
	{
		if ( ! p3l_is_numeric ( args ) )
			return p3l_respond ( POP3_ERR, "Invalid argument" );

		msg = strtoul ( args, (char **) NULL, 10 );
		minfo = g_list_nth_data ( control->msg_info, msg - 1 );
		if ( minfo == NULL || ( minfo && minfo->deleted == TRUE ) )
		{
			return p3l_respond ( POP3_ERR, "No such message" );
		}
		return p3l_respond ( POP3_OK, g_strdup_printf ("%lu %u", msg, minfo->virtual_size ) );
	}

	res = TRANS_status ( control, NULL );
	control->send_response ( control, res->code, res->message );
	g_free ( res );

	for ( i = 0; i < g_list_length ( control->msg_info ); i++ )
	{
		minfo=g_list_nth_data ( control->msg_info, i );
		if ( minfo->deleted == FALSE )
		{
			char *message;

			message = g_strdup_printf ( "%lu %u\r\n", i+1, minfo->virtual_size );
			control->send_raw ( control, message, strlen ( message ) );
			g_free ( message );
		}
	}

	return p3l_respond ( POP3_ANSWERED, "." );
}

CommandResponse *
default_cmd_trans_uidl ( P3LControl *control, const char *args )
{
	unsigned long i, msg;
	MailInfo *minfo;

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

	if ( args != NULL )
	{
	if ( ! p3l_is_numeric ( args ) )
		return p3l_respond ( POP3_ERR, "Invalid argument" );

		msg = strtoul ( args, (char **) NULL, 10 );

		minfo = g_list_nth_data ( control->msg_info, msg - 1);

		if ( minfo == NULL || ( minfo && minfo->deleted == TRUE ) )
			return p3l_respond ( POP3_ERR, "No such message" );

		return p3l_respond ( POP3_OK, g_strdup_printf ("%lu %s", msg,
				 bintohex(minfo->digest, strlen ( minfo->digest ), 1 ) ) );
	}

	control->send_response ( control, POP3_OK,
				 "But remember to DELETE messages REGULARLY" );

	for ( i = 0; i < g_list_length ( control->msg_info ); i++ )
	{
		minfo=g_list_nth_data ( control->msg_info, i );
		if ( minfo->deleted == FALSE )
		{
			char *message;

			message = g_strdup_printf ( "%lu %s\r\n", i+1,
						  bintohex ( minfo->digest, 
						  	     strlen ( minfo->digest ), 1 ) );
			control->send_raw ( control, message, strlen ( message ) );
			g_free ( message );
		}
	}

	return p3l_respond ( POP3_ANSWERED, "." );
}

/**
 * TRANS_clear_dflag: a GFunc
 * @data: MailInfo data
 * @user_data: ignored
 *
 * This is a GFunc, used by the RSET handler to clear the
 * deleted flag of all the messages.
 *
 * Returns: nothing
 **/
static void
TRANS_clear_dflag ( gpointer data, gpointer user_data )
{
	((MailInfo *)data)->deleted=FALSE;
}

/**
 * TRANS_status: internal status function
 * @control: the main control struct
 * @args: ignored
 *
 * A convenience function, used somewhere in the source...
 *
 * Returns: The Mailbox status
 **/
static CommandResponse *
TRANS_status ( P3LControl *control, const char *args )
{
	unsigned long count=0, i;
	size_t size=0;
	MailInfo *minfo;

	for ( i=0; i<g_list_length(control->msg_info); i++ )
	{
		minfo = (MailInfo *)g_list_nth_data (control->msg_info, i);
		if ( minfo->deleted == FALSE )
		{
			size+=minfo->virtual_size;
			count++;
		}
	}

	return p3l_respond ( POP3_OK, 
		g_strdup_printf ("%lu messages (%d octets)", count, size ) );
}


syntax highlighted by Code2HTML, v. 0.9.1