Gonzalez Posted February 8, 2010 Report Posted February 8, 2010 Hi there people, welcome back to another episode of coding in php with mrwutang . How was Xmas? Hope yall had fun. Today we'll be learning how to build IRC bots in PHP.Whats and IRC bot and why would anyone use it?IRC bots are scripts or programs that connects to an IRC server and acts like a client, the only difference is that it doesnt let the user interact with the server, it generates automated response or none at all according to what its builder, you want it to do. And ytf would anyone use it? Think about this scenario, you own an IRC server which is busy as shit, but you dont have the time to manage it or greet every user that visits it. Thats when our bot comes in. If you code it properly, it can do anything from greeeting each and every user, hosting text based games, quote jokes and facts, to moderating the server. A bot can also be used for malicious intents like flooding a channel. You could also make one cos it fun Cheesy. So lets get crackin, damn I like saying that General purpose IRC commands:NICK- Used to set your nick . Usage- NICK <YourNickHere> \r\nPASS-Sets the password for your accound. Incase your IRC host needs it. For eg, NickServ needs you to provide a password for the nick you registered in. Usage- PASS <YourPassword> \r\nUSERNAME-Lets you set parameters like your real name domain name and things like that. I dont know much about this command. Usage- USERNAME <username> <domain> <name> <realname>. Just type your real name or nick for all these parameters. Sorry I cant give much info about this.JOIN- You guessed it, lets you join a #channel. Usage- JOIN <#channelName>PRIVMSG-Sends a message to a channel or user, Usage-PRIVMSG <#channelName/userName> <Message>For more info visit RFC 1459You gotta note that you should use the commands in the order when logging in PASS, USER and NICK. Because the protocol wants you to send it the PASS before username. You disobey it and the IRC police will be at your doorsteps before you can say Hamburger. Dont worry if you didnt understand anything I just said. Its all gonna make sense soon.Functions we're gonna be using:fsockopen()- Name says it all, opens a socket to read, write or both. Like fopen.fwrite()-Writes to the fsockopen-ed socket. Some wise guy said everything in 'Nix is a file.fgets()-Reads from the fsockopen-ed socket.preg_match()-Regular expression. Matches text or patterns. We use it to get commands or messages from the server.As usual Im gonna be posting the script and doing the explaining later.<?php$nick="Bot";$name="Bot";$pass="Password";$irc="irc.evilzone.org";$port=6667;#chan="#evilzone";$fp=fsockopen($irc,$port);if($fp){die("Couldnt connect to the server");}fwrite($fp,"PASS $pass\r\nUSER $name\r\nNICK $nick\r\nJOIN$chan\r\n");while($msg=fgets($fp)){if(preg_match("/:(.*)\!.*JOIN.*:#.*/",$msg,$usr)){$usr=$usr[1];fwrite($fp,"PRIVMSG $chan Hi $usr\r\n");}if(preg_match("/:(.*)\!.*PART|QUIT.*:.*/",$msg,$usr)){$usr=$usr[1];fwrite($fp,"PRIVMSG $chan Bye $usr\r\n");}if(preg_match("/:(.*)\!.*Hi $nick.*/",$msg,$usr)){$usr=$usr[1];fwrite($fp,"PRIVMSG $chan Hi $usr\r\n");}}?>The bot in our example is pretty basic. It visits an IRC server specified in the variable $irc using password, nick and username stored in $pass,$user, and $nick and joins the channel specified in $chan. We use fsockopen, fwrite and fgets to open a socket to the server, send commands and read messages from the server. Our bot greets any user that greets the bot or joins or leaves the channel. See? Easy-Peasy.We have the basic bot up and running now we can get it to do pretty much anything we want it to do.Use your new found powers wisely. Like that Spiderman's uncle said. With great power comes great responsibilities, Dont ask it to rob banks or take control of the world Tongue. Yea I got carried away I'll stop making lame jokes now Lips Sealed. Hope this article made s ense and you learned something. Message me if you didnt. Thanx for reading. Take care and have fun.By mrwutang Quote
Flubber Posted February 8, 2010 Report Posted February 8, 2010 PING-ul si PONG-ul? Nu cumva trebuie sa trimita un PONG inapoi la server cu un anumit text in ordine sa se conecteze la server? Sau asta nu e valabil pentru toate serverele? Quote