[PHP IRC] Bot authentication


This time around you will learn how to force your users to identify to the bot in order to use commands.

First off, lets open ext/botExt.php

All edits will be done to this file for the first few segments released. This file contains all the commands and basic functions for the bot. botCore and botDbWrapper are pretty much done unless you wish to expand them further for advanced functions.

Anyway, lets begin with the following;

Find:
PHP Code
  1. private $mIRC_Socket;


Add After:
PHP Code
  1. private $identifiedUsers = array();


This piece of code will hold all the users who have identified with the bot.

Find:
PHP Code
  1. case '!botstat':
  2. fputs($this->mIRC_Socket, "PRIVMSG $user :Oh hai! this bot was coded as a tutorial for the RMB-Scripting community at rmb-scripting.com It was posted by MOD-Shadow.\n"); //simple bot descripion.
  3. break;


Add After:
PHP Code
  1. case '!identify':
  2. $getUser = parent::sqlQuery("SELECT * FROM `ch_modes` WHERE `username` = '$user';");
  3. if(parent::sqlCount($getUser) == 0){
  4. fputs($this->mIRC_Socket, "PRIVMSG $user :Sorry, but your username was not found in the database.\n");
  5. }else{
  6. $usrData = parent::sqlFetch($getUser);
  7. $usrPasswd = $this->Encrypt($data['4']);
  8. if($usrData['password'] == $usrPasswd){
  9. fputs($this->mIRC_Socket, "PRIVMSG $user :The password you have provided is invalid.\n");
  10. }else{
  11. if($this->checkIdentified($user)){
  12. fputs($this->mIRC_Socket, "PRIVMSG $user :You are already identified.\n");
  13. }else{
  14. $this->identifiedUsers = $user;
  15. fputs($this->mIRC_Socket, "PRIVMSG $user :You have now been identified to the bot.\n");
  16. }
  17. }
  18. }
  19. break;


This is our function to force users to !identify [passwordhere] with the bot. If the user is found it will continue on with password checking, then finally to check if the user is already identified or not.

Next, we will add two new functions to the file which will manage encrypted passwords and identify checking.

So, before the final } in the file add these.
PHP Code
  1. function checkIdentified($user){
  2. if(in_array($user, $this->identifiedUsers)){
  3. return true;
  4. }else{
  5. return false;
  6. }
  7. }
  8. function Encrypt($passwd){
  9. $string1 = hash("md4","RMB IRC Bot Tutorial ".$passwd." Password hash");
  10. $string2 = hash("md5",$string1);
  11. $string3 = hash("haval256,5", $string2);
  12. return md5("Good bye").sha1($string3).md5("world!");
  13. }


Encrypt will encrypt your password with md4, md5 and another random password hash.

Finally, you can add a test command so people can message the bot and it will return if they are or aren't identified.

PHP Code
  1. case '!test':
  2. if(!$this->checkIdentified($user)){
  3. fputs($this->mIRC_Socket, "PRIVMSG $user :You are not identified.\n");
  4. }else{
  5. //do things here
  6. fputs($this->mIRC_Socket, "PRIVMSG $user :You are identified.\n");
  7. }
  8. break;


Now, if you wish to add more commands and want them to be identified just add an if statement like above and it should go through.

If you have any troubles PM me on the site, or if you have my MSN send a message on there.
ShadowMage's Avatar
Author:
Views:
4,723
Rating:
There are currently no comments for this tutorial, login or register to leave one.