cvs: embed /php-irssi/examples linkchan.php

From: Date: Wed, 02 Apr 2003 20:49:38 +0000
Subject: cvs: embed /php-irssi/examples linkchan.php
Groups: php.embed.cvs 
Request: Send a blank email to [email protected] to get a copy of this message
bs		Wed Apr  2 15:49:38 2003 EDT

  Modified files:              
    /embed/php-irssi/examples	linkchan.php 
  Log:
  made a class out of it to use with namespace construct
  


Index: embed/php-irssi/examples/linkchan.php diff -u embed/php-irssi/examples/linkchan.php:1.7 embed/php-irssi/examples/linkchan.php:1.8 --- embed/php-irssi/examples/linkchan.php:1.7 Tue Apr 1 18:02:59 2003 +++ embed/php-irssi/examples/linkchan.php Wed Apr 2 15:49:38 2003 @@ -1,313 +1,299 @@ <?php -/* - +----------------------------------------------------------------------+ - | links channels over different networks | - | inspired by the perl version in the irssi distro | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2003 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 2.02 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available at through the world-wide-web at | - | http://www.php.net/license/2_02.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | [email protected] so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: Benjamin Schulz <[email protected]> | - +----------------------------------------------------------------------+ - $Id: linkchan.php,v 1.7 2003/04/01 23:02:59 bs Exp $ -*/ +/* $Id: linkchan.php,v 1.8 2003/04/02 20:49:38 bs Exp $ + * + * Links channels over different networks by forwarding messages, actions, + * modes, joins and kick. + * Inspired by linkchan.pl + * + * Usage: + * Load this script, then type /linkchan help + * + * Author: Benjamin Schulz <[email protected]> */ -// uncomment what you don't want to be linked -irssi_signal_add('message public', 'linkchan_message_public', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message own_public', 'linkchan_message_own_public', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message irc action', 'linkchan_message_action', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message irc own_action', 'linkchan_message_own_action', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message join', 'linkchan_message_join', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message part', 'linkchan_message_part', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message topic', 'linkchan_message_topic', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message kick', 'linkchan_message_kick', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('event mode', 'linkchan_event_mode', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message nick', 'linkchan_message_nick', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message own_nick', 'linkchan_message_own_nick', IRSSI_SIGNAL_PRIORITY_LOW); -irssi_signal_add('message quit', 'linkchan_message_quit', IRSSI_SIGNAL_PRIORITY_LOW); +namespace samples { - -define('LINKCHAN_USAGE', -"usage: - /linkchan (help|list) - /linkchan (add|del) from-net from-channel to-net to-channel\n"); - -$_linkchan = array(); -$_linkchan_lock_own = false; - -irssi_settings_add_str('linkchan', 'linkchan_list', base64_encode(serialize(array()))); - -$links = irssi_settings_get_str('linkchan_list'); - -if (strlen($links)) { - $_linkchan = unserialize(base64_decode($links)); -} - -function linkchan_get_target(&$server, $channel) -{ - global $_linkchan; - - if (!isset($_linkchan[$net = strtolower($server->tag)])) - return false; - - if (!isset($_linkchan[$net][$channel = strtolower($channel)])) - return false; - - // check if we're on the target net - if (!$target_server = irssi_server_find_chatnet($_linkchan[$net][$channel][0])) - return false; - - // check if we're on the target channel - if (!$target_server->channel_find($_linkchan[$net][$channel][1])) - return false; - - return array($target_server, $_linkchan[$net][$channel][1]); -} - -function linkchan_get_target_by_nick(&$server, $nick) -{ - global $_linkchan; - - if (!isset($_linkchan[$net = strtolower($server->tag)])) - return false; + class linkchan + { - $targets = array(); - - foreach(array_keys($_linkchan[$net]) as $channel) - { - // check if we're on source channel - if (!$chan = $server->channel_find($channel)) - continue; - - // check if nick is on source channel - if(!$chan->nick_find($nick)) - continue; + var $_links = array(); + var $_lock = false; + var $_usage = "usage:\n/linkchan (help|list)\n/linkchan (add|del) from-net from-channel to-net to-channel\n"; + + function __construct() + { + irssi_settings_add_str('linkchan', 'linkchan', base64_encode(serialize($this->_links))); + + $links = irssi_settings_get_str('linkchan'); + + if (strlen($links)) { + $this->_links = unserialize(base64_decode($links)); + } + + // uncomment what you don't want linked + irssi_signal_add('message public', array($this, 'message_public'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message own_public', array($this, 'message_own_public'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message irc action', array($this, 'message_action'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message irc own_action', array($this, 'message_own_action'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message join', array($this, 'message_join'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message part', array($this, 'message_part'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message topic', array($this, 'message_topic'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message kick', array($this, 'message_kick'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('event mode', array($this, 'event_mode'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message nick', array($this, 'message_nick'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message own_nick', array($this, 'message_own_nick'), IRSSI_SIGNAL_PRIORITY_LOW); + irssi_signal_add('message quit', array($this, 'message_quit'), IRSSI_SIGNAL_PRIORITY_LOW); + + irssi_command_bind('linkchan', array($this, 'cmd')); + } - // check if we're on the target net - if (!$target_server = irssi_server_find_chatnet($_linkchan[$net][$channel][0])) - continue; + function _get_target(&$server, $channel) + { + if (!isset($this->_links[$net = strtolower($server->tag)])) + return false; + + if (!isset($this->_links[$net][$channel = strtolower($channel)])) + return false; + + // check if we're on the target net + if (!$target_server = irssi_server_find_chatnet($this->_links[$net][$channel][0])) + return false; + + // check if we're on the target channel + if (!$target_server->channel_find($this->_links[$net][$channel][1])) + return false; + + return array($target_server, $this->_links[$net][$channel][1]); + } + + function _get_target_by_nick(&$server, $nick) + { + if (!isset($this->_links[$net = strtolower($server->tag)])) + return false; + + $targets = array(); - // check if we're on the target channel - if (!$target_server->channel_find($_linkchan[$net][$channel][1])) - return false; - - $targets[] = array($target_server, $_linkchan[$net][$channel][1]); - } - return (count($targets) > 0 ? $targets : false); -} - -function linkchan_message(&$target, $msg) -{ - global $_linkchan_lock_own; - - $_linkchan_lock_own = true; - $target[0]->privmsg($target[1], $msg, IRSSI_SEND_TARGET_CHANNEL); - $_linkchan_lock_own = false; -} - -function linkchan_raw_message(&$server, $msg) -{ - global $_linkchan_lock_own; - - $_linkchan_lock_own = true; - $server->send_cmd($msg); - $_linkchan_lock_own = false; -} - -function linkchan_message_public($server, $msg, $nick, $address, $channel) -{ - if (false === $target = linkchan_get_target(&$server, $channel)) - return; - - linkchan_message(&$target, sprintf('(%s) <%s> %s', $server->tag, $nick, $msg)); -} - - -function linkchan_message_own_public($server, $msg, $channel) -{ - global $_linkchan_lock_own; - - if (true === $_linkchan_lock_own) - return; - - if (false === $target = linkchan_get_target(&$server, $channel)) - return; - - linkchan_message(&$target,$msg); -} - - -function linkchan_message_action($server, $msg, $nick, $address, $channel) -{ - if (false === $target = linkchan_get_target(&$server, $channel)) - return; - - linkchan_message(&$target, sprintf('(%s) * %s %s', $server->tag, $nick, $msg)); -} - - -function linkchan_message_own_action($server, $msg, $channel) -{ - global $_linkchan_lock_own; - - if (true === $_linkchan_lock_own) - return; - - if (false === $target = linkchan_get_target(&$server, $channel)) - return; - - linkchan_raw_message(&$target[0], sprintf("PRIVMSG %s :\001ACTION %s\001", $target[1], $msg)); - - $_linkchan_lock_own = true; - irssi_signal_emit("message irc own_action", $target[0], $msg, $target[1]); - $_linkchan_lock_own = false; -} - -function linkchan_message_join($server, $channel, $nick, $address) -{ - if (false === $target = linkchan_get_target(&$server, $channel)) - return; - - linkchan_message(&$target, sprintf('(%s) %s [%s] has joined %s', $server->tag, $nick, $address, $channel)); -} - -function linkchan_message_part($server, $channel, $nick, $address, $reason) -{ - if (false === $target = linkchan_get_target(&$server, $channel)) - return; - - linkchan_message(&$target, sprintf('(%s) %s [%s] has left %s [%s]', $server->tag, $nick, $address, $channel, $reason)); -} - -function linkchan_message_topic($server, $channel, $topic, $nick, $address) -{ - if (false === $target = linkchan_get_target(&$server, $channel)) - return; - - linkchan_message(&$target, sprintf('(%s) %s changed the topic of %s to: %s', $server->tag, $nick, $channel, $topic)); -} - -function linkchan_message_kick($server, $channel, $nick, $kicker, $address, $reason) -{ - if (false === $target = linkchan_get_target(&$server, $channel)) - return; - - linkchan_message(&$target, sprintf('(%s) %s was kicked from %s by %s [%s]', $server->tag, $nick, $channel, $kicker, $reason)); -} - -function linkchan_event_mode($server, $data, $nick) -{ - list($channel, $mode) = explode(' ', $data, 2); - - if (false === $target = linkchan_get_target(&$server, $channel)) - return; + foreach(array_keys($this->_links[$net]) as $channel) + { + // check if we're on source channel + if (!$chan = $server->channel_find($channel)) + continue; + + // check if nick is on source channel + if(!$chan->nick_find($nick)) + continue; + + // check if we're on the target net + if (!$target_server = irssi_server_find_chatnet($this->_links[$net][$channel][0])) + continue; + + // check if we're on the target channel + if (!$target_server->channel_find($this->_links[$net][$channel][1])) + return false; + + $targets[] = array($target_server, $this->_links[$net][$channel][1]); + } + return (count($targets) > 0 ? $targets : false); + } + + function _message(&$target, $msg) + { + $this->_lock = true; + $target[0]->privmsg($target[1], $msg, IRSSI_SEND_TARGET_CHANNEL); + $this->_lock = false; + } + + function _raw_message(&$server, $msg) + { + $this->_lock = true; + $server->send_cmd($msg); + $this->_lock = false; + } + + function message_public($server, $msg, $nick, $address, $channel) + { + if (false === $target = $this->_get_target(&$server, $channel)) + return; + + $this->_message(&$target, sprintf('(%s) <%s> %s', $server->tag, $nick, $msg)); + } + + + function message_own_public($server, $msg, $channel) + { + if (true === $this->_lock) + return; + + if (false === $target = $this->_get_target(&$server, $channel)) + return; + + $this->_message(&$target,$msg); + } + + + function message_action($server, $msg, $nick, $address, $channel) + { + if (false === $target = $this->_get_target(&$server, $channel)) + return; + + $this->_message(&$target, sprintf('(%s) * %s %s', $server->tag, $nick, $msg)); + } + + + function message_own_action($server, $msg, $channel) + { + if (true === $this->_lock) + return; + + if (false === $target = $this->_get_target(&$server, $channel)) + return; + + $this->_raw_message(&$target[0], sprintf("PRIVMSG %s :\001ACTION %s\001", $target[1], $msg)); + + $this->_lock = true; + irssi_signal_emit("$this->_message irc own_action", $target[0], $msg, $target[1]); + $this->_lock = false; + } + + function message_join($server, $channel, $nick, $address) + { + if (false === $target = $this->_get_target(&$server, $channel)) + return; + + $this->_message(&$target, sprintf('(%s) %s [%s] has joined %s', $server->tag, $nick, $address, $channel)); + } + + function message_part($server, $channel, $nick, $address, $reason) + { + if (false === $target = $this->_get_target(&$server, $channel)) + return; + + $this->_message(&$target, sprintf('(%s) %s [%s] has left %s [%s]', $server->tag, $nick, $address, $channel, $reason)); + } + + function message_topic($server, $channel, $topic, $nick, $address) + { + if (false === $target = $this->_get_target(&$server, $channel)) + return; + + $this->_message(&$target, sprintf('(%s) %s changed the topic of %s to: %s', $server->tag, $nick, $channel, $topic)); + } + + function message_kick($server, $channel, $nick, $kicker, $address, $reason) + { + if (false === $target = $this->_get_target(&$server, $channel)) + return; + + $this->_message(&$target, sprintf('(%s) %s was kicked from %s by %s [%s]', $server->tag, $nick, $channel, $kicker, $reason)); + } + + function event_mode($server, $data, $nick) + { + list($channel, $mode) = explode(' ', $data, 2); + + if (false === $target = $this->_get_target(&$server, $channel)) + return; + + $this->_message(&$target, sprintf('(%s) %s sets mode: %s', $server->tag, $nick, $mode)); + } + + function message_nick($server, $newnick, $oldnick, $address) + { + if (false === $targets = $this->_get_target_by_nick(&$server, $newnick)) + return; + + foreach($targets AS $target) + $this->_message(&$target, sprintf('(%s) %s is now known as %s', $server->tag, $oldnick, $newnick)); + } + + function message_own_nick($server, $newnick, $oldnick, $address) + { + if (false === $targets = $this->_get_target_by_nick(&$server, $newnick)) + return; + + foreach($targets AS $target) + $this->_message(&$target, sprintf('(%s) %s is now known as %s', $server->tag, $oldnick, $newnick)); + } + + function message_quit($server, $nick, $address, $reason) + { + if (false === $targets = $this->_get_target_by_nick(&$server, $nick)) + return; + + foreach($targets AS $target) + $this->_message(&$target, sprintf('(%s) %s [%s] has quit [%s]', $server->tag, $nick, $address, $reason)); + } + + function cmd($params) + { + if ('' === $params = trim($params)) + $args = array('help'); + else + $args = explode(' ', $params); + + foreach($args AS $k => $v) + $args[$k] = strtolower($v); + + switch(array_shift($args)) + { + case 'add': + if (count($args) !== 4) { + echo $this->_usage; + return; + } + + $this->_links[ $args[0] ][ $args[1] ] = array($args[2],$args[3]); + echo "added link from $args[0]/$args[1] to $args[2]/$args[3]\n"; + break; + + case 'del': + if (count($args) < 2) { + echo $this->_usage; + return; + } + + if(!isset($this->_links[ $args[0] ][ $args[1] ]) || !isset($this->_links[ $args[0] ][ $args[1] ])) + return print("link $args[0]/$args[1]not found\n"); + + printf("deleting link %s/%s to %s/%s\n", + $args[0], + $args[1], + $this->_links[ $args[0] ][ $args[1] ][0], + $this->_links[ $args[0] ][ $args[1] ][1]); + + unset($this->_links[ $args[0] ][ $args[1] ]); + + if (count($this->_links[ $args[0] ]) === 0) + unset($this->_links[ $args[0] ][ $args[1] ]); + + break; + + case 'list': + if (count($this->_links) < 1) + return print("no links set\n"); + + echo "linked chans:\n"; + foreach($this->_links AS $from_net => $links) + { + foreach ($links AS $from_chan => $to) + { + echo "$from_net/$from_chan => $to[0]/$to[1]\n"; + } + } + return; + + default: + case 'help': + echo $this->_usage; + return; + } + irssi_settings_set_str('linkchan', base64_encode(serialize($this->_links))); + + } - linkchan_message(&$target, sprintf('(%s) %s sets mode: %s', $server->tag, $nick, $mode)); -} - -function linkchan_message_nick($server, $newnick, $oldnick, $address) -{ - if (false === $targets = linkchan_get_target_by_nick(&$server, $newnick)) - return; - - foreach($targets AS $target) - linkchan_message(&$target, sprintf('(%s) %s is now known as %s', $server->tag, $oldnick, $newnick)); -} - -function linkchan_message_own_nick($server, $newnick, $oldnick, $address) -{ - if (false === $targets = linkchan_get_target_by_nick(&$server, $newnick)) - return; - - foreach($targets AS $target) - linkchan_message(&$target, sprintf('(%s) %s is now known as %s', $server->tag, $oldnick, $newnick)); -} - -function linkchan_message_quit($server, $nick, $address, $reason) -{ - if (false === $targets = linkchan_get_target_by_nick(&$server, $nick)) - return; - - foreach($targets AS $target) - linkchan_message(&$target, sprintf('(%s) %s [%s] has quit [%s]', $server->tag, $nick, $address, $reason)); -} - -function linkchan_cmd($params) -{ - global $_linkchan; - - if ('' === $params = trim($params)) - $args = array('help'); - else - $args = explode(' ', $params); - - foreach($args AS $k => $v) - $args[$k] = strtolower($v); - - switch(array_shift($args)) - { - case 'add': - if (count($args) !== 4) { - echo LINKCHAN_USAGE; - return; - } - - $_linkchan[ $args[0] ][ $args[1] ] = array($args[2],$args[3]); - echo "added link from $args[0]/$args[1] to $args[2]/$args[3]\n"; - break; - - case 'del': - if (count($args) < 2) { - echo LINKCHAN_USAGE; - return; - } - - if(!isset($_linkchan[ $args[0] ][ $args[1] ]) || !isset($_linkchan[ $args[0] ][ $args[1] ])) - return print("link $args[0]/$args[1]not found\n"); - - printf("deleting link %s/%s to %s/%s\n", - $args[0], - $args[1], - $_linkchan[ $args[0] ][ $args[1] ][0], - $_linkchan[ $args[0] ][ $args[1] ][1]); - - unset($_linkchan[ $args[0] ][ $args[1] ]); - - if (count($_linkchan[ $args[0] ]) === 0) - unset($_linkchan[ $args[0] ][ $args[1] ]); - - break; - - case 'list': - if (count($_linkchan) < 1) - return print("no links set\n"); - - echo "linked chans:\n"; - foreach($_linkchan AS $from_net => $links) - { - foreach ($links AS $from_chan => $to) - { - echo "$from_net/$from_chan => $to[0]/$to[1]\n"; - } - } - return; + } - default: - case 'help': - echo LINKCHAN_USAGE; - return; - } - irssi_settings_set_str('linkchan_list', base64_encode(serialize($_linkchan))); - } -irssi_command_bind('linkchan', 'linkchan_cmd'); +new samples::linkchan; -?> +?> \ No newline at end of file

Thread (1 message)

  • Benjamin Schulz
« previous php.embed.cvs (#78) next »