From 46f22770945b7f9d7bc236090cf39524c0048d71 Mon Sep 17 00:00:00 2001 From: zYne Date: Sat, 10 Nov 2007 21:25:15 +0000 Subject: [PATCH] irc bot example --- tools/Bot.php | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 tools/Bot.php diff --git a/tools/Bot.php b/tools/Bot.php new file mode 100644 index 000000000..bbbad0023 --- /dev/null +++ b/tools/Bot.php @@ -0,0 +1,154 @@ + 'irc.freenode.net', + 'port' => 6667, + 'username' => 'Doctrine', + 'hostname' => 'phpdoctrine.net', + 'servername' => 'Doctrine', + 'realname' => 'Doctrine bot', + 'nick' => 'Doctrine', + 'channels' => array('#doctrine-test')); + + protected $_socket; + + + public function connect() + { + // Open the socket to the IRC server + $this->_socket = fsockopen($this->_options['server'], $this->_options['port']); + + unlink('log.txt'); + + sleep(1); + + Doctrine_Manager::connection('sqlite::memory:'); + + + // Send auth info + $this->execute('USER ' . $this->_options['username'] . ' ' . + $this->_options['hostname'] . ' ' . + $this->_options['servername'] . ' :' . + $this->_options['realname'] . "\n"); + + $this->execute('NICK ' . $this->_options['nick'] . "\n"); + + foreach ($this->_options['channels'] as $channel) { + $this->execute('JOIN ' . $channel . "\n"); + } + } + public function execute($command) + { + fputs($this->_socket, $command); + + $this->log('>>> ' . $command); + } + public function log($command) + { + $fp = fopen('log.txt', 'a+'); + + fwrite($fp, $command); + + fclose($fp); + } + public function disconnect() + { + $this->execute('QUIT' . "\n"); + + fclose($this->_socket); + } + // IRC Functions [BEGIN] + + // Joins channel + public function join($channel) + { + $this->execute('JOIN ' . $channel . "\r\n"); + } + + // Leaves the channel + public function part($channel){ + $this->execute('PART ' . $channel . "\r\n"); + } + + // send message to channel/user + public function say($to, $msg){ + $this->execute('PRIVMSG '. $to . ' :' . $msg . "\r\n"); + } + + // modes: +o, -o, +v, -v, etc. + public function setMode($user, $mode){ + $this->execute('MODE ' . $this->channel . ' ' . $mode . ' ' . $user . "\r\n"); + } + // kicks user from the channel + public function kick($user, $from, $reason = "") + { + $this->execute('KICK ' . $from . ' ' . $user . ' :' . $reason . "\r\n"); + } + // changes the channel topic + public function topic($channel, $topic) + { + $this->execute('TOPIC ' . $channel . ' :' . $topic . "\r\n"); + } + public function run() + { + $this->connect(); + // Force an endless while + + while( ! feof($this->_socket)) { + + // Continue the rest of the script here + $data = fgets($this->_socket, 4096); + + print $data . "
"; + // Separate all data + $ex = explode(' ', $data); + + // Send PONG back to the server + if ($ex[0] == 'PING') { + $this->execute('PONG ' . $ex[1] . "\n"); + } + //$this->log($data); + + // Say something in the channel + $command = str_replace(array(chr(10), chr(13)), '', $ex[3]); + + // strip out ':' + $command = substr($command, 1); + + array_shift($ex); + array_shift($ex); + $scope = array_shift($ex); + array_shift($ex); + + $argsStr = implode(' ', $ex); + + + //$this->log($command . ' ' . $scope); + + switch ($command) { + case '!shutdown': + $this->disconnect(); + exit; + break; + case '!native-expr': + $portableExpr = $ex[0]; + + break; + } + } + + } +} +$bot = new Dbot(); +$bot->run();