Skip to content

Commit

Permalink
Add UserModeParser from irc-project to fix NamReply bug with userhost…
Browse files Browse the repository at this point in the history
…-in-names

Signed-off-by: NanoSector <rick@nanosector.nl>
  • Loading branch information
NanoSector committed Apr 22, 2019
1 parent c10dcd1 commit a626acf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/RPL/NamReply.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use WildPHP\Messages\Traits\ChannelTrait;
use WildPHP\Messages\Traits\NicknameTrait;
use WildPHP\Messages\Traits\ServerTrait;
use WildPHP\Messages\Utility\UserModeParser;

/**
* Class RPL_NAMREPLY
Expand Down Expand Up @@ -72,7 +73,9 @@ public static function fromIncomingMessage(IrcMessageInterface $incomingMessage)

$prefixes = [];
foreach ($nicknames as $key => $prefixString) {
$prefix = Prefix::fromString($prefixString);
$prefixStringNoModes = '';
UserModeParser::extractFromNickname($prefixString, $prefixStringNoModes);
$prefix = Prefix::fromString($prefixStringNoModes);

// no nickname means this isn't a full prefix. do not try any further.
if (empty($prefix->getNickname())) {
Expand Down
47 changes: 47 additions & 0 deletions src/Utility/UserModeParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright 2019 The WildPHP Team
*
* You should have received a copy of the MIT license with the project.
* See the LICENSE file for more information.
*/

declare(strict_types=1);

namespace WildPHP\Messages\Utility;

class UserModeParser
{
/**
* @var array
*/
protected static $prefixMap = [
'@' => 'o',
'%' => 'h',
'+' => 'v'
];

/**
* @param string $nickname
* @param string $remainders
*
* @return array
*/
public static function extractFromNickname(string $nickname, string &$remainders): array
{
$parts = str_split($nickname);
$modes = [];

foreach ($parts as $key => $part) {
if (!array_key_exists($part, self::$prefixMap)) {
$remainders = implode('', $parts);
break;
}

unset($parts[$key]);
$modes[] = self::$prefixMap[$part];
}

return $modes;
}
}

0 comments on commit a626acf

Please sign in to comment.