/*
 * (POP3Lite) cpp-test - 3lite POP3 Server (Lame C++ module test)
 * 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
 */

extern "C" {
static const char rcsid[]="$Id: cpp-test.cpp,v 1.4.2.1 2001/04/13 09:51:28 algernon Exp $";
}

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

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

#ifdef DEBUG
#	include <syslog.h>
#endif

static p3l_pop3_command B_cpp_test_cmd_test;

/*
 * First class.
 * test_cpp_cmd_test: implements the TEST command.
 * get_i: get the value of i
 * set_i: set the value of i
 */

class test_c
{
	int i;

	public:
		CommandResponse *test_cpp_cmd_test ( P3LControl *control, const char *args );
		int get_i ( void ) { return this->i; };
		void set_i ( int j = 0 ) { this->i=j; };
};

/*
 * Second class.
 * test_c_2: constructor, sets i to 128.
 */

class test_c_2 : public test_c
{
	public:
		test_c_2 ( void ) { set_i ( 128 ); };
};

/**
 * test_c::test_cpp_cmd_test: TEST handler
 * @control: the usual control struct
 * @args: unused
 *
 * Tests some very lame C++ stuff. The output is meaningless
 * when one doesn't understand this code.
 *
 * Returns: CommandResponse *
 **/

CommandResponse *
test_c::test_cpp_cmd_test ( P3LControl *control, const char *args )
{
	test_c *test_p;
	test_c_2 *test_2_p;
	char *tmp;

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

	control->send_response ( control, POP3_OK, "C++ Test" );

	test_p = new test_c;
	test_p->set_i ( 254 );

	tmp = g_strdup_printf ( "Class test #1 (should be 254): %d\r\n", test_p->get_i () );

	control->send_raw ( control, tmp, strlen ( tmp ) );

	delete test_p;

	test_2_p = new test_c_2;
	tmp = g_strdup_printf ( "Class test #2 (should be 128): %d\r\n", test_2_p->get_i () );
	control->send_raw ( control, tmp, strlen ( tmp ) );
	delete test_2_p;

	if ( B_cpp_test_cmd_test != NULL )
	{
		control->send_response ( control, POP3_ANSWERED, "." );
		(*B_cpp_test_cmd_test) ( control, args );
	}

	return p3l_respond ( POP3_ANSWERED, "." );
}

/**
 * test_cmd: GLUE code
 * @control: the usual control struct
 * @args: arguments
 *
 * This is a small GLUE code, because I couldn't insert
 * test.test_cpp_cmd_test into the hash table.
 **/

CommandResponse *
test_cmd ( P3LControl *control, char *args )
{
	test_c_2 test;

	return test.test_cpp_cmd_test ( control, args );
}

/**
 * NOTE! Extern "C" { ... } neede here, because if it is not
 * here, the C++ compiler will mangle function names, and
 * libtool won't find [modulename]_LTX_module_init.
 *
 * In the library, without this extern "C" stuff, it'll be called
 * something like cpp_test_LTX_module_init_F123456, and that
 * won't do us any good.
 **/
extern "C" {

int
cpp_test_LTX_module_init ( P3LControl *control )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: init mod-cpp-test",
			       __FILE__, __LINE__ );
#endif

	B_cpp_test_cmd_test = (p3l_pop3_command)
		p3l_command_replace ( control->auth_commands, "TEST", (gpointer) test_cmd );

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

	g_hash_table_insert ( control->auth_commands, g_strdup ( "TEST" ), (gpointer) B_cpp_test_cmd_test );

	return 0;
}

} /** extern "C" **/


syntax highlighted by Code2HTML, v. 0.9.1