<?

$settings['ext']['http_login']['#gamestv.org'] = array("url" => "http://gamestv.org/test.php",
                                                       "chans" => array("#gamestv.org", "#gtv.prv", "#gtv.sa"));

newtrigger('join','true','http_login::check($nick,$chan,false);',0);
newtrigger('say' ,'$first == "\login"' ,'http_login::check($nick,$replyto,true);',0);

class http_login {
   function check($nick, $replyto, $all) {
      global $settings, $session;
      if(!$all && isset($session['users'][$nick]->modules['http_user'][$replyto])) {
         http_login::give_perms($nick, true);
      }

      if($all || isset($settings['ext']['http_login'][$replyto])) {
         if($all && time() - $session['users'][$nick]->modules['http_user']['lasttry'] < 120) {
            notice($nick, "To avoid \login spam, I will not recheck your login for another ".(120 - (time() - $session['users'][$nick]->modules['http_user']['lasttry']))."sec");
         }
         else {
            $auth = $session['users'][$nick]->getauth();
            if($auth) {
               if($all) {
                  $chans = array_keys($settings['ext']['http_login']);
                  $session['users'][$nick]->modules['http_user']['lasttry'] = time();
               }
               else {
                  $chans = array($replyto);
               }
               foreach($chans AS $chan) {
                  if(time() - $session['users'][$nick]->modules['http_user'][$chan]['lastok'] > 3600) {
                     if(time() - $session['users'][$nick]->modules['http_user'][$chan]['lasttry'] > 120) {
                        $session['users'][$nick]->modules['http_user'][$chan]['lasttry'] = time();
                        new http_login_worker($nick, $chan, $replyto, $all);
                     }
                  }
               }
               if($all) {
                  http_login::give_perms($nick, false);
               }
            }
            else {
               if($all) {
                  notice($this->nick, "Sorry $nick to use the \login function you have to be authed with Q");
               }
            }
         }
      }
   }

   function give_perms($nick, $silent) {
      global $session;
      foreach($session['users'][$nick]->modules['http_user'] AS $chan => $perm) {
         if(isset($perm['name']) && !$silent) {
            notice($nick, "Welcome to $chan ".$perm['name']." ".$perm['name']);
         }
         foreach($perm['perms'] AS $type => $flags) {
            if($type == "o") {
               if(isset($session['chans'][$chan]->nicks[$nick])) {
                  if(!isset($session['chans'][$chan]->nicks[$nick]->modes['o'])) {
                     op($chan, $nick);
                  }
               }
            }
            elseif($type == "v") {
               if(isset($session['chans'][$chan]->nicks[$nick])) {
                  if(!isset($session['chans'][$chan]->nicks[$nick]->modes['v'])) {
                     voice($chan, $nick);
                  }
               }
            }
            elseif($type == "i") {
               if(!isset($session['chans'][$chan]->nicks[$nick])) {
                  invite($nick, $chan);
               }
            }
         }
      }
   }
}

class http_login_worker {
   function __construct($nick, $chan, $replyto, $all) {
       $this->nick    = $nick;
       $this->chan    = $chan;
       $this->replyto = $replyto;
       $this->all     = $all;
       $this->request();
   }
   function request() {
      global $session, $settings;
      $auth = $session['users'][$this->nick]->getauth();
      $url  = $settings['ext']['http_login'][$this->chan]['url'];
      debug("Sending ip query for $this->nick in $this->chan to $url: $auth");
      new http_conn($url . urlencode($auth), '$me->parse($data);', $this);
   }

   function parse($data) {
      global $session;
      @list(,$html) = explode("\n\r",$data[0]);
      $userinfo = unserialize(trim($html));
      if(isset($userinfo['ok'])) {
         debug("Recieved http userinfo for $this->nick for $this->chan");
         $session['users'][$this->nick]->modules['http_user'][$this->chan]['lastok'] = time();
         $session['users'][$this->nick]->modules['http_user'][$this->chan]['name'] = $userinfo['name'];
         $session['users'][$this->nick]->modules['http_user'][$this->chan]['misc'] = $userinfo['misc'];
         foreach($userinfo['chans'] AS $chan => $perm) {
            if(in_array($chan, $settings['ext']['http_login'][$this->chan]['chans'])) {
               $session['users'][$this->nick]->modules['http_user'][$chan]['perms'] = $perm;
            }
            else {
               debug("Attention, $this->chan is sending user info for $chan - no permission");
            }
         }
      }

      if($this->all) {
         $silent = false;
      }
      else {
         $silent = true;
      }
      http_login::give_perms($this->nick, $silent);
   }
}