/*
** Wallchop plugin
**
** This sample plugin defines a new command /WALLCHOP,
** which writes a message to all operators on a channel,
** and /WALLNOP, which addresses all NON-operators
*/

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/utility.h>
#include <libraries/mui.h>
#include <string.h>

#define BUILDPLUGIN
#include "amirc_plugin.h"

struct amiplug_cmd mycmd = {
	{ NULL, NULL },
	"WALLCHOP",				// name
	"[channel] text",		// template
	1,						// command ID
	1,						// minimum number of args
	TRUE					// needs channel
};

struct amiplug_cmd mycmd2 = {
	{ NULL, NULL },
	"WALLNOP",				// name
	"[channel] text",		// template
	2,						// command ID (not really used here)
	1,						// minimum number of args
	TRUE					// needs channel
};

struct TagItem mytaglist[] = {
	AMIPLUG_Query_Version, 1,
	AMIPLUG_Query_Revision, 4,
	AMIPLUG_Query_Copyright, (ULONG)"Copyright © 1997-98 Oliver Wagner <owagner@vapor.com>",
	AMIPLUG_Query_Infostring, (ULONG)"/WALLCHOP command extension",

	AMIPLUG_Query_CustomCommand, (ULONG)&mycmd,
	AMIPLUG_Query_CustomCommand, (ULONG)&mycmd2,

	TAG_DONE
};


// Setup & Query function
struct TagItem * __asm __saveds AMIPLUG_Setup( void )
{
	return( mytaglist );
}

void __asm AMIPLUG_Cleanup( void )
{
	// just a dummy
}

int __asm AMIPLUG_Hook_Rawline( void )
{
	// just a dummy
	return( 0 );
}

int __asm AMIPLUG_Hook_NumericMsg( void )
{
	// just a dummy
	return( 0 );
}

int __asm AMIPLUG_Hook_DCC( void )
{
	// just a dummy
	return( 0 );
}

void __asm AMIPLUG_DoMenu( void )
{
	// just a dummy
}

//
// Core
//
//	sprintf() replacement
UWORD fmtfunc[] = { 0x16c0, 0x4e75 };
void __stdargs sprintf( char *to, char *fmt, ... )
{
	RawDoFmt( fmt, &fmt + 1, (APTR)fmtfunc, to );
}

void __asm __saveds AMIPLUG_DoCommand(
	register __a0 struct amiplug_functable *ft,
	register __d0 ULONG cmdid,
	register __a1 struct amiplug_cmdparm *parms
)
{
	APTR userlv = ft->amiplug_getuserlv( ft, parms->channelname );
	char buffer[ 1024 ];
	int c;
	int comma = FALSE;
	int cnt = 0, tcnt = 0;

	if( !userlv )
	{
		ft->amiplug_out_defwin( ft, 3, "\033b«WallCHOP»", "Unable to find user list for channel." );
		return;
	}

	strcpy( buffer, "NOTICE " );

	for( c = 0; ; c++ )
	{
		char *u;

		DoMethod( userlv, MUIM_List_GetEntry, c, &u );
		if( !u || cnt > 6 )
		{
			strcat( buffer, cmdid == 2 ? " :[No-@" :" :[@" );
			strcat( buffer, parms->channelname );
			strcat( buffer, "] " );
			strcat( buffer, parms->parms );
			strcat( buffer, "\r\n" );

			if( tcnt )
				ft->amiplug_sendraw( ft, buffer, strlen( buffer ) );
			cnt = 0;
			comma = FALSE;

			strcpy( buffer, "NOTICE " );
		}

		if( !u )
			break;

		if( cmdid == 1 && *u++ != '@' )
			continue; // no OP
		else if( cmdid == 2 && *u == '@' )
			continue; // no OP

		if( comma )
			strcat( buffer, "," );
		else
			comma = TRUE;

		strcat( buffer, u );

		tcnt++;
	}

	sprintf( buffer, "[%ld Recipiants/%s] %s", tcnt, parms->channelname, parms->parms );
		

	ft->amiplug_out_defwin( ft, amirc_tc_local, cmdid == 1 ? "\033b«WallCHOP»" : "\033b«WallNOP»", buffer );
		
}

//
// Let's lower the priority of our libnode to -128 so it
// doesn't get into the way of all other libraries
// in the system which are supposedly opened more often
//

long __saveds __asm __UserLibInit( register __a6 struct Library *libbase )
{
	libbase->lib_Node.ln_Pri = -128;
	return( 0 );
}
