/*
 * (POP3Lite) capa - 3lite POP3 Daemon (CAPA support)
 * 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 <string.h>

#ifdef DEBUG
#	include <syslog.h>
#endif

static const char rcsid[]="$Id: capa.c,v 1.5 2001/01/12 15:58:59 algernon Exp $";

int capa_LTX_module_init ( P3LControl *control );
int capa_LTX_module_done ( P3LControl *control );

static CommandResponse *capa_cmd_all_capa ( P3LControl *control,
					    const char *args );
static char *capa_is_implemented ( P3LControl *control,
				   const char *capa );

static void capa_echo ( gpointer key, gpointer value, gpointer user_data );

/**
 * capa_cmd_all_capa: CAPA command handler
 * @control: the usual control struct
 * @args: ignored
 *
 * Handles the CAPA command.
 *
 * Returns: a CommandResponse.
 **/

static CommandResponse *
capa_cmd_all_capa ( P3LControl *control, const char *args )
{
	char *capa;

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

	control->send_response ( control, POP3_OK, "Capability list follows" );

	/*
	 * List the capabilities mentioned in RFC 2449
	 */

	if ( ( capa = capa_is_implemented ( control, "TOP" ) ) )
		control->send_raw ( control, capa, strlen (capa) );
	if ( ( capa = capa_is_implemented ( control, "USER" ) ) )
		control->send_raw ( control, capa, strlen (capa) );
	if ( ( capa = capa_is_implemented ( control, "SASL" ) ) )
		control->send_raw ( control, capa, strlen (capa) );
	if ( ( capa = capa_is_implemented ( control, "RESP_CODES" ) ) )
		control->send_raw ( control, capa, strlen (capa) );
	if ( ( capa = capa_is_implemented ( control, "LOGIN-DELAY" ) ) )
		control->send_raw ( control, capa, strlen (capa) );
	if ( ( capa = capa_is_implemented ( control, "PIPELINING" ) ) )
		control->send_raw ( control, capa, strlen (capa) );
	if ( ( capa = capa_is_implemented ( control, "EXPIRE" ) ) )
		control->send_raw ( control, capa, strlen (capa) );
	if ( ( capa = capa_is_implemented ( control, "UIDL" ) ) )
		control->send_raw ( control, capa, strlen (capa) );
	if ( ( capa = capa_is_implemented ( control, "IMPLEMENTATION" ) ) )
		control->send_raw ( control, capa, strlen (capa) );

	g_hash_table_foreach ( control->capabilities, (GHFunc) capa_echo, (gpointer) control );

	return p3l_respond ( POP3_ANSWERED, "." );
}

/**
 * capa_is_implemented: helper function
 * @control: control structure
 * @capa: capability
 *
 * Looks up a capability in different places.
 *
 * Returns: the value to echo
 **/

static char *
capa_is_implemented ( P3LControl *control, const char *capa )
{
	gpointer p;

	/*
	 * Capabilities are listed elsewhere...
	 */
	p = g_hash_table_lookup ( control->capabilities, capa );
	if ( p != NULL )
		return "";
	p = g_hash_table_lookup ( control->auth_commands, capa );
	if ( p != NULL )
		return g_strdup_printf ( "%s\r\n", capa );
	p = g_hash_table_lookup ( control->trans_commands, capa );
	if ( p != NULL )
		return g_strdup_printf ( "%s\r\n", capa );

	return "";
}

/**
 * capa_echo: GHFunc
 *
 * Loop function.
 *
 * Returns: nothing.
 **/
static void
capa_echo ( gpointer key, gpointer value, gpointer user_data )
{
	P3LControl *control;
	char *message;

	control = (P3LControl *) user_data;
	message = g_strdup_printf ( "%s %s\r\n", (char *) key, (char *) value );
	control->send_raw ( control, message, strlen ( message ) );
	g_free ( message );
}

int
capa_LTX_module_init ( P3LControl *control )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: init mod-CAPA",
			       __FILE__, __LINE__ );
#endif

	g_hash_table_insert ( control->auth_commands, "CAPA",
			      (gpointer) capa_cmd_all_capa );
	g_hash_table_insert ( control->trans_commands, "CAPA",
			      (gpointer) capa_cmd_all_capa );

	return 0;
}

int
capa_LTX_module_done ( P3LControl *control )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: done mod-CAPA",
			       __FILE__, __LINE__ );
#endif
	g_hash_table_remove ( control->auth_commands, "CAPA" );
	g_hash_table_remove ( control->trans_commands, "CAPA" );

	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1