Jragon Posted February 13, 2011 Share Posted February 13, 2011 Hey guys, I've been working on a IRC bot, but there is something wrong with it, it is ment to be an endless loop, but it runs out of memory before it joins D= Error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261900 bytes) in /var/www/random/bot2.php on line 90 Code: <?php set_time_limit(0); ini_set('desplay_errors', 'on'); $configure = array( 'server' => 'irc.freenode.net', 'port' => 6667, 'nick' => 'TNB_BOT', 'name' => 'JBot', 'channel' => '##thenewboston', 'pass' => '***' ); class JBot{ //TCP connection holder. public $socket; //Message holder. public $msg = array(); /* * Constucter. * Opens the server connection, and logs in the bot. * * @param array. */ function __construct($configure){ $this->socket = fsockopen($configure['server'], $configure['port']); $this->login($configure); $this->main(); $this->send_data('JOIN', $this->configure['channel']); } /* * Logs bot in to server * * @param array. */ function login($configure){ $this->send_data('USER', $configure['nick'] . ' Jragon.co.uk ' . $configure['nick'] . ' :' . $configure['name']); $this->send_data('NICK', $configure['nick']); } /* * Main function, used to grab all data. * */ function main(){ $data = fgets($this->socket, 128); flush(); $this->ex = explode(' ', $data); if($this->ex[0] == 'PING'){ //Plays ping-pong with the server.. $this->send_data('PONG', $this->ex[1]); } $command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]); //List of commands the bot responds to. switch($command){ case ':!say': $this->say(); break; case ':!join': $this->join_channel($this->ex[4]); break; case ':!quit': $this->send_data('QUIT', $this->ex[4]); break; case ':!op': $this->op_user(); break; case ':!deop': $this->op_user('','', false); break; case ':!time': $this->send_data("PRIVMSG", "Time" . date("F j, Y, g:i a" . time())); break; case 'Hello': fputs($socket,"PRIVMSG " . $this->ex[2] . " : Hi!\n"); break; } $this->main(); } function say(){ $arraysize = sizeof($this->ex); //1,2,3 are just nick and chan, 4 is where text starts $count = 4; while($count <= $arraysize) { $text = $text . " " . $this->ex[$count]; $count++; } $this->privmsg($text, $this->ex[2]); unset($text); } function privmsg($message, $to){ fputs($socket,"PRIVMSG " . $to . " :" . $message . "\n"); } /* * Sends data to the server. */ function send_data($cmd, $msg = null){ if($msg == null){ fputs($this->socket, $cmd . "\n"); echo "<b>$cmd</b>"; }else{ fputs($this->socket, $cmd.' '.$msg."\n"); echo "<b>$cmd $msg</b>"; } } /* * Joins a channel. * * @param text */ function join_channel($channel){ $this->send_data('JOIN', $channel); } /* * Give/Take operator status. */ function op_user($channel = '', $user = '', $op = true){ if($channel == '' || $user == ''){ if($channel == ''){ $channel = $this->ex[2]; } if($user == ''){ $user = strstr($this->ex[0], '!', true); }; if($op){ $this->send_data('MODE', $channel . ' +o ' . $user); }else{ $this->send_data('MODE', $channel . ' -o ' . $user); } } } } $bot = new JBot($configure); ?> Link to comment https://forums.phpfreaks.com/topic/227559-irc-bot-help/ Share on other sites More sharing options...
Jragon Posted February 14, 2011 Author Share Posted February 14, 2011 <?php //Opens the socket $socket = fsockopen("irc.freenode.com",6667); //Sends Host name fputs($socket, "USER Jragon_Bot Jragon.co.uk PHP :Jragon_Bot\n"); //Send the nickname fputs($socket, "NICK Jragon_Bot\n"); //Joins ##thenewboston fputs($socket, "JOIN ##thenewboston\n"); //Set commands $commands = array( "!version", "!say", "!exit", "Hello" ); while(1){ //Gets data while($data = fgets($socket, 128)){ //Puts data into an array. $get = explode(' ', $data); //Server pinged, time to reply if($get[0] == "PING"){ fputs($socket, "PONG " . $get[1] . "\n"); } if(substr_count($get[2],"#")) { $nick = explode(':',$get[0]); $nick = explode('!',$nick[1]); $nick = $nick[0]; $chan = $get[2]; $num = 3; if($num == 3){ $split = explode(':',$get[3],2); $text = $split[1]; //Processing of commands //This is where we start processing the commands we entered in earlier if(in_array(rtrim($text),$commands)) { switch(rtrim($text)) { case "!version": fputs($socket,"PRIVMSG $chan : Jragon bot, version 0.01 Alpha\n Developed by Jragon(Rory Anderson)\n"); break; case "!say": $arraysize = sizeof($get); //1,2,3 are just nick and chan, 4 is where text starts $count = 4; while($count <= $arraysize) { $saytext = $saytext." ".$get[$count]; $count++; } fputs($socket,"PRIVMSG ".$get[2]." :".$saytext."\n"); unset($saytext); break; case "!exit": fputs($socket,"PRIVMSG $chan :Shutting Down Jragon's very awesome bot!\n"); fputs($socket,"QUIT Client Disconnected!\n"); //IMPORANT TO HAVE THE DIE(), this throws the script out of the infinite while loop! die('Client Disconnected!'); break; case "Hello": fputs($socket,"PRIVMSG $chan :Hi!\n"); break; } } } } } } //Shows the text in the browser as Time - Text echo nl2br(date('G:i:s')."-".$data); //Flush it out to the browser flush(); ?> That is my first bot that worked... Link to comment https://forums.phpfreaks.com/topic/227559-irc-bot-help/#findComment-1173952 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.