Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for userhost-in-names capability (fixes #7) #20

Merged
merged 1 commit into from
Apr 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/RPL/NamReply.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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: <nickname, Prefix>
*/
protected $prefixes = [];

/**
* @param IrcMessageInterface $incomingMessage
*
Expand All @@ -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());

Expand Down Expand Up @@ -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;
}
}
23 changes: 23 additions & 0 deletions tests/RPL_NamReplyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down