From b9cac3bcd13b06a10ee7ce1c31f7eec702547562 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 10 Jan 2012 10:39:06 -0800 Subject: [PATCH] Improve phabot handling of private messages Summary: When private messaged, the bot responds via private message to the sender, instead of sending a private message to itself. Test Plan: Mentioned tasks in public channels and private messages. Reviewers: btrahan, jungejason Reviewed By: btrahan CC: aran, btrahan Maniphest Tasks: T274 Differential Revision: https://secure.phabricator.com/D1350 --- .../handler/base/PhabricatorIRCHandler.php | 4 ++ .../PhabricatorIRCObjectNameHandler.php | 42 ++++++++++++------- .../irc/message/PhabricatorIRCMessage.php | 21 +++++++++- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/infrastructure/daemon/irc/handler/base/PhabricatorIRCHandler.php b/src/infrastructure/daemon/irc/handler/base/PhabricatorIRCHandler.php index 069c33c434..157978fc84 100644 --- a/src/infrastructure/daemon/irc/handler/base/PhabricatorIRCHandler.php +++ b/src/infrastructure/daemon/irc/handler/base/PhabricatorIRCHandler.php @@ -47,6 +47,10 @@ abstract class PhabricatorIRCHandler { return $this->bot->getConfig('conduit.uri').$path; } + final protected function isChannelName($name) { + return (strpos($name, '#') === 0); + } + abstract public function receiveMessage(PhabricatorIRCMessage $message); public function runBackgroundTasks() { diff --git a/src/infrastructure/daemon/irc/handler/objectname/PhabricatorIRCObjectNameHandler.php b/src/infrastructure/daemon/irc/handler/objectname/PhabricatorIRCObjectNameHandler.php index de771d4e2b..52e27208c7 100644 --- a/src/infrastructure/daemon/irc/handler/objectname/PhabricatorIRCObjectNameHandler.php +++ b/src/infrastructure/daemon/irc/handler/objectname/PhabricatorIRCObjectNameHandler.php @@ -1,7 +1,7 @@ getCommand()) { case 'PRIVMSG': - $channel = $message->getChannel(); - if (!$channel) { + $reply_to = $message->getReplyTo(); + if (!$reply_to) { break; } @@ -164,23 +164,37 @@ class PhabricatorIRCObjectNameHandler extends PhabricatorIRCHandler { foreach ($output as $phid => $description) { - // Don't mention the same object more than once every 10 minutes, so - // we avoid spamming the chat over and over again for discsussions of - // a specific revision, for example. - $quiet_until = idx($this->recentlyMentioned, $phid, 0) + (60 * 10); - if (time() < $quiet_until) { - continue; - } - $this->recentlyMentioned[$phid] = time(); + // Don't mention the same object more than once every 10 minutes + // in public channels, so we avoid spamming the chat over and over + // again for discsussions of a specific revision, for example. In + // direct-to-bot chat, respond to every object reference. - $this->write('PRIVMSG', "{$channel} :{$description}"); + if ($this->isChannelName($reply_to)) { + if (empty($this->recentlyMentioned[$reply_to])) { + $this->recentlyMentioned[$reply_to] = array(); + } + + $quiet_until = idx( + $this->recentlyMentioned[$reply_to], + $phid, + 0) + (60 * 10); + + if (time() < $quiet_until) { + // Remain quiet on this channel. + continue; + } + + $this->recentlyMentioned[$reply_to][$phid] = time(); + } + + $this->write('PRIVMSG', "{$reply_to} :{$description}"); } break; } } private function handleSymbols(PhabricatorIRCMessage $message) { - $channel = $message->getChannel(); + $reply_to = $message->getReplyTo(); $text = $message->getMessageText(); $matches = null; @@ -209,7 +223,7 @@ class PhabricatorIRCObjectNameHandler extends PhabricatorIRCHandler { $response = "No symbol '{$symbol}' found anywhere."; } - $this->write('PRIVMSG', "{$channel} :{$response}"); + $this->write('PRIVMSG', "{$reply_to} :{$response}"); } } diff --git a/src/infrastructure/daemon/irc/message/PhabricatorIRCMessage.php b/src/infrastructure/daemon/irc/message/PhabricatorIRCMessage.php index aaab3b2daa..a9cfe22f0f 100644 --- a/src/infrastructure/daemon/irc/message/PhabricatorIRCMessage.php +++ b/src/infrastructure/daemon/irc/message/PhabricatorIRCMessage.php @@ -1,7 +1,7 @@ command; } - public function getChannel() { + public function getReplyTo() { + switch ($this->getCommand()) { + case 'PRIVMSG': + $target = $this->getTarget(); + if ($target[0] == '#') { + return $target; + } + + $matches = null; + if (preg_match('/^:([^!]+)!/', $this->sender, $matches)) { + return $matches[1]; + } + break; + } + return null; + } + + public function getTarget() { switch ($this->getCommand()) { case 'PRIVMSG': $matches = null;