mirror of
https://we.phorge.it/source/phorge.git
synced 2025-04-11 11:58:33 +02:00
Summary: Decided the best approach for refactoring the message/command stuff would be to actually start implementing the campfire adapter to get a better idea of what the abstractions should look like. It feels awkward and unwieldy trying to maintain the irc command interface (notice the message instantiation in the `processReadBuffer()` method. However, i'm still not clear what the best approach is without requiring a re-write of nearly all the existing handlers and defining essentially a custom dsl on top of irc's. I suppose given that alternative, implementing to irc's dsl doesn't sound all that bad. Just feels like poor coupling. Also, I know that there is some http stuff in libphutil's futures library, but the https future is shit and I need to do some custom curlopt stuff I wasn't sure how to do with that. But if you think this should be refactored, let me know. I tested this with the ObjectHandler (messages with DXXX initiate the bot to respond with the title/link just as with irc), but beyond that, I haven't tried any of the other handlers, so if there are complications you think i'm going to run into, just let me know (this is one of the reasons for requesting review early on). Also, this diff is against my last one, even though that hasn't been merged down yet. It was starting to get large and I'd prefer to keep to two conversations separate. Fixing some lint issues. Test Plan: Ran the bot with the Object Handler in campfire and observed it behaving properly. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Maniphest Tasks: T2462 Differential Revision: https://secure.phabricator.com/D4830
66 lines
1.7 KiB
PHP
66 lines
1.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Responds to IRC messages. You plug a bunch of these into a
|
|
* @{class:PhabricatorBot} to give it special behavior.
|
|
*
|
|
* @group irc
|
|
*/
|
|
abstract class PhabricatorBotHandler {
|
|
|
|
private $bot;
|
|
|
|
final public function __construct(PhabricatorBot $irc_bot) {
|
|
$this->bot = $irc_bot;
|
|
}
|
|
|
|
final protected function writeMessage(PhabricatorBotMessage $message) {
|
|
$this->bot->writeMessage($message);
|
|
return $this;
|
|
}
|
|
|
|
final protected function getConduit() {
|
|
return $this->bot->getConduit();
|
|
}
|
|
|
|
final protected function getConfig($key, $default = null) {
|
|
return $this->bot->getConfig($key, $default);
|
|
}
|
|
|
|
final protected function getURI($path) {
|
|
$base_uri = new PhutilURI($this->bot->getConfig('conduit.uri'));
|
|
$base_uri->setPath($path);
|
|
return (string)$base_uri;
|
|
}
|
|
|
|
abstract public function receiveMessage(PhabricatorBotMessage $message);
|
|
|
|
public function runBackgroundTasks() {
|
|
return;
|
|
}
|
|
|
|
public function replyTo($original_message, $body) {
|
|
if ($original_message->getCommand() != 'MESSAGE') {
|
|
throw new Exception(
|
|
"Handler is trying to reply to something which is not a message!");
|
|
}
|
|
|
|
$reply = id(new PhabricatorBotMessage())
|
|
->setCommand('MESSAGE');
|
|
|
|
if ($original_message->isPublic()) {
|
|
// This is a public target, like a chatroom. Send the response to the
|
|
// chatroom.
|
|
$reply->setTarget($original_message->getTarget());
|
|
} else {
|
|
// This is a private target, like a private message. Send the response
|
|
// back to the sender (presumably, we are the target).
|
|
$reply->setTarget($original_message->getSender())
|
|
->setPublic(false);
|
|
}
|
|
|
|
$reply->setBody($body);
|
|
|
|
return $this->writeMessage($reply);
|
|
}
|
|
}
|