/*
 * (POP3Lite) VHost - 3lite POP3 Daemon (IP-less virtual hosting)
 * 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

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

int vhost_LTX_module_done ( P3LControl *control );
int vhost_LTX_module_init ( P3LControl *control );

static P3LHook_get_mailbox B_vhost_get_mailbox;
static p3l_pop3_command B_vhost_auth_user;

static char *vhost_default_get_mailbox ( P3LControl *control );
static CommandResponse *vhost_cmd_auth_user ( P3LControl *control, const char *user );

static char *
vhost_default_get_mailbox ( P3LControl *control )
{
	gpointer buffer;
	char *filename, **line_s, *line, *mbox, *user;
	P3LString **lines;
	unsigned long i=0;
	size_t bsize, linecnt;

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

	mbox = NULL;

	user = P3L_GET_DATA ( "CLIENT_USER" );
	if ( user == NULL )
		user = P3L_GET_DATA ( "USER" );
	if ( user == NULL )
		return NULL;

	/*
	 * Get the filename...
	 */

	filename = P3L_GET_FIRST_OPTION ( "VHOST.USER_MAP" );

	if ( filename == NULL )
		filename = SYSCONFDIR "/pop3lite.usermap";

	/*
	 * Read the file
	 */
	if ( ( buffer = p3l_read_file ( filename, &bsize ) ) == NULL )
	{
		if ( B_vhost_get_mailbox != NULL )
			return (*B_vhost_get_mailbox) ( control );
		else
			return NULL;
	}

	/*
	 * Get the mailbox
	 */

	lines = p3l_split_lines ( buffer, bsize, &linecnt );
	while ( lines[i] != NULL && i < linecnt )
	{
		line = (char *) g_malloc ( lines[i]->length + 1 );
		memcpy ( line, lines[i]->str, lines[i]->length );
		line[lines[i]->length-1]=0;
		/*
		 * We use the third one...
		 */
		line_s = g_strsplit ( line, ":", 3 );
		g_free ( line );
		if ( ! strcmp ( line_s[0], user ) )
		{
			mbox = g_strdup ( line_s[2] );
			g_strfreev ( line_s );
			break;
		}
		i++;
		g_strfreev ( line_s );
	}

	g_free ( buffer );

	if ( mbox == NULL && B_vhost_get_mailbox != NULL )
		return (*B_vhost_get_mailbox) ( control );

	return mbox;
}

static CommandResponse *
vhost_cmd_auth_user ( P3LControl *control, const char *user )
{
	char *alias, *tmp, *euser;

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

	euser = g_strdup ( user );

	if ( euser == NULL )
		return p3l_respond ( POP3_ERR, "Invalid username" );

	/** Replace first % with @ **/
	tmp = strchr ( euser, '%' );
	if ( tmp )
		tmp[0]='@';

	alias = P3L_CALL_HOOK ( P3LHook_alias_get_alias,
				"ALIAS-GET-ALIAS" ) ( control, euser );

	g_hash_table_insert ( control->data, "CLIENT_USER", g_strdup ( euser ) );
	g_hash_table_insert ( control->data, "USER", g_strdup ( alias ) );

	tmp = strchr ( euser, '@' );

	if ( tmp )
		tmp[0]=0;

	return p3l_respond ( POP3_OK, g_strdup_printf ( "%s selected", euser ) );
}

int
vhost_LTX_module_init ( P3LControl *control )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: init mod-VHost",
			       __FILE__, __LINE__ );
#endif

	/*
	 * If we have an existing USER handler, everything is fine, if not,
	 * we do not install ourselves, because we depend on the underlying
	 * handler.
	 *
	 * We also require an underlying ALIAS module...
	 */

	if ( P3L_CMD_EXISTS ( auth_commands, "USER" ) &&
	     P3L_HOOK_EXISTS ( "ALIAS-GET-ALIAS" ) )
	{
		B_vhost_get_mailbox = (P3LHook_get_mailbox)
			p3l_command_replace ( control->hooks, "GET-MAILBOX",
					      (gpointer) vhost_default_get_mailbox );
		B_vhost_auth_user = (p3l_pop3_command)
			p3l_command_replace ( control->auth_commands, "USER",
					      (gpointer) vhost_cmd_auth_user );
		return 0;
	}

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

	g_hash_table_insert ( control->hooks, "GET-MAILBOX",
			      (gpointer) B_vhost_get_mailbox );
	g_hash_table_insert ( control->auth_commands, "USER",
			      (gpointer) B_vhost_auth_user );

	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1