kb3llm Posted January 11, 2008 Share Posted January 11, 2008 Hi all - I followed a PHP tutorial (because I am new to PHP. I know SOME Perl, so learning PHP is seemingly easy so far (I'm following a book) but I wanted to make a project that I could add on to as I learn more.... So - I'm making an IRC bot (that runs in php-cli).. Thing is, the tut. didn't tell you where to go from here. I can add something, say !test, and it will send "Works" to the channel. See, in Perl, everything was handed to me, by my use of a module. This one (PHP) is from scratch. So, how do I check user input? Like if someone said !blah 20 how could I make the bot say "You just said '!blah 20'"? Here's my code so far: <?php // A PHP IRC bot originally for #teenlug by KB3LLM require("config.php"); // Dont change the line below: set_time_limit(0); // Make sure the config file was changed if($network_address == "default") { print "Please change all of the variables in config.php and the run again\n"; die (); } // Connect to the network using the variables in config.php $socket = fsockopen("$network_address", $network_port); // Now that we connected - Lets talk to the server and tell it our nick and // Any other info it needs... fputs($socket,"USER $botuser $bothostname $servername: $botrealname\n"); fputs($socket,"NICK $botnick\n"); // Define BOT's nickname // Join all channels in the array in the config foreach ($botchannels as &$botjoin) { fputs($socket,"JOIN $botjoin\n"); } while(1) { // Everything else down here (ok, not functions.. lets keep the code clean :-] ) while($data = fgets($socket, 128)) { if($displayreadout == 1){ echo $data; } // Separate all data $ex = explode(' ', $data); // Send PONG back to the server if($ex[0] == "PING"){ fputs($socket, "PONG ".$ex[1]."\n"); if($displayreadout == 1){ echo "PONG".$ex[1]."\n"; } } // Lets start adding commands // Filter out newlines and junk: $command = str_replace(array(chr(10), chr(13)), '', $ex[3]); if ($command == ":!sayit") { fputs($socket, "PRIVMSG ".$ex[2]." :WORKS!!!!!!\n"); } } } ?> and in config.php <?php // Network Information $network_address = "irc.freenode.net"; // Address of the irc net we want to connect to $network_port = 6667; // Usually this is fine. $botnick = "kb3llm_php"; // Nickname of the bot $botpass = "password"; // Password for NickSERV $botuser = $botnick; // Bot USER name - for now, same as nickname. $botrealname = $botnick; // Bot's Realname that will show in /whois. $bothostname = "kb3llm.org"; // A domain to tell the server (dont know why, but....) $servername = $network_address; // A server name (again, dont know why. set as same as $network_address for now..) // Channels // This might be confusing.. Just add then in quotes with a comma between them. // EXAMPLE: $botchannels = array("#channel1","#channel2","#channel3"); $botchannels = array("#testest"); $displayreadout = 0; ?> Thanks for any help.. EDIT: Forgot to say.. the Perl equivlent of what I want to do is: if($variable =~ /^!blah ([\w\s]*){ #then -- using the module that I used for my Perl bot it would be: $irc->yield( privmsg => $channel, "You entered \"!blah $1\""); } Thanks again Link to comment https://forums.phpfreaks.com/topic/85481-irc-bot-followed-tutorial-but-where-to-go-from-here/ Share on other sites More sharing options...
dsaba Posted January 11, 2008 Share Posted January 11, 2008 you can use preg_match http://php.net/preg_match Link to comment https://forums.phpfreaks.com/topic/85481-irc-bot-followed-tutorial-but-where-to-go-from-here/#findComment-436310 Share on other sites More sharing options...
kb3llm Posted January 14, 2008 Author Share Posted January 14, 2008 Hi - thanks for the response.. Could you give me an example of using it? Just one or two to get me started.. Thanks a lot.. you can use preg_match http://php.net/preg_match Link to comment https://forums.phpfreaks.com/topic/85481-irc-bot-followed-tutorial-but-where-to-go-from-here/#findComment-438583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.