Guest User

ircbot.php

a guest
Apr 23rd, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.71 KB | None | 0 0
  1. <?php
  2. /*
  3.     suphpbot - A modular, procedural IRC bot written entirely in PHP.
  4.     https://github.com/flotwig/suphpbot
  5.     (c)2011 Zachary Bloomquist [email protected] http://za.chary.us/
  6. */
  7. if (PHP_SAPI !== 'cli') { die('This script can\'t be run from a web browser.'); }
  8. define('START_TIME',time()); // so we can have core::uptime
  9. set_time_limit(0); // so your bot doesn't die after 30 seconds
  10. date_default_timezone_set(date_default_timezone_get()); // because PHP can be a bitch sometimes
  11. ini_set('error_reporting',E_ALL-E_NOTICE);
  12. if (count(getopt('c:'))>0) {
  13.     $config = getopt('c:');
  14.     $config = $config['c'];
  15. } else {
  16.     $config = 'ircconfig.ini';
  17. }
  18. load_settings();
  19. if (count(getopt(NULL,array('running')))<1) {
  20.     fork_bot();
  21.     die();
  22. }
  23. shell_send('Script started.');
  24. define('IRC_VERSION',$GLOBALS['settings']['version']);
  25. // let's load up our interface
  26. $interface = $GLOBALS['settings']['interface'];
  27. if (empty($interface)) {
  28.     $interface = 'irc';
  29. }
  30. $required_functions = array('interface_connect','interface_startup','interface_retrieve_buffer','interface_loop_extraction','interface_loop_upkeep','interface_loop_command','send','send_msg');
  31. if (!file_exists('./interfaces/' . $interface . '.php')) {
  32.     shell_send('The interface "' . $interface . '" was not found at ./interfaces/' . $interface . '.php.','FATAL');
  33.     die();
  34. }
  35. require_once('./interfaces/' . $interface . '.php');
  36. foreach ($required_functions as $required_function) {
  37.     if (!function_exists($required_function)) {
  38.         shell_send('The interface "' . $interface . '" does not specify the core function "' . $required_function . '".','FATAL');
  39.     }
  40. }
  41. // hooks in the house! woo woo
  42. $hooks = array('data_in'=>array(),
  43.     'data_out'=>array());
  44. // preload some modules
  45. $commands = array();
  46. $help = array();
  47. $loaded_modules = array();
  48. $strikes = array();
  49. foreach ($premods as $premod) {
  50.     $premod = trim($premod);
  51.     load_module($premod);
  52. }
  53. $lastsent = array(); // Array of timestamps for last command from nicks - helps prevent flooding
  54. $bnick = $settings['nick'];
  55. $tries = 0;
  56. $socket = NULL; // doot doot
  57. while (1) {
  58.     $tries++;
  59.     interface_connect(); // refer to interface file for connection command, yo
  60.     if (!$socket) {
  61.         shell_send('Unable to connect! Retrying in ' . round(pow(5,.5*$tries)) . ' seconds...');
  62.     } else {
  63.         interface_startup();
  64.         stream_set_timeout($socket, 60);
  65.         while(is_resource($socket) && !feof($socket)) {
  66.             interface_retrieve_buffer();
  67.             interface_loop_extraction();
  68.             if (!in_array($hostname,$ignore)) {
  69.                 call_hook('data_in');
  70.                 shell_send($buffer,'IN');
  71.                 interface_loop_upkeep();
  72.                 $command = interface_loop_command();
  73.                 if ($command) {
  74.                     $blocked = explode(',',$settings['blockedcommands']);
  75.                     if ($lastsent[$hostname]<(time()-$settings['floodtimer'])||$admin) { // we let admins flood the bot lul
  76.                         $lastsent[$hostname]=time();
  77.                         if (in_array($hostname,$ignore)) {
  78.                             // do nothing - we're ignoring them :p
  79.                         } else {
  80.                             if (in_array($command,$blocked)) {
  81.                                 send_msg($nick,$command . ' is a blocked command. Contact a bot administrator for guidance.',1);
  82.                             } elseif (function_exists($commands[$command])) {
  83.                                 call_hook('command_' . $command);
  84.                                 call_user_func($commands[$command]);
  85.                             } else {
  86.                                 send_msg($nick,$command . ' is not a valid command. Maybe you need to load a plugin?',1);
  87.                             }
  88.                         }
  89.                     } else {
  90.                         // they dun goofed - bot spamming? not on my watch, let's add a strike to they
  91.                         if (!isset($strikes[$hostmask])) {
  92.                             $strikes[$hostmask] = 1;
  93.                         } else {
  94.                             $strikes[$hostmask]++;
  95.                         }
  96.                         if ($strikes[$hostmask]==$settings['strikes']) {
  97.                             $ignore[] = $hostmask;
  98.                             $settings['ignore'] = implode(',',$ignore);
  99.                             save_settings($settings,$config);
  100.                             send_msg($nick,'Hi, you\'ve been added to my ignore list for flooding! Congratulations! Contact a bot administrator for guidance.',1);
  101.                             $strikes[$hostmask]=0;
  102.                         }
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.     }
  108. /*
  109. Uncomment this if you want it to stop attempting to reconnect.
  110. If you're gone for extended periods of time it's generally a good idea to allow it to reconnect indefinitely.
  111.  
  112.     sleep(round(pow(5,.5*$tries))); // reconnecting too fast like woah. exponential growth is quick to occur. if your irc server is down for a day and this bot is trying to reconnect the whole time, you should probably just restart the process :p
  113.    
  114. */
  115. }
  116. // borrowed from gtoxic of avestribot, who borrowed it from somebody else...
  117. function save_settings($array, $file) {
  118.     $res = array();
  119.     foreach($array as $key => $val) {
  120.         if(is_array($val)) {
  121.             $res[] = "[$key]";
  122.             foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
  123.         } else {
  124.             $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
  125.         }
  126.     }
  127.     $res = implode("\r\n", $res);
  128.     $res = '; IRC bot config file' . "\r\n" . '; For more info, check the README' . "\r\n" . $res;
  129.     file_put_contents($file,$res);
  130. }
  131. // supertrim function by jose cruz
  132. // josecruz at josecruz dot com dot br
  133. function xtrim($str) {
  134.     $str = trim($str);
  135.     for($i=0;$i < strlen($str);$i++) {
  136.         if(substr($str, $i, 1) != " ") {
  137.             $ret_str .= trim(substr($str, $i, 1));
  138.         } else  {
  139.             while(substr($str,$i,1) == " ") {
  140.                 $i++;
  141.             }
  142.             $ret_str.= " ";
  143.             $i--; // ***
  144.         }
  145.     }
  146.     return $ret_str;
  147. }
  148. function load_settings() {
  149.     global $settings,$premods,$ignore,$config;
  150.     $settings = parse_ini_file($config);
  151.     $premods = explode(',',$settings['module_preload']);
  152.     $ignore = explode(',',$settings['ignore']);
  153. }
  154. function call_hook($hook) {
  155.     global $hooks;
  156.     $hook = $hooks[$hook];
  157.     if (is_array($hook)) {
  158.         foreach ($hook as $hookah) {
  159.             call_user_func($hookah);
  160.         }
  161.     }
  162. }
  163. function shell_send($message,$type='NOTE') {
  164.     echo '[' . date('H:i:s m-d-Y') . '] [' . $type . ']' . "\t" . $message . "\n";
  165. }
  166. function load_module($modname) {
  167.     global $commands, $function_map, $hook_map, $hooks;
  168.     global $help, $help_map, $loaded_modules;
  169.     $load = @include_once('./modules/' . trim($modname) . '.php');
  170.     if ($load) {
  171.         $commands = array_merge($commands,$function_map[trim($modname)]);
  172.         if (isset($hook_map[trim($modname)])) {
  173.             foreach ($hook_map[trim($modname)] as $hook_id => $hook_function) {
  174.                 $hooks[$hook_id][] = $hook_function;
  175.             }
  176.         }
  177.         $loaded_modules[] = $modname;
  178.         if (is_array($help_map[trim($modname)])) {
  179.             $help = array_merge($help,$help_map[trim($modname)]);
  180.         }
  181.     }
  182. }
  183. function unload_module($modname) {
  184.     global $commands,$function_map,$hook_map,$help_map,$loaded_modules,$hooks,$help;
  185.     $function_map = array_diff($function_map,array($modname));
  186.     if (is_array($hook_map[$modname])) {
  187.         foreach ($hook_map[$modname] as $hook_name => $hook_function) {
  188.             $hooks[$hook_name] = array_diff($hooks[$hook_name],array($hook_function));
  189.         }
  190.     }
  191.     if (is_array($help_map[$modname])) {
  192.         $help = array_diff($help,$help_map[$modname]);
  193.     }
  194.     $loaded_modules = array_diff($loaded_modules,array($modname));
  195. }
  196. function fx($filter,$text,$ignorecc=FALSE) {
  197.     global $settings;
  198.     if (defined('C_' . strtoupper($filter)) && $settings['control_codes']!==0 && $ignorecc) {
  199.         return constant('C_' . strtoupper($filter)) . $text . constant('C_' . strtoupper($filter));
  200.     } else {
  201.         return $text;
  202.     }
  203. }
  204. function fork_bot() {
  205.     global $settings,$config;
  206.     if ($settings['logging']) {
  207.         shell_send(shell_exec('echo "php ' . basename(__FILE__) . ' --running -c ' . $config . ' >> ' . $settings['logfile'] . '" | at now'));
  208.         shell_send('Forked ' . basename(__FILE__) . ' into background using at. Logging to '. $settings['logfile'] .'.');
  209.     } else {
  210.         shell_send(shell_exec('php ' . basename(__FILE__) . ' --running'));
  211.     }
  212. }
  213. ?>
Advertisement
Add Comment
Please, Sign In to add comment