1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-17 18:21:11 +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:
epriestley 2011-05-17 13:15:19 -07:00
parent 8fc79035b6
commit f1d43bc3c5
6 changed files with 95 additions and 3 deletions

View file

@ -7,5 +7,9 @@
],
"handlers" : [
"PhabricatorIRCProtocolHandler"
]
],
"conduit.uri" : null,
"conduit.user" : null,
"conduit.cert" : null
}

View file

@ -48,6 +48,7 @@ class PhabricatorPeopleEditController extends PhabricatorPeopleController {
'basic' => 'Basic Information',
'password' => 'Reset Password',
'role' => 'Edit Role',
'cert' => 'Conduit Certificate',
);
if (!$user->getID()) {
@ -78,6 +79,9 @@ class PhabricatorPeopleEditController extends PhabricatorPeopleController {
case 'role':
$response = $this->processRoleRequest($user);
break;
case 'cert':
$response = $this->processCertificateRequest($user);
break;
}
if ($response instanceof AphrontResponse) {
@ -376,4 +380,47 @@ class PhabricatorPeopleEditController extends PhabricatorPeopleController {
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);
}
}

View file

@ -12,8 +12,10 @@ phutil_require_module('phabricator', 'applications/people/controller/base');
phutil_require_module('phabricator', 'applications/people/storage/user');
phutil_require_module('phabricator', 'view/form/base');
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/text');
phutil_require_module('phabricator', 'view/form/control/textarea');
phutil_require_module('phabricator', 'view/form/error');
phutil_require_module('phabricator', 'view/layout/panel');
phutil_require_module('phabricator', 'view/layout/sidenav');

View file

@ -34,6 +34,8 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
private $writeBuffer;
private $readBuffer;
private $conduit;
public function run() {
$argv = $this->getArgv();
@ -68,6 +70,25 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
$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;
$error = null;
$socket = fsockopen($server, $port, $errno, $error);
@ -130,7 +151,9 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
} while (strlen($this->writeBuffer));
}
$this->processReadBuffer();
do {
$routed_message = $this->processReadBuffer();
} while ($routed_message);
} while (true);
}
@ -147,7 +170,7 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
private function processReadBuffer() {
$until = strpos($this->readBuffer, "\r\n");
if ($until === false) {
return;
return false;
}
$message = substr($this->readBuffer, 0, $until);
@ -171,6 +194,8 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
$matches['data']);
$this->routeMessage($irc_message);
return true;
}
private function routeMessage(PhabricatorIRCMessage $message) {
@ -190,4 +215,13 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
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;
}
}

View file

@ -9,6 +9,7 @@
phutil_require_module('phabricator', 'infrastructure/daemon/base');
phutil_require_module('phabricator', 'infrastructure/daemon/irc/message');
phutil_require_module('phutil', 'conduit/client');
phutil_require_module('phutil', 'filesystem');
phutil_require_module('phutil', 'utils');

View file

@ -35,6 +35,10 @@ abstract class PhabricatorIRCHandler {
return $this;
}
final protected function getConduit() {
return $this->bot->getConduit();
}
abstract public function receiveMessage(PhabricatorIRCMessage $message);
}