Skip to content

Instantly share code, notes, and snippets.

@sarahhodne
Created May 12, 2009 12:01
Show Gist options
  • Save sarahhodne/110435 to your computer and use it in GitHub Desktop.
Save sarahhodne/110435 to your computer and use it in GitHub Desktop.
<?php
$conf = array(
'startmodules' => array('m_basic','m_clueirc'),
'server' => array(
'host' => 'lanserv',
'port' => 6667),
'nick' => "modbot",
'daemon' => 0,
//Begin m_basic.php conf
'version' => 'Php-bot-thingy. contact hawken for sourcecode',
);
?>
<?php
class ircbotd{
function register($modname, $arr){
foreach($arr as $func){
$func = strtolower($func);
echo "$modname -> $func\n";
switch($func){
case 'quit': $this->mods[$modname]['func']['quit'] = 1; break;
case 'privmsg_action': $this->mods[$modname]['func']['privmsg_action'] = 1; break; //DONE
case 'notice': $this->mods[$modname]['func']['notice'] = 1; break; //DONE
case 'mode': $this->mods[$modname]['func']['mode'] = 1; break;
case 'kick': $this->mods[$modname]['func']['kick'] = 1; break; //DONE
case 'part': $this->mods[$modname]['func']['part'] = 1; break;
case 'join': $this->mods[$modname]['func']['join'] = 1; break;
case 'invite': $this->mods[$modname]['func']['invite'] = 1; break; //DONE
case 'ctcp': $this->mods[$modname]['func']['ctcp'] = 1; break; //DONE
case 'num': $this->mods[$modname]['func']['num'] = 1; break; //DONE
case 'ping': $this->mods[$modname]['func']['ping'] = 1; break; //DONE
case 'privmsg': $this->mods[$modname]['func']['privmsg'] = 1; break; //DONE
}
}
}
function unload($modname){
if(!isset($this->mods[$modname])) return 1;
unset($this->mods[$modname]);
}
function addmodule($modname){
if(isset($this->mods[$mod])) return 2;
if(!include("modules/".$modname.".php")){
return 1;
}
$this->mods[$modname]['mod'] = new $modname;
$this->mods[$modname]['mod']->init($this);
$this->register($modname, $functions);
return 3;
}
function prt($line){
if(!$this->daemon) echo $line;
}
function send($data){
$echo = str_replace(array("\r\n","\n"), array("\n","\n"), $data);
$data = str_replace(array("\r\n","\n"), array("\r\n","\r\n"), $data);
if(substr($echo, -1) == "\n") $echo = substr($echo, 0, -1);
$echo = explode("\n", $echo);
foreach($echo as $m00)$this->prt("[SEND] $m00\n");
return fwrite($this->sock, $data, strlen($data));
}
function recieve(){
$data = fgets($this->sock, 999);
$echo = str_replace(array("\r\n","\n"), array("\n","\n"), $data);
if(substr($echo, -1) == "\n") $echo = substr($echo, 0, -1);
$this->prt("[RECV] $echo\n");
return $echo;
}
function daemon(){
$this->daemon = 1;
$pid = pcntl_fork();
if($pid == -1){echo "Failed forking\n"; $this->daemon = 0;}
else if($pid)die("Daemon started with PID $pid.\n");
}
function start(){
include("config.php");
$this->config = $conf;
if($this->config['daemon']) $this->daemon();
$this->connect();
}
function connect(){
$this->sock = fsockopen($this->config['server']['host'], $this->config['server']['port'], $errno, $errstr);
if(!$this->sock) die("($errno) $errstr\n");
socket_set_blocking($this->sock, 1);
foreach($this->config['startmodules'] as $mod)
$this->addmodule($mod);
$this->sayhi();
$this->loop();
}
function sayhi(){
$this->send("PASS NOPASS\n");
$this->send("USER {$this->config['nick']} USING PHP IRC\n");
$this->send("NICK {$this->config['nick']}\n");
}
function loop(){
while(!feof($this->sock)){
$raw = $this->recieve();
$this->parse_raw($raw);
}
}
function parse_raw($raw){
$regexbase['server'] = '\:(?<nick>[\.A-Za-z0-9_-]*)';
$regexbase['nick'] = '(?<nick>[\\A-Za-z0-9_-]*)';
$regexbase['host'] = '(?<host>[\~A-Za-z0-9_-]*\@[\.A-Za-z0-9_-]*)';
$regexbase['id'] = '\:{'.$regexbase['nick'].'\!'.$regexbase['host'].'|'.$regexbase['server'].'}';
$regexbase['place'] = '(?<place>.[^\ ]*)';
$regex['mode'] = '/^'.$regexbase['id'].' mode '.$regexbase['place'].' (?<tags>.*)$/';
$regex['kick'] = '/^'.$regexbase['id'].' kick '.$regexbase['place'].' (.[^\ ]*) \:(?<line>.*)$/i';
$regex['invite'] = '/^'.$regexbase['id'].' invite (.[^\ ]*) '.$regexbase['place'].' \:(?<line>.*)$/i';
$regex['notice'] = '/^'.$regexbase['id'].' notice '.$regexbase['place'].' \:(?<line>.*)$/i';
$regex['privmsg'] = '/^'.$regexbase['id'].' privmsg '.$regexbase['place'].' \:(?<line>.*)$/i';
$regex['privmsg_action'] = '/^'.$regexbase['id'].' privmsg '.$regexbase['place'].' \:\x01ACTION (?<line>.*)\x01$/i';
$regex['ctcp'] = '/^'.$regexbase['id'].' privmsg '.$regexbase['place'].' \:\x01(?<line>.*)\x01$/i';
$regex['num'] = '/^'.$regexbase['id'].' (?<num>[0-9]*) (.*) \:(?<line>.*)$/i';
$regex['ping'] = '/^ping \:(?<string>.*)$/i';
$exp = explode(" ", $raw);
if(preg_match($regex['ping'], $raw, $m))
$this->event_ping($m['string']);
else if(preg_match($regex['num'], $raw, $m))
$this->event_num($m['nick'], $m['num'], $m['line']);
else if(preg_match($regex['privmsg_action'], $raw, $m))
$this->event_privmsg_action($m['nick'], $m['place'], $m['line'], $m['host']);
else if(preg_match($regex['ctcp'], $raw, $m))
$this->event_ctcp($m['nick'], $m['place'], $m['line'], $m['host']);
else if(preg_match($regex['privmsg'], $raw, $m))
$this->event_privmsg($m['nick'], $m['place'], $m['line'], $m['host']);
else if(preg_match($regex['notice'], $raw, $m))
$this->event_notice($m['nick'], $m['place'], $m['line'], $m['host']);
else if(preg_match($regex['invite'], $raw, $m))
$this->event_invite($m['nick'], $m['place'], $m['line'], $m['host']);
else if(preg_match($regex['kick'], $raw, $m))
$this->event_kick($m['nick'], $m['place'], $m['line'], $m['host']);
}
function event_notice($from, $to, $line, $host){
foreach($this->mods as $key=>$val){
if($val['func']['notice']) $this->mods[$key]['mod']->event_notice($from, $to, $line, $host);
}
}
function event_invite($from, $to, $line, $host){
foreach($this->mods as $key=>$val){
if($val['func']['invite']) $this->mods[$key]['mod']->event_invite($from, $to, $line, $host);
}
}
function event_kick($from, $to, $line, $host){
foreach($this->mods as $key=>$val){
if($val['func']['kick']) $this->mods[$key]['mod']->event_kick($from, $to, $line, $host);
}
}
function event_privmsg_action($from, $to, $line, $host){
foreach($this->mods as $key=>$val){
if($val['func']['privmsg']) $this->mods[$key]['mod']->event_privmsg_action($from, $to, $line, $host);
}
}
function event_privmsg($from, $to, $line, $host){
foreach($this->mods as $key=>$val){
if($val['func']['privmsg']) $this->mods[$key]['mod']->event_privmsg($from, $to, $line, $host);
}
}
function event_ctcp($from, $to, $line, $host){
foreach($this->mods as $key=>$val){
if($val['func']['ctcp']) $this->mods[$key]['mod']->event_ctcp($from, $to, $line, $host);
}
}
function event_num($from, $num, $line){
foreach($this->mods as $key=>$val){
if($val['func']['num']) $this->mods[$key]['mod']->event_num($from, $num, $line);
}
}
function event_ping($line){
foreach($this->mods as $key=>$val){
if($val['func']['ping']) $this->mods[$key]['mod']->event_ping($line);
}
}
}
$irc = new ircbotd;
$irc->start();
die("End of script\n");
?>
<?php
class m_basic{
function init(&$irc){
$this->irc = &$irc;
}
function event_ping($line){
$this->irc->send("PONG :$line\n");
}
function event_num($from, $num, $line){
switch($num){
case 433:
$this->irc->config['nick'] = $this->irc->config['nick']."_".rand(0, 100);
$this->irc->send("NICK {$this->irc->config['nick']}\n");
}
}
function event_ctcp($from, $to, $line, $user, $host){
switch(strtolower($line)){
case 'version':
$this->irc->send("NOTICE $from :\x01VERSION {$this->irc->config['version']}\x01\n");
break;
}
}
}
$functions = array('ping', 'num', 'ctcp');
?>
<?php
class m_clueirc{
function init(&$irc){
$this->irc = &$irc;
}
}
$functions = array();
?>
<?php
class m_debug{
function init($irc){
$this->irc = &$irc;
}
function event_privmsg($a, $variable, $or, $more){
echo "event_privmsg($a, $variable, $or, $more);\n";
}
function event_ping($line){
echo "event_ping($line);\n";
}
function event_ctcp($a, $variable, $or, $more){
echo "event_ctcp($a, $variable, $or, $more);\n";
}
function event_privmsg_action($a, $variable, $or, $more){
echo "event_privmsg_action($a, $variable, $or, $more);\n";
}
function event_num($i, $like, $banana){
echo "event_num($i, $like, $banana);\n";
}
function event_notice($a, $variable, $or, $more){
echo "event_notice($a, $variable, $or, $more);\n";
}
function event_invite($a, $variable, $or, $more){
echo "event_invite($a, $variable, $or, $more);\n";
}
function event_kick($a, $variable, $or, $more){
echo "event_kick($a, $variable, $or, $more);\n";
}
}
$functions = array('privmsg', 'ping', 'ctcp', 'privmsg_action', 'num', 'notice', 'invite', 'kick');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment