Advertisement
Eeems

server.class.php

May 21st, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.95 KB | None | 0 0
  1. <?php
  2.  
  3.     class server{
  4.  
  5.         function __construct($se,$po,$ni,$na,$cha = array()){
  6.  
  7.             $this->md5 = md5_file('users.ini');
  8.  
  9.             $this->server = $se;
  10.  
  11.             $this->port = $po;
  12.  
  13.             $this->nick = $ni;
  14.  
  15.             $this->name = $na;
  16.  
  17.             $this->socket = fsockopen($se,$po);
  18.  
  19.             if(!$this->socket){
  20.  
  21.                 $this->out('Could not connect to '.$se.':'.$po);
  22.  
  23.                 return false;
  24.  
  25.             }
  26.  
  27.             $this->send('USER',$this->nick.' eeezor.ec3club.tk '.$this->nick.' :'.$this->name);
  28.  
  29.             $this->send('NICK', $this->nick);
  30.  
  31.             $l = 0;
  32.  
  33.             while($l < 10) {
  34.  
  35.                 $l++;
  36.  
  37.                 $data = fgets($this->socket, 128);
  38.  
  39.                 if(trim($data)!=''){
  40.  
  41.                     $this->out(trim($data));
  42.  
  43.                 }
  44.  
  45.                 // Separate all data
  46.  
  47.                 $this->ex = explode(' ', $data);
  48.  
  49.                 // Send PONG back to the server
  50.  
  51.                 if($this->ex[0] == "PING"){
  52.  
  53.                     $this->send("PONG ".$this->ex[1]);
  54.  
  55.                 }
  56.  
  57.             }
  58.  
  59.             sleep(1);
  60.  
  61.             foreach($cha as $c){
  62.  
  63.                 $this->join($c);
  64.  
  65.             }
  66.  
  67.         }
  68.  
  69.         public function recieve(){
  70.  
  71.             while($this->active){
  72.  
  73.                 $data = fgets($this->socket, 128);
  74.  
  75.                 if(trim($data)!=''){
  76.  
  77.                     if(md5_file('users.ini')!=$this->md5){
  78.  
  79.                         $this->out("-----Reloading User Database----");
  80.  
  81.                         set_ini_array(parse_ini_file('users.ini',true),'USERS');
  82.  
  83.                     }
  84.  
  85.                     $this->ex = explode(' ', $data);
  86.  
  87.                     foreach($this->ex as $i => $v){
  88.  
  89.                         $this->ex[$i] = trim($v);
  90.  
  91.                     }
  92.  
  93.                     if($this->ex[0] == 'PING'){
  94.  
  95.                         $this->send('PONG', $this->ex[1]);
  96.  
  97.                         continue;
  98.  
  99.                     }elseif($this->ex[0] == 'ERROR'){
  100.  
  101.                         $this->out("CONNECTION ERROR");
  102.  
  103.                         $this->active = false;
  104.  
  105.                         continue;
  106.  
  107.                     }
  108.  
  109.                     if(isset($this->ex[3])){
  110.  
  111.                         $this->args['cmd'] = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
  112.  
  113.                         if(substr($this->args['cmd'],0,1)==':'){
  114.  
  115.                             $this->args['cmd'] = substr($this->args['cmd'],1);
  116.  
  117.                         }
  118.  
  119.                     }else{
  120.  
  121.                         $this->args['cmd'] = '';
  122.  
  123.                     }
  124.  
  125.                     $this->args['chan'] = str_replace(array(chr(10), chr(13)), '', $this->ex[2]);
  126.  
  127.                     $this->args['nick'] = explode(':',$this->ex[0]);
  128.  
  129.                     $this->args['nick'] = explode('!',$this->args['nick'][1]);
  130.  
  131.                     $this->args['nick'] = $this->args['nick'][0];
  132.  
  133.                     $this->args['ischan'] = true;
  134.  
  135.                     $this->args['server'] = $this;
  136.  
  137.                     if(!substr_count($this->args['chan'],"#")){
  138.  
  139.                         $this->args['ischan'] = false;
  140.  
  141.                         $this->args['chan'] = $this->args['nick'];
  142.  
  143.                     }else{
  144.  
  145.                         $this->out(trim($data));
  146.  
  147.                     }
  148.  
  149.                     $this->checkACTION();
  150.  
  151.                     $this->checkALL();
  152.  
  153.                     $this->checkMSG();
  154.  
  155.                     foreach($GLOBALS['MODULES'] as $m){
  156.  
  157.                         call_user_func($m['run'],$this->args,$this->ex);
  158.  
  159.                     }
  160.  
  161.                 }
  162.  
  163.             }
  164.  
  165.             $this->out("Connection Ended");
  166.  
  167.         }
  168.         function checkACTION(){
  169.             if(isset($this->ex[1])){
  170.  
  171.                 if($this->ex[1] == 'JOIN'){
  172.  
  173.                     $this->channels[$this->channel(substr($this->args['chan'],1))]->addUser($this->args['nick']);
  174.  
  175.                 }elseif($this->ex[1] == 'PART'){
  176.  
  177.                     $this->channels[$this->channel($this->args['chan'])]->removeUser($this->args['nick']);
  178.  
  179.                 }elseif($this->ex[1]=='KICK'){
  180.  
  181.                     $this->channels[$this->channel($this->args['chan'])]->removeUser($this->ex[3]);
  182.  
  183.                 }elseif($this->ex[1]=='QUIT'){
  184.  
  185.                     foreach($this->channels as $i=>$c){
  186.  
  187.                         $this->channels[$i]->removeUser($this->args['nick']);
  188.  
  189.                     }
  190.  
  191.                 }
  192.  
  193.             }
  194.         }
  195.         function checkALL(){
  196.             switch($this->args['cmd']){
  197.  
  198.                 case '!exit':
  199.  
  200.                     if($u=& user($this->args['nick'])&&$u['rank']>=6){
  201.  
  202.                         foreach($this->channels as $c){
  203.  
  204.                             $this->part($c->name);
  205.  
  206.                         }
  207.  
  208.                         $this->send('QUIT', '*NOMS*');
  209.  
  210.                         $this->active = false;
  211.  
  212.                         $this->socket = false;
  213.  
  214.                     }
  215.  
  216.                 break;
  217.  
  218.                 case '!debug':
  219.  
  220.                     if($u=&user($this->args['nick'])&&$u['rank']>=6){
  221.  
  222.                         print_r($this->ex);
  223.  
  224.                         $this->out('channel: '.$this->args['chan']);
  225.  
  226.                         $this->out('nick: '.$this->args['nick']);
  227.  
  228.                         if($this->args['ischan']){
  229.  
  230.                             $this->out('ischan: true');
  231.  
  232.                         }else{
  233.  
  234.                             $this->out('ischan: false');
  235.  
  236.                         }
  237.  
  238.                         print_r($this->channels);
  239.  
  240.                     }
  241.  
  242.                 break;
  243.  
  244.                 case '!join':
  245.  
  246.                     if($u=&user($this->args['nick'])&&$u['rank']>=3){
  247.  
  248.                         $this->join($this->ex[4]);
  249.  
  250.                     }
  251.  
  252.                 break;
  253.  
  254.                 case '!part':
  255.  
  256.                     if($u=&user($this->args['nick'])&&$u['rank']>=3){
  257.  
  258.                         $this->part($this->ex[4]);
  259.  
  260.                     }
  261.  
  262.                 break;
  263.  
  264.             }
  265.         }
  266.         function checkMSG(){
  267.             if(!$this->args['ischan']){
  268.  
  269.                 switch($this->args['cmd']){
  270.  
  271.                     case 'changepass':
  272.  
  273.                         changePassword($this->args['nick'],$this->ex[4],$this->ex[5]);
  274.  
  275.                     break;
  276.  
  277.                     case 'adduser':
  278.  
  279.                         if($u=&user($this->args['nick'])&&$u['rank']>=3){
  280.  
  281.                             createUser($this->ex[4],$this->ex[5]);
  282.  
  283.                         }
  284.  
  285.                     break;
  286.                     case 'annouce':
  287.                         if($u=&user($this->args['nick'])&&$u['rank']>=3){
  288.                             $msg = '';
  289.                             foreach($this->ex as $i => $v){
  290.                                 if($i>5){
  291.                                     $msg = $msg.' '.$v;
  292.                                 }elseif($i == 5){
  293.                                     $msg = $v;
  294.                                 }
  295.                             }
  296.                             $this->send('PRIVMSG',$this->ex[4].' :'.$msg);
  297.                         }
  298.                     break;
  299.  
  300.                     case 'listusers':
  301.  
  302.                         if($u=&user($this->args['nick'])&&$u['rank']>=4){
  303.  
  304.                             foreach($GLOBALS['USERS'] as $k=>$v){
  305.  
  306.                                 $this->reply($k.': '.$v['rank']);
  307.  
  308.                                 sleep(1);
  309.  
  310.                             }
  311.  
  312.                         }
  313.  
  314.                     break;
  315.  
  316.                     case 'changerank':
  317.  
  318.                         changeRank($this->args['nick'],$this->ex[4],$this->ex[5]);
  319.  
  320.                     break;
  321.  
  322.                 }
  323.  
  324.             }
  325.         }
  326.  
  327.         function join($chan){
  328.  
  329.             $this->send('JOIN',$chan);
  330.  
  331.             $this->channels[] =  new channel($chan);
  332.  
  333.             $matches = array();
  334.  
  335.             $data = '';
  336.  
  337.             do{
  338.  
  339.                 $data = trim(fgets($this->socket, 128));
  340.  
  341.                 preg_match_all('/:([\S]+) (353) [\S]+ (?:=|@) #([\S]+) :(.+)/',$data,$matches);
  342.  
  343.                 $this->out($data);
  344.  
  345.                 if(count($matches)>0&&isset($matches[4][0])&&$matches[4][0]!=''){
  346.  
  347.                     $t = explode(" ",$matches[4][0]);
  348.  
  349.                     foreach($t as $u){
  350.  
  351.                         $this->channels[count($this->channels)-1]->addUser($u);
  352.  
  353.                     }
  354.  
  355.                 }else{
  356.  
  357.                     $matches = array();
  358.  
  359.                 }
  360.  
  361.             }while(count($matches)<1);
  362.  
  363.         }
  364.  
  365.         public function reply($msg){
  366.  
  367.             $this->send('PRIVMSG',$this->args['chan'].' :'.$msg);
  368.  
  369.         }
  370.  
  371.         public function send($cmd, $msg = null){
  372.  
  373.             if($msg == null){
  374.  
  375.                 fputs($this->socket, $cmd."\r\n");
  376.  
  377.                 $this->out($cmd);
  378.  
  379.             }else{
  380.  
  381.                 fputs($this->socket, $cmd.' '.$msg."\r\n");
  382.  
  383.                 $this->out($cmd.' '.$msg);
  384.  
  385.             }
  386.  
  387.         }
  388.  
  389.         private function out($msg){
  390.  
  391.             out('['.$this->server.':'.$this->port.'] '.$msg);
  392.  
  393.         }
  394.  
  395.         function channel($chan){
  396.  
  397.             foreach($this->channels as $c=>$n){
  398.  
  399.                 if($chan==$n->name){
  400.  
  401.                     return $c;
  402.  
  403.                 }
  404.  
  405.             }
  406.  
  407.             return false;
  408.  
  409.         }
  410.  
  411.         function part($chan){
  412.  
  413.             foreach($this->channels as $k =>$c){
  414.  
  415.                 if($c->name==$chan){
  416.  
  417.                     $this->send('PART',$chan);
  418.  
  419.                     unset($this->channels[$k]);
  420.  
  421.                 }
  422.  
  423.             }
  424.  
  425.         }
  426.  
  427.         private $md5;
  428.  
  429.         public $socket;
  430.  
  431.         public $server;
  432.  
  433.         public $port;
  434.  
  435.         public $nick;
  436.  
  437.         public $active = true;
  438.  
  439.         public $channels;
  440.  
  441.         private $ex;
  442.  
  443.         private $args;
  444.  
  445.     }
  446.  
  447. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement