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

[email protected] ("Benjamin Schulz") Tue, 01 Apr 2003 19:26:18 -0000
Newsgroups php.embed.cvs
Message-ID <cvsbs1049225178@cvsserver>
--bs1049225178
Content-Type: text/plain

bs		Tue Apr  1 14:26:18 2003 EDT

  Added files:                 
    /embed/php-irssi/examples	linkchan.php 
  Log:
  another sample
  
--bs1049225178
Content-Type: text/plain
Content-Disposition: attachment; filename="bs-20030401142618.txt"


Index: embed/php-irssi/examples/linkchan.php
+++ embed/php-irssi/examples/linkchan.php
<?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.23 2003/04/01 19:24:35 bs Exp $
*/

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;
        
	$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;

		// check if we're on the target net
        if (!$target_server = irssi_server_find_chatnet($_linkchan[$net][$channel][0]))
        	continue;
        	
        // 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));
}
irssi_signal_add('message public', 'linkchan_message_public');


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);
}
irssi_signal_add('message own_public', 'linkchan_message_own_public');


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));
}
irssi_signal_add('message irc action', 'linkchan_message_action');


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;
    
    
}
irssi_signal_add('message irc own_action', 'linkchan_message_own_action');


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));
}
irssi_signal_add('message join', 'linkchan_message_join');


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));
}
irssi_signal_add('message part', 'linkchan_message_part');


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));
}
irssi_signal_add('message topic', 'linkchan_message_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));
}
irssi_signal_add('message kick', 'linkchan_message_kick');

function linkchan_event_mode($server, $data, $nick)
{
	list($channel, $mode) = explode(' ', $data, 2);
    
    if (false === $target = linkchan_get_target(&$server, $channel))
        return;
    
    linkchan_message(&$target, sprintf('(%s) %s sets mode: %s', $server->tag, $nick, $mode));
}
irssi_signal_add('event mode', 'linkchan_event_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));
}
irssi_signal_add('message nick', 'linkchan_message_nick');


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));
}
irssi_signal_add('message own_nick', 'linkchan_message_own_nick');


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));
}
irssi_signal_add('message quit', 'linkchan_message_quit');


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");				

			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');

?>

--bs1049225178--