IRC user listing script

Ice/DBus, Web-Interfaces, Management tools

IRC user listing script

Postby spedex » Wed Aug 18, 2010 3:59 pm

So yeah, I've been running mumble for a long time and at some point I made a small script to spam mumble users, the channel they're in, and their idle time to irc. Couple of things to note before using my script:

1) It's coded in PHP because I can code PHP pretty well, and couldn't be arsed to spend n+y hours tinkering with TCL. So only use if you don't care about this.
2) It requires the ice php bindings (aptitude install php-zeroc-ice if I recall correctly)

Image

... And oh yeah, I start the script with "screen -S mumble-bot php irc.php"
Last edited by spedex on Sat Jul 02, 2011 9:49 pm, edited 1 time in total.
spedex
 
Posts: 2
Joined: Wed Aug 18, 2010 3:52 pm

Re: IRC user listing script

Postby spedex » Sat Jul 02, 2011 7:53 pm

Code: Select all
<?
/**
* @package MumbleBot
* @author spede
* @author thanks2Gazelle
* @version CVS: $Id: mumble.php,v 1.07 2011/06/26 20:05:19 spede Exp $
**/

define("LOGFILE", "./mumblebot.log");
define("PRIV_ANSWER", "MumbleBot v. 1.07 by spede@dota.fi/#qq.dota@quakenet");

restore_error_handler();
global $ICE, $MumbleBot;

class MUMBLE_BOT {

/****************
* CONFIGURATION *
****************/
   protected $Host = 'fr.quakenet.org';
   protected $Port = '6667';
   protected $Channel = '#your_channel';
   protected $Nick = 'Mumbled';

   protected $Debug = false;
   protected $Socket = false;
   protected $Data = false;
   protected $Whois = false;
   protected $Identified = array();
   protected $Channels = array();
   protected $Messages = array();
   protected $LastChan = false;
   protected $ListenSocket =false;
   protected $Listened = false;
   protected $State = 1;
   public $Restart = 0;

   public function __construct() {
      logger("MumbleBot->construct()\n");
      Ice_loadProfile();
      //restore_error_handler(); - php virhehallinta
      set_time_limit(0);
   }

   public function connect() {
      logger(sprintf("MumbleBot->connect() -> %s:%s", $this->Host, $this->Port) . "\n");
      $this->Socket = fsockopen($this->Host, $this->Port);
      stream_set_blocking($this->Socket, 0);
      fwrite($this->Socket, "NICK ". $this->Nick ." Init\n");
      fwrite($this->Socket, "USER ". $this->Nick ." * * :MumbleBot by spede\n");
      $this->listen();
   }

    public function disconnect() {
      logger("MumbleBot->disconnect()\n");
      socket_close($this->ListenSocket);
      $this->State = 0;
   }

   public function get_channel() {
      preg_match('/.+ PRIVMSG ([^:]+) :.+/', $this->Data, $Channel);
      if(preg_match('/#.+/',$Channel[1])) {
         return $Channel[1];
      } else {
         return false;
      }
   }

   public function get_nick() {
      preg_match('/:([^!:]+)!.+@[^\s]+ PRIVMSG [^:]+ :.+/', $this->Data, $Nick);
      return $Nick[1];
   }

   protected function get_message() {
      preg_match('/:.+ PRIVMSG [^:]+ :(.+)/', $this->Data, $Msg);
      return trim($Msg[1]);
   }
   
   protected function get_line() {
      $Bulk = explode(" ", $this->get_message());
      array_shift($Bulk);
      
      return implode(" ", $Bulk);
   }

   protected function get_host() {
      preg_match('/:[^!:]+!.+@([^\s]+) PRIVMSG [^:]+ :.+/', $this->Data, $Host);
      return trim($Host[1]);
   }

   protected function get_word($Select=1) {
      preg_match('/:.+ PRIVMSG [^:]+ :(.+)/', $this->Data, $Word);
      $Word = split(' ',$Word[1]);
      return trim($Word[$Select]);
   }

   protected function get_action() {
      preg_match('/:.+ PRIVMSG [^:]+ :!(\S+)/', $this->Data, $Action);
      if( sizeof($Action) >= 1)
         return strtoupper($Action[1]);
      else
         return "";
   }

   protected function send_raw($Text) {
      fwrite($this->Socket, $Text."\n");
   }

   public function send_to($Channel, $Text) {
      fwrite($this->Socket, "PRIVMSG $Channel :$Text\n");
   }

   protected function whois($Nick) {
      $this->Whois = $Nick;
      $this->send_raw("WHOIS $Nick");
   }
   
   public function time_difference($Sec) {
        $Stamp = $Sec;
        $Hours = floor( $Stamp / 3600 );
        $Remain = $Stamp - $Hours * 3600;

        $Min = floor( $Remain / 60 );
        $Remain = $Remain - $Min * 60;

        $Sec = $Remain;

        return "" . ($Hours > 0 ? $Hours . "h " : "") . ($Min > 0 ? $Min . "min " : "") . $Sec . "s";
   }

   public function mumble_user_list() {
      global $ICE;
      $base = $ICE->stringToProxy("Meta:tcp -h 127.0.0.1 -p 6502");
      $meta = $base->ice_checkedCast("::Murmur::Meta");
      $str = "";
      $servers = $meta->getBootedServers();

      foreach($servers as $s) {
         $channels = $s->getChannels();
         $players = $s->getUsers();
         
         foreach($players as $id => $state) {
            $chan = $channels[$state->channel];
            $str .= "\002\0037" . $state->name."\003\002(#".$chan->name.")(idle: " .  $this->time_difference($state->idlesecs) . ") ";
         }
      }
      return $str;
   }
   
   public function channel_events() {
         $mStart = microtime(true);
         $Served = 0;

      
      switch( $this->get_action() ) {
         case "MUMBLE":
            $this->send_to($this->Channel, $this->mumble_user_list());
            $Served = 1;
         break;
         
         case "DIE":
            logger("mumblebot->die() invoked by " . $this->get_nick() . "\n");
            $this->send_raw("QUIT :dying");
            $this->disconnect();
            die("!DIE");
         break;
         
      if( $Served ) {
         $mEnd = microtime(true);
         logger("command=" . $this->get_message() . " from=" . $this->get_nick() . " took " . ($mEnd-$mStart) . "sec\n");
      }
   }
   
   public function connect_events() {
      logger("MumbleBot->connect_events()\n");
      $this->send_raw("JOIN :" . $this->Channel);
   }
   
   public function query_events() {
   }
   
   public function listener_events() {
   }
         
   protected function listen() {
      stream_set_timeout($this->Socket, 10000000000);
      while($this->State == 1){
         if($this->Data = fgets($this->Socket, 256)) {

            if($this->Whois !== false) {
               $Exp = explode(' ',$this->Data);
               if($Exp[1] == '307') {
                  $this->Identified[$this->Whois] = 1;
                  $this->send_to($this->LastChan, "$this->Whois correctly identified as a real person!");
                  $this->Whois = false;
                  $this->LastChan = false;
               } elseif($Exp[6] == '/WHOIS') {
                  $this->Whois = false;
               }
            }

            if(preg_match("/:([^!]+)![^\s]* QUIT.* /", $this->Data, $Nick)) {
               if(isset($this->Identified[$Nick[1]])) {
                  unset($this->Identified[$Nick[1]]);
               }
            }

            if(preg_match("/End of \/MOTD command./", $this->Data)) {
               $this->connect_events();
            }

            if(preg_match('/PING :(.+)/', $this->Data, $Ping)) {
               $this->send_raw('PONG :'.$Ping[1]);
            }

            if(preg_match('/.*PRIVMSG #.*/',$this->Data)) {
               $this->channel_events();
            }

            if(preg_match("/.* PRIVMSG ". $this->Nick . " .*/",$this->Data)) {
               $this->query_events();
            }
         }
         
         usleep(5000);
      }
   }
}

if (!isset($argv)) {
        die('This is not a web-accessible script. Sorry.');
}

function logger( $line ) {
   $line = "[" . date("Y-m-d H:i:s") . "]:" . $line;
   
   $Log = fopen( LOGFILE, 'a');
   fwrite($Log, $line);
   fclose($Log);
}

$MumbleBot = new MUMBLE_BOT;
$MumbleBot->connect();

?>


New and (infinitely) better version, automatically joins the channel and logs stuff. Easy to add more commands into the giant switch should the need arise. I've url title fetcher, google and imdb search but stripped them out for simplicity's sake. Just change the stuff under "configuration" to your channel, etc.

edeps@quakenet in case of questions
thanks to p-gazelle
spedex
 
Posts: 2
Joined: Wed Aug 18, 2010 3:52 pm


Return to Scripting

Who is online

Users browsing this forum: No registered users and 4 guests

cron