Plaintext Version
================
| classBot.php
================
| PHP Bot Class
================
author: pyr0t3chnician[at]gmail.com
---------
Tutorial:
---------
1. Download the classBot.txt and change the extension to .php
2. Open a new PHP file.
3. Place this at the top of the page: require("classBot.php");
4. Create a new bot: $bot = new bot;
5. Edit the variables as you see fit:
$bot->IrcHost="your.irc.chan";
$bot->BotNick="MyCoolBot";
6. Create your functions:
function echohello($bot){
$bot->Output("PRIVMSG", $bot->IrcChan, "Hello World");
}
7. Add your functions to the bot:
$bot->AddCommand("!echohello","echohello")
8. Connect your bot!
$bot->OpenConnection();
9. Give him about 30 seconds to load, and test your command:
<pyr0t3chnician>!echohello
10. You should see the command executed!
----------------
Class Variables:
----------------
The variables are pretty self explanatory.
*****************************************************
These are the public variables that can be set using:
$bot->VariableName="Some Value";
*****************************************************
$BotName="MrBot";
The name of the bot, not the nickname, but the real name.
$BotNick="MrBot";
The nick of the bot, as shown in the channel.
$BotPass="password";
The bots password, if the bot is registered in the channel.
Used for simply identifying the bot with NickServ.
$BotOperPass="operpassword";
The Op password, if your bot is to be an Op.
$AdminName="pyr0t3chnician";
The owners name.
Certain functions, you may want to validate who is calling the function.
$AdminEmail="pyr0t3chnician@gmail.com";
The admin's email address.
$AdminHostMask="chatzilla@DME-Client-9299C001.phnx.qwest.net";
The complete host mask of the admin.
Again, used for admin validation.
$IrcHost="zeroirc.info";
The IRC Host network.
$IrcPort=6667;
The port of the network.
Standard port is 6667.
$IrcChan="#zeroidentity";
The channel for initial connection.
$MySQLHost="localhost";
The host of your MySQL database.
$MySQLUser;
Your MySQL username.
$MySQLPass;
Your MySQL password.
$MySQLDB;
Your MySQL database.
**********************************************************************
These are the variables that can be called in your functions by using:
echo $bot->VariableName;
**********************************************************************
var $Line;
The entire line of data received by bot before it is parsed.
var $LastSpeaker;
The last person to speak.
var $LastHow;
How the bot received the info.
Typically, it is "PRIVMSG", but can be "NOTICE", "JOIN", "QUIT", etc.
var $LastWhere
Where the message was posted.
Typically, the IRC Channel, but could be a private message.
In the case of a PM, $LastWhere would be the name of the user who sent it.
var $LastMessage;
The entire message that was sent.
var $LastCommand;
The first word that was send, which is typically a command.
var $LastArgs;
All the words after the first word.
Easiest way to parse the arguments if your command needs to is to:
$args=explode(' ',$bot->LastArgs);
var $LastHost;
The host, usually containing the DNS of the person connecting.
var $LastHostMask;
The host AND the connection they used.
Formatted: NAME@DNSBunch-of-numbers.and.letters
Usually the real name or chat client they are using.
var $LastBrowser;
The real name, or chat client of the user.
********************************************************************
These variables just store info for the bot, and are not accessible.
********************************************************************
private $IrcSock;
The connection to the host.
private $Commands = array();
Commands array storing all the bots commands.
private $EventCommands = array();
EventCommands array storing all the event commands.
private $MySQLCon;
Stores the MySQL connection.
----------------
Class Functions:
----------------
function _construct()
When the bot is created, it turns of the timeout that typically
exists with a PHP script.
This means that it will run forever until a command is given to close it.
function OpenConnection($host='',$channel='',$port=0)
Opens a connection to an IRC channel.
If no variables are passed, it will use the class variables to connect.
private function ConnectionLoop()
This is the main program.
The bot loops until it is told to quit or it encounters an error.
It has basic commands to return a ping, or give version info.
function Output($type, $who, $message)
This is the output function that you will use to display text.
$type is typically "PRIVMSG", but you could also use "NOTICE" or other types.
$who is where the data will be displayed.
It could be the $IrcChan, or $LastSpeaker for a PM, or $LastWhere if the bot
is in multiple channels at once.
$message is the text you want to display.
private function ParseLine()
This function parses the data received by the bot.
It sets up all the variables starting with the word "Last".
You can parse it yourself if you are looking for specific information.
function AddCommand($command,$function)
This adds a command to the bot.
$command is the string the bot will search for in the FIRST WORD:
"!help", "lol", "@quit"
$function is the name of the function the bot will call once that command is found.
"help" will call function help($bot)
private function ExecuteCommand()
This function executes a command.
The function is called and passes all the current variables.
Your functions MUST allow the bot to pass its variables:
function myfunction($foo) $foo will be the $bot.
function AddEventCommand($action,$function)
An event command will search for any string ANYWHERE in $Line.
If you want the bot to say hello when someone joins, have it search for JOIN.
Keep in mind, in the above example, a sample line could be:
"Bot!Bot@here-dns.ip.stuff JOIN :#mychannel\r\n" or
"Bot!Bot@here-dns.ip.stuff PRIVMSG #mychannel :I JOINED!\r\n"
Both instances will execute the function unless you parse the data a little yourself.
private function ExecuteEventCommand()
Search the entire $Line for specific strings.
Once found, it will execute a command the same way as ExecuteCommand()
function BasicOutput($string)
This is a raw output.
This is useful if you want to quit a channel, join a channel, kick someone, etc.
You don't need to add a line break.
function BotQuery($string)
This executes a query. It doesn't return any results.
$string is just the MySQL query string.
The function connects to the database using preset variables, all you need
to do is call the function.
function BotReturnArray($string)
Like BotQuery, it executes a MySQL query and will return the results as an array.
The returned data is $return[number][indexes]=value
If there is only one row returned, it would be $return[0][indexes]=value
Easiest way to loop through multiple (or single) rows is a foreach loop.
function BotReturnCount($string)
Returns the number of rows from a specific MySQL query.
Useful if you need to check to see if a row exists.
----------
Conclusion
----------
The bot class is very versatile. These are my recommendations:
Use terminal, ssh, or cmd.exe to run the program.
You can point a browser to the script, and it will run, but the script doesn't
show the incoming data until the script is stopped.
Program a quit function immediately. Failing to do so may result in a bot being
stuck and running for 10-15 minutes before it quits by itself.
Use global variables for some of your functions, that way a certain command can only be
run once or twice. An example would be a function that says the bot is loaded.
Check out the sample script at http://www.notan00b.com/bot2.php to see a simple bot.
------------
Version Info
------------
v0.3
-Fixed minor errors.
-Added MySQL support.
v0.21
-Fixed a couple minor errors.
v0.2
-Added BasicOutput function for raw output.
-Add $LastHost, $LastHostMask, $LastBrowser variables.
v0.1
-Working and tested.
-Fixed minor errors.
-Uploaded to websites.
v0.01
-Working, in testing phase.
-Script tested by multiple people.