<?php
/**************************************************
* NZBirc v1
* Copyright (c) 2006 Harry Bragg
* tiberious.org
* Module: base file to be extended
**************************************************
*
* Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
*
* This program 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.
*
* This program 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
*/
class Net_SmartIRC_module_base
{
// default module variable
var $name = 'base';
var $version = 'v0.1';
var $description = 'base module for other modules';
var $author = 'Harry \'tiberious\' Bragg www.tiberious.org';
var $license = 'GPL';
var $actionids = array();
/*
var $_config = array(
'publicCommand' => array( '^base', '^!base' ),
'privateCommand' => array( '^@base' ),
);
*/
var $_commands = array();
/**
* Defines the formatting to irc code operators
*/
var $_format = array(
'from' => array(
'/(?<!_)__(.+?)__(?!_)/i', // underline: __text__
'/(?<!\*)\*\*(.+?)\*\*(?!\*)/i' // bold: **text**
),
'to' => array(
'$1',
'$1'
)
);
function module_init( &$irc )
{
if ( get_class( $this ) == 'net_smartirc_module_base' )
{
return;
}
$this->_configCheck( $this->_config, $irc->config->moduleConf[$this->name] );
if ( isset( $this->_config['publicCommand'] ) )
{
if ( is_array( $this->_config['publicCommand'] ) ) {
foreach( $this->_config['publicCommand'] as $command ) {
$this->actionids[] = $irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL|SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE, $command, $this, 'cmd');
}
}
else {
$this->actionids[] = $irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL|SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE, $this->_config['publicCommand'], $this, 'cmd');
}
}
if ( isset( $this->_config['privateCommand'] ) )
{
if ( is_array( $this->_config['privateCommand'] ) ) {
foreach( $this->_config['privateCommand'] as $command ) {
$this->actionids[] = $irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL|SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE, $command, $this, 'cmdPriv');
}
}
else {
$this->actionids[] = $irc->registerActionhandler(SMARTIRC_TYPE_CHANNEL|SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE, $this->_config['privateCommand'], $this, 'cmdPriv');
}
}
if ( method_exists( $this, '_registerCommands' ) )
{
$this->_registerCommands();
}
}
function module_exit( &$irc )
{
foreach( $this->actionids as $value )
{
$irc->unregisterActionid( $value );
}
if ( method_exists( $this, '_customClose' ) )
{
$this->_customClose();
}
}
function _configCheck( &$conf, &$modConf )
{
if ( is_array( $modConf ) ) {
foreach( $modConf as $id => $value ) {
if ( is_numeric( $id ) )
{
$conf = $modConf;
break;
}
if ( is_array( $value ) )
$this->_configCheck( $conf[$id], $modConf[$id] );
else
$conf[$id] = $value;
}
}
}
/**
* Set an internal handler
*
* @param string $regex - matching regex
* @param object $object - object of the function
* @param string $method - function name
* @return void
* @access private
*/
function _setHandler( $regex, $object, $method )
{
$this->_commands[] = array(
'regex' => $regex,
'object' => $object,
'method' => $method
);
}
/*****************************************************
* Bot functions
*****************************************************/
function cmd( &$irc, &$data, $notice = false )
{
// adminonly check
if ( !$irc->_modules['func']->hasAccess( $irc, $data->nick ) )
{
$irc->_modules['func']->reply( $irc, $data, 'Error: Insufficient privaleges to run this command', false );
return;
}
// check functions
foreach( $this->_commands as $command )
{
if (substr($command['regex'], 0, 1) == '/') {
$regex = $command['regex'];
} else {
$regex = '/'.$command['regex'].'/i';
}
if ( preg_match( $regex, $data->strippedMessage, $trig ) )
{
$irc->modDebug( $this->name, 'actionhandler match found message: "'.$data->strippedMessage.'" regex: "'.$regex.'"', __FILE__, __LINE__);
$methodobject = $command['object'];
$method = $command['method'];
if ( method_exists( $methodobject, $method ) )
{
$irc->modDebug( $this->name, 'calling method: '.get_class( $methodobject ).'->'.$method, __FILE__, __LINE__ );
$data->subMessage = trim( preg_replace( $regex, '', $data->strippedMessage, 1 ) );
$data->trig = $trig;
$methodobject->$method( $irc, $data, $notice );
}
else
{
$irc->modDebug( $this->name, 'method: '.get_class( $methodobject ).'->'.$method.' does not exist', __FILE__, __LINE__ );
}
}
}
}
function cmdPriv( &$irc, &$data )
{
$data->notice = true;
$this->cmd( $irc, $data, true );
}
function _registerCommands()
{
// put: $this->_setHanlder( '^regex', $this, 'method' );
// in here
}
/***********************************
* Template functions
***********************************/
/**
* parse an array into a template
*/
function parseTemplate( &$msg, $name, $vars = array() )
{
global $irc;
$this->_format['from'][] = '/(?<!\{)\{([^}]+)\}(?!\})/ie';
$this->_format['to'][] = '$this->_templateHandler($vars, \'$1\')';
$this->_format['from'][] = '/\{\{([^}]+)\}\}/i';
$this->_format['to'][] = '{$1}';
if ( isset( $this->_template[$name]) )
{
if ( is_array( $this->_template[$name]) )
{
foreach( $this->_template[$name] as $templ )
{
$out[] = preg_replace( $this->_format['from'], $this->_format['to'], $templ );
}
}
else
{
$out[] = preg_replace( $this->_format['from'], $this->_format['to'], $this->_template[$name] );
}
}
$irc->_modules['func']->array_append( $msg, $out );
}
function _templateHandler( $vars, $name )
{
if ( isset( $vars[$name] ) )
{
if ( is_array( $vars[$name] ) )
{
$sep = ( isset( $vars['seperator'] ) )? $vars['seperator']:', ';
foreach( $vars[$name] as $entry )
{
if ( isset( $this->_template[$name] ) )
{
$this->parseTemplate( $tmp, $name, $entry );
}
else
{
$tmp[0] = preg_replace( $this->_format['from'], $this->_format['to'], $entry );
}
$out .= $tmp[0].$sep;
unset( $tmp );
}
$out = substr( $out, 0, -strlen( $sep ) );
return $out;
}
else
{
return preg_replace( $this->_format['from'], $this->_format['to'], $vars[$name] );
}
}
}
/***********************************
* Template functions
***********************************/
function parseHelp( &$msg, $name, $vars )
{
global $irc;
$this->_format['from'][] = '/(?<!\{)\{([^}]+)\}(?!\})/ie';
$this->_format['to'][] = '$this->_templateHandler($vars, \'$1\')';
$this->_format['from'][] = '/\{\{([^}]+)\}\}/i';
$this->_format['to'][] = '{$1}';
if ( isset( $this->_help[$name] ) )
{
$hVar = ( is_array( $this->_help[$name] ) )? $this->_help[$name]:$this->_help[$this->_help[$name]];
$out[] = preg_replace( $this->_format['from'], $this->_format['to'], $hVar['command'] );
if ( is_array( $hVar['longDescription'] ) )
{
foreach( $hVar['longDescription'] as $desc )
{
$out[] = preg_replace( $this->_format['from'], $this->_format['to'], $desc );
}
}
else
{
$out[] = preg_replace( $this->_format['from'], $this->_format['to'], $hVar['longDescription'] );
}
}
$irc->_modules['func']->array_append( $msg, $out );
}
/***********************************
* Proper conversion to something useful
***********************************/
function stringDecode( $string )
{
global $irc;
$str = $string;
$str = preg_replace('~&#x([0-9a-f]+);~ei', '$this->code2utf(hexdec("\\1"))', $str);
$str = preg_replace('~&#([0-9]+);~e', '$this->code2utf("\\1")', $str);
if ( $irc->config->encoding != 'UTF-8' )
{
return mb_convert_encoding( $str, $irc->config->encoding, 'UTF-8' );
}
return $str;
}
function code2utf($number)
{
if ($number < 0)
return FALSE;
if ($number < 128)
return chr($number);
// Removing / Replacing Windows Illegals Characters
if ($number < 160)
{
if ($number==128) $number=8364;
elseif ($number==129) $number=160; // (Rayo:) #129 using no relevant sign, thus, mapped to the saved-space #160
elseif ($number==130) $number=8218;
elseif ($number==131) $number=402;
elseif ($number==132) $number=8222;
elseif ($number==133) $number=8230;
elseif ($number==134) $number=8224;
elseif ($number==135) $number=8225;
elseif ($number==136) $number=710;
elseif ($number==137) $number=8240;
elseif ($number==138) $number=352;
elseif ($number==139) $number=8249;
elseif ($number==140) $number=338;
elseif ($number==141) $number=160; // (Rayo:) #129 using no relevant sign, thus, mapped to the saved-space #160
elseif ($number==142) $number=381;
elseif ($number==143) $number=160; // (Rayo:) #129 using no relevant sign, thus, mapped to the saved-space #160
elseif ($number==144) $number=160; // (Rayo:) #129 using no relevant sign, thus, mapped to the saved-space #160
elseif ($number==145) $number=8216;
elseif ($number==146) $number=8217;
elseif ($number==147) $number=8220;
elseif ($number==148) $number=8221;
elseif ($number==149) $number=8226;
elseif ($number==150) $number=8211;
elseif ($number==151) $number=8212;
elseif ($number==152) $number=732;
elseif ($number==153) $number=8482;
elseif ($number==154) $number=353;
elseif ($number==155) $number=8250;
elseif ($number==156) $number=339;
elseif ($number==157) $number=160; // (Rayo:) #129 using no relevant sign, thus, mapped to the saved-space #160
elseif ($number==158) $number=382;
elseif ($number==159) $number=376;
} //if
if ($number < 2048)
return chr(($number >> 6) + 192) . chr(($number & 63) + 128);
if ($number < 65536)
return chr(($number >> 12) + 224) . chr((($number >> 6) & 63) + 128) . chr(($number & 63) + 128);
if ($number < 2097152)
return chr(($number >> 18) + 240) . chr((($number >> 12) & 63) + 128) . chr((($number >> 6) & 63) + 128) . chr(($number & 63) + 128);
return FALSE;
} //code2utf()
}
?>