From e23ad0bdb34418161b46d4b15a5306ceb6a8a5a1 Mon Sep 17 00:00:00 2001 From: Luke Fitzgerald Date: Sat, 7 Aug 2010 13:25:05 -0700 Subject: [PATCH] Commands can now be whitelisted to allow response to the channel --- plugins/Irc/ChannelResponseChannel.php | 47 ++++++++++++ plugins/Irc/IrcPlugin.php | 72 +++++++++++++++++-- .../phergie/Phergie/Plugin/Statusnet.php | 13 +++- 3 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 plugins/Irc/ChannelResponseChannel.php diff --git a/plugins/Irc/ChannelResponseChannel.php b/plugins/Irc/ChannelResponseChannel.php new file mode 100644 index 0000000000..0fee49c8eb --- /dev/null +++ b/plugins/Irc/ChannelResponseChannel.php @@ -0,0 +1,47 @@ +. + * + * @category Network + * @package StatusNet + * @author Luke Fitzgerald + * @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); + } +} \ No newline at end of file diff --git a/plugins/Irc/IrcPlugin.php b/plugins/Irc/IrcPlugin.php index 8fda359452..75eaf687b2 100644 --- a/plugins/Irc/IrcPlugin.php +++ b/plugins/Irc/IrcPlugin.php @@ -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; } diff --git a/plugins/Irc/extlib/phergie/Phergie/Plugin/Statusnet.php b/plugins/Irc/extlib/phergie/Phergie/Plugin/Statusnet.php index ec2b427466..fb75f1d798 100644 --- a/plugins/Irc/extlib/phergie/Phergie/Plugin/Statusnet.php +++ b/plugins/Irc/extlib/phergie/Phergie/Plugin/Statusnet.php @@ -75,7 +75,18 @@ class Phergie_Plugin_Statusnet extends Phergie_Plugin_Abstract { $sender = $event->getNick(); $message = trim($event->getText()); - call_user_func($this->messageCallback, array('source' => $source, 'sender' => $sender, 'message' => $message)); + 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)); + } } }