IRC for kittehs, and also PHP programmers ...
We are going to have some IRC based collaboration tools (from the late 90's), we required a bot.
This repository contains a multithreaded PHP IRC bot ... because apparently I'm 15 again ...
U can haz codez ...
<?phprequire_once("vendor/autoload.php");
useDatingVIP\IRC\Connection;
useDatingVIP\IRC\Listener;
useDatingVIP\IRC\Logger;
useDatingVIP\IRC\Message;
useDatingVIP\IRC\Task;
useDatingVIP\IRC\Robot;
class Log implements Logger {
publicfunctiononSend($line) { printf("> %s\n", $line); }
publicfunctiononReceive($line) { printf("< %s\n", $line); }
}
class Repeat extends Task {
publicfunction__construct(Connection$irc, Message$msg) {
$this->irc = $irc;
$this->msg = $msg;
}
publicfunction__invoke() {
/* we can do whatever we want here: search docs, take as long as we want/need */$this->irc->msg(
$this->msg->getNick(),
$this->msg->getText());
$this->irc->msg(
$this->msg->getChannel(),
sprintf(
"%s said \"%s\" to %s",
__CLASS__,
$this->msg->getText(),
$this->msg->getNick()));
}
protected$irc;
protected$msg;
}
class Listen implements Listener {
publicfunction__construct($nick) {
$this->nick = $nick;
}
publicfunctiononReceive(Connection$irc, Message$msg) {
if ($msg->getType() == "PRIVMSG" &&
$msg->getNick() == $this->nick) {
/* returning a Task object threads the work */returnnewRepeat($irc, $msg);
}
}
protected$nick;
}
set_time_limit(0);
/* open connection to server */$connection = newConnection("irc.efnet.org", 6667);
/* make sure we see all input/output */$connection->setLogger(newLog());
/* create robot with default pool */$robot = newRobot($connection, newPool(4));
/* add listeners */$robot->addListener(
newListen("test-user2"));
/* login, join channels and enter main loop */$robot->login("bot")
->join("#devs")
->loop();
?>The example code above shows how to use the Listener and Task interfaces to respond asynchronously to specific messages.
In addition, this package includes a Manager interface for the Robot, executed synchronously by the Robot during execution, it allows the programmer to perform administration that must be performed in the same context as the Robot.
<?phprequire_once("vendor/autoload.php");
useDatingVIP\IRC\Connection;
useDatingVIP\IRC\Listener;
useDatingVIP\IRC\Logger;
useDatingVIP\IRC\Message;
useDatingVIP\IRC\Task;
useDatingVIP\IRC\Robot;
useDatingVIP\IRC\Manager;
/* ... */class Manage implements Manager {
publicfunctiononStartup(Robot$robot) {
printf("startup\n");
}
publicfunctiononJoin (Robot$robot, Message$message) {}
publicfunctiononNick (Robot$robot, Message$message) {}
publicfunctiononPart (Robot$robot, Message$message) {}
publicfunctiononPriv (Robot$robot, Message$message) {}
publicfunctiononShutdown(Robot$robot) {
printf("shutdown\n");
}
}
set_time_limit(0);
/* open connection to server */$connection = newConnection
("irc.datingvip.com", 9867, true);
/* make sure we see all input/output */$connection->setLogger(newLog());
/* create robot with default pool */$robot = newRobot($connection, newPool(4), newManage());
/* add listeners */$robot->addListener(
newListen("krakjoe"));
/* login, join channels and enter main loop */$robot->login("bot")
->join("#devs")
->loop();
?>The code above shows how to employ both the Listener and Manager functionality.