summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Fitzgerald <lw.fitzgerald@googlemail.com>2010-08-07 13:25:05 -0700
committerLuke Fitzgerald <lw.fitzgerald@googlemail.com>2010-08-07 13:25:05 -0700
commite23ad0bdb34418161b46d4b15a5306ceb6a8a5a1 (patch)
tree45bce87ab0d512ec55f629d8cad89107106c7bd5
parenta6e865c47ad923f2c4cf97b1966f9eb9baad4553 (diff)
Commands can now be whitelisted to allow response to the channel
-rw-r--r--plugins/Irc/ChannelResponseChannel.php47
-rw-r--r--plugins/Irc/IrcPlugin.php72
-rw-r--r--plugins/Irc/extlib/phergie/Phergie/Plugin/Statusnet.php13
3 files changed, 126 insertions, 6 deletions
diff --git a/plugins/Irc/ChannelResponseChannel.php b/plugins/Irc/ChannelResponseChannel.php
new file mode 100644
index 000000000..0fee49c8e
--- /dev/null
+++ b/plugins/Irc/ChannelResponseChannel.php
@@ -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);
+ }
+} \ No newline at end of file
diff --git a/plugins/Irc/IrcPlugin.php b/plugins/Irc/IrcPlugin.php
index 8fda35945..75eaf687b 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 ec2b42746..fb75f1d79 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));
+ }
}
}