Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Extract useful information from connection url #30

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
86 changes: 79 additions & 7 deletions Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function __construct(HoaSocket\Client $client)
)
);

$this->registerListeners();

return;
}

Expand Down Expand Up @@ -244,17 +246,13 @@ protected function _send($message, HoaSocket\Node $node)
public function join($username, $channel, $password = null)
{
if (null !== $password) {
$this->send('PASS ' . $password);
$this->setPassword($password);
}

$this->send('USER ' . $username . ' 0 * :' . $username);

$node = $this->getConnection()->getCurrentNode();
$node->setUsername($username);
$node->setChannel($channel);
$this->setUsername($username);
$this->setNickname($username);

return $this->send('JOIN ' . $channel);
return $this->setChannel($channel);
}

/**
Expand Down Expand Up @@ -303,6 +301,43 @@ public function setNickname($nickname)
return $this->send('NICK ' . $nickname);
}

/**
* Set username.
*
* @param string $username Username.
* @return int
*/
public function setUsername($username)
{
$this->getConnection()->getCurrentNode()->setUsername($username);

return $this->send('USER ' . $username . ' 0 * :' . $username);
}

/**
* Set password.
*
* @param string $password Password.
* @return int
*/
public function setPassword($password)
{
return $this->send('PASS ' . $password);
}

/**
* Set channel.
*
* @param string $channel Channel.
* @return int
*/
public function setChannel($channel)
{
$this->getConnection()->getCurrentNode()->setChannel($channel);

return $this->send('JOIN ' . $channel);
}

/**
* Set topic.
*
Expand Down Expand Up @@ -369,4 +404,41 @@ public function parseNick($nick)

return $matches;
}

/**
* Register client listeners to interact with socket connection.
* Help to handle default actions based on socket URI
*/
protected function registerListeners()
{
$this->on('open', function (Event\Bucket $bucket) {
$source = $bucket->getSource();
$socket = $source->getConnection()->getSocket();
if (!($socket instanceof Socket)) {
return;
}

if (null !== $password = $socket->getPassword()) {
$source->setPassword($password);
}

if ($socket->getEntitytype() === Socket::CHANNEL_ENTITY) {
if (null !== $username = $socket->getUsername()) {
$source->setUsername($username);
$source->setNickname($username);
}
if (null !== $entity = $socket->getEntity()) {
$source->setChannel("#" . $entity);
}
}
if (
$socket->getEntitytype() === Socket::USER_ENTITY &&
null !== $entity = $socket->getEntity()
) {
$username = $socket->getUsername();
$source->setUsername(null === $username?$entity:$username);
$source->setNickname($entity);
}
});
}
}
Loading