Skip to content

Instantly share code, notes, and snippets.

@Gkiokan
Created July 8, 2022 21:17
Show Gist options
  • Save Gkiokan/9ca1fc753640653c232ec83173c007f8 to your computer and use it in GitHub Desktop.
Save Gkiokan/9ca1fc753640653c232ec83173c007f8 to your computer and use it in GitHub Desktop.
IRC Client PHP
<?php
namespace App\Http\Controllers\WCX;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class IRCClient extends Controller
{
protected $socket;
protected $nick;
protected $pass;
static $host = "yourhost.com";
static $port = "6667";
public $m = [];
public function __construct($nick=null, $pass=null)
{
$this->nick = $nick;
$this->pass = $pass;
}
public function connect()
{
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (socket_connect($this->socket, self::$host, self::$port) === FALSE) {
return null;
}
$this->m = [];
$this->m[] = "Connect Socket connection " . self::$host . ' ' . self::$port;
// $this->authenticate();
$this->setNick();
}
public function authenticate()
{
$this->send(sprintf("PASS %s", $this->pass));
}
public function setNick()
{
$this->send(sprintf("NICK %s", $this->nick));
$this->send(sprintf("USER %s 0 * %s", $this->nick, $this->nick));
}
public function joinChannel($channel)
{
$this->send(sprintf("JOIN %s", $channel));
}
public function getLastError()
{
return socket_last_error($this->socket);
}
public function isConnected()
{
return !is_null($this->socket);
}
public function read($size = 256)
{
if (!$this->isConnected()) {
$this->m[] = "Not connected";
return null;
}
$content = socket_read($this->socket, $size);
$this->m[] = "[READ] " . $content;
return $content;
}
public function send($message)
{
if (!$this->isConnected()) {
$this->m[] = "Not connected";
return null;
}
$this->m[] = "[SEND] " . $message;
return socket_write($this->socket, $message . "\r\n");
}
public function close()
{
$this->m[] = "Closing Socket connection";
socket_close($this->socket);
}
public function getLogs(){
return $this->m;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment