Commands can now be whitelisted to allow response to the channel

This commit is contained in:
Luke Fitzgerald 2010-08-07 13:25:05 -07:00
parent a6e865c47a
commit e23ad0bdb3
3 changed files with 126 additions and 6 deletions

View File

@ -0,0 +1,47 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Override the IMChannel class to allow commands to send messages
* to a channel instead of PMing a user
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Network
* @package StatusNet
* @author Luke Fitzgerald <lw.fitzgerald@googlemail.com>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
class ChannelResponseChannel extends IMChannel {
protected $ircChannel;
public function __construct($imPlugin, $ircChannel) {
$this->ircChannel = $ircChannel;
parent::__construct($imPlugin);
}
public function output($user, $text) {
$text = $user->nickname.': ['.common_config('site', 'name') . '] ' . $text;
$this->imPlugin->send_message($this->ircChannel, $text);
}
}

View File

@ -66,6 +66,7 @@ class IrcPlugin extends ImPlugin {
public $regregexp = null;
public $transport = 'irc';
public $whiteList;
public $fake_irc;
/**
@ -125,6 +126,7 @@ class IrcPlugin extends ImPlugin {
include_once $dir . '/'.strtolower($cls).'.php';
return false;
case 'Fake_Irc':
case 'ChannelResponseChannel':
include_once $dir . '/'. $cls .'.php';
return false;
default:
@ -181,11 +183,12 @@ class IrcPlugin extends ImPlugin {
public function receive_raw_message($data) {
if (strpos($data['source'], '#') === 0) {
$message = $data['message'];
$nickpos = strpos($message, $this->nick);
$nicklen = strlen($this->nick);
$colonpos = strpos($message, ':', $nicklen);
if ($nickpos === 0 && $colonpos == $nicklen) {
$this->handle_incoming($data['sender'], substr($message, $colonpos+1));
$parts = explode(' ', $message, 2);
$command = $parts[0];
if (in_array($command, $this->whiteList)) {
$this->handle_channel_incoming($data['sender'], $data['source'], $message);
} else {
$this->handle_incoming($data['sender'], $message);
}
} else {
$this->handle_incoming($data['sender'], $data['message']);
@ -193,6 +196,59 @@ class IrcPlugin extends ImPlugin {
return true;
}
protected function handle_channel_incoming($nick, $channel, $notice_text) {
$user = $this->get_user($nick);
// For common_current_user to work
global $_cur;
$_cur = $user;
if (!$user) {
$this->send_from_site($nick, 'Unknown user; go to ' .
common_local_url('imsettings') .
' to add your address to your account');
common_log(LOG_WARNING, 'Message from unknown user ' . $nick);
return;
}
if ($this->handle_channel_command($user, $channel, $notice_text)) {
common_log(LOG_INFO, "Command message by $nick handled.");
return;
} else if ($this->is_autoreply($notice_text)) {
common_log(LOG_INFO, 'Ignoring auto reply from ' . $nick);
return;
} else if ($this->is_otr($notice_text)) {
common_log(LOG_INFO, 'Ignoring OTR from ' . $nick);
return;
} else {
common_log(LOG_INFO, 'Posting a notice from ' . $user->nickname);
$this->add_notice($nick, $user, $notice_text);
}
$user->free();
unset($user);
unset($_cur);
unset($message);
}
/**
* Attempt to handle a message from a channel as a command
*
* @param User $user user the message is from
* @param string $channel Channel the message originated from
* @param string $body message text
* @return boolean true if the message was a command and was executed, false if it was not a command
*/
protected function handle_channel_command($user, $channel, $body) {
$inter = new CommandInterpreter();
$cmd = $inter->handle_command($user, $body);
if ($cmd) {
$chan = new ChannelResponseChannel($this, $channel);
$cmd->execute($chan);
return true;
} else {
return false;
}
}
/**
* Send a confirmation code to a user
*
@ -277,6 +333,12 @@ class IrcPlugin extends ImPlugin {
}
$this->fake_irc = new Fake_Irc;
/*
* Commands allowed to return output to a channel
*/
$this->whiteList = array('stats', 'last', 'get');
return true;
}

View File

@ -75,9 +75,20 @@ class Phergie_Plugin_Statusnet extends Phergie_Plugin_Abstract {
$sender = $event->getNick();
$message = trim($event->getText());
if (strpos($source, '#') === 0) {
$botNick = $this->getConnection()->getNick();
$nickPos = strpos($message, $botNick);
$nickLen = strlen($botNick);
$colonPos = strpos($message, ':', $nickLen);
$commandStr = trim(substr($message, $colonPos+1));
if ($nickPos === 0 && $colonPos == $nickLen && !empty($commandStr)) {
call_user_func($this->messageCallback, array('source' => $source, 'sender' => $sender, 'message' => $commandStr));
}
} else {
call_user_func($this->messageCallback, array('source' => $source, 'sender' => $sender, 'message' => $message));
}
}
}
/**
* Catches the response from NickServ