Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /** PHP IRC Snoop/Relay Bot **
- *
- * ####TODO: Think and then some..
- * ####TODO: THINK of extras.
- */
- $sbot = SnoopBot::getInst();
- $sbot->config = array(
- 'mode' => 'relay', /// SNOOP OR RELAY
- 'serv' => 'localhost',
- 'port' => 6667,
- 'nick' => 'Nickeroo',
- 'name' => 'MrRooo',
- 'chan' => array('#chan'),
- 'nick_ignore'=> array('nigger')
- );
- $sbot->Init();
- ###########################################################
- class SnoopBot
- {
- private $is_CLI = false;
- private $socket = null;
- private $ex = null;
- private $tstamp = '0000-00-00 00:00:00';
- private $nicks = array();
- private $chans = array();
- private $curruser = null;
- private $currchan = null;
- private $lastchan = null;
- private $userwhois = null;
- public $config = array(
- 'mode' => 'snoop', # Bot Mode Snoop OR Relay
- 'serv' => 'please.change.me', # Bot Serv
- 'port' => 6667, # Bot Port
- 'nick' => 'sbot', # Bot Nick
- 'name' => 'RelayBot', # Bot Name
- 'chan' => array('#chan1'), # Chan to join
- 'nick_ignore' => array('nigger') # Nicks to Ignore
- );
- public static function getInst()
- {
- static $inst = null;
- if ($inst === null)
- {
- $inst = new self;
- }
- return $inst;
- }
- // Initital functions
- public function init()
- {
- // Start connection to IRC
- $this->socket = fsockopen($this->config['serv'], $this->config['port']);
- // Send USER/NICK to IRC
- $this->sendData('USER', "{$this->config['nick']} irc.land {$this->config['nick']} :{$this->config['name']}\r\n");
- $this->sendData('NICK', "{$this->config['nick']}\r\n");
- while(true)
- {
- // Timestamps
- $this->tstamp = (new \DateTime())->format('H:i:s');
- // Bot Core
- $this->ex = $this->recvData();
- // Play Ping-Pong
- if($this->ex[0] == 'PING')
- {
- $this->sendData('PONG', "{$this->ex[1]}");
- }
- // Set Current User/Chan
- $this->curruser = substr(explode('!', $this->ex[0])[0], 1);
- $this->currfunc = @$this->ex[1];
- $this->currchan = @$this->ex[2];
- // Switch Functions
- $this->preSwitches();
- if($this->config['mode'] == 'relay')
- {
- $this->doRelay();
- }
- $this->lastchan = $this->currchan;
- } // While(true)
- }
- // Initital Core functions
- private function preSwitches()
- {
- // preSwitches include Pre commands, and post nick/join/kick/part/quit
- switch(@$this->ex[1])
- {
- case '001': // Check for time to join
- if(is_array($this->config['chan']))
- {
- foreach($this->config['chan'] as $chan)
- {
- $this->sendData('JOIN', "$chan\r\n");
- }
- }
- else
- {
- $this->sendData('JOIN', "{$this->config['chan']}\r\n");
- }
- break;
- #####################
- case '311': // Fetch Whois Info
- $ex = $this->ex;
- unset($ex[0], $ex[1], $ex[2], $ex[6], $ex[7]);
- $this->userwhois = $ex;
- break;
- #####################
- case '353': // Fetch Nicks from Channel
- $ex = $this->ex;
- $chan = $ex[4];
- unset($ex[0], $ex[1], $ex[2], $ex[3], $ex[4]);
- $nicks = implode(' ', $ex);
- $nicks = str_replace(':', '', $nicks);
- $nicks = str_replace('@', '', $nicks);
- $this->nicks[$chan] = explode(' ', ucwords($nicks));
- break;
- #####################
- case '433': // Set random nick, if Nick inuse
- $randId = substr(mt_rand(), 0, strlen(mt_rand())/2);
- $this->sendData('NICK', "{$this->config['nick']}$randId\r\n");
- break;
- #####################
- case 'NICK': // Update Nicks array on Nick Change
- $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
- $newuser = trim(ucwords($this->ex[2]));
- if($olduser == ucwords($this->config['nick']))
- {
- $this->sendData('NICK', "{$this->config['nick']}\r\n");
- }
- foreach($this->chans as $chan)
- {
- foreach($this->nicks[$chan] as $nickId => $nick)
- {
- if($olduser == $nick)
- {
- $this->nicks[$chan][$nickId] = $newuser;
- }
- }
- }
- break;
- #####################
- case 'PRIVMSG':
- //echo "PRIVMSG\r\n";
- if($this->config['mode'] == 'snoop')
- {
- $user = substr(explode('!', $this->ex[0])[0], 1);
- if(!in_array($user, $this->config['nick_ignore']))
- {
- $this->doSnoop('PRIVMSG');
- }
- }
- else
- {
- $this->doRelay();
- }
- break;
- #####################
- case 'JOIN':
- if($this->config['mode'] == 'snoop')
- {
- $this->doSnoop('JOIN');
- }
- $chan = trim(strtolower(substr($this->ex[2], 1)));
- $user = trim(ucwords(explode("!", substr($this->ex[0], 1))[0]));
- $this->chans[$chan] = $chan;
- $this->nicks[$chan][] = $user;
- break;
- #####################
- case 'KICK':
- if($this->config['mode'] == 'snoop')
- {
- $this->doSnoop('PRIVMSG');
- }
- $this->sendData('JOIN', "{$this->ex[2]}\r\n");
- break;
- #####################
- case 'PART':
- if($this->config['mode'] == 'snoop')
- {
- $this->doSnoop('PART');
- }
- $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
- $this->currchan = trim($this->currchan);
- foreach($this->nicks[$this->currchan] as $nickId => $nick)
- {
- if($olduser == $nick)
- {
- unset($this->nicks[$this->currchan][$nickId]);
- }
- }
- break;
- #####################
- case 'QUIT':
- if($this->config['mode'] == 'snoop')
- {
- $this->doSnoop('QUIT');
- }
- $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
- foreach($this->chans as $chan)
- {
- foreach($this->nicks[$chan] as $nickId => $nick)
- {
- if($olduser == $nick)
- {
- unset($this->nicks[$chan][$nickId]);
- }
- }
- }
- break;
- }
- }
- private function doSnoop($func = 'PRIVMSG')
- {
- //echo "SNOOP\r\n";
- $ex = $this->ex;
- switch($func)
- {
- case 'JOIN':
- unset($ex[0]);
- $this->currchan = trim(substr($this->currchan, 1));
- break;
- case 'PART':
- unset($ex[0]);
- break;
- case 'QUIT':
- unset($ex[0]);
- $this->currchan = $this->lastchan;
- break;
- case 'PRIVMSG':
- unset($ex[0], $ex[1], $ex[2]);
- break;
- }
- $this->writeLog(implode(" ", $ex), $this->currchan);
- }
- private function doRelay()
- {
- //echo "RELAY\r\n";
- foreach($this->chans as $chan)
- {
- if(file_exists("xferchans/$chan"))
- {
- $data = trim(file_get_contents("xferchans/$chan"));
- foreach(explode("\n", $data) as $line)
- {
- //echo "$line\r\n";
- $this->sendData("PRIVMSG $chan :", "$line\r\n");
- }
- unlink("xferchans/$chan");
- }
- }
- }
- // Send/Recv/Log Functions
- private function sendData($command, $message = null)
- {
- echo 'SND-MSG: ';
- if($message == null)
- {
- echo "$command" . PHP_EOL;
- fputs($this->socket, (string)"$command\r\n");
- }
- else
- {
- echo "$command -> $message" . PHP_EOL;
- fputs($this->socket, (string)"$command $message\r\n");
- }
- }
- private function recvData()
- {
- $data = fgets($this->socket, 1024);
- if($data != '')
- {
- echo "RCV-MSG: $data" . PHP_EOL;
- }
- return explode(' ', $data);
- }
- private function writeLog($input = null, $chan = null)
- {
- $input = trim($input);
- $user = substr(explode('!', $this->ex[0])[0], 1);
- echo "SYS-MSG: Append Log: $chan -> <$user> $input" . PHP_EOL . PHP_EOL;
- file_put_contents("xferchans/$chan", (string)"4,0{$this->tstamp} {$this->config['serv']}:{$this->config['port']} -> <$user> $input" . PHP_EOL, FILE_APPEND);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment