-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 63023ef
Showing
21 changed files
with
977 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor | ||
/.idea | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 René Preuß | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# PHP Twitch Messaging Interface | ||
|
||
Inspired by [tmi.js](https://github.com/tmijs/tmi.js), [php-irc-client](https://github.com/jerodev/php-irc-client) this package is a full featured, high performance Twitch IRC client written in PHP 7.4. | ||
|
||
Also have a look at [ghostzero/tmi-cluster](https://github.com/ghostzero/tmi-cluster). TMI Cluster is a Laravel package that makes the PHP TMI client scalable. | ||
|
||
## Features | ||
|
||
- Connecting to Twitch IRC with SSL | ||
- Generic IRC Commands | ||
- Supports Twitch IRC Tags (IRC v3) | ||
- Supports Twitch IRC Membership | ||
- Supports Twitch IRC Commands | ||
|
||
## Getting Started (w/o OAuth Token) | ||
|
||
```php | ||
use GhostZero\Tmi\Channel; | ||
use GhostZero\Tmi\Client; | ||
use GhostZero\Tmi\ClientOptions; | ||
use GhostZero\Tmi\Tags; | ||
|
||
$client = new Client(new ClientOptions([ | ||
'connection' => [ | ||
'secure' => true, | ||
'reconnect' => true, | ||
'rejoin' => true, | ||
], | ||
'channels' => ['ghostzero'] | ||
])); | ||
|
||
$client->on('message', function (Channel $channel, Tags $tags, string $user, string $message, bool $self) use ($client) { | ||
print "{$tags['display-name']}: {$message}"; | ||
}); | ||
|
||
$client->connect(); | ||
``` | ||
|
||
## Getting Started (w/ OAuth Token) | ||
|
||
```php | ||
use GhostZero\Tmi\Channel; | ||
use GhostZero\Tmi\Client; | ||
use GhostZero\Tmi\ClientOptions; | ||
use GhostZero\Tmi\Tags; | ||
|
||
$client = new Client(new ClientOptions([ | ||
'options' => ['debug' => true], | ||
'connection' => [ | ||
'secure' => true, | ||
'reconnect' => true, | ||
'rejoin' => true, | ||
], | ||
'identity' => [ | ||
'username' => 'ghostzero', | ||
'password' => 'oauth:...', | ||
], | ||
'channels' => ['ghostzero'] | ||
])); | ||
|
||
$client->on('message', function (Channel $channel, Tags $tags, string $user, string $message, bool $self) use ($client) { | ||
if ($self) return; | ||
|
||
if (strtolower($message) === '!hello') { | ||
$client->say($channel->getName(), "@{$user}, heya!"); | ||
} | ||
}); | ||
|
||
$client->connect(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "ghostzero/tmi", | ||
"description": "PHP Twitch Messaging Interface", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "René Preuß", | ||
"email": "rene@preuss.io" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"GhostZero\\Tmi\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"php": "^7.4", | ||
"react/socket": "^1.6", | ||
"ext-mbstring": "*" | ||
}, | ||
"minimum-stability": "dev" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace GhostZero\Tmi; | ||
|
||
class Channel | ||
{ | ||
private string $channel; | ||
|
||
private string $topic = ''; | ||
|
||
private array $users = []; | ||
|
||
public function __construct(string $channel) | ||
{ | ||
$this->channel = $channel; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->channel; | ||
} | ||
|
||
public function getTopic(): string | ||
{ | ||
return $this->topic; | ||
} | ||
|
||
public function setTopic(string $topic): void | ||
{ | ||
$this->topic = $topic; | ||
} | ||
|
||
public function getUsers(): array | ||
{ | ||
return $this->users; | ||
} | ||
|
||
public function setUsers(array $users): void | ||
{ | ||
$this->users = $users; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
namespace GhostZero\Tmi; | ||
|
||
use Closure; | ||
use GhostZero\Tmi\Events\EventHandler; | ||
use GhostZero\Tmi\Messages\IrcMessage; | ||
use GhostZero\Tmi\Messages\IrcMessageParser; | ||
use React\Dns\Resolver\Factory; | ||
use React\EventLoop\LoopInterface; | ||
use React\Promise\Promise; | ||
use React\Socket\ConnectionInterface; | ||
use React\Socket\DnsConnector; | ||
use React\Socket\SecureConnector; | ||
use React\Socket\TcpConnector; | ||
use RuntimeException; | ||
|
||
class Client | ||
{ | ||
use Traits\Irc; | ||
|
||
protected ConnectionInterface $connection; | ||
|
||
protected bool $connected; | ||
|
||
protected LoopInterface $loop; | ||
|
||
protected ClientOptions $options; | ||
|
||
protected IrcMessageParser $ircMessageParser; | ||
|
||
protected array $channels = []; | ||
|
||
protected EventHandler $eventHandler; | ||
|
||
public function __construct(ClientOptions $options) | ||
{ | ||
$this->options = $options; | ||
$this->loop = \React\EventLoop\Factory::create(); | ||
$this->ircMessageParser = new IrcMessageParser(); | ||
$this->eventHandler = new EventHandler(); | ||
} | ||
|
||
public function connect(): void | ||
{ | ||
$tcpConnector = new TcpConnector($this->loop); | ||
$dnsResolverFactory = new Factory(); | ||
$dns = $dnsResolverFactory->createCached($this->options->getNameserver(), $this->loop); | ||
$dnsConnector = new DnsConnector($tcpConnector, $dns); | ||
$connectorPromise = $this->getConnectorPromise($dnsConnector); | ||
|
||
$connectorPromise->then(function (ConnectionInterface $connection) { | ||
$this->connection = $connection; | ||
$this->connected = true; | ||
|
||
// login & request all twitch Kappabilities | ||
$identity = $this->options->getIdentity(); | ||
$this->write("PASS {$identity['password']}"); | ||
$this->write("NICK {$identity['username']}"); | ||
$this->write('CAP REQ :twitch.tv/membership twitch.tv/tags twitch.tv/commands'); | ||
|
||
$channels = $this->options->getChannels(); | ||
|
||
foreach ($channels as $channel) { | ||
$this->join($channel); | ||
} | ||
|
||
$this->connection->on('data', function ($data) { | ||
foreach ($this->ircMessageParser->parse($data) as $message) { | ||
$this->handleIrcMessage($message); | ||
} | ||
}); | ||
|
||
$this->connection->on('close', function () { | ||
$this->connected = false; | ||
}); | ||
$this->connection->on('end', function () { | ||
$this->connected = false; | ||
}); | ||
}); | ||
|
||
$this->loop->run(); | ||
} | ||
|
||
public function close(): void | ||
{ | ||
if ($this->isConnected()) { | ||
$this->connection->close(); | ||
$this->loop->stop(); | ||
} | ||
} | ||
|
||
public function write(string $rawCommand): void | ||
{ | ||
if (!$this->isConnected()) { | ||
throw new RuntimeException('No open connection was found to write commands to.'); | ||
} | ||
|
||
// Make sure the command ends in a newline character | ||
if (mb_substr($rawCommand, -1) !== "\n") { | ||
$rawCommand .= "\n"; | ||
} | ||
|
||
$this->connection->write($rawCommand); | ||
} | ||
|
||
private function isConnected(): bool | ||
{ | ||
return isset($this->connection) && $this->connected; | ||
} | ||
|
||
private function handleIrcMessage(IrcMessage $message): void | ||
{ | ||
if ($this->options->isDebug()) { | ||
print $message->rawMessage . PHP_EOL; | ||
} | ||
|
||
$message->injectChannel($this->channels); | ||
$message->handle($this); | ||
|
||
foreach ($message->getEvents() as $event) { | ||
$this->eventHandler->invoke($event); | ||
} | ||
} | ||
|
||
public function getLoop(): LoopInterface | ||
{ | ||
return $this->loop; | ||
} | ||
|
||
public function getOptions(): ClientOptions | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function on(string $event, Closure $closure): self | ||
{ | ||
$this->eventHandler->addHandler($event, $closure); | ||
|
||
return $this; | ||
} | ||
|
||
private function getConnectorPromise(DnsConnector $dnsConnector): Promise | ||
{ | ||
if ($this->options->shouldConnectSecure()) { | ||
return (new SecureConnector($dnsConnector, $this->loop)) | ||
->connect('irc.chat.twitch.tv:6697'); | ||
} | ||
|
||
return $dnsConnector->connect('irc.chat.twitch.tv:6667'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace GhostZero\Tmi; | ||
|
||
class ClientOptions | ||
{ | ||
private array $options; | ||
|
||
public function __construct(array $options) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
public function isDebug(): bool | ||
{ | ||
return $this->options['options']['debug'] ?? false; | ||
} | ||
|
||
public function getIdentity(): array | ||
{ | ||
$default = ['username' => 'justinfan1337', 'password' => null]; | ||
return $this->options['identity'] ?? $default; | ||
} | ||
|
||
public function getChannels(): array | ||
{ | ||
return $this->options['channels'] ?? []; | ||
} | ||
|
||
public function getNickname(): string | ||
{ | ||
return $this->options['identity']['username'] ?? 'justinfan1337'; | ||
} | ||
|
||
public function shouldAutoRejoin(): bool | ||
{ | ||
return $this->options['connection']['rejoin'] ?? true; | ||
} | ||
|
||
public function shouldReconnect(): bool | ||
{ | ||
return $this->options['connection']['reconnect'] ?? true; | ||
} | ||
|
||
public function shouldConnectSecure(): bool | ||
{ | ||
return $this->options['connection']['secure'] ?? true; | ||
} | ||
|
||
public function getNameserver(): string | ||
{ | ||
return $this->options['connection']['nameserver'] ?? '1.1.1.1'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace GhostZero\Tmi\Events; | ||
|
||
class Event | ||
{ | ||
private string $event; | ||
|
||
private array $arguments; | ||
|
||
public function __construct(string $event, array $arguments = []) | ||
{ | ||
$this->event = $event; | ||
$this->arguments = $arguments; | ||
} | ||
|
||
public function getArguments(): array | ||
{ | ||
return $this->arguments; | ||
} | ||
|
||
public function getEvent(): string | ||
{ | ||
return $this->event; | ||
} | ||
} |
Oops, something went wrong.