1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-20 04:20:55 +01:00

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
This commit is contained in:
epriestley 2012-01-10 10:39:06 -08:00
parent 2a29a51080
commit b9cac3bcd1
3 changed files with 51 additions and 16 deletions

View file

@ -47,6 +47,10 @@ abstract class PhabricatorIRCHandler {
return $this->bot->getConfig('conduit.uri').$path; return $this->bot->getConfig('conduit.uri').$path;
} }
final protected function isChannelName($name) {
return (strpos($name, '#') === 0);
}
abstract public function receiveMessage(PhabricatorIRCMessage $message); abstract public function receiveMessage(PhabricatorIRCMessage $message);
public function runBackgroundTasks() { public function runBackgroundTasks() {

View file

@ -1,7 +1,7 @@
<?php <?php
/* /*
* Copyright 2011 Facebook, Inc. * Copyright 2012 Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,8 +33,8 @@ class PhabricatorIRCObjectNameHandler extends PhabricatorIRCHandler {
switch ($message->getCommand()) { switch ($message->getCommand()) {
case 'PRIVMSG': case 'PRIVMSG':
$channel = $message->getChannel(); $reply_to = $message->getReplyTo();
if (!$channel) { if (!$reply_to) {
break; break;
} }
@ -164,23 +164,37 @@ class PhabricatorIRCObjectNameHandler extends PhabricatorIRCHandler {
foreach ($output as $phid => $description) { foreach ($output as $phid => $description) {
// Don't mention the same object more than once every 10 minutes, so // Don't mention the same object more than once every 10 minutes
// we avoid spamming the chat over and over again for discsussions of // in public channels, so we avoid spamming the chat over and over
// a specific revision, for example. // again for discsussions of a specific revision, for example. In
$quiet_until = idx($this->recentlyMentioned, $phid, 0) + (60 * 10); // direct-to-bot chat, respond to every object reference.
if (time() < $quiet_until) {
continue;
}
$this->recentlyMentioned[$phid] = time();
$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; break;
} }
} }
private function handleSymbols(PhabricatorIRCMessage $message) { private function handleSymbols(PhabricatorIRCMessage $message) {
$channel = $message->getChannel(); $reply_to = $message->getReplyTo();
$text = $message->getMessageText(); $text = $message->getMessageText();
$matches = null; $matches = null;
@ -209,7 +223,7 @@ class PhabricatorIRCObjectNameHandler extends PhabricatorIRCHandler {
$response = "No symbol '{$symbol}' found anywhere."; $response = "No symbol '{$symbol}' found anywhere.";
} }
$this->write('PRIVMSG', "{$channel} :{$response}"); $this->write('PRIVMSG', "{$reply_to} :{$response}");
} }
} }

View file

@ -1,7 +1,7 @@
<?php <?php
/* /*
* Copyright 2011 Facebook, Inc. * Copyright 2012 Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,7 +40,24 @@ final class PhabricatorIRCMessage {
return $this->command; return $this->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()) { switch ($this->getCommand()) {
case 'PRIVMSG': case 'PRIVMSG':
$matches = null; $matches = null;