Browse Source

implement rooms navigation

main
ghost 7 months ago
parent
commit
1d71d667bc
  1. 23
      config/example.json
  2. 140
      src/Server/Ratchet.php

23
config/example.json

@ -11,7 +11,11 @@
}, },
"wallet": "wallet":
{ {
"namespace":"", "namespace":
{
"whitelist":[],
"blacklist":[]
},
"account":null "account":null
}, },
"event": "event":
@ -60,7 +64,7 @@
"debug": "debug":
{ {
"enabled":true, "enabled":true,
"template":"[{time}] [init] listen on {host}:{port} balance: {keva}" "template":"[{time}] [init] listen on {host}:{port} balance: {keva} allowed: {room}"
} }
}, },
"open": "open":
@ -84,13 +88,26 @@
{ {
"success": "success":
[ [
"\u001b[34mWell, enter your message (dot to commit)\u001b[0m" "\u001b[34mWell, select room number\u001b[0m",
"\u001b[35m{room:list}\u001b[0m"
], ],
"failure": "failure":
[ [
"\u001b[31mIncorrect captcha code, try again later!\u001b[0m" "\u001b[31mIncorrect captcha code, try again later!\u001b[0m"
] ]
}, },
"room":
{
"success":
[
"\u001b[34mRoom changed to \u001b[35m{room}\u001b[0m",
"\u001b[34mEnter your message (dot to commit)\u001b[0m"
],
"failure":
[
"\u001b[31mRequested room number not found, try again!\u001b[0m"
]
},
"submit": "submit":
{ {
"success": "success":

140
src/Server/Ratchet.php

@ -10,6 +10,8 @@ class Ratchet implements MessageComponentInterface
private object $_config; private object $_config;
private array $_namespaces;
public function __construct( public function __construct(
object $config object $config
) { ) {
@ -31,6 +33,50 @@ class Ratchet implements MessageComponentInterface
throw new \Exception(); // @TODO throw new \Exception(); // @TODO
} }
// Init allowed chat rooms registry
if ($namespaces = $this->_kevacoin->kevaListNamespaces())
{
$i = 1; foreach ((array) $namespaces as $namespace)
{
// Skip system namespaces
if (str_starts_with($namespace['displayName'], '_'))
{
continue;
}
// Skip blacklist
if (in_array($namespace['namespaceId'], $this->_config->kevacoin->wallet->namespace->blacklist))
{
continue;
}
// Check whitelist on contain records
if (count($this->_config->kevacoin->wallet->namespace->whitelist) &&
!in_array($namespace['namespaceId'], $this->_config->kevacoin->wallet->namespace->whitelist))
{
continue;
}
// Append room to the namespace registry
$this->_namespaces[$i++] = [
'hash' => $namespace['namespaceId'],
'name' => $namespace['displayName']
];
}
// Sort rooms by name ASC
array_multisort(
array_column(
$this->_namespaces,
'name'
),
SORT_ASC,
SORT_STRING | SORT_NATURAL | SORT_FLAG_CASE
);
}
else throw new \Exception(); // @TODO
// Dump event on enabled // Dump event on enabled
if ($this->_config->nps->event->init->debug->enabled) if ($this->_config->nps->event->init->debug->enabled)
{ {
@ -40,7 +86,8 @@ class Ratchet implements MessageComponentInterface
'{time}', '{time}',
'{host}', '{host}',
'{port}', '{port}',
'{keva}' '{keva}',
'{room}'
], ],
[ [
(string) date('c'), (string) date('c'),
@ -48,6 +95,10 @@ class Ratchet implements MessageComponentInterface
(string) $this->_config->nps->server->port, (string) $this->_config->nps->server->port,
(float) $this->_kevacoin->getBalance( (float) $this->_kevacoin->getBalance(
$this->_config->kevacoin->wallet->account $this->_config->kevacoin->wallet->account
),
(string) print_r(
$this->_namespaces,
true
) )
], ],
$this->_config->nps->event->init->debug->template $this->_config->nps->event->init->debug->template
@ -104,6 +155,9 @@ class Ratchet implements MessageComponentInterface
// Init connection confirmed // Init connection confirmed
$connection->confirmed = false; $connection->confirmed = false;
// Init connection room
$connection->namespace = null;
// Init connection counter // Init connection counter
$connection->count = 0; $connection->count = 0;
@ -179,6 +233,9 @@ class Ratchet implements MessageComponentInterface
// Connection confirmed // Connection confirmed
if ($connection->confirmed) if ($connection->confirmed)
{
// Room selected, begin message compose
if ($connection->namespace)
{ {
// Check message commit by dot // Check message commit by dot
if ($request == '.') if ($request == '.')
@ -198,7 +255,7 @@ class Ratchet implements MessageComponentInterface
else else
{ {
// Save massage to KevaCoin blockchain // Save massage to KevaCoin blockchain
if ($txid = $this->_kevacoin->kevaPut($this->_config->kevacoin->wallet->namespace, time(), $connection->message)) if ($txid = $this->_kevacoin->kevaPut($connection->namespace, time(), $connection->message))
{ {
// Return success response // Return success response
$connection->send( $connection->send(
@ -208,7 +265,7 @@ class Ratchet implements MessageComponentInterface
'{txid}' '{txid}'
], ],
[ [
(string) $this->_config->kevacoin->wallet->namespace, (string) $connection->namespace,
(string) $txid (string) $txid
], ],
implode( implode(
@ -235,7 +292,7 @@ class Ratchet implements MessageComponentInterface
(string) date('c'), (string) date('c'),
(string) $connection->remoteAddress, (string) $connection->remoteAddress,
(string) $connection->resourceId, (string) $connection->resourceId,
(string) $this->_config->kevacoin->wallet->namespace, (string) $connection->namespace,
(string) $txid, (string) $txid,
(string) $this->_kevacoin->getBalance( (string) $this->_kevacoin->getBalance(
$this->_config->kevacoin->wallet->account $this->_config->kevacoin->wallet->account
@ -293,27 +350,99 @@ class Ratchet implements MessageComponentInterface
} }
} }
// Captcha request // Room not selected yet and number given match registry
else if (isset($this->_namespaces[$request]))
{
// Update connection namespace
$connection->namespace = $this->_namespaces[$request]['hash'];
// Send room selection request
$connection->send(
str_ireplace(
[
'{room}'
],
[
str_replace( // filter possible meta mask in the room names
'%',
'%%',
$this->_namespaces[$request]['name']
)
],
implode(
PHP_EOL,
$config->response->room->success
) . PHP_EOL
)
);
}
// Room number not found in registry
else
{
// Reset connection room anyway
$connection->namespace = null;
// Send fail response
$connection->send(
implode(
PHP_EOL,
$config->response->room->failure
) . PHP_EOL
);
// Keep connection alive for new attempt..
}
}
// Captcha confirmation code expected
else else
{ {
// Request match captcha // Request match captcha
if ($request == $connection->captcha) if ($request == $connection->captcha)
{ {
// Set connection confirmed
$connection->confirmed = true; $connection->confirmed = true;
// Build room list
$rooms = [];
foreach ($this->_namespaces as $number => $namespace)
{
$rooms[] = sprintf(
'[%d] %s',
$number,
$namespace['name']
);
}
// Send room selection request
$connection->send( $connection->send(
str_ireplace(
[
'{room:list}'
],
[
implode(
PHP_EOL,
$rooms
)
],
implode( implode(
PHP_EOL, PHP_EOL,
$config->response->captcha->success $config->response->captcha->success
) . PHP_EOL ) . PHP_EOL
)
); );
} }
// Captcha request invalid // Captcha request invalid
else else
{ {
// Reset confirmed status
$connection->confirmed = false; $connection->confirmed = false;
// Send fail response
$connection->send( $connection->send(
implode( implode(
PHP_EOL, PHP_EOL,
@ -321,6 +450,7 @@ class Ratchet implements MessageComponentInterface
) . PHP_EOL ) . PHP_EOL
); );
// Drop connection to prevent brute force
$connection->close(); $connection->close();
} }
} }

Loading…
Cancel
Save