mirror of
https://we.phorge.it/source/phorge.git
synced 2025-01-29 16:08:22 +01:00
Establish a Conduit connection from PhabricatorIRCBot
Summary: Allow construction of handlers which use Conduit. Test Plan: Made a bot that connects to local and runs conduit.ping. Reviewed By: mroch Reviewers: mroch, codeblock, aran, jungejason, tuomaspelkonen CC: aran, mroch Differential Revision: 299
This commit is contained in:
parent
8fc79035b6
commit
f1d43bc3c5
6 changed files with 95 additions and 3 deletions
|
@ -7,5 +7,9 @@
|
||||||
],
|
],
|
||||||
"handlers" : [
|
"handlers" : [
|
||||||
"PhabricatorIRCProtocolHandler"
|
"PhabricatorIRCProtocolHandler"
|
||||||
]
|
],
|
||||||
|
|
||||||
|
"conduit.uri" : null,
|
||||||
|
"conduit.user" : null,
|
||||||
|
"conduit.cert" : null
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,7 @@ class PhabricatorPeopleEditController extends PhabricatorPeopleController {
|
||||||
'basic' => 'Basic Information',
|
'basic' => 'Basic Information',
|
||||||
'password' => 'Reset Password',
|
'password' => 'Reset Password',
|
||||||
'role' => 'Edit Role',
|
'role' => 'Edit Role',
|
||||||
|
'cert' => 'Conduit Certificate',
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!$user->getID()) {
|
if (!$user->getID()) {
|
||||||
|
@ -78,6 +79,9 @@ class PhabricatorPeopleEditController extends PhabricatorPeopleController {
|
||||||
case 'role':
|
case 'role':
|
||||||
$response = $this->processRoleRequest($user);
|
$response = $this->processRoleRequest($user);
|
||||||
break;
|
break;
|
||||||
|
case 'cert':
|
||||||
|
$response = $this->processCertificateRequest($user);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($response instanceof AphrontResponse) {
|
if ($response instanceof AphrontResponse) {
|
||||||
|
@ -376,4 +380,47 @@ class PhabricatorPeopleEditController extends PhabricatorPeopleController {
|
||||||
return array($error_view, $panel);
|
return array($error_view, $panel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function processCertificateRequest($user) {
|
||||||
|
$request = $this->getRequest();
|
||||||
|
$admin = $request->getUser();
|
||||||
|
|
||||||
|
|
||||||
|
$form = new AphrontFormView();
|
||||||
|
$form
|
||||||
|
->setUser($admin)
|
||||||
|
->setAction($request->getRequestURI())
|
||||||
|
->appendChild(
|
||||||
|
'<p class="aphront-form-instructions">You can use this certificate '.
|
||||||
|
'to write scripts or bots which interface with Phabricator over '.
|
||||||
|
'Conduit.</p>');
|
||||||
|
|
||||||
|
if ($user->getIsSystemAgent()) {
|
||||||
|
$form
|
||||||
|
->appendChild(
|
||||||
|
id(new AphrontFormTextControl())
|
||||||
|
->setLabel('Username')
|
||||||
|
->setValue($user->getUsername()))
|
||||||
|
->appendChild(
|
||||||
|
id(new AphrontFormTextAreaControl())
|
||||||
|
->setLabel('Certificate')
|
||||||
|
->setValue($user->getConduitCertificate()));
|
||||||
|
} else {
|
||||||
|
$form->appendChild(
|
||||||
|
id(new AphrontFormStaticControl())
|
||||||
|
->setLabel('Certificate')
|
||||||
|
->setValue(
|
||||||
|
'You may only view the certificates for System Agents. Mark '.
|
||||||
|
'this account as a "system agent" in the "Edit Role" tab to '.
|
||||||
|
'view the corresponding certificate.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$panel = new AphrontPanelView();
|
||||||
|
$panel->setHeader('Conduit Certificate');
|
||||||
|
$panel->setWidth(AphrontPanelView::WIDTH_FORM);
|
||||||
|
|
||||||
|
$panel->appendChild($form);
|
||||||
|
|
||||||
|
return array($panel);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,10 @@ phutil_require_module('phabricator', 'applications/people/controller/base');
|
||||||
phutil_require_module('phabricator', 'applications/people/storage/user');
|
phutil_require_module('phabricator', 'applications/people/storage/user');
|
||||||
phutil_require_module('phabricator', 'view/form/base');
|
phutil_require_module('phabricator', 'view/form/base');
|
||||||
phutil_require_module('phabricator', 'view/form/control/checkbox');
|
phutil_require_module('phabricator', 'view/form/control/checkbox');
|
||||||
|
phutil_require_module('phabricator', 'view/form/control/static');
|
||||||
phutil_require_module('phabricator', 'view/form/control/submit');
|
phutil_require_module('phabricator', 'view/form/control/submit');
|
||||||
phutil_require_module('phabricator', 'view/form/control/text');
|
phutil_require_module('phabricator', 'view/form/control/text');
|
||||||
|
phutil_require_module('phabricator', 'view/form/control/textarea');
|
||||||
phutil_require_module('phabricator', 'view/form/error');
|
phutil_require_module('phabricator', 'view/form/error');
|
||||||
phutil_require_module('phabricator', 'view/layout/panel');
|
phutil_require_module('phabricator', 'view/layout/panel');
|
||||||
phutil_require_module('phabricator', 'view/layout/sidenav');
|
phutil_require_module('phabricator', 'view/layout/sidenav');
|
||||||
|
|
|
@ -34,6 +34,8 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
|
||||||
private $writeBuffer;
|
private $writeBuffer;
|
||||||
private $readBuffer;
|
private $readBuffer;
|
||||||
|
|
||||||
|
private $conduit;
|
||||||
|
|
||||||
public function run() {
|
public function run() {
|
||||||
|
|
||||||
$argv = $this->getArgv();
|
$argv = $this->getArgv();
|
||||||
|
@ -68,6 +70,25 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
|
||||||
$this->handlers[] = $obj;
|
$this->handlers[] = $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$conduit_uri = idx($config, 'conduit.uri');
|
||||||
|
if ($conduit_uri) {
|
||||||
|
$conduit_user = idx($config, 'conduit.user');
|
||||||
|
$conduit_cert = idx($config, 'conduit.cert');
|
||||||
|
|
||||||
|
$conduit = new ConduitClient($conduit_uri);
|
||||||
|
$response = $conduit->callMethodSynchronous(
|
||||||
|
'conduit.connect',
|
||||||
|
array(
|
||||||
|
'client' => 'PhabricatorIRCBot',
|
||||||
|
'clientVersion' => '1.0',
|
||||||
|
'clientDescription' => php_uname('n').':'.$nick,
|
||||||
|
'user' => $conduit_user,
|
||||||
|
'certificate' => $conduit_cert,
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->conduit = $conduit;
|
||||||
|
}
|
||||||
|
|
||||||
$errno = null;
|
$errno = null;
|
||||||
$error = null;
|
$error = null;
|
||||||
$socket = fsockopen($server, $port, $errno, $error);
|
$socket = fsockopen($server, $port, $errno, $error);
|
||||||
|
@ -130,7 +151,9 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
|
||||||
} while (strlen($this->writeBuffer));
|
} while (strlen($this->writeBuffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->processReadBuffer();
|
do {
|
||||||
|
$routed_message = $this->processReadBuffer();
|
||||||
|
} while ($routed_message);
|
||||||
|
|
||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
|
@ -147,7 +170,7 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
|
||||||
private function processReadBuffer() {
|
private function processReadBuffer() {
|
||||||
$until = strpos($this->readBuffer, "\r\n");
|
$until = strpos($this->readBuffer, "\r\n");
|
||||||
if ($until === false) {
|
if ($until === false) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = substr($this->readBuffer, 0, $until);
|
$message = substr($this->readBuffer, 0, $until);
|
||||||
|
@ -171,6 +194,8 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
|
||||||
$matches['data']);
|
$matches['data']);
|
||||||
|
|
||||||
$this->routeMessage($irc_message);
|
$this->routeMessage($irc_message);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function routeMessage(PhabricatorIRCMessage $message) {
|
private function routeMessage(PhabricatorIRCMessage $message) {
|
||||||
|
@ -190,4 +215,13 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
|
||||||
echo "\n";
|
echo "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getConduit() {
|
||||||
|
if (empty($this->conduit)) {
|
||||||
|
throw new Exception(
|
||||||
|
"This bot is not configured with a Conduit uplink. Set 'conduit.uri', ".
|
||||||
|
"'conduit.user' and 'conduit.cert' in the configuration to connect.");
|
||||||
|
}
|
||||||
|
return $this->conduit;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
phutil_require_module('phabricator', 'infrastructure/daemon/base');
|
phutil_require_module('phabricator', 'infrastructure/daemon/base');
|
||||||
phutil_require_module('phabricator', 'infrastructure/daemon/irc/message');
|
phutil_require_module('phabricator', 'infrastructure/daemon/irc/message');
|
||||||
|
|
||||||
|
phutil_require_module('phutil', 'conduit/client');
|
||||||
phutil_require_module('phutil', 'filesystem');
|
phutil_require_module('phutil', 'filesystem');
|
||||||
phutil_require_module('phutil', 'utils');
|
phutil_require_module('phutil', 'utils');
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,10 @@ abstract class PhabricatorIRCHandler {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final protected function getConduit() {
|
||||||
|
return $this->bot->getConduit();
|
||||||
|
}
|
||||||
|
|
||||||
abstract public function receiveMessage(PhabricatorIRCMessage $message);
|
abstract public function receiveMessage(PhabricatorIRCMessage $message);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue