From 6e1018a29b7d296a3ee5fbf18c9c1fedb30e1213 Mon Sep 17 00:00:00 2001 From: NanoSector Date: Sun, 21 Apr 2019 19:00:57 +0200 Subject: [PATCH] Add support for userhost-in-names capability (fixes #7) Signed-off-by: NanoSector --- src/RPL/NamReply.php | 47 ++++++++++++++++++++++++++++++++++++++ tests/RPL_NamReplyTest.php | 23 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/RPL/NamReply.php b/src/RPL/NamReply.php index a8eaeee..1e01dc1 100644 --- a/src/RPL/NamReply.php +++ b/src/RPL/NamReply.php @@ -12,6 +12,7 @@ use InvalidArgumentException; use WildPHP\Messages\Generics\BaseIRCMessageImplementation; +use WildPHP\Messages\Generics\Prefix; use WildPHP\Messages\Interfaces\IncomingMessageInterface; use WildPHP\Messages\Interfaces\IrcMessageInterface; use WildPHP\Messages\Traits\ChannelTrait; @@ -23,6 +24,7 @@ * @package WildPHP\Messages * * Syntax: :server 353 nickname visibility channel :nicknames + * Syntax (userhost-in-names): :server 353 nickname visibility channel :prefixes */ class NamReply extends BaseIRCMessageImplementation implements IncomingMessageInterface { @@ -32,10 +34,23 @@ class NamReply extends BaseIRCMessageImplementation implements IncomingMessageIn protected static $verb = '353'; + /** + * @var string + */ protected $visibility = ''; + /** + * @var array + */ protected $nicknames = []; + /** + * @var array + * + * Format: + */ + protected $prefixes = []; + /** * @param IrcMessageInterface $incomingMessage * @@ -55,11 +70,27 @@ public static function fromIncomingMessage(IrcMessageInterface $incomingMessage) [$nickname, $visibility, $channel, $nicknames] = $incomingMessage->getArgs(); $nicknames = explode(' ', $nicknames); + $prefixes = []; + foreach ($nicknames as $key => $prefixString) { + $prefix = Prefix::fromString($prefixString); + + // no nickname means this isn't a full prefix. do not try any further. + if (empty($prefix->getNickname())) { + break; + } + + $prefixes[$prefix->getNickname()] = $prefix; + + // override the original nickname with the parsed result. + $nicknames[$key] = $prefix->getNickname(); + } + $object = new self(); $object->setNickname($nickname); $object->setVisibility($visibility); $object->setChannel($channel); $object->setNicknames($nicknames); + $object->setPrefixes($prefixes); $object->setServer($server); $object->setTags($incomingMessage->getTags()); @@ -97,4 +128,20 @@ public function setNicknames(array $nicknames) { $this->nicknames = $nicknames; } + + /** + * @return array + */ + public function getPrefixes(): array + { + return $this->prefixes; + } + + /** + * @param array $prefixes + */ + public function setPrefixes(array $prefixes): void + { + $this->prefixes = $prefixes; + } } diff --git a/tests/RPL_NamReplyTest.php b/tests/RPL_NamReplyTest.php index 9e671ca..4c59640 100644 --- a/tests/RPL_NamReplyTest.php +++ b/tests/RPL_NamReplyTest.php @@ -11,6 +11,7 @@ use InvalidArgumentException; use PHPUnit\Framework\TestCase; use WildPHP\Messages\Generics\IrcMessage; +use WildPHP\Messages\Generics\Prefix; use WildPHP\Messages\RPL\NamReply; class RPL_NamReplyTest extends TestCase @@ -27,6 +28,28 @@ public function testFromIncomingMessage(): void $this->assertEquals('nickname', $rpl_namreply->getNickname()); $this->assertEquals('+', $rpl_namreply->getVisibility()); $this->assertEquals(['nickname1', 'nickname2', 'nickname3'], $rpl_namreply->getNicknames()); + $this->assertEquals([], $rpl_namreply->getPrefixes()); + } + + public function testFromIncomingMessageWithUserhostInNames() + { + $prefix = 'server'; + $verb = '353'; + $args = ['nickname', '+', '#channel', 'nickname1!user1@host1 nickname2!user2@host2 nickname3!user3@host3']; + $incoming = new IrcMessage($prefix, $verb, $args); + $rpl_namreply = NamReply::fromIncomingMessage($incoming); + + $expectedPrefixes = [ + 'nickname1' => new Prefix('nickname1', 'user1', 'host1'), + 'nickname2' => new Prefix('nickname2', 'user2', 'host2'), + 'nickname3' => new Prefix('nickname3', 'user3', 'host3') + ]; + + $this->assertEquals('server', $rpl_namreply->getServer()); + $this->assertEquals('nickname', $rpl_namreply->getNickname()); + $this->assertEquals('+', $rpl_namreply->getVisibility()); + $this->assertEquals(['nickname1', 'nickname2', 'nickname3'], $rpl_namreply->getNicknames()); + $this->assertEquals($expectedPrefixes, $rpl_namreply->getPrefixes()); } public function testFromIncomingMessageThrowsException(): void