cvs: embed /php-irssi ext-irssi.c php-irssi.h /php-irssi/examples autoop.php

[email protected] ("Wez Furlong") Tue, 01 Apr 2003 13:45:34 -0000
Newsgroups php.embed.cvs
Message-ID <cvswez1049204734@cvsserver>
wez		Tue Apr  1 08:45:34 2003 EDT

  Added files:                 
    /embed/php-irssi/examples	autoop.php 

  Modified files:              
    /embed/php-irssi	ext-irssi.c php-irssi.h 
  Log:
  Add irssi_irc_get_mask(), which will generate a hostmask for a given
  nick/address.
  Add auto-op script example that demonstrates usage of the settings API,
  massjoin signal and the new mask functions.
  
  
  
Index: embed/php-irssi/ext-irssi.c
diff -u embed/php-irssi/ext-irssi.c:1.24 embed/php-irssi/ext-irssi.c:1.25
--- embed/php-irssi/ext-irssi.c:1.24	Tue Apr  1 08:03:22 2003
+++ embed/php-irssi/ext-irssi.c	Tue Apr  1 08:45:34 2003
@@ -14,7 +14,7 @@
   +----------------------------------------------------------------------+
   | Author: Wez Furlong <[email protected]>                                    |
   +----------------------------------------------------------------------+
-  $Id: ext-irssi.c,v 1.24 2003/04/01 13:03:22 tal Exp $
+  $Id: ext-irssi.c,v 1.25 2003/04/01 13:45:34 wez Exp $
 */
 #include "php-irssi.h"
 #include "php-signals-list.h" /* generated by php during make */
@@ -1046,6 +1046,26 @@
 }
 
 
+/* proto string irssi_irc_get_mask(string nick, string address [, long flags])
+   Generate a mask for a nick */
+PHP_FUNCTION(irssi_irc_get_mask)
+{
+	char *nick, *address, *mask;
+	long nicklen, addrlen, flags = IRC_MASK_HOST|IRC_MASK_USER;
+
+	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l",
+				&nick, &nicklen, &address, &addrlen, &flags)) {
+		return;
+	}
+
+	mask = irc_get_mask(nick, address, flags);
+
+	if (mask) {
+		RETVAL_STRING(mask, 1);
+		g_free(mask);
+	}
+}
+
 
 
 static void sig_rec_dtor(void *pDest)
@@ -1119,6 +1139,11 @@
 	REGISTER_LONG_CONSTANT("IRSSI_SIGNAL_PRIORITY_DEFAULT", SIGNAL_PRIORITY_DEFAULT, CONST_CS|CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("IRSSI_SIGNAL_PRIORITY_LOW", SIGNAL_PRIORITY_LOW, CONST_CS|CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("IRSSI_SIGNAL_PRIORITY_HIGH", SIGNAL_PRIORITY_HIGH, CONST_CS|CONST_PERSISTENT);
+	
+	REGISTER_LONG_CONSTANT("IRSSI_IRC_MASK_NICK", IRC_MASK_NICK, CONST_CS|CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("IRSSI_IRC_MASK_USER", IRC_MASK_USER, CONST_CS|CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("IRSSI_IRC_MASK_HOST", IRC_MASK_HOST, CONST_CS|CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("IRSSI_IRC_MASK_DOMAIN", IRC_MASK_DOMAIN, CONST_CS|CONST_PERSISTENT);
 
 	REGISTER_STRING_CONSTANT("PHP_IRSSI_VERSION", PHP_IRSSI_VERSION, CONST_CS|CONST_PERSISTENT);
 	
@@ -1173,6 +1198,7 @@
 	PHP_FE(irssi_settings_set_time, NULL)
 	PHP_FE(irssi_settings_set_level, NULL)
 	PHP_FE(irssi_settings_set_size, NULL)
+	PHP_FE(irssi_irc_get_mask, NULL)
 	{NULL, NULL, NULL}
 };
 
Index: embed/php-irssi/php-irssi.h
diff -u embed/php-irssi/php-irssi.h:1.10 embed/php-irssi/php-irssi.h:1.11
--- embed/php-irssi/php-irssi.h:1.10	Tue Apr  1 07:28:04 2003
+++ embed/php-irssi/php-irssi.h	Tue Apr  1 08:45:34 2003
@@ -14,7 +14,7 @@
   +----------------------------------------------------------------------+
   | Author: Wez Furlong <[email protected]>                                    |
   +----------------------------------------------------------------------+
-  $Id: php-irssi.h,v 1.10 2003/04/01 12:28:04 wez Exp $
+  $Id: php-irssi.h,v 1.11 2003/04/01 13:45:34 wez Exp $
 */
 
 #define MODULE_NAME "php/core"
@@ -26,6 +26,7 @@
 #include "misc.h"
 #include "irc.h"
 #include "irc-servers.h"
+#include "irc-masks.h"
 #include "channels.h"
 #include "nicklist.h"
 #include "masks.h"

Index: embed/php-irssi/examples/autoop.php
+++ embed/php-irssi/examples/autoop.php
<?php
/* $Id: autoop.php,v 1.1 2003/04/01 13:45:34 wez Exp $
 *
 * Autoop.
 *
 * This script demonstrates how to bind to signals to create an auto-op
 * function.  The op details are stored in the irssi configuration so that they
 * will be available if your client quits.
 *
 * Inspired by the perl version in the irssi distro.
 */

class AutoOp {

	/* The autoop command allows you to define who gets ops and where.
	 * Remember to /save after making changes to the oplist.
	 * /autoop add nick
	 */
	function cmd_autoop(/* string */ $params, /* SERVER */ $server, /* CHANNEL */ $channel)
	{
		global $AutoOpList;

		$args = explode(' ', $params);
		
		$channame = $channel->name;

		switch(array_shift($args)) {
			case "add":
				$nick = $channel->nick_find(array_shift($args));
				printf("Adding %s (%s)\n", $nick->nick, $nick->host);
				$AutoOpList[$channame][] = irssi_irc_get_mask($nick->nick, $nick->host);
				break;
			case "list":
				echo "OpList is currently:\n";
				print_r($AutoOpList);
				break;
		}

		/* store */
		irssi_settings_set_str('autoop_oplist', base64_encode(serialize($AutoOpList)));
	}

	function handle_massjoin(/* CHANNEL */ $channel, /* array of NICK */ $nicks)
	{
		global $AutoOpList;

		$masks = array();

		if (!$channel->chanop) {
			/* can't give ops if we don't have it :) */
			return;
		}

		if (isset($AutoOpList[$channel->name])) {
			$masks[] = implode(' ', $AutoOpList[$channel->name]);
		}
		if (isset($AutoOpList['*'])) {
			$masks[] = implode(' ', $AutoOpList['*']);
		}

		$masks = implode(' ', $masks);
		$server = $channel->server;
		$to_op = array();
		
		foreach ($nicks as $nick) {
			if ($server->masks_match($masks, $nick->nick, $nick->host)) {
				$to_op[] = $nick->nick;
			}
		}
		
		if (count($to_op)) {
			$channel->command('op ' . implode(' ', $to_op));
		}
	}

}

irssi_settings_add_str('autoop', 'autoop_oplist', '');
irssi_command_bind('autoop', array('AutoOp', 'cmd_autoop'));
irssi_signal_add('massjoin', array('AutoOp', 'handle_massjoin'), IRSSI_SIGNAL_PRIORITY_LOW);

/* keyed by channel */
$GLOBALS['AutoOpList'] = irssi_settings_get_str('autoop_oplist');

if (strlen($GLOBALS['AutoOpList'])) {
	$GLOBALS['AutoOpList'] = unserialize(base64_decode($GLOBALS['AutoOpList']));
} else {
	$GLOBALS['AutoOpList'] = array();
}
?>