[NeoStats-Devel] [Commits] r2604 - in trunk: modules/limitserv src

[email protected]
Newsgroups gmane.comp.neostats.devel
Message-ID <[email protected]>
Author: Mark
Date: Sat May 21 04:55:30 2005
New Revision: 2604

Modified:
   trunk/modules/limitserv/help.c
   trunk/modules/limitserv/limitserv.h
   trunk/modules/limitserv/main.c
   trunk/src/ircsend.c
Log:
add join capability and limit setting capability to limitserv

Modified: trunk/modules/limitserv/help.c
==============================================================================
--- trunk/modules/limitserv/help.c	(original)
+++ trunk/modules/limitserv/help.c	Sat May 21 04:55:30 2005
@@ -25,15 +25,15 @@
 
 const char *ls_about[] = 
 {
-	"\2TextServ\2 is a text messaging service",
+	"\2LimitServ\2 manages channel limit settings",
 	NULL
 };
 
-const char ls_help_add_oneline[] ="Add a channel";
-const char ls_help_del_oneline[] ="Delete a channel";
-const char ls_help_list_oneline[] ="List channels";
+const char help_add_oneline[] ="Add a channel";
+const char help_del_oneline[] ="Delete a channel";
+const char help_list_oneline[] ="List channels";
 
-const char *ls_help_add[] = {
+const char *help_add[] = {
 	"Syntax: \2ADD <channel>\2",
 	"",
 	"Register a channel with limitserv.",
@@ -41,16 +41,22 @@
 	NULL
 };
 
-const char *ls_help_del[] = {
+const char *help_del[] = {
 	"Syntax: \2DEL <channel>\2",
 	"",
 	"Delete a channel.",
 	NULL
 };
 
-const char *ls_help_list[] = {
+const char *help_list[] = {
 	"Syntax: \2LIST\2",
 	"",
 	"Lists channels.",
 	NULL
 };
+
+const char *help_set_join[] = {
+	"\2JOIN <ON|OFF>\2",
+	"Whether to join channels managed by limitserv",
+	NULL
+};

Modified: trunk/modules/limitserv/limitserv.h
==============================================================================
--- trunk/modules/limitserv/limitserv.h	(original)
+++ trunk/modules/limitserv/limitserv.h	Sat May 21 04:55:30 2005
@@ -23,10 +23,12 @@
 
 extern const char *ls_about[];
 
-extern const char ls_help_add_oneline[];
-extern const char ls_help_del_oneline[];
-extern const char ls_help_list_oneline[];
+extern const char help_add_oneline[];
+extern const char help_del_oneline[];
+extern const char help_list_oneline[];
 
-extern const char *ls_help_add[];
-extern const char *ls_help_del[];
-extern const char *ls_help_list[];
+extern const char *help_add[];
+extern const char *help_del[];
+extern const char *help_list[];
+
+extern const char *help_set_join[];

Modified: trunk/modules/limitserv/main.c
==============================================================================
--- trunk/modules/limitserv/main.c	(original)
+++ trunk/modules/limitserv/main.c	Sat May 21 04:55:30 2005
@@ -28,10 +28,20 @@
 	char name[MAXCHANLEN];
 }ls_channel;
 
+int joinchannels = 0;
+
 /** Bot command function prototypes */
-static int ls_cmd_add( CmdParams *cmdparams );
-static int ls_cmd_list( CmdParams *cmdparams );
-static int ls_cmd_del( CmdParams *cmdparams );
+static int cmd_add( CmdParams *cmdparams );
+static int cmd_list( CmdParams *cmdparams );
+static int cmd_del( CmdParams *cmdparams );
+
+/** Event function prototypes */
+static int event_join( CmdParams *cmdparams );
+static int event_part( CmdParams *cmdparams );
+
+/** Setting callback prototypes */
+static int set_join_cb( CmdParams* cmdparams, SET_REASON reason );
+
 
 /** hash to store ls_channel and bot info */
 static hash_t *qshash;
@@ -63,19 +73,20 @@
 /** Bot comand table */
 static bot_cmd ls_commands[]=
 {
-	{"ADD",		ls_cmd_add,		1,	NS_ULEVEL_ADMIN,	ls_help_add,	ls_help_add_oneline },
-	{"DEL",		ls_cmd_del,		1, 	NS_ULEVEL_ADMIN,	ls_help_del,	ls_help_del_oneline },
-	{"LIST",	ls_cmd_list,	0, 	NS_ULEVEL_ADMIN,	ls_help_list,	ls_help_list_oneline },
-	{NULL,		NULL,			0, 	0,					NULL, 			NULL}
+	{"ADD",		cmd_add,	1,	NS_ULEVEL_ADMIN,	help_add,	help_add_oneline },
+	{"DEL",		cmd_del,	1, 	NS_ULEVEL_ADMIN,	help_del,	help_del_oneline },
+	{"LIST",	cmd_list,	0, 	NS_ULEVEL_ADMIN,	help_list,	help_list_oneline },
+	{NULL,		NULL,		0, 	0,					NULL, 			NULL}
 };
 
 /** Bot setting table */
 static bot_setting ls_settings[]=
 {
-	{NULL,		NULL,			0,			0, 0, 		0,				 NULL,		NULL,			NULL	},
+	{"JOIN",	&joinchannels,	SET_TYPE_BOOLEAN,	0, 0, 		NS_ULEVEL_ADMIN, NULL,	help_set_join,	set_join_cb,	( void* )0	},
+	{NULL,		NULL,			0,					0, 0, 		0,				 NULL,	NULL,			NULL	},
 };
 
-/** TextServ BotInfo */
+/** Bot info */
 static BotInfo ls_botinfo = 
 {
 	"LimitServ", 
@@ -85,10 +96,98 @@
 	"Limit service",
 	BOT_FLAG_SERVICEBOT|BOT_FLAG_DEAF, 
 	ls_commands, 
-	NULL,
+	ls_settings,
 };
 
-/** @brief load_ls_channel
+/** Module Events */
+ModuleEvent module_events[] = 
+{
+	{EVENT_JOIN,	event_join},
+	{EVENT_PART,	event_part},
+	{EVENT_NULL,	NULL}
+};
+
+/** @brief ManageLimit
+ *
+ *  manage channel limit
+ *
+ *  @param none
+ *
+ *  @return none
+ */
+
+static void ManageLimit( char *name, int users, int curlimit, int add )
+{
+	static char limitsize[10];
+	int limit;
+
+	limit = curlimit;
+	if( limit < users )
+		limit = users;
+	if( add )
+		limit++;
+	else
+		limit--;
+	ircsnprintf( limitsize, 10, "%d", limit );	
+	irc_cmode( ls_bot, name, "+l", limitsize );
+}
+
+/** @brief JoinChannels
+ *
+ *  join channels
+ *
+ *  @param none
+ *
+ *  @return none
+ */
+
+static void JoinChannels( void )
+{
+	ls_channel *db;
+	hnode_t *hn;
+	hscan_t hs;
+
+	hash_scan_begin( &hs, qshash );
+	while( ( hn = hash_scan_next( &hs ) ) != NULL ) 
+	{
+		Channel *c;
+
+		db =( ( ls_channel * )hnode_get( hn ) );
+		c = FindChannel( db->name );
+		if( c )
+		{
+			if( joinchannels )
+				irc_join (ls_bot, db->name, "+o");
+			ManageLimit( db->name, c->users, 0, 1 );
+		}
+	}
+}
+
+/** @brief PartChannels
+ *
+ *  part channels
+ *
+ *  @param none
+ *
+ *  @return none
+ */
+
+static void PartChannels( void )
+{
+	ls_channel *db;
+	hnode_t *hn;
+	hscan_t hs;
+
+	hash_scan_begin( &hs, qshash );
+	while( ( hn = hash_scan_next( &hs ) ) != NULL ) 
+	{
+		db =( ( ls_channel * )hnode_get( hn ) );
+		if( FindChannel( db->name ) )
+			irc_part( ls_bot, db->name, NULL);
+	}
+}
+
+/** @brief LoadChannel
  *
  *  load ls_channel
  *
@@ -97,7 +196,7 @@
  *  @return none
  */
 
-static int load_ls_channel( void *data, int size )
+static int LoadChannel( void *data, int size )
 {
 	ls_channel *db;
 
@@ -123,7 +222,7 @@
 		nlog( LOG_CRITICAL, "Unable to create ls_channel hash" );
 		return -1;
 	}
-	DBAFetchRows( "channels", load_ls_channel );
+	DBAFetchRows( "channels", LoadChannel );
 	ModuleConfig( ls_settings );
 	return NS_SUCCESS;
 }
@@ -142,9 +241,8 @@
 {
 	ls_bot = AddBot( &ls_botinfo );
 	if( !ls_bot ) 
-	{
 		return NS_FAILURE;
-	}
+	JoinChannels();
 	return NS_SUCCESS;
 }
 
@@ -165,7 +263,8 @@
 
 	SET_SEGV_LOCATION();
 	hash_scan_begin( &hs, qshash );
-	while( ( hn = hash_scan_next( &hs ) ) != NULL ) {
+	while( ( hn = hash_scan_next( &hs ) ) != NULL )
+	{
 		db =( ( ls_channel * )hnode_get( hn ) );
 		hash_delete( qshash, hn );
 		hnode_destroy( hn );
@@ -175,7 +274,7 @@
 	return NS_SUCCESS;
 }
 
-/** @brief ls_cmd_add
+/** @brief cmd_add
  *
  *  Command handler for ADD
  *
@@ -185,7 +284,7 @@
  *  @return NS_SUCCESS if succeeds, else NS_FAILURE
  */
 
-static int ls_cmd_add( CmdParams *cmdparams )
+static int cmd_add( CmdParams *cmdparams )
 {
 	ls_channel *db;
 
@@ -200,10 +299,14 @@
 	strlcpy( db->name, cmdparams->av[0], MAXCHANLEN );
 	hnode_create_insert( qshash, db, db->name );
 	DBAStore( "channels", db->name,( void * )db->name, MAXCHANLEN );
+	CommandReport( ls_bot, "%s added %s to the channel list",
+		cmdparams->source->name, cmdparams->av[0] );
+	nlog( LOG_NOTICE, "%s added %s to the channel list",
+		cmdparams->source->name, cmdparams->av[0] );
 	return NS_SUCCESS;
 }
 
-/** @brief ls_cmd_list
+/** @brief cmd_list
  *
  *  Command handler for LIST
  *
@@ -212,30 +315,30 @@
  *  @return NS_SUCCESS if succeeds, else NS_FAILURE
  */
 
-static int ls_cmd_list( CmdParams *cmdparams )
+static int cmd_list( CmdParams *cmdparams )
 {
 	ls_channel *db;
 	hnode_t *hn;
 	hscan_t hs;
-	int i = 1;
 
 	SET_SEGV_LOCATION();
-	if( hash_count( qshash ) == 0 ) {
+	if( hash_count( qshash ) == 0 )
+	{
 		irc_prefmsg( ls_bot, cmdparams->source, "No channels are defined." );
 		return NS_SUCCESS;
 	}
 	hash_scan_begin( &hs, qshash );
 	irc_prefmsg( ls_bot, cmdparams->source, "channels" );
-	while( ( hn = hash_scan_next( &hs ) ) != NULL ) {
+	while( ( hn = hash_scan_next( &hs ) ) != NULL )
+	{
 		db =( ( ls_channel * )hnode_get( hn ) );
-		irc_prefmsg( ls_bot, cmdparams->source, "%d - %s", i, db->name );
-		i++;
+		irc_prefmsg( ls_bot, cmdparams->source, "%s", db->name );
 	}
 	irc_prefmsg( ls_bot, cmdparams->source, "End of list." );
 	return NS_SUCCESS;
 }
 
-/** @brief ls_cmd_del
+/** @brief cmd_del
  *
  *  Command handler for DEL
  *    cmdparams->av[0] = ls_channel to delete
@@ -245,7 +348,7 @@
  *  @return NS_SUCCESS if succeeds, else NS_FAILURE
  */
 
-static int ls_cmd_del( CmdParams *cmdparams )
+static int cmd_del( CmdParams *cmdparams )
 {
 	ls_channel *db;
 	hnode_t *hn;
@@ -253,9 +356,11 @@
 
 	SET_SEGV_LOCATION();
 	hash_scan_begin( &hs, qshash );
-	while( ( hn = hash_scan_next( &hs ) ) != NULL ) {
+	while( ( hn = hash_scan_next( &hs ) ) != NULL )
+	{
 		db =( ls_channel * )hnode_get( hn );
-		if( ircstrcasecmp( db->name, cmdparams->av[0] ) == 0 ) {
+		if( ircstrcasecmp( db->name, cmdparams->av[0] ) == 0 )
+		{
 			hash_scan_delete( qshash, hn );
 			irc_prefmsg( ls_bot, cmdparams->source, 
 				"Deleted %s from the channel list", cmdparams->av[0] );
@@ -272,3 +377,72 @@
 	irc_prefmsg( ls_bot, cmdparams->source, "No entry for %s", cmdparams->av[0] );
 	return NS_SUCCESS;
 }
+
+/** @brief event_join
+ *
+ *  join event handler
+ *  join channels if we need to and manage limit on channels
+ *
+ *  @cmdparams pointer to commands param struct
+ *
+ *  @return NS_SUCCESS if suceeds else NS_FAILURE
+ */
+
+static int event_join( CmdParams *cmdparams )
+{
+	ls_channel *db;
+
+	SET_SEGV_LOCATION();
+	db = (ls_channel *)hnode_find( qshash, cmdparams->channel->name );
+	if( db )
+	{
+		/* Join channel if we are not a member */
+		if( joinchannels && !IsChannelMember( cmdparams->channel, ls_bot->u ) )
+			irc_join( ls_bot, db->name, "+o" );
+		ManageLimit( cmdparams->channel->name, cmdparams->channel->limit, cmdparams->channel->users, 1 );
+	}
+	return NS_SUCCESS;
+}
+
+/** @brief event_part
+ *
+ *  part event handler
+ *  manage limit on channels
+ *
+ *  @cmdparams pointer to commands param struct
+ *
+ *  @return NS_SUCCESS if suceeds else NS_FAILURE
+ */
+
+static int event_part( CmdParams *cmdparams )
+{
+	ls_channel *db;
+
+	SET_SEGV_LOCATION();
+	db = (ls_channel *)hnode_find( qshash, cmdparams->channel->name );
+	if( db )
+		ManageLimit( cmdparams->channel->name, cmdparams->channel->limit, cmdparams->channel->users, 0 );
+	return NS_SUCCESS;
+}
+
+/** @brief set_join_cb
+ *
+ *  SET JOIN callback
+ *
+ *  @param cmdparams
+ *  @param reason
+ *
+ *  @return NS_SUCCESS 
+ */
+
+static int set_join_cb( CmdParams* cmdparams, SET_REASON reason )
+{
+	if( reason == SET_CHANGE )
+	{
+		if( joinchannels )
+			JoinChannels();
+		else
+			PartChannels();
+	}
+	return NS_SUCCESS;
+}
\ No newline at end of file

Modified: trunk/src/ircsend.c
==============================================================================
--- trunk/src/ircsend.c	(original)
+++ trunk/src/ircsend.c	Sat May 21 04:55:30 2005
@@ -396,7 +396,8 @@
 	if( ircd_srv.protocol & PROTOCOL_P10 )
 		send_cmd( "%s %s %s %s %s %lu", me.s->name64, TOK_MODE, chan, mode, args, ts );
 	else
-		send_cmd( ":%s %s %s %s %s %lu", sourceserver, MSGTOK( MODE ), chan, mode, args, ts );
+		/* TS of 0 forces the ircd to set the mode */
+		send_cmd( ":%s %s %s %s %s 0", sourceserver, MSGTOK( MODE ), chan, mode, args );
 }
 
 static void _send_quit( const char *source, const char *quitmsg )
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.