All pastes #988352 Raw Edit

Norbot 1

public php v1 · immutable
#988352 ·published 2008-04-17 14:30 UTC
rendered paste body
//===================================================================//========================CLASS.PHP==================================//===================================================================<?phpdefine("VERSION", "1.0");set_time_limit(0); // Set time limit at 0, so it wont be cut off after 30secerror_reporting(E_ALL); // Make it report all errors.class ZeroBot {    // Class variables    var $CONFIG = Array();    var $con = array();    var $buffer = array();    var $perform = true;    var $onText = Array();    var $onJoin = Array();    var $onPart = Array();    var $onCtcp = Array();    var $onUserMode = Array();    var $onNumeric = Array();    var $channels = Array();    var $usersOnChannel = Array();    function ZeroBot ()  {        // Define some default values.        $this->CONFIG['nick'] = "Norbot36";        $this->CONFIG['ident'] = "Norek Bot ". VERSION;        $this->buffer['old_buffer'] = "";        $this->buffer['new'] = false;        $this->CONFIG['admin'] = "";    }    function connect () {        // if configured, connect.        if (count($this->CONFIG) >= 4) {            $this->con['socket'] = fsockopen($this->CONFIG['server'], $this->CONFIG['port']);            if (!$this->con['socket']) {                $this->error("Could not connect to: ". $this->CONFIG['server'] ." on port ". $this->CONFIG['port']);            } else {                   $this->send("USER ". $this->CONFIG['nick'] ." ". $this->CONFIG['server'] ." ". $this->CONFIG['server'] ." :". $this->CONFIG['ident']);                $this->send("NICK ". $this->CONFIG['nick'] ." ". $this->CONFIG['server']);            }        } else {            $this->error("Not configured correctly!");        }    }    function newData () {        // check if there's new data to be read, and handle if ping.        $this->buffer['new'] = false;        $data = trim(fgets($this->con['socket']));        if (!empty($data)) {            $this->buffer['all'] = $data;            if(substr($data, 0, 6) == 'PING :') {                $this->send('PONG :'.substr($data, 6), false);                if ($this->perform) {                    $this->perform();                                }                return false;            }            $this->parse_buffer();            $this->buffer['new'] = true;            return true;        }        return false;    }    function perform () {        // actions to be done on connect.        $this->join($this->CONFIG['chan'], $this->CONFIG['key']);        $this->perform = false;        }        function printData () {        //  output the data        if ($this->buffer['new'] && count($this->buffer['parsed']) > 4) {            echo date("[H:i:s]") ."{".$this->buffer['parsed']['username']." / ". $this->buffer['parsed']['command'] ." / ". $this->buffer['parsed']['channel'] ."} ". $this->buffer['parsed']['text']. "\n\r";        }    }    function writeToLog () {        // Functie waarmee je logs kan maken.        $file = "C:\web\www\bot2\Logs\\" . $this->buffer['parsed']['channel'] . ".txt";        $fh = fopen($file, 'a');        fwrite($fh, date("[H:i:s]") ."{".$this->buffer['parsed']['username']." / ". $this->buffer['parsed']['command'] ." / ". $this->buffer['parsed']['channel'] ."} ". $this->buffer['parsed']['text']. "\n");        fclose($fh);    }        function events () {        // check which event needs to be triggered, and trigger it.        if (!empty($this->buffer['parsed']['command'])) {            if (strtoupper($this->buffer['parsed']['command']) == "PRIVMSG") {                if (substr(trim($this->buffer['parsed']['text']),0,1) == chr(001)) {                    $this->onCtcp(trim($this->buffer['parsed']['text']));                } elseif (!empty($this->CONFIG['admin']) && strtolower($this->buffer['parsed']['username']) == strtolower($this->CONFIG['admin'])) {                    $this->onText(trim($this->buffer['parsed']['text']));                }            }            if (strtoupper($this->buffer['parsed']['command']) == "NUMERIC") {                $this->onNumeric($this->buffer['parsed']['text'], $this->buffer['parsed']['channel'], $this->buffer['parsed']['target']);            }                if (strtoupper($this->buffer['parsed']['command']) == "NICK") {                $this->onNick($this->buffer['parsed']['old'], $this->buffer['parsed']['new']);            }            if (strtoupper($this->buffer['parsed']['command']) == "JOIN") {                $this->onJoin($this->buffer['parsed']['username'], $this->buffer['parsed']['channel']);            }            if (strtoupper($this->buffer['parsed']['command']) == "PART") {                $this->onPart($this->buffer['parsed']['username'], $this->buffer['parsed']['channel']);            }            if (strtoupper($this->buffer['parsed']['command']) == "MODE" && strtoupper($this->buffer['parsed']['channel']) != strtoupper($this->CONFIG['nick'])) {                $this->onMode($this->buffer['parsed']['username'], $this->buffer['parsed']['channel'], $this->buffer['parsed']['text']);             }            elseif (strtoupper($this->buffer['parsed']['command']) == "MODE" && strtoupper($this->buffer['parsed']['channel']) == strtoupper($this->CONFIG['nick'])) {                $this->onUserMode($this->buffer['parsed']['username'], $this->buffer['parsed']['text']);             }        }    }        function setServer ($server, $port = 6667) {        if (!empty($server)) {            $this->CONFIG['server'] = $server;            $this->CONFIG['port'] = $port;        } else {            $this->error("Need valid server adres");        }    }    function setName ($name = "ZeroBot", $ident = false) {        $this->CONFIG['nick'] = $name;        if ($ident) {            $this->CONFIG['ident'] = $ident . " [Zero bot v". VERSION . " by DaeDaluz]";        }    }    function setChannel ($chan, $key = "") {        if (substr($chan,0,1) == "#") {            $this->CONFIG['chan'] = $chan;        } else {            $this->CONFIG['chan'] = "#" . $chan;        }        $this->CONFIG['key'] = $key;    }    function setAdmin ($nick) {        $this->CONFIG['admin'] = $nick;    }    function error ($error) {        trigger_error($error, E_USER_ERROR);    }    function send ($data, $output = true) {        if ($output) {            echo date("[H:i:s]") ." -> " . $data . "\n";        }        fputs($this->con['socket'], $data."\n\r");    }    function connected () {        if (!feof($this->con['socket'])) {            return true;        } else {            return false;        }    }    // Make sense of the input data.    function parse_buffer() {        $buffer = NUll;        $this->buffer['parsed'] = Array();        $buffer = $this->buffer['all'];        $buffer = explode(" ", $buffer, 4);        if (count($buffer) >= 3) {            if (strpos($buffer['0'], "!")) {                $buffer['username'] = substr($buffer[0], 1, strpos($buffer['0'], "!") - 1);            } else {                $buffer['username'] = substr($buffer[0], 1);            }            $posExcl = strpos($buffer[0], "!");            $posAt = strpos($buffer[0], "@");            $buffer['identd'] = substr($buffer[0], $posExcl+1, $posAt-$posExcl-1);            $buffer['hostname'] = substr($buffer[0], strpos($buffer[0], "@")+1);            $buffer['user_host'] = substr($buffer[0],1);            switch (strtoupper($buffer[1])) {                case "JOIN":                    $buffer['text'] = $buffer['username']." ( ".$buffer['user_host']." )";                    $buffer['command'] = "JOIN";                    $buffer['channel'] = $buffer[2];                    if (substr($buffer['channel'],0,1) == ":") {                        $buffer['channel'] = substr($buffer['channel'],1);                    }                break;                case "QUIT":                    $buffer['text'] = $buffer['username']." ( ".$buffer['user_host']." )";                    $buffer['command'] = "QUIT";                    $buffer['channel'] = "";                break;                case "NOTICE":                    $buffer['text'] = substr($buffer[3], 1);                    $buffer['command'] = "NOTICE";                    $buffer['channel'] = $buffer[2];                break;                case "PART":                    $buffer['text'] = $buffer['username']." ( ".$buffer['user_host']." )";                    $buffer['command'] = "PART";                    $buffer['channel'] = $buffer[2];                break;                case "MODE":                    if (substr($buffer[3], 0, 1) == ":") {                        $buffer['text'] = substr($buffer[3], 1);                    } else {                        $buffer['text'] = $buffer[3];                    }                    $buffer['command'] = "MODE";                    $buffer['channel'] = $buffer[2];                break;                case "NICK":                    $buffer['text'] = $buffer['username']." => ". $buffer[2]." ( ".$buffer['user_host']." )";                    $buffer['old'] = $buffer['username'];                    $buffer['new'] = $buffer[2];                    $buffer['command'] = "NICK";                    $buffer['channel'] = "";                break;                case is_numeric($buffer[1]) == true:                    $buffer['text'] = $buffer[3];                    $buffer['command'] = "NUMERIC";                    $buffer['channel'] = $buffer[1];                    $buffer['target'] = $buffer[2];                break;                default:                    $buffer['command'] = $buffer[1];                    if (strtoupper($buffer[2]) == strtoupper($this->CONFIG['nick'])) {                        $buffer['channel'] = $buffer['username'];                    } else {                        $buffer['channel'] = $buffer[2];                    }                    $buffer['text'] = substr($buffer[3], 1);                break;            }            $this->buffer['parsed'] = $buffer;        }}    function meIson ($chan) {        if (in_array(strtolower($chan), $this->channels)) {            return true;        } else {            return false;        }    }    function join ($chan, $key = "") {        $this->send("JOIN " . $chan . " " . $key);    }    function msg ($chan, $msg) {            $this->send('PRIVMSG '. $chan .' :'.$msg);    }    function ctcp ($to, $msg) {        $split = explode(" ", $msg, 2);        $this->send("NOTICE ". $to ." :" . chr(001) . strtoupper($split[0]) . " " . $split[1] . chr(001));    }    function part ($chan) {        $this->send("PART " . $chan);    }    function quit ($msg = "Zerobot singing off..") {        $this->send("QUIT :" . $msg);    }    function mode ($chan, $modes) {        $this->send("MODE ". $chan . " " . $modes);    }    function Op ($chan, $nick) {        $this->mode ($chan, "+o " . $nick);    }    function deOp ($chan, $nick) {        $this->mode ($chan, "-o " . $nick);    }        function changeName ($name) {        $this->send("NICK :" . $name);    }        function randomName () {        $newName = substr($this->CONFIG['nick'],0,3) . rand(1000,9999);        $this->changeName($newName);    }        function storeUsers () {        $text = $this->buffer['parsed']['text'];        $exp = explode(" ", $text, 8);        $exp[0] = strtolower($exp[0]);                if (in_array($exp[0], array_keys($this->usersOnChannel))) {            $getal = count($this->usersOnChannel[$exp[0]]);        } else {            $getal = 0;        }        $this->usersOnChannel[$exp[0]][$getal]['nick'] = $exp[4];        $this->usersOnChannel[$exp[0]][$getal]['ident'] = $exp[1];        $this->usersOnChannel[$exp[0]][$getal]['host'] = $exp[2];        $this->usersOnChannel[$exp[0]][$getal]['info'] = $exp[7];    }            function ison ($chan, $nick) {        $chan = strtolower($chan);        foreach ($this->usersOnChannel[$chan] as $value) {            if (strtolower($value['nick']) == strtolower($nick)) {                    return true;            }        }        return false;    }        function onMode ($nick, $chan, $modes) {        $split = explode(" ", $modes);        $extra = count($split) - 1;        $extrac = 1;        $specials = Array("o", "v", "h", "q", "a", "k");        for ($i = 0; $i < strlen($split[0]); $i++) {                $value = substr($split[0], $i, 1);                if ($value == "+") {                    $temp = true;                } elseif ($value == "-") {                    $temp = false;                } else {                    if ($temp) {                        $tmp['mode'] = $value;                        if (in_array($value, $specials) && $extra > 0) {                            $tmp['ex'] = $split[$extrac];                            if ($value == "o") {                                $this->onOp($nick, $chan, $split[$extrac]);                            }                            $extrac++;                        }                        $plus[] = $tmp;                    } else {                        $tmp['mode'] = $value;                        if (in_array($value, $specials) && $extra > 0) {                            $tmp['ex'] = $split[$extrac];                            if ($value == "o") {                                $this->onDeop($nick, $chan, $split[$extrac]);                            }                            $extrac++;                        }                        $min[] = $tmp;                    }                }                    }    }    function onUserMode ($nick, $modes) {        $split = explode(" ", $modes);        $extra = count($split) - 1;        $extrac = 1;        for ($i = 0; $i < strlen($split[0]); $i++) {            $value = substr($split[0], $i, 1);            if ($value == "+") {                $temp = true;            } elseif ($value == "-") {                $temp = false;            } else {                if ($temp) {                    foreach ($this->onUserMode as $key => $val) {                        if ("+" . $value . "" == $val['mode']) {                            call_user_func(Array(&$this, $val['cmd']));                            break;                        }                    }                } else {                    foreach ($this->onUserMode as $key => $val) {                        if ("-" . $value . "" == $val['mode']) {                            call_user_func(Array(&$this, $val['cmd']));                            break;                        }                    }                }            }                    }    }        function onNumeric ($text, $numeric) {        if ($this->buffer['parsed']['target'] != $this->CONFIG['nick'] && $this->buffer['parsed']['target'] != "*") {            $this->CONFIG['nick'] = $this->buffer['parsed']['target'];        }        if ($numeric == '433') {            $exp = explode(" ", $text);            if ($exp[0] == $this->CONFIG['nick']) {                $this->randomName();            }        }        foreach ($this->onNumeric as $key => $val) {            if ($numeric == $val['numeric']) {                call_user_func(Array(&$this, $val['cmd']));                break;            }        }    }        function onNick ($old, $new) {        if ($this->buffer['parsed']['old'] == $this->CONFIG['nick']) {            $this->CONFIG['nick'] = $this->buffer['parsed']['new'];        }    }    function onOp ($by, $chan, $on) {        if (strtolower($on) == strtolower($this->CONFIG['nick']) && strtolower($chan) == strtolower($this->CONFIG['chan'])) {            $this->CONFIG['secured'] = true;            $this->mode($chan, '+ims');        }    }    function onDeop ($by, $chan, $on) {        if (strtolower($on) == strtolower($this->CONFIG['admin']) && strtolower($on) != strtolower($by)) {            $this->deop($chan, $by);            $this->op($chan, $on);        }    }    function onPart($nick, $chan) {        if (strtoupper($nick) == strtoupper($this->CONFIG['nick'])) {            if (in_array(strtolower($chan), $this->channels)) {                $this->channels[] = strtolower($chan);            }        } else {            if (!empty($chan) && !empty($nick)) {                if (count($this->onPart) > 0) {                    foreach ($this->onPart as $key => $val) {                        $this->JPProcess($val, $key);                    }                }            }        }    }    function onJoin($nick, $chan) {        if (strtoupper($nick) == strtoupper($this->CONFIG['nick'])) {            if (!in_array(strtolower($chan), $this->channels)) {                $key = Array_search(strtolower($chan), $this->channels);                unset($this->channels[$key]);            }        }        if (!empty($chan) && !empty($nick)) {            if (count($this->onJoin) > 0) {                foreach ($this->onJoin as $key => $val) {                    $this->JPProcess($val, $key);                }            }        }    }    function JPProcess ($val, $key) {        if (!empty($val['chan'])) {            if (is_array($val['chan'])) {                if (in_array_i($key, $val['chan'])) {                    $chan_s = true;                } else {                    $chan_s = false;                }            } elseif (strtoupper($val['chan']) == strtoupper($chan)) {                $chan_s = true;            } else {                $chan_s = false;            }        } else {            $chan_s = true;        }        if (!empty($val['nick'])) {            $val['nick'] = str_replace("*me", $this->CONFIG['nick'] , $val['nick']);            if (is_array($val['nick'])) {                if (in_array_i($this->buffer['parsed']['username'], $val['nick'])) {                    $nick_s = true;                } else {                    $nick_s = false;                }            } elseif (strtoupper($val['nick']) == strtoupper($this->buffer['parsed']['username'])) {                $nick_s = true;            } else {                $nick_s = false;            }        } else {            $nick_s = true;        }        if ($nick_s && $chan_s) {            if (is_array($val['para'])) {                foreach ($val['para'] as $waarde) {                    $par = str_replace("*nick", $this->buffer['parsed']['username'] , $waarde);                    $para[] = str_replace("*chan", $this->buffer['parsed']['channel'] , $par);                }                call_user_func_array(Array(&$this, $val['cmd']), $para);            } else {                call_user_func(Array(&$this, $val['cmd']));            }        }    }    function onCtcp ($text) {        if (count($this->onCtcp) > 0) {            $text = str_replace(chr(001), "", $text);            $trig = explode(" ", $text, 2);            $trig = $trig[0];            $text = $trig[1];            $word = explode(" ", $text);                foreach ($this->onCtcp as $key => $val) {                if (strtoupper($trig) == strtoupper($val['trigger']) && (count($word) - 1) >= $val['req']) {                    if (is_array($val['para'])) {                        foreach ($val['para'] as $sleutell => $par) {                            $par = str_replace("*fulltime", date("D M j G:i:s Y"), $par);                            $par = str_replace("*version", VERSION , $par);                            $par = str_replace("*nick", $this->buffer['parsed']['username'] , $par);                            $par = str_replace("*time", date('H:i:s') , $par);                            $para[] = $par;                        }                        call_user_func_array(Array(&$this, $val['cmd']), $para);                    } else {                        call_user_func(Array(&$this, $val['cmd']));                    }                    break;                }            }        }    }    function tokenRep ($string, $zin) {        preg_match_all("*\*[0-9]{1,2}\-*", $string, $output, PREG_SET_ORDER);        foreach ($output as $value) {            $val = str_replace("*", "", $value[0]);            $val = str_replace("-", "", $val);            settype($val, "int");            $split = explode(" ", $zin, $val);            if (!empty($split[$val -1])) {                $string = str_replace($value[0], $split[$val -1], $string);            }        }        preg_match_all("*\*[0-9]{1,2}*", $string, $output, PREG_SET_ORDER);        foreach ($output as $value) {            $val = str_replace("*", "", $value[0]);            settype($val, "int");            $split = explode(" ", $zin);            if (!empty($split[$val - 1])) {                $string = str_replace($value[0], $split[$val - 1], $string);            }        }        return $string;    }    function onText ($text) {        if (count($this->onText) > 0) {            $word = explode(" ", $text);            $trig = explode(" ", $text, 2);            foreach ($this->onText as $key => $val) {                if (strtoupper($word[0]) == strtoupper($val['trigger']) && (count($word) - 1) >= $val['req']) {                    if (is_array($val['para'])) {                        foreach ($val['para'] as $sleutel => $waarde) {                            if (count($trig) < 2) {                                break;                            } else {                                $val['para'][$sleutel] = $this->tokenRep($waarde, $trig[1]);                            }                        }                        foreach ($val['para'] as $sleutell => $par) {                            $par = str_replace("*fulltime", date("D M j G:i:s Y"), $par);                            $par = str_replace("*version", VERSION , $par);                            $par = str_replace("*nick", $this->buffer['parsed']['username'] , $par);                            $par = str_replace("*time", date('H:i:s') , $par);                            $para[] = str_replace("*chan", $this->buffer['parsed']['channel'] , $par);                        }                        call_user_func_array(Array(&$this, $val['cmd']), $para);                    } else {                        call_user_func(Array(&$this, $val['cmd']));                    }                    break;                }            }        }    }    function newOnText ($trigger, $cmd, $reqPar = 0, $para = "") {        if (is_callable(array('ZeroBot', $cmd))) {            if (!empty($trigger)) {                if (is_int($reqPar)) {                    $getal = count($this->onText);                    $this->onText[$getal]['trigger'] = $trigger;                    $this->onText[$getal]['cmd'] = $cmd;                    $this->onText[$getal]['req'] = $reqPar;                    $this->onText[$getal]['para'] = $para;                } else {                    $this->error("The number of required parameters must be an INT");                }            } else {                $this->error("Trigger required");            }        } else {            $this->error("Not callable function! :" . $cmd);        }    }    function newOnCtcp ($trigger, $cmd, $reqPar = 0, $para = "") {        if (is_callable(array('ZeroBot', $cmd))) {            if (!empty($trigger)) {                if (is_int($reqPar)) {                    $getal = count($this->onCtcp);                    $this->onCtcp[$getal]['trigger'] = $trigger;                    $this->onCtcp[$getal]['cmd'] = $cmd;                    $this->onCtcp[$getal]['req'] = $reqPar;                    $this->onCtcp[$getal]['para'] = $para;                } else {                    $this->error("The number of required parameters must be an INT");                }            } else {                $this->error("Trigger required");            }        } else {        $this->error("Not callable function! :" . $cmd);        }    }    function newOnJoin ($cmd, $para = "", $nick = "", $chan = "") {        if (is_callable(array('ZeroBot', $cmd))) {            if (is_array($para)) {                $getal = count($this->onJoin);                $this->onJoin[$getal]['cmd'] = $cmd;                $this->onJoin[$getal]['para'] = $para;                $this->onJoin[$getal]['nick'] = $nick;                $this->onJoin[$getal]['chan'] = $chan;            } else {                $this->error("Parameters must be in array format!");            }        } else {            $this->error("Not callable function! :" . $cmd);        }    }    function newOnPart ($cmd, $para = "", $nick = "", $chan = "") {        if (is_callable(array('ZeroBot', $cmd))) {            if (is_array($para)) {                $getal = count($this->onPart);                $this->onPart[$getal]['cmd'] = $cmd;                $this->onPart[$getal]['para'] = $para;                $this->onPart[$getal]['nick'] = $nick;                $this->onPart[$getal]['chan'] = $chan;            } else {                $this->error("Parameters must be in array format!");            }        } else {            $this->error("Not callable function! :" . $cmd);        }    }    function newOnUserMode ($mode, $cmd) {        if (is_callable(array('ZeroBot', $cmd))) {            $getal = count($this->onUserMode);            $this->onUserMode[$getal]['mode'] = $mode;            $this->onUserMode[$getal]['cmd'] = $cmd;        } else {            $this->error("Not callable function! :" . $cmd);        }    }    function newOnNumeric ($numeric, $cmd) {        if (is_callable(array('ZeroBot', $cmd))) {            $getal = count($this->onNumeric);            $this->onNumeric[$getal]['numeric'] = $numeric;            $this->onNumeric[$getal]['cmd'] = $cmd;        } else {            $this->error("Not callable function! :" . $cmd);        }    }}function in_array_i($search, &$array) {  $search = strtolower($search);  foreach ($array as $item)    if (strtolower($item) == $search)        return TRUE;  return FALSE;}?> //===================================================================//========================INDEX.PHP==================================//===================================================================<?phpinclude("class.php");$bot = new ZeroBot;$bot->setServer("irc.gtanet.com");$bot->setName("Norbot36", "AKAZAR");$bot->setChannel("#norek.test");$bot->setAdmin("[TNC]Norek");// newOnText( String trigger, String Functie, Int required parameters [, Array parameter]);$bot->newOnText('!say', 'msg', 1, Array("*chan", "*1-"));$bot->newOnText('!msg', 'msg', 1, Array("*1", "*2-"));$bot->newOnText('!ops', 'op', 1, Array("*chan", "*1"));$bot->newOnText('!ops', 'op', 0, Array("*chan", "*nick"));$bot->newOnText('!join', 'join', 2, Array("*1", "*2"));$bot->newOnText('!join', 'join', 1, Array("*1"));$bot->newOnText('!part', 'part', 1, Array("*1-"));$bot->newOnText('!part', 'part', 0, Array("*chan"));$bot->newOnText('!quit', 'quit', 1, Array("*1-"));$bot->newOnText('!quit', 'quit', 0, Array("Quitting..."));$bot->newOnText('!name', 'changeName', 1, Array("*1"));// newOnText( String trigger, String functie, int required parameters [, Array parameters]);$bot->newOnCtcp('version', 'ctcp', 0, Array("*nick", "( :: ZeroBot :: ) [v*version] ( :: by DaeDaluz :: )"));$bot->newOnCtcp('time', 'ctcp', 0, Array("*nick", "*fulltime"));$bot->newOnCtcp('finger', 'ctcp', 0, Array("*nick", "Nice ^^"));// newOnUserMode(string usermode, string functie);$bot->newOnUserMode('+xviw', 'perform');// newOnNumerc(string numeric, string functie);$bot->newOnNumeric('352', 'storeUsers');// newOnJoin('String functie, Array parameters [, String on who ] [, String channel]);$bot->newOnJoin('send', Array('who *chan'), '*me');// newOnPart('String functie, Array parameters [, String on who ] [, String channel]);$bot->connect();while ($bot->connected()) {    if ($bot->newData()) {        $bot->printData();        $bot->events();    }     usleep(100);}?>