--- /dev/null
+<?php\r
+/**\r
+ * StatusNet, the distributed open-source microblogging tool\r
+ *\r
+ * Override the IMChannel class to allow commands to send messages\r
+ * to a channel instead of PMing a user\r
+ *\r
+ * PHP version 5\r
+ *\r
+ * LICENCE: This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Affero General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU Affero General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Affero General Public License\r
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ * @category Network\r
+ * @package StatusNet\r
+ * @author Luke Fitzgerald <lw.fitzgerald@googlemail.com>\r
+ * @copyright 2010 StatusNet, Inc.\r
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0\r
+ * @link http://status.net/\r
+ */\r
+\r
+if (!defined('STATUSNET') && !defined('LACONICA')) {\r
+ exit(1);\r
+}\r
+\r
+class ChannelResponseChannel extends IMChannel {\r
+ protected $ircChannel;\r
+\r
+ public function __construct($imPlugin, $ircChannel) {\r
+ $this->ircChannel = $ircChannel;\r
+ parent::__construct($imPlugin);\r
+ }\r
+\r
+ public function output($user, $text) {\r
+ $text = $user->nickname.': ['.common_config('site', 'name') . '] ' . $text;\r
+ $this->imPlugin->send_message($this->ircChannel, $text);\r
+ }\r
+}
\ No newline at end of file
public $regregexp = null;
public $transport = 'irc';
+ public $whiteList;
public $fake_irc;
/**
include_once $dir . '/'.strtolower($cls).'.php';
return false;
case 'Fake_Irc':
+ case 'ChannelResponseChannel':
include_once $dir . '/'. $cls .'.php';
return false;
default:
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']);
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
*
}
$this->fake_irc = new Fake_Irc;
+
+ /*
+ * Commands allowed to return output to a channel
+ */
+ $this->whiteList = array('stats', 'last', 'get');
+
return true;
}
$sender = $event->getNick();\r
$message = trim($event->getText());\r
\r
- call_user_func($this->messageCallback, array('source' => $source, 'sender' => $sender, 'message' => $message));\r
+ if (strpos($source, '#') === 0) {\r
+ $botNick = $this->getConnection()->getNick();\r
+ $nickPos = strpos($message, $botNick);\r
+ $nickLen = strlen($botNick);\r
+ $colonPos = strpos($message, ':', $nickLen);\r
+ $commandStr = trim(substr($message, $colonPos+1));\r
+ if ($nickPos === 0 && $colonPos == $nickLen && !empty($commandStr)) {\r
+ call_user_func($this->messageCallback, array('source' => $source, 'sender' => $sender, 'message' => $commandStr));\r
+ }\r
+ } else {\r
+ call_user_func($this->messageCallback, array('source' => $source, 'sender' => $sender, 'message' => $message));\r
+ }\r
}\r
}\r
\r