Zie hieronder :-):
PHP
<?php
ini_set('max_execution_time', -1);
class configuration {
public $botConfig;
public function __construct() {
$this->botConfig = array();
}
public function createNewBot($hostname, $identity, $server, $realname, $nickname, $port, $domain) {
if (count($this->botConfig) == 0) {
$botConfigID = 0;
} else {
$botConfigID = count($this->botConfig);
}
$this->botConfig[$botConfigID]['hostname'] = $hostname;
$this->botConfig[$botConfigID]['identity'] = $identity;
$this->botConfig[$botConfigID]['server'] = $server;
$this->botConfig[$botConfigID]['realname'] = $realname;
$this->botConfig[$botConfigID]['nickname'] = $nickname;
$this->botConfig[$botConfigID]['port'] = $port;
$this->botConfig[$botConfigID]['domain'] = $domain;
return $botConfigID;
}
}
class socket {
public $sockets;
public $socketError;
public function __construct() {
$this->sockets = array();
$this->socketError = array();
}
public function create_socket($domain = 'ipv4') {
if ($domain == 'ipv6') {
$this->sockets[] = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
} else {
$this->sockets[] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
}
$insertedArrayID = (count($this->sockets) - 1);
if ($this->sockets[$insertedArrayID] == false) {
$this->$socketError[$insertedArrayID]['errorCode'] = socket_last_error($this->sockets[$insertedArrayID]);
$this->$socketError[$insertedArrayID]['errorMessage'] = socket_strerror($errorCode);
socket_clear_error($this->sockets[$insertedArrayID]);
return $socketError;
} else {
return $insertedArrayID;
}
}
public function bind_socket($socketID = 0, $hostname) {
if (is_int($socketID)) {
/* We can not make any more sockets until SO_REUSEPORT comes available on linux */
socket_set_option($this->sockets[$socketID], SOL_SOCKET, SO_REUSEADDR, 1);
if (socket_bind($this->sockets[$socketID], $hostname, $this->configurationClass->botConfig[$socketID]['port']) == false) {
$this->$socketError[$socketID]['errorCode'] = socket_last_error($this->sockets[$socketID]);
$this->$socketError[$socketID]['errorMessage'] = socket_strerror($errorCode);
socket_clear_error($this->sockets[$socketID]);
return $socketError;
} else {
return true;
}
} else {
return false;
}
}
public function connect_socket($socketID = 0, $ip, $port) {
if (is_int($socketID)) {
socket_connect($this->sockets[$socketID], $ip, $port);
if ($this->sockets[$socketID] == false) {
$this-$socketError[$socketID]['errorCode'] = socket_last_error($this-$sockets[$socketID]);
$this-$socketError[$socketID]['errorMessage'] = socket_strerror($errorCode);
socket_clear_error($this-$socket[$socketID]);
return $socketError;
} else {
return true;
}
} else {
return false;
}
}
public function write_socket($socketID = 0, $bufferData) {
if (is_int($socketID)) {
socket_write($this->sockets[$socketID], $bufferData . "\r\n");
} else {
return false;
}
}
public function read_socket($socketID = 0) {
if (is_int($socketID)) {
$buffer = socket_read($this->sockets[$socketID], 65000, PHP_NORMAL_READ);
if ($buffer == '\n' OR $buffer == '\r') {
continue;
}
return $buffer;
} else {
return false;
}
}
public function shutdown_socket($socketID = 0) {
if (is_int($socketID)) {
socket_shutdown($this->sockets[$socketID], 2);
$this->close_socket[$socketID];
} else {
return false;
}
}
protected function close_socket($socketID = 0) {
if (is_int($socketID)) {
socket_close($this->sockets[$socketID]);
return;
} else {
return false;
}
}
}
class irc extends socket {
private $configurationClass;
public function setconfigrationClass($class) {
$this->configurationClass = $class;
}
public function connectIRC($botID) {
$this->create_socket($this->configurationClass->botConfig[$botID]['domain']);
$this->bind_socket($botID, $this->configurationClass->botConfig[$botID]['hostname'] . $botID);
$this->connect_socket($botID, $this->configurationClass->botConfig[$botID]['server'], $this->configurationClass->botConfig[$botID]['port']);
$this->initIRCConnection($botID);
return true;
}
protected function initIRCConnection($socketID = 0) {
$this->write_socket($socketID, 'USER ' . $this->configurationClass->botConfig[$socketID]['identity'] . ' ' .
$this->configurationClass->botConfig[$socketID]['hostname'] . ' ' .
$this->configurationClass->botConfig[$socketID]['server'] . ' :' .
$this->configurationClass->botConfig[$socketID]['realname']);
$this->write_socket($socketID, 'NICK ' . $this->configurationClass->botConfig[$socketID]['nickname']);
return true;
}
public function playPingPong($socketID = 0, $pingID) {
$this->write_socket($socketID, 'PONG ' . $pingID);
return true;
}
}
$configurationClass = new configuration();
$ircClass = new irc();
$configurationClass->createNewBot(0, 'viibot', 'irc.onlinegamesnet.net', 'vii realname', 'viibot', 6667, 'ipv4');
$ircClass->setconfigrationClass($configurationClass);
foreach ($configurationClass->botConfig AS $bot => $botData) {
$ircClass->connectIRC($bot);
}
$i = 0;
while (1) {
foreach ($configurationClass->botConfig AS $botID => $botData) {
$usableID = intval($botID);
$bufferData = $ircClass->read_socket($usableID);
$splitBuffer = split(' ', $bufferData);
if ($splitBuffer[0] = 'PING') {
$ircClass->playPingPong($usableID, $splitBuffer[1]);
}
}
}
?>
Toon Meer
Ik ga hem zelf rebuilden met het gebruik met pcntl_fork :-).
Enige wat de bot nu kan is connecten naar een irc server en idle'en :-).
in de while (1) { } loop zal je het een en ander nog wel zelf moeten defineren aan commands :).
Let op: ondanks dat er wel de functionaliteit in zit dat er meerdere sockets te gelijk kunnen opereren binnen script kan het nu niet dat 2 bots over de zelfde port luisteren (Standaard 6667).