Last active
August 29, 2015 14:02
-
-
Save wpottier/4905706fe67d4d7e747e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php | |
| include_once('vendor/autoload.php'); | |
| class Bot | |
| { | |
| protected $user; | |
| protected $nick; | |
| protected $server; | |
| protected $serverPassword; | |
| /** | |
| * @var \Socket\Raw\Socket | |
| */ | |
| protected $socket; | |
| protected $ircMessageHandler; | |
| protected $commandHeader; | |
| protected $commands; | |
| public function __construct($server, $user, $nick, $serverPassword = null) | |
| { | |
| $this->server = $server; | |
| $this->user = $user; | |
| $this->nick = $nick; | |
| $this->serverPassword = $serverPassword; | |
| $this->ircMessageHandler = [ | |
| 'PING :' => array($this, 'handlePing') | |
| ]; | |
| $this->commandHeader = sprintf('%s!', $nick); | |
| // Define command | |
| $this->commands = [ | |
| 'status' => array($this, 'sendStatus'), | |
| ]; | |
| } | |
| public function run(callable $connectedCallback, $throttle = 50000) | |
| { | |
| $this->ircMessageHandler = [ | |
| 'Auth :Welcome' => $connectedCallback | |
| ]; | |
| $this->doConnect(); | |
| while (true) { | |
| $this->doRead(); | |
| usleep($throttle); | |
| } | |
| } | |
| protected function doConnect() | |
| { | |
| $factory = new \Socket\Raw\Factory(); | |
| $this->socket = $factory->createClient('tcp://licorne.bdx.la:6667'); | |
| $this->doRead(); | |
| if ($this->serverPassword) { | |
| $this->socket->write(sprintf("PASSWORD %s\r\n", $this->serverPassword)); | |
| } | |
| $this->socket->write(sprintf("USER %s\r\n", $this->user)); | |
| $this->socket->write(sprintf("NICK %s\r\n", $this->nick)); | |
| } | |
| protected function doRead() | |
| { | |
| try { | |
| $line = $this->socket->recv(1024, MSG_DONTWAIT); | |
| echo $line; | |
| foreach ($this->ircMessageHandler as $command => $callback) { | |
| if (strpos($line, $command) !== false) { | |
| $data = explode($command, $line); | |
| $data = explode("\r\n", $data[1]); | |
| $callback(trim($data[0])); | |
| } | |
| } | |
| } catch (Socket\Raw\Exception $ex) { | |
| if ($ex->getCode() != 35) { | |
| var_dump($ex); | |
| die(); | |
| } | |
| } | |
| } | |
| public function joinChannel($channel, $message = 'Bonjour je suis un Bot en PHP') | |
| { | |
| $this->socket->write(sprintf("JOIN %s\r\n", $channel)); | |
| $this->ircMessageHandler[sprintf('PRIVMSG %s :', $channel)] = function ($data) use ($channel) { | |
| $this->handleMessage($channel, $data); | |
| }; | |
| if ($message && strlen($message) > 0) { | |
| $this->socket->write(sprintf("PRIVMSG %s :%s\r\n", $channel, $message)); | |
| } | |
| } | |
| protected function handlePing($data) | |
| { | |
| $this->socket->write(sprintf('PONG :%s', $data)); | |
| } | |
| protected function handleMessage($channel, $data) | |
| { | |
| if (strpos($data, $this->commandHeader) !== 0) { | |
| return; | |
| } | |
| $data = substr($data, strlen($this->commandHeader)); | |
| $dataExploded = explode(' ', $data, 2); | |
| if(!array_key_exists(0, $dataExploded)) { | |
| $this->sendLine($channel, 'No command found in message!'); | |
| } | |
| $command = $dataExploded[0]; | |
| $args = null; | |
| if(array_key_exists(1, $dataExploded)) { | |
| $args = $dataExploded[1]; | |
| } | |
| if(!array_key_exists($command, $this->commands)) { | |
| $this->sendLine($channel, 'Unavailable command!'); | |
| } | |
| $this->commands[$command]($channel, $args); | |
| } | |
| protected function sendStatus($channel, $args) | |
| { | |
| $this->sendLine($channel, sprintf('Octower Bot v.Pouet - %s', gethostname())); | |
| } | |
| protected function sendLine($channel, $line) | |
| { | |
| $this->socket->write(sprintf("PRIVMSG %s :%s\r\n", $channel, $line)); | |
| } | |
| } | |
| $bot = new Bot('tcp://puet:6667', 'octower ecash-web1 bla : Bottine', 'octopouet', 'toto'); | |
| $bot->run(function () use ($bot) { | |
| $bot->joinChannel('#test', null); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment