Guest User

irc.php ($interface)

a guest
Apr 23rd, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.40 KB | None | 0 0
  1. <?php
  2. // IRC interface for suphpbot
  3. /*
  4.     FUNCTIONS FOR INTERFACE CONNECTIONS, UPKEEP AND PARSING
  5. */
  6. function interface_connect() { // should initialize global variable $socket as a file handle
  7.     global $socket,$settings;
  8.     if ($settings['ssl']==1) {
  9.         $socket = @fsockopen('ssl://' . $settings['server'], $settings['port'], $errno, $errstr, 20);
  10.     } else {
  11.         $socket =  fsockopen($settings['server'], $settings['port'], $errno, $errstr, 20);
  12.     }
  13. }
  14. function interface_startup() { // function to run if the connection is established. set $bnick to the bot's username/nick
  15.     global $socket,$settings,$bnick;
  16.     stream_set_blocking($socket, 1); // we fix the dreaded 100% CPU issue
  17.     if ($settings['pass']!=='') {
  18.         send('PASS ' . $settings['pass']);
  19.     }
  20.     send('USER ' . $settings['ident'] . ' 8 * :' . $settings['realname']);
  21.     send('NICK ' . $bnick);
  22. }
  23. function interface_retrieve_buffer() { // $buffer = fgets($socket);
  24.     global $buffer,$socket;
  25.     $buffer = fgets($socket);
  26. }
  27. function interface_loop_extraction() { // function to extract data from the current global, $buffer into a bunch of vars
  28.     global $bnick,$buffer,$admin,$buffwords,$nick,$channel,$in_convo,$hostname,$bw,$arguments,$args,$ignore,$settings,$hostmask;
  29.     $buffer = str_replace(array("\n","\r"),'',$buffer);
  30.     $buffer = xtrim($buffer); // get rid of doubles
  31.     $admin = FALSE;
  32.     $buffwords = explode(' ',$buffer);
  33.     $nick = explode('!',$buffwords[0]);
  34.     $nick = substr($nick[0],1);
  35.     $channel = $buffwords[2];
  36.     if ($channel==$bnick) {
  37.         $in_convo = TRUE;
  38.         $channel = $nick;
  39.     } else {
  40.         $in_convo = FALSE;
  41.     }
  42.     $hostnameexplode = explode('@', $buffwords[0]);
  43.     $hostname = end($hostnameexplode);
  44.     $hostmask = $hostname;
  45.     $bw = $buffwords;
  46.     $bw[0]=NULL; $bw[1]=NULL; $bw[2]=NULL; $bw[3]=NULL;
  47.     $arguments = trim(implode(' ',$bw));
  48.     $args = explode(' ',$arguments);
  49.     $ignore = explode(',',$settings['ignore']);
  50. }
  51.  
  52. function interface_loop_upkeep() { // do what you need to do to keep the connection alive and in good health here. in IRC, we check for nick collisions and pings and such
  53.     global $buffwords,$bnick,$settings,$buffer;
  54.     if ($buffwords[1]=='002') {
  55.         // The server just sent us a connection message. We're in.
  56.         sleep(1); // because IRC is serious business
  57.         // usermodes are important
  58.         if (!empty($settings['automode'])) {
  59.             send('MODE ' . $bnick . ' ' . $settings['automode']);
  60.             send('VHOST ' . $settings['vhost'] . ' ' . $settings['vhostpass']);
  61.         }
  62.         if ($settings['nickserv_pass']!=='') {
  63.             // Let's identify before we join any channels.
  64.             send_msg($settings['nickserv_nick'],'IDENTIFY ' . $settings['nickserv_pass']);
  65.         }
  66.         $channels = explode(',',$settings['channels']);
  67.         foreach ($channels as $channel) {
  68.             send('JOIN ' . trim($channel));
  69.         }
  70.     } elseif ($buffwords[1]=='433') {
  71.         // Nick collision! a waooo. fix $bnick and renick, pronto!
  72.         //$bnick = $settings['nick'] . '_' . mt_rand(100,999);
  73.         //send('NICK ' . $bnick);
  74.         send_msg($settings['nickserv_nick'],'RECOVER ' . $settings['nickserv_nick'] . ' ' . $settings['nickserv_pass']);
  75.         send_msg($settings['nickserv_nick'],'RELEASE ' . $settings['nickserv_nick'] . ' ' . $settings['nickserv_pass']);
  76.        
  77.     } elseif ($buffwords[0]=='PING') {
  78.         $replace = explode(' ', $buffer, 2);
  79.         $replace = end($replace);
  80.         send('PONG ' . str_replace("\n",'',$replace));
  81.         announce_class();
  82.         forum_newtopic();
  83.         forum_newpost();
  84.     }
  85.     elseif ($buffwords[1] == 'JOIN' && $buffwords[2] == ':#shout') {
  86.         $userjoined = explode('!', $buffwords[0]);
  87.         $userjoined = str_replace(':', '', $userjoined[0]);
  88.         check_ongoing_class($userjoined);
  89.     }
  90. /*  if($buffwords[1]=='376') {
  91.         send_msg($settings['nickserv_nick'],'IDENTIFY ' . $settings['nickserv_pass']);
  92.     } */
  93. }
  94. function interface_loop_command() { // return the string with the command name if we need to run a command, otherwise boolean FALSE
  95.     global $buffwords,$settings,$in_convo;
  96.     if ($buffwords[1]=='PRIVMSG'&&((substr($buffwords[3],1,strlen($settings['commandchar']))==$settings['commandchar'])||$in_convo)) {
  97.         if ($in_convo) {
  98.             $command = trim(substr($buffwords[3],strlen($settings['commandchar'])));
  99.         } else {
  100.             $command = trim(substr($buffwords[3],strlen($settings['commandchar'])+1));
  101.         }
  102.         $command = strtolower($command);
  103.         return $command;
  104.     } else {
  105.         return FALSE;
  106.     }
  107. }
  108. /*
  109.     REQUIRED FUNCTIONS FOR GENERAL SCRIPT USAGE
  110. */
  111. function send($raw) { // send raw data through the socket
  112.     global $socket;
  113.     call_hook('data_out');
  114.     fwrite($socket,"{$raw}\n\r");
  115.     shell_send($raw,'OUT');
  116. }
  117. function send_msg($target,$message,$type=0) { // format data all pretty-like and send it out
  118.     global $nick,$settings;
  119.     $types = array('PRIVMSG','NOTICE'); // not everything needa be sent to the channel
  120.     // Let's chunk up the message so it all gets sent.
  121.     $message = str_split($message,intval($settings['maxlen']));
  122.     if (count($message)>intval($settings['maxsend'])) {
  123.         // We want to use maxsend-1 in this situation because we'll be appending an error telling the user what
  124.         // exactly happened to the rest of their output.
  125.         $message = array_slice($message,0,intval($settings['maxsend']-1));
  126.         $message[] = 'The output for your command was too long to send fully.';
  127.     }
  128.     $badwords = explode(',',$settings['censor_badwords']);
  129.     foreach ($message as $msg) {
  130.         if ($settings['censor_output']==1) {
  131.             $msg = str_replace($badwords,$settings['censor_word'],$msg);
  132.         }
  133.         $msg = str_replace('&quot;', '"', $msg);
  134.         $msg = str_replace(array('%nick%','%message%'),array(fx('BOLD',$nick),xtrim($msg)),$settings['message_style']);
  135.         send($types[$type] . ' ' . $target . ' :' . $msg);
  136.     }
  137. }
  138. function noperms() { // we gonna use this to notify people trying to do things they shouldn't be doing
  139.     global $nick;
  140.     send_msg($nick,'Sorry, you don\'t have the appropriate permissions for that.',1);
  141. }
  142. /*
  143.     WRAP CONSTANTS FOR FX()
  144. */
  145. define('C_BOLD', chr(2));
  146. define('C_COLOR', chr(3));
  147. define('C_ITALIC', chr(29));
  148. define('C_REVERSE', chr(22));
  149. define('C_UNDERLINE', chr(31));
  150. /*
  151.     MISC. INTERFACE-SPECIFIC FUNCTIONS
  152. */
  153. function get_response($command,$terminating,$musthave) { // this just returns an array with all the responses to a particular command with a particular precode
  154.     global $socket;
  155.     send($command);
  156.     $response = array();
  157.     while(1) {
  158.         $buffer = trim(fgets($socket));
  159.         $bw = explode(' ',$buffer);
  160.         if ($bw[1]==$terminating) {
  161.             break;
  162.         }
  163.         if (strlen($buffer)>0&&$bw[1]==$musthave) {
  164.             $response[] = $buffer;
  165.         }
  166.     }
  167.     return $response;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment