bssanchez93

PHP - Simple Bot

May 20th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.13 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @autor:      kid_goth
  5.  * @copyright   (None) 2014, Brandon Sanchez
  6.  * @todo        Consultar los RSS de los foros en un archivo txt para
  7.  *              enviar por privado al usuario que lo pida con !news
  8.  *
  9.  * @filesource  $archivoURL es la ruta del archivo que tiene los foros
  10.  *              y sus respectivas urls, el formato debe ser así en cada
  11.  *              linea:
  12.  *              NombreForo::http://urldelforo.dom/?uri=rss
  13.  */
  14. error_reporting(E_ALL);
  15.  
  16. class bot_foro {
  17.  
  18.     private static $servidor = 'irc.servidor.dom';
  19.     private static $ip_servidor;
  20.     private static $puerto = 6667;
  21.     private static $canal = '#canal';
  22.     private static $nick = "forum_feeds";
  23.     private static $usuario = "ff";
  24.     private static $nombre = "ForumFeeds";
  25.     private static $archivoURL = './foros.txt';
  26.     private $socket;
  27.     private $conexion;
  28.  
  29.     public function __construct() {
  30.         self::$ip_servidor = gethostbyname(self::$servidor);
  31.         $this->crearSocket();
  32.         $this->conectarSocket();
  33.         $this->iniciarTransaccion();
  34.     }
  35.  
  36.     private function crearSocket() {
  37.         $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  38.  
  39.         if ($this->socket === false) {
  40.             die("socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "\n");
  41.         } else {
  42.             print "Conexion socket_create(): OK.\n";
  43.         }
  44.     }
  45.  
  46.     private function conectarSocket() {
  47.         $this->conexion = socket_connect($this->socket, self::$ip_servidor, self::$puerto);
  48.  
  49.         if ($this->conexion === false) {
  50.             die("socket_connect() falló.\nRazón: ($result) " . socket_strerror(socket_last_error($socket)) . "\n");
  51.         } else {
  52.             print "OK.\n";
  53.         }
  54.     }
  55.  
  56.     private function leerXml($url, $foro, $nick) {
  57.         $url = chop($url);
  58.         $feeds = simplexml_load_file($url);
  59.         //var_dump($feeds);
  60.         if ($feeds) {
  61.             foreach ($feeds->channel->item as $feed) {
  62.                 $this->escribir("PRIVMSG " . $nick . " :________________________________________");
  63.                 $mensaje = "[ " . $feed->title . " ] -> " . $feed->link;
  64.                 $this->escribir("PRIVMSG " . $nick . " :" . $mensaje);
  65.             }
  66.         } else {
  67.             $mensaje = "No se pudieron cargar los datos del foro " . $foro;
  68.             $this->escribir("PRIVMSG " . $nick . " :" . $mensaje);
  69.         }
  70.     }
  71.  
  72.     private function leerUrls($nick) {
  73.         $txt = fopen(self::$archivoURL, 'r');
  74.         while (($linea = fgets($txt, 4096)) !== false) {
  75.             $datos = explode('::', $linea);
  76.            
  77.             $this->escribir("PRIVMSG " . $nick . " :[Ultimos post de " . $datos[0] . "]");
  78.             $this->leerXml($datos[1], $datos[0], $nick);
  79.         }
  80.         fclose($txt);
  81.     }
  82.  
  83.     private function iniciarTransaccion() {
  84.         $out = '';
  85.         $arrOut = array();
  86.         $in = 0;
  87.  
  88.         while ($out = socket_read($this->socket, 2048)) {
  89.             $cping = 0;
  90.             $arrOut = explode(" ", $out);
  91.  
  92.             $_nick = substr($arrOut[0], 1, (strpos($arrOut[0], "!") - 1));
  93.             $_accion = $arrOut[1];
  94.             $_canal = $arrOut[2];
  95. //$_mensaje = $arrOut[3];
  96.  
  97.             $arrOut2 = explode(" :", $out);
  98. //$comando = $arrOut2[0];
  99.             $texto = $arrOut2[1];
  100.  
  101.             print $_canal . ": " . $_nick . " -> " . $_accion . " - " . $texto . "\r\n";
  102.  
  103.             if (preg_match('/004/', $out)) {
  104.                 print "Aceptado...\r\n";
  105.             } else if (preg_match('/433/', $out)) {
  106.                 die(self::$nick . " ya esta en uso...");
  107.             } else if (preg_match('/^PING(.*)$/i', $out)) {
  108.                 $pong = explode(" ", $out);
  109.                 $this->escribir("PONG " . $pong[1]);
  110.                 $cping++;
  111.             } else if (preg_match('/376/i', $out)) {
  112.                 print "Accediendo al canal...\r\n";
  113.                 $this->escribir("JOIN " . self::$canal);
  114.                 print "Adentro y saludando\r\n";
  115.                 $this->escribir("PRIVMSG " . self::$canal . " :Listo para el trabajo");
  116.                 $in = 1;
  117.             }
  118.             if (preg_match('/NOTICE AUTH/i', $out)) {
  119.                 print "Enviando datos...\r\n";
  120.  
  121.                 $this->escribir("NICK " . self::$nick);
  122.                 $this->escribir("USER " . self::$usuario . " 8 * :" . self::$nombre);
  123.             } else if (preg_match('/(MOTD)/i', $out) && $in != 1) {
  124.                 print "Accediendo al canal...\r\n";
  125.                 $this->escribir("JOIN " . self::$canal);
  126.                 print "Adentro y saludando\r\n";
  127.                 $this->escribir("PRIVMSG " . self::$canal . " :Listo para el trabajo");
  128.                 $in = 1;
  129.             } else if (preg_match('/^(!news)/', $texto)) {
  130.                 $this->leerUrls($_nick);
  131. //$this->escribir("PRIVMSG " . $_nick . " :Wola zapatin con cola");
  132.             }
  133.  
  134.  
  135.             if ($cping == 100) {
  136.                 $cping = 0;
  137.             }
  138.         }
  139.     }
  140.  
  141.     private function escribir($mensaje = "") {
  142.         socket_write($this->socket, $mensaje . "\r\n");
  143.     }
  144.  
  145. }
  146.  
  147. new bot_foro();
Advertisement
Add Comment
Please, Sign In to add comment