Skip to content

Commit

Permalink
Merge pull request #1 from WildPHP/ircv32-capabilities
Browse files Browse the repository at this point in the history
Update CAP command for IRCv3.2 compatibility
  • Loading branch information
NanoSector authored Jan 21, 2019
2 parents 2bb28aa + 5dfd95d commit ddfbeb0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
74 changes: 63 additions & 11 deletions src/Cap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
use WildPHP\Messages\Interfaces\IncomingMessageInterface;
use WildPHP\Messages\Interfaces\IrcMessageInterface;
use WildPHP\Messages\Interfaces\OutgoingMessageInterface;
use WildPHP\Messages\Traits\NicknameTrait;
use WildPHP\Messages\Traits\PrefixTrait;

/**
* Class Cap
* @package WildPHP\Messages
*
* Syntax: prefix CAP nickname command [:capabilities]
* Syntax: prefix CAP nickname sub-command (*) [:capabilities]
*
* This definition implements version 3.1 and 3.2 of the IRCv3 capability negotiation spec
* as described in the following documents:
* https://ircv3.net/specs/core/capability-negotiation-3.1.html
* https://ircv3.net/specs/core/capability-negotiation-3.2.html
*/
class Cap extends BaseIRCMessageImplementation implements IncomingMessageInterface, OutgoingMessageInterface
{
use PrefixTrait;
use NicknameTrait;

protected static $verb = 'CAP';

Expand All @@ -34,25 +37,37 @@ class Cap extends BaseIRCMessageImplementation implements IncomingMessageInterfa
*/
protected $command = '';

/**
* @var string
*/
protected $clientIdentifier = '';

/**
* @var array
*/
protected $capabilities = [];

/**
* @var bool
*/
private $finalMessage;

/**
* Cap constructor.
*
* @param string $command
* @param array $capabilities
* @param bool $finalMessage
*/
public function __construct(string $command, array $capabilities = [])
public function __construct(string $command, array $capabilities = [], bool $finalMessage = true)
{
if (!in_array($command, ['LS', 'LIST', 'REQ', 'ACK', 'NAK', 'END'])) {
throw new \InvalidArgumentException('Cap subcommand not valid');
if (!in_array($command, ['LS', 'LIST', 'REQ', 'ACK', 'NAK', 'END', 'NEW', 'DEL']) && preg_match('/^LS \d{3}$/', $command) === 0) {
throw new \InvalidArgumentException('Cap sub-command not valid');
}

$this->setCommand($command);
$this->setCapabilities($capabilities);
$this->finalMessage = $finalMessage;
}

/**
Expand All @@ -68,12 +83,17 @@ public static function fromIncomingMessage(IrcMessageInterface $incomingMessage)

$prefix = Prefix::fromIncomingMessage($incomingMessage);
$args = $incomingMessage->getArgs();
$nickname = array_shift($args);
$isFinal = count($args) == 3;
$clientIdentifier = array_shift($args);
$command = array_shift($args);
$capabilities = explode(' ', array_shift($args));

$object = new self($command, $capabilities);
$object->setNickname($nickname);
if (!$isFinal)
array_shift($args);

$capabilities = explode(' ', trim(array_shift($args)));

$object = new self($command, $capabilities, $isFinal);
$object->setClientIdentifier($clientIdentifier);
$object->setPrefix($prefix);

return $object;
Expand Down Expand Up @@ -118,6 +138,38 @@ public function __toString(): string
{
$capabilities = implode(' ', $this->getCapabilities());

return 'Cap ' . $this->getCommand() . (!empty($capabilities) ? ' :' . $capabilities : '') . "\r\n";
return 'CAP ' . $this->getCommand() . (!empty($capabilities) ? ' :' . $capabilities : '') . "\r\n";
}

/**
* @return string
*/
public function getClientIdentifier(): string
{
return $this->clientIdentifier;
}

/**
* @param string $clientIdentifier
*/
public function setClientIdentifier(string $clientIdentifier): void
{
$this->clientIdentifier = $clientIdentifier;
}

/**
* @return bool
*/
public function isFinalMessage(): bool
{
return $this->finalMessage;
}

/**
* @param bool $finalMessage
*/
public function setFinalMessage(bool $finalMessage): void
{
$this->finalMessage = $finalMessage;
}
}
22 changes: 20 additions & 2 deletions tests/IrcMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,19 @@ public function testCapCreate()

$this->assertEquals('REQ', $cap->getCommand());
$this->assertEquals(['cap1', 'cap2'], $cap->getCapabilities());
$this->assertTrue($cap->isFinalMessage());

$expected = 'Cap REQ :cap1 cap2' . "\r\n";
$expected = 'CAP REQ :cap1 cap2' . "\r\n";
$this->assertEquals($expected, $cap->__toString());

$cap = new Cap('REQ', ['cap1', 'cap2'], false);
$this->assertFalse($cap->isFinalMessage());
}

public function testCapCreateInvalidSubcommand()
{
new Cap('LS 302');

$this->expectException(\InvalidArgumentException::class);

new Cap('INVALID');
Expand All @@ -165,7 +171,19 @@ public function testCapReceive()

$this->assertEquals('LS', $cap->getCommand());
$this->assertEquals(['cap1', 'cap2'], $cap->getCapabilities());
$this->assertEquals('*', $cap->getNickname());
$this->assertEquals('*', $cap->getClientIdentifier());
$this->assertTrue($cap->isFinalMessage());

$prefix = 'server';
$verb = 'CAP';
$args = ['*', 'LS', '*', 'cap1 cap2'];
$incoming = new IrcMessage($prefix, $verb, $args);
$cap = Cap::fromIncomingMessage($incoming);

$this->assertEquals('LS', $cap->getCommand());
$this->assertEquals(['cap1', 'cap2'], $cap->getCapabilities());
$this->assertEquals('*', $cap->getClientIdentifier());
$this->assertFalse($cap->isFinalMessage());

$prefix = ':server';
$verb = 'TEEHEE';
Expand Down

0 comments on commit ddfbeb0

Please sign in to comment.