mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-23 15:22:41 +01:00
Merge IRCProtocolHandler into IRCAdapter
Summary: Clearly silly to have a separate handler for this. I also made most of the protocol stuff direct writes so we don't need to ship them through handlers, and made the adapter ignore message it does not understand by default instead of sending them to IRC, and added PASTE "support". We could still let handlers react to these messages by emitting them all as 'RAWIRC' or similar, but there's currently no need for that so I didn't bother. Also fix an issue in D4924 with nickpass. Test Plan: Had bot join IRC, talked to it. Reviewers: indiefan Reviewed By: indiefan CC: aran Differential Revision: https://secure.phabricator.com/D4925
This commit is contained in:
parent
d5995d574d
commit
0a8b0d1392
2 changed files with 67 additions and 86 deletions
|
@ -41,21 +41,11 @@ final class PhabricatorIRCProtocolAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->socket = $socket;
|
$this->socket = $socket;
|
||||||
$this->writeMessage(
|
$this->write("USER {$user} 0 * :{$user}");
|
||||||
id(new PhabricatorBotMessage())
|
|
||||||
->setCommand('USER')
|
|
||||||
->setBody("{$user} 0 * :{$user}"));
|
|
||||||
if ($pass) {
|
if ($pass) {
|
||||||
$this->writeMessage(
|
$this->write("PASS {$pass}");
|
||||||
id(new PhabricatorBotMessage())
|
|
||||||
->setCommand('PASS')
|
|
||||||
->setBody("{$pass}"));
|
|
||||||
}
|
}
|
||||||
|
$this->write("NICK {$nick}");
|
||||||
$this->writeMessage(
|
|
||||||
id(new PhabricatorBotMessage())
|
|
||||||
->setCommand('NICK')
|
|
||||||
->setBody("{$nick}"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getNextMessages($poll_frequency) {
|
public function getNextMessages($poll_frequency) {
|
||||||
|
@ -111,33 +101,31 @@ final class PhabricatorIRCProtocolAdapter
|
||||||
} while (strlen($this->writeBuffer));
|
} while (strlen($this->writeBuffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
while ($m = $this->processReadBuffer()) {
|
while (($m = $this->processReadBuffer()) !== false) {
|
||||||
$messages[] = $m;
|
if ($m !== null) {
|
||||||
|
$messages[] = $m;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $messages;
|
return $messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function write($message) {
|
private function write($message) {
|
||||||
$this->writeBuffer .= $message;
|
$this->writeBuffer .= $message."\r\n";
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function writeMessage(PhabricatorBotMessage $message) {
|
public function writeMessage(PhabricatorBotMessage $message) {
|
||||||
$irc_command = $this->getIRCCommand($message->getCommand());
|
|
||||||
switch ($message->getCommand()) {
|
switch ($message->getCommand()) {
|
||||||
case 'MESSAGE':
|
case 'MESSAGE':
|
||||||
$data = $irc_command.' '.
|
case 'PASTE':
|
||||||
$message->getTarget()->getName().' :'.
|
$name = $message->getTarget()->getName();
|
||||||
$message->getBody()."\r\n";
|
$body = $message->getBody();
|
||||||
break;
|
$this->write("PRIVMSG {$name} :{$body}");
|
||||||
default:
|
return true;
|
||||||
$data = $irc_command.' '.
|
default:
|
||||||
$message->getBody()."\r\n";
|
return false;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->write($data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function processReadBuffer() {
|
private function processReadBuffer() {
|
||||||
|
@ -161,6 +149,10 @@ final class PhabricatorIRCProtocolAdapter
|
||||||
throw new Exception("Unexpected message from server: {$message}");
|
throw new Exception("Unexpected message from server: {$message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->handleIRCProtocol($matches)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
$command = $this->getBotCommand($matches['command']);
|
$command = $this->getBotCommand($matches['command']);
|
||||||
list($target, $body) = $this->parseMessageData($command, $matches['data']);
|
list($target, $body) = $this->parseMessageData($command, $matches['data']);
|
||||||
|
|
||||||
|
@ -180,6 +172,31 @@ final class PhabricatorIRCProtocolAdapter
|
||||||
return $bot_message;
|
return $bot_message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function handleIRCProtocol(array $matches) {
|
||||||
|
$data = $matches['data'];
|
||||||
|
switch ($matches['command']) {
|
||||||
|
case '422': // Error - no MOTD
|
||||||
|
case '376': // End of MOTD
|
||||||
|
$nickpass = $this->getConfig('nickpass');
|
||||||
|
if ($nickpass) {
|
||||||
|
$this->write("PRIVMSG nickserv :IDENTIFY {$nickpass}");
|
||||||
|
}
|
||||||
|
$join = $this->getConfig('join');
|
||||||
|
if (!$join) {
|
||||||
|
throw new Exception("Not configured to join any channels!");
|
||||||
|
}
|
||||||
|
foreach ($join as $channel) {
|
||||||
|
$this->write("JOIN {$channel}");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case 'PING':
|
||||||
|
$this->write("PONG {$data}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private function getBotCommand($irc_command) {
|
private function getBotCommand($irc_command) {
|
||||||
if (isset(self::$commandTranslations[$irc_command])) {
|
if (isset(self::$commandTranslations[$irc_command])) {
|
||||||
return self::$commandTranslations[$irc_command];
|
return self::$commandTranslations[$irc_command];
|
||||||
|
@ -189,36 +206,26 @@ final class PhabricatorIRCProtocolAdapter
|
||||||
return $irc_command;
|
return $irc_command;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getIRCCommand($original_bot_command) {
|
|
||||||
foreach (self::$commandTranslations as $irc_command=>$bot_command) {
|
|
||||||
if ($bot_command === $original_bot_command) {
|
|
||||||
return $irc_command;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $original_bot_command;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function parseMessageData($command, $data) {
|
private function parseMessageData($command, $data) {
|
||||||
switch ($command) {
|
switch ($command) {
|
||||||
case 'MESSAGE':
|
case 'MESSAGE':
|
||||||
$matches = null;
|
$matches = null;
|
||||||
if (preg_match('/^(\S+)\s+:?(.*)$/', $data, $matches)) {
|
if (preg_match('/^(\S+)\s+:?(.*)$/', $data, $matches)) {
|
||||||
|
|
||||||
$target_name = $matches[1];
|
$target_name = $matches[1];
|
||||||
if (strncmp($target_name, '#', 1) === 0) {
|
if (strncmp($target_name, '#', 1) === 0) {
|
||||||
$target = id(new PhabricatorBotChannel())
|
$target = id(new PhabricatorBotChannel())
|
||||||
->setName($target_name);
|
->setName($target_name);
|
||||||
} else {
|
} else {
|
||||||
$target = id(new PhabricatorBotUser())
|
$target = id(new PhabricatorBotUser())
|
||||||
->setName($target_name);
|
->setName($target_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
$target,
|
||||||
|
rtrim($matches[2], "\r\n"));
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
return array(
|
|
||||||
$target,
|
|
||||||
rtrim($matches[2], "\r\n"));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// By default we assume there is no target, only a body
|
// By default we assume there is no target, only a body
|
||||||
|
@ -228,7 +235,7 @@ final class PhabricatorIRCProtocolAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct() {
|
public function __destruct() {
|
||||||
$this->write("QUIT Goodbye.\r\n");
|
$this->write("QUIT Goodbye.");
|
||||||
fclose($this->socket);
|
fclose($this->socket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,41 +1,15 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the base IRC protocol so servers don't kick you off.
|
* @deprecated
|
||||||
*
|
|
||||||
* @group irc
|
|
||||||
*/
|
*/
|
||||||
final class PhabricatorIRCProtocolHandler extends PhabricatorBotHandler {
|
final class PhabricatorIRCProtocolHandler extends PhabricatorBotHandler {
|
||||||
|
|
||||||
public function receiveMessage(PhabricatorBotMessage $message) {
|
public function receiveMessage(PhabricatorBotMessage $message) {
|
||||||
switch ($message->getCommand()) {
|
static $warned;
|
||||||
case '422': // Error - no MOTD
|
if (!$warned) {
|
||||||
case '376': // End of MOTD
|
$warned = true;
|
||||||
$nickpass = $this->getConfig('nickpass');
|
phlog("The PhabricatorIRCProtocolHandler has been deprecated.");
|
||||||
if ($nickpass) {
|
|
||||||
$this->writeMessage(
|
|
||||||
id(new PhabricatorBotMessage())
|
|
||||||
->setCommand('MESSAGE')
|
|
||||||
->setTarget('nickserv')
|
|
||||||
->setBody("IDENTIFY {$nickpass}"));
|
|
||||||
}
|
|
||||||
$join = $this->getConfig('join');
|
|
||||||
if (!$join) {
|
|
||||||
throw new Exception("Not configured to join any channels!");
|
|
||||||
}
|
|
||||||
foreach ($join as $channel) {
|
|
||||||
$this->writeMessage(
|
|
||||||
id(new PhabricatorBotMessage())
|
|
||||||
->setCommand('JOIN')
|
|
||||||
->setBody($channel));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'PING':
|
|
||||||
$this->writeMessage(
|
|
||||||
id(new PhabricatorBotMessage())
|
|
||||||
->setCommand('PONG')
|
|
||||||
->setBody($message->getBody()));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue