User Control Panel
Advertisements

HELP US, HELP YOU!

IRC Bot

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Bots
View unanswered posts
Author Message
Rezert
Newbie
Newbie


Joined: 26 Jul 2008
Posts: 2


PostPosted: Sat Jul 26, 2008 4:21 pm    Post subject: IRC Bot Reply with quote

I made an PHP IRC Bot for a channel and if you have any requests for features just tell me. By the way, you will need a MySQL Database with a table `users` with the columns `user` and `level`. You can then insert yourself in it with the values `(your_nick)` and `2`.

2 : Botmaster
1 : Regular User
0 : Ignored

There are no `1`s in the database.

Code:
<?php

set_time_limit(0);

$host     = "localhost";
$username = "root";
$password = "*********";
$database = "bot";

if(!isset($_POST['submit'])) {
   print "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
<html>
   <head>
      <meta http-equiv='Content-type' content='text/html;charset=UTF-8'>
      <title> Zero_Cool || Settings </title>
      <style type='text/css'>
         body {
            background-color: #000000;
            color: #FFFFFF;
         }
      </style>
   </head>
   
   <body>
      Welcome to the PHP version of ZeroBot!  Let's get started by choosing some settings:
      <br /><br />
      <form method='post' name='chan_form' action='" . $_SERVER['PHP_SELF'] . "'>
         <table>
            <tr>
               <td>Server: </td>
               <td><input type='text' name='server' value='irc.tdirc.net' /></td>
            </tr>
            <tr>
               <td>Channel: </td>
               <td><input type='text' name='chan' value='#zeroidentity' /></td>
            </tr>
            <tr>
               <td>Channel Pass(optional): </td>
               <td><input type='password' name='chanpass' value='' /></td>
            <tr>
               <td>Nick: </td>
               <td><input type='text' name='nick' value='Zero_Cool' /></td>
            </tr>
            <tr>
               <td>Pass (optional): </td>
               <td><input type='password' name='pass' value='' /></td>
            </tr>
            <tr>
               <td><input type='submit' name='submit' value='Connect' /></td>
            </tr>
         </table>
      </form>
   </body>
</html>";
} else {
   $handle   = fopen(date('m_d_y_H_i_s') . '.txt', 'w+');
   $nick     = $_POST['nick'];
   $pass     = $_POST['pass'];
   $server   = $_POST['server'];
   $channel  = $_POST['chan'];
   $chanpass = $_POST['chanpass'];
   if($server  == "") $server  = "irc.tdirc.net";
   if($nick    == "") { $nick  = "PHPZeroBot"; $pass = "b0tpwn5"; }
   if($channel == "") $channel = "#zeroidentity";
   print "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
<html>
   <head>
      <meta http-equiv='Content-type' content='text/html;charset=UTF-8'>
      <title> Zero_Cool || Control View </title>
      <style type='text/css'>
         body {
            background-color: #000000;
            color: #FFFFFF;
         }
         h1 {
            background-color: #FFFFFF;
            color: #000000;
         }
      </style>
   </head>
   
   <body>
      <h1>{$server}/{$channel} - <strong><span id='nick'>{$nick}</span></strong></h1>
      <br /><br />\n";
   $sock = fsockopen($server, 6667);
   fputs($sock, "USER {$nick} mlfmp.com {$nick} :Rezert's Bot\n");
   fputs($sock, "NICK {$nick}\n");
   
   if($pass != "") {
      fputs($sock, "PRIVMSG NickServ IDENTIFY {$pass}\n");
   }
   
   fputs($sock, "JOIN {$channel}\n");
   
   if($chanpass != "") {
      fputs($sock, "PASS {$chanpass}\n");
   }
   
   while(TRUE) {
      while($data = htmlentities(fgets($sock, 99999))) {
         
         $conn = mysql_pconnect($host, $username, $password);
         mysql_select_db($database, $conn);
         print(nl2br($data));
         fwrite($handle, html_entity_decode($data));
         flush();
         
         $dat   = str_replace(array(chr(10), chr(13)), "", $data);
         $split = explode(":", $dat);
         $name  = explode("!", $split[1]);
         $them  = $name[0];
         $words = explode(" ", $split[2]);
         $whate = explode(" ", $split[1]);
         $what  = $whate[1];
         $cmd   = $words[0];
         $arg   = $words[1];
         $arg2  = $words[2];
         if($what == "PRIVMSG") {
            $chan = $whate[2];
            if($chan == $nick) {
               $chan = $them;
            }
         }
         $args = "";
         foreach($words as $key => $val) {
            if($key > 1) {
               $args .= $val . " ";
            }
         }
         $args = html_entity_decode($args);
         
         $result = mysql_query("SELECT * FROM `users` WHERE `user`='{$them}'", $conn);
         $user   = mysql_fetch_array($result);
         if(mysql_num_rows($result) != 1) {
            $level = 1;
         } else {
            $level = $user['level'];
         }
         if($them == "Rezert") {
            $level = 2;
         }
         
         if($split[0] == "PING ") {
            fputs($sock, "PONG :{$split[1]}\n");
         } else {
            if($level != 0) {
               if($cmd == "*help") {
                  fputs($sock, "PRIVMSG {$them} ~ Menu for {$nick} Bot v1.7 ~\n");
                  fputs($sock, "PRIVMSG {$them} ~ These commands work from any channel.  This means you can private message him a command also. ~\n");
                  fputs($sock, "PRIVMSG {$them} ~ *help [shows this menu] ~\n");
                  fputs($sock, "PRIVMSG {$them} ~ *info [shows bot info] ~\n");
                  fputs($sock, "PRIVMSG {$them} ~ *flip [flips a coin] ~\n");
                  fputs($sock, "PRIVMSG {$them} ~ *masters [shows the botmasters] ~\n");
                  fputs($sock, "PRIVMSG {$them} ~ *cmds [show botmaster commands] ~\n");
                  fputs($sock, "PRIVMSG {$them} ~ End of Menu for {$nick} ~\n");
               } elseif($cmd == "*info") {
                  fputs($sock, "PRIVMSG {$them} This is a bot made by Rezert for ZeroIdentity.org.  He loves me very much, so he programmed me with lots of commands and more come very often.\n");
               } elseif($cmd == "*flip") {
                  $coin = rand(1, 2);
                  if($coin == 1) {
                     fputs($sock, "PRIVMSG {$chan} Flipping coin...Heads!\n");
                  } else {
                     fputs($sock, "PRIVMSG {$chan} Flipping coin...Tails!\n");
                  }
               } elseif($cmd == "*masters") {
                  $result = mysql_query("SELECT * FROM `users` WHERE `level`='2'", $conn);
                  $check  = mysql_num_rows($result);
                  $i = 1;
                  
                  unset($masters);
                  while($row = mysql_fetch_array($result)) {
                     if(!isset($masters)) {
                        $masters = $row['user'];
                     } else {
                        if($i == $check) {
                           $masters .= " and " . $row['user'];
                        } else {
                           $masters .= ", " . $row['user'];
                        }
                     }
                     $i++;
                  }
                  if($masters == "") {
                     fputs($sock, "PRIVMSG {$chan} I have no botmasters.\n");
                  }
                  if($masters != "") {
                     if($check == 1) {
                        fputs($sock, "PRIVMSG {$chan} {$masters} is my botmaster.\n");
                     } else {
                        fputs($sock, "PRIVMSG {$chan} {$masters} are my botmasters.\n");
                     }
                  }
               } elseif($cmd == "*ignored") {
                  $result = mysql_query("SELECT * FROM `users` WHERE `level`='0'", $conn);
                  $check  = mysql_num_rows($result);
                  $i = 1;
                  
                  unset($ignored);
                  while($row = mysql_fetch_array($result)) {
                     if(!isset($ignored)) {
                        $ignored = $row['user'];
                     } else {
                        if($i == $check) {
                           $ignored .= " and " . $row['user'];
                        } else {
                           $ignored .= ", " . $row['user'];
                        }
                     }
                     $i++;
                  }
                  if($ignored == "") {
                     fputs($sock, "PRIVMSG {$chan} Nobody is on my ignore list.\n");
                  }
                  if($ignored != "") {
                     if($check == 1) {
                        fputs($sock, "PRIVMSG {$chan} {$ignored} is the only person on my ignore list.\n");
                     } else {
                        fputs($sock, "PRIVMSG {$chan} {$ignored} are on my ignore list.\n");
                     }
                  }
               } elseif($cmd == "*cmds") {
                  if($level == 2) {
                     fputs($sock, "PRIVMSG {$them} ~ Botmaster Menu for {$nick} Bot v1.7 ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ These commands work from any channel.  This means you can private message him a command also. ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *say (nick/channel) (message) [says to nick/channel the message] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *me (nick/channel) (message) [says to nick/channel /me the message] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *nick (new nick) (pass..optional) [changes to new nick and if you specified a password it will identify itself] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *givev (nick) [gives nick voice] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *takev (nick) [takes voice from nick] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *giveo (nick) [gives nick ops] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *takeo (nick) [takes ops from nick] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *addbm (nick) [set nick to botmaster] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *reg (nick) [set nicks level to regular] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *ignore (nick) [sets nicks level to ignore] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ *quit [quits the bot] ~\n");
                     fputs($sock, "PRIVMSG {$them} ~ End of Botmaster Menu for {$nick} ~\n");
                  }
               } elseif($cmd == "*givev") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
                     } else {
                        fputs($sock, "MODE {$chan} +v {$arg}\n");
                     }
                  }
               } elseif($cmd == "*takev") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
                     } else {
                        fputs($sock, "MODE {$chan} -v {$arg}\n");
                     }
                  }
               } elseif($cmd == "*giveo") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
                     } else {
                        fputs($sock, "MODE {$chan} +o {$arg}\n");
                     }
                  }
               } elseif($cmd == "*takeo") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
                     } else {
                        fputs($sock, "MODE {$chan} -o {$arg}\n");
                  }
               }
               } elseif($cmd == "*addbm") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
                     } else {
                        $check = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `user`='{$arg}'", $conn));
                        if($check != 1) {
                           mysql_query("INSERT INTO `users` (user, level) VALUES('{$arg}', '2')", $conn);
                        } else {
                           mysql_query("UPDATE `users` SET `level`='2' WHERE `user`='{$arg}'", $conn);
                        }
                        fputs($sock, "PRIVMSG {$chan} {$arg} is now one of {$nick}'s botmasters.\n");
                     }
                  }
               } elseif($cmd == "*reg") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
                     } else {
                        mysql_query("DELETE FROM `users` WHERE `user`='{$arg}'", $conn);
                        fputs($sock, "PRIVMSG {$chan} {$arg} is now a regular user.\n");
                     }
                  }
               } elseif($cmd == "*ignore") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing nick argument.\n");
                     } else {
                        $check = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `user`='{$arg}'"));
                        if($check != 1) {
                           mysql_query("INSERT INTO `users` (user, level) VALUES('{$arg}', '0')", $conn);
                        } else {
                           mysql_query("UPDATE `users` SET `level`='0' WHERE`user`='{$arg}'", $conn);
                        }
                        fputs($sock, "PRIVMSG {$chan} {$arg} is now ignored by {$nick}\n");
                     }
                  }
               } elseif($cmd == "*say") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing channel/nick argument.\n");
                     } else {
                        if($arg2 == NULL) {
                           fputs($sock, "PRIVMSG {$chan} Missing message argument.\n");
                        } else {
                           fputs($sock, "PRIVMSG {$arg} {$args}\n");
                        }
                     }
                  }
               } elseif($cmd == "*me") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing channel/nick argument.\n");
                     } else {
                        if($arg2 == NULL) {
                           fputs($sock, "PRIVMSG {$chan} Missing message argument.\n");
                        } else {
                           fputs($sock, "PRIVMSG {$arg} ACTION {$args}\n");
                        }
                     }
                  }
               } elseif($cmd == "*nick") {
                  if($level == 2) {
                     if($arg == NULL) {
                        fputs($sock, "PRIVMSG {$chan} Missing new nick argument.\n");
                     } else {
                        fputs($sock, "NICK {$arg}\n");
                        print "<script>document.getElementById('nick').innerHTML='{$arg}';</script>";
                        if($arg2 != "") {
                           fputs($sock, "PRIVMSG NickServ IDENTIFY {$arg2}\n");
                        }   $nick = $arg;
                     }
                  }
               } elseif($cmd == "*quit") {
                  if($level == 2) {
                     fputs($sock, "PRIVMSG {$chan} Later.\n");
                  }
                  fputs($sock, "QUIT");
                  fclose($handle);
                  die("
   </body>
</html>");
               }
            }
         }
      }
   }
   print "
   </body>
</html>";
}

?>
Back to top
Teario
Member
Member


Joined: 25 Jun 2004
Posts: 130
Location: Liverpool(home) or Derby(uni), UK
Reputation: 59.8
votes: 3

PostPosted: Mon Jul 28, 2008 12:37 pm    Post subject: Reply with quote

How are you executing this script to get it in a browser window without your web server killing it for taking too long to execut?

How come you open a text file for the settings but you have users in a database? Wouldnt it better to have it one way or the other?

Also im not sure everyone is going to like having "Rezert's bot" as their bots "real name" in IRC.

Looks like youve put a lot of work into it though and done some research on the IRC protocol so well done, a refreshing change from most of the other new people who come here and just ask people to make their bot for them. How long did it take you? Looks like you have a decent grasp of PHP too, what other languages do you know?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Bots All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group