I wanna make a command that actually lets you do php from inside irc. For example, if I was in an irc channel and I typed:

! $words = "Hi"; echo $words;

Then the bot would respond by saying Hi in the channel.

How could I make a command like that?

This is the code for my bot:

<?php

set_time_limit(0);

$conf['nick'] = 'bot;
$conf['server'] = 'irc.gtanet.com';
$conf['port'] = 6667;
$conf['ident'] = 'bot';
$conf['realname'] = 'bot';

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $conf['server'], $conf['port']) || die("\nfailed conn\n");
socket_write($socket, 'NICK '.$conf['nick']."\r\n");
socket_write($socket, 'USER '.$conf['ident'].' email.com Simps :'.$conf['realname']."\r\n");

while(($line = socket_read($socket, 1024, PHP_NORMAL_READ)) !== false)
{
	if (substr($line, 0, 5) == 'PING ')
	{
		socket_write($socket,'PONG '.substr($line,5)); continue;
	} else {
        	echo $line;
        	$ex = explode(' ', $line);
		if($ex[1] == '001')
		{
			socket_write($socket,'JOIN #gta'."\r\n");
		} elseif($ex[1] == 'PRIVMSG')
		{
			unset($chan);
			list($nick,$addr) = explode('!',substr($ex[0],1));
			$text = substr(rtrim(implode(' ', array_slice($ex, 3))), 1);
			if (substr($ex[2], 0, 1) == '#')
			{
				$chan = $ex[2];
			}
			if ($text == 'die')
			{
				if($chan) { 
					socket_write($socket,'PRIVMSG '.$chan.' :Bye'."\r\n");
				} else {
					socket_write($socket,'PRIVMSG '.$nick.' :Dying'."\r\n");
				}
				die("\nRequested\n");
			}
		}
	}
}
?>

    You'd need to use eval(). This is, however, a very, very poor idea. You'd be introducing huge security risks into your code.

      That's ok, I could add an admin system. Can you please walk me through what to do (I'm still a beginner in php).

        Same way you'd parse any input from the bot, except if it's matching !php or whatever, hook it through eval() and output the result instead of just doing your own processing.

        I don't know what bot skeleton you're using or how its command architecture is configured, so I can't write it for you. From a quick look, you'd do something like:

        if ($text == "php") {
          $output = eval ($text[1]);
          socket_write($chan, $output, // etc etc etc
        }
        

          Thanks for your reply.

          Would this eval function be correct?

          function Evaluate( $String )
          {
                  ob_start(); 
                  eval( "$String" ); 
                  $ret = ob_get_contents(); 
                  ob_end_clean();
          
              return $ret;
          }

          And this is the command:

          			if ($message[0] == '&&')
          			{
          				if($chan)
          				{
          					if(!isset($message[1]))
          					{
                						socket_write($socket,"PRIVMSG ".$chan." Syntax is && [raw code] \r\n");
                					} else {
                						$output = Evaluate($message[1]);
                						socket_write($socket,"PRIVMSG ".$chan." :".$output."\r\n");
                					}
              				}
            			}
          

            Ok I got it to work but it only works when words aren't spaced out.

            This would work:

            && $new="fish";echo$new;

            But this wouldn't:

            && $new="fish"; echo $new;

            It doesn't work when the words are spaced out. Any ideas why? Here's the command:

              			if ($message[0] == '&&')
            			{
                				if($chan)
            				{
                  					if(!isset($message[1]))
            					{
                  						socket_write($socket,"PRIVMSG ".$chan." Syntax is && [raw code] \r\n");
                  					} else {
            						$output = Evaluate($message[1]);
            						socket_write($socket,"PRIVMSG ".$chan." :".$output."\r\n");
                  					}
                				}
              			}
            

              Please don't bump your threads. Someone will help you if they're able. This is a volunteer forum, and your threads are not any more or less important than anyone else's.

              That said, I've never worked with eval(), really, so I'm not sure why the white space would be an issue. A quick glance over the manual page at php.net/eval doesn't have anything jump out at me, but in the comments someone wrote:

              Replace the line:-
              $string = preg_replace("/<\?=\s+(.?)\s+\?>/", "<? echo $1; ?>", $string);
              with
              $string = preg_replace("/<\?=\s
              (.?)\s\?>/", "<? echo $1; ?>", $string);

              The "\s*" in the two places will enable 0 or more white spaces to exist after the "=" instead of the existing "\s+" which enables only 1 or more white spaces.

              Also instead of using the eval function in the return statement of the function, it would be better to return only the string ready to perform eval and then do the eval in the main program where the scope and visibility of variables are known.

              Which seems like it may have some relevance, but if you want to read through more of the comments, there might be more information there.

                do a print_r( $message ), it might show you why it isn't working.

                  Write a Reply...