#include <proto/dos.h>
#include <string.h>
#include <time.h>

BPTR h;
int Done = FALSE;

#define NUMTOPS 7
static char *topictable[ NUMTOPS ] = {
	"Dies ist ein Topic!",
	"Welcome To PLURIBUS ;-)",
	"Mir fällt nix ein...",
	"Das Leben, das Universum und der ganze Chat",
	"42",
	"The Entry Hell",
	"The Entry Hall"

};

#define NUMCH 9
static char *chtable[ NUMCH ] = {
	"MAIN",
	"AMIGA",
	"MAIN",
	"PC",
	"ATARI",
	"C-64",
	"PHILOSOPHIE",
	"GEHEIM",
	"NETSEX"
};

static char mynick[ 40 ] = { "EDDIE" };

#define NUMNICKS 5
static char *nicktable[ NUMCH ] = {
	"Marvin",
	"Eddie",
	"HAL",
	"PCJr",
	"Eddie"
};

static char *seltable( char **table, int mx )
{
	return( table[ rand() % mx ] );
}

static void doaction( void )
{
	switch( rand() % 8 )
	{
		case 0:
			FPrintf( h, "/me gähnt...\n" );
			break;

		case 1:
			FPrintf( h, "/me kratzt sich an der linken, unteren Diode.\n" );
			break;

		case 2:
			FPrintf( h, "/me schaut sich interessiert sein Socket an.\n" );
			break;

		case 3:
			FPrintf( h, "/me stolpert fast über ein verirrtes IP-Paket.\n" );
			break;

		case 4:
			FPrintf( h, "/me rätselt über den Sinn von Segmentregistern.\n" );
			break;

		case 5:
			FPrintf( h, "/topic %s\n", topictable[ rand() % NUMTOPS ] );
			break;

		case 6:
			FPrintf( h, "/join %s\n", chtable[ rand() % NUMCH ] );
			break;

		case 7:
			FPrintf( h, "/nick %s\n", nicktable[ rand() % NUMNICKS ] );
			break;

	}
}

static void handlepriv( char *from, char *cmd, char *rest )
{
	if( !stricmp( cmd, "DO_QUIT" ) )
	{
		FPrintf( h, "/me muss in die Heia!\n" );
		Done = TRUE;
	}
	if( !stricmp( cmd, "DO_KISS" ) )
	{
		FPrintf( h, "/me gibt %s einen gewaltigen Schmatzer!\n", rest );
	}
	if( !stricmp( cmd, "DO_HUG" ) )
	{
		FPrintf( h, "/me klopft %s auf die Schulter!\n", rest );
	}
	if( !stricmp( cmd, "DO_TICKLE" ) )
	{
		FPrintf( h, "/me kitzelt %s an den Füßen!\n", rest );
	}
	if( !stricmp( cmd, "DO_SAY" ) )
	{
		FPrintf( h, "%s\n", rest );
	}
}

static void handleserv( char *from, char *cmd, char *rest )
{
	if( !stricmp( from, "[ENTER]" ) && stricmp( cmd, "EDDIE" ) )
	{
		switch( rand() %5 )
		{
			case 0:
				FPrintf( h, "Hey %s!\n", cmd );
				break;

			case 1:
				FPrintf( h, "Hallo %s!\n", cmd );
				break;

			case 2:
				FPrintf( h, "Herzlich willkommen, %s!\n", cmd );
				break;

			case 3:
				FPrintf( h, "Leute, %s ist da!\n", cmd );
				break;
		}
	}
	if( !stricmp( from, "[EXIT]" ) )
	{
		switch( rand() % 3 )
		{
			case 0:
				FPrintf( h, "Byebye %s!\n", cmd );
				break;

		}
	}
	if( !stricmp( from, "[TOPIC]" ) && stricmp( cmd, mynick ) )
	{
		switch( rand() % 3 )
		{
			case 0:
				FPrintf( h, "Schönes Topic!\n" );
				break;

			case 1:
				FPrintf( h, "Nettes Topic, gefällt mir.\n" );
				break;

			case 2:
				FPrintf( h, "Naja, Du hattest schonmal bessere Einfälle, %s ;-)\n", cmd );
				break;

		}
	}
	if( !stricmp( from, "[KICK]" ) )
	{
		switch( rand() % 3 )
		{
			case 0:
				FPrintf( h, "Sowas aber auch!\n", cmd );
				break;

		}
	}
	if( rest )
	{
		strupr( rest );
		if( !stricmp( from, "[OP]" ) && strstr( rest, mynick ) )
		{
			FPrintf( h, "Danke, %s!\n", cmd );
		}
		if( !stricmp( from, "[DEOP]" ) && strstr( rest, mynick ) )
		{
			FPrintf( h, "Hey, was soll das? Was hab' ich getan?\n", cmd );
		}
	}
}

#define NUMSMILIES 7
static char *smilietable[ NUMSMILIES ] = {
	";-)\n",
	":-)\n",
	":)\n",
	":-}\n",
	":-]\n",
	":(\n",
	":-|\n"
};

#define NOCLUENUM 5
static char *noclues[ NOCLUENUM ] = {
	"Woher soll ich das denn wissen?\n",
	"Bin ich Jesus? Hab ich Latschen an?\n",
	"Beratung DM 50,--/Stunde!\n",
	"Frag' jemand anderen, %s.\n",
	"Schluchz...Wieso denken eigentlich alle, ich wüßte alles, nur weil ich ein Roboter bin???\n"
};

static void handletxt( char *from, char *cmd, char *rest )
{
	char buffer[ 256 ], *p;

	p = strrchr( from, ':' );
	if( p )
		*p = 0;

	sprintf( buffer, "%s %s", cmd, rest );
	strupr( buffer );

	// Eddie kam vor
	if( strstr( buffer, mynick ) )
	{
		if( strstr( buffer, "HALLO" ) || strstr( buffer, "HI" ) || strstr( buffer, "HOI" ) )
			FPrintf( h, "Hallo %s! Wie geht's?\n", from );
		else if( strstr( buffer, "?" ) )
			FPrintf( h, seltable( noclues, NOCLUENUM ), from );
	}
	else if( stricmp( from, mynick ) && strstr( buffer, "?" ) && !( rand() % 7 ) )
	{
		strlwr( buffer );
		FPrintf( h, "Ja genau, %s\n", buffer );
	}
	else if( !( rand() % 26 ) )
	{
		FPrintf( h, seltable( smilietable, NUMSMILIES ) );
	}
}

#define NUMWELCOMES 5
static char *welcometable[ NUMWELCOMES ] = {
	"Hallo Leute!",
	"Na, wie isses?",
	"Hey Kumpels!",
	"Da bin ich wieder!",
	"Hi, wo ist die Party?"
};

void __stdargs __main( void )
{
	char  buffer[ 256 ], *p, *p2;
	int counter = 0;
	int nextaction = 5;

	h = Open( "TCP:pluribus/5555", MODE_NEWFILE );
	if( !h )
		return;

	srand( time( NULL ) );

	FPrintf( h, "Eddie Ihr freundlicher Roboter!\n" );
	Delay( 100 );
	FPrintf( h, "%s\n", seltable( welcometable, NUMWELCOMES ) );

	while( FGets( h, buffer, 255 ) && !Done )
	{
		if( CheckSignal( SIGBREAKF_CTRL_C ) )
			break;

		p = strchr( buffer, '\n' );
		if( p )
			*p = 0;
		p = strchr( buffer, ' ' );
		// Idle
		if( !p )
		{
			if( ( counter++ > nextaction ) )
			{
				doaction();
				nextaction = 3 + ( rand() % 7 );
				counter = 0;
			}
			continue;
		}
		Printf( "* %s\n", buffer );
		*p++ = 0;

		if( buffer[ 0 ] == 2 )
		{
			strcpy( mynick, &buffer[ 1 ] );
			strupr( mynick );
		}

		p2 = strchr( p, ' ' );
		if( p2 )
			*p2++ = 0;

		// Buffer: Prefix, p = Erster String, p2 = Reset		
		if( buffer[ 0 ] == '<' )
			handlepriv( buffer, p, p2 );
		else if( buffer[ 0 ] == '[' )
			handleserv( buffer, p, p2 );
		else handletxt( buffer, p, p2 );
	}

	Close( h );
}
