added whois command
authorpk910 <philipp@zoelle1.de>
Sun, 26 Apr 2015 18:19:46 +0000 (20:19 +0200)
committerpk910 <philipp@zoelle1.de>
Sun, 26 Apr 2015 18:19:46 +0000 (20:19 +0200)
config.example.php
zncadmin.php
zncadmin/ZNCServerV1.class.php

index a687984d376d0556d2f209497fa9a0f6af37e2a7..207e720c75fa214d7cb88269febd5aed0324f124 100644 (file)
@@ -76,7 +76,7 @@ $add_settings = array(
        "netmodules" => array(
                "autocycle" => "",
                "awaynick" => "%nick%|ZNC",
-               "simple_away" => ""
+               "simple_away" => "",
                "kickrejoin" => "",
                "keepnick" => "",
        ),
index 4552025d8a08ef3a68c6bcf0d2b268fa58dadd7a..f5397ffb62838e5cfa46eaf3d3f2795cf0b45ea4 100644 (file)
@@ -47,6 +47,9 @@ switch(strtolower($argv[1])) {
     case "search":
         zncadmin_search();
         break;
+       case "whois":
+               zncadmin_whois();
+               break;
        case "seen":
         zncadmin_seen();
         break;
@@ -230,7 +233,7 @@ function zncadmin_search() {
     }
 }
 
-//SUBCOMMAND: search
+//SUBCOMMAND: seen
 function zncadmin_seen() {
     global $argv, $zncservers;
        $time = $argv[2];
@@ -482,6 +485,114 @@ function zncadmin_simul() {
     }
 }
 
+//SUBCOMMAND: whois
+function zncadmin_whois() {
+    global $argv, $zncservers, $force;
+    $username = strtolower($argv[2]);
+    $server = strtolower($argv[3]);
+    if(!$username) {
+        error("missing username");
+        return;
+    }
+    if($server) {
+        $found = false;
+        foreach($zncservers as $zncserver) {
+            if(strtolower($server) == strtolower($zncserver['name'])) {
+                $found = true;
+                break;
+            }
+        }
+        if(!$found) {
+            $server = null;
+        }
+    }
+    $foundusers = array();
+    foreach($zncservers as $zncserver) {
+        if($server && (strtolower($server) != strtolower($zncserver['name']))) continue;
+        if($username == strtolower($zncserver['auser'])) continue;
+        $zncserver['conn'] = new ZNCServer($zncserver['host'], $zncserver['port'], (isset($zncserver['version']) ? $zncserver['version'] : NULL));
+        $zncserver['conn']->login($zncserver['auser'], $zncserver['apass']);
+        foreach($zncserver['conn']->getUserList() as $user) {
+            if(strtolower($user['user']) == $username) {
+                $foundusers[] = array("server" => $zncserver, "user" => $user);
+            }
+        }
+    }
+    if(count($foundusers) > 1) {
+        error($argv[2]." exists on multiple servers! please add the server name the user is on.");
+        echo"Found User on following Servers:\n";
+        foreach($foundusers as $server) {
+            echo "\002".$server['server']['name']."\002  Server: ".$server['user']['server']."  Nick: ".$server['user']['nick']."  Clients: ".$server['user']['clients']."\n";
+        }
+    } else if(count($foundusers) == 0) {
+        error("Couldn't find an user called \002".$argv[2]."\002.");
+    } else if($foundusers[0]['server']['protected'] && !$force) {
+        error("Access denied\n");
+    } else {
+        $founduser = $foundusers[0];
+        $info = $founduser['server']['conn']->getUserInfo($founduser['user']['user'], true);
+        if(!$info) {
+            error("failed getting infos for user ".$founduser['user']['user']." on ".$founduser['server']['name']);
+        } else {
+                       
+                       echo "User: ".$founduser['user']['user']." (".$founduser['server']['name'].") || Conected to IRC: ".$founduser['user']['server']." || Conected to ZNC: ".$founduser['user']['clients']."\n";
+                       
+                       $modulesstr = "User Modules: ";
+                       foreach($info['loadmod'] as $module) {
+                               if(strlen($module) == 0) continue;
+                               if($modulesstr == "")
+                                       $modulesstr = "  ";
+                               $modulesstr .= $module." ";
+                               if(strlen($modulesstr) >= 170) {
+                                       echo $modulesstr."\n";
+                                       $modulesstr = "";
+                               }
+                       }
+                       if($modulesstr != "")
+                               echo $modulesstr."\n";
+                       
+                       foreach($info['networks'] as $network) {
+                               echo "Network \002".$network['name']."\002\n";
+                               echo "  Nickname: ".$network['nick']."\n";
+                               $servers = str_replace(array("\r"), array(""), $network['settings']['servers']);
+                               echo "  Server:\n";
+                               foreach(explode("\n", $servers) as $server) {
+                                       if(strlen($server) == 0) continue;
+                                       echo "    ".$server."\n";
+                               }
+                               $channelstr = "  Channel: ";
+                               foreach($network['settings']['channel'] as $chan) {
+                                       if(strlen($chan) == 0) continue;
+                                       if($channelstr == "")
+                                               $channelstr = "    ";
+                                       $channelstr .= $chan." ";
+                                       if(strlen($channelstr) >= 170) {
+                                               echo $channelstr."\n";
+                                               $channelstr = "";
+                                       }
+                               }
+                               if($channelstr != "")
+                                       echo $channelstr."\n";
+                               
+                               $modulesstr = "  Modules: ";
+                               foreach($network['settings']['loadmod'] as $module) {
+                                       if(strlen($module) == 0) continue;
+                                       if($modulesstr == "")
+                                               $modulesstr = "    ";
+                                       $modulesstr .= $module." ";
+                                       if(strlen($modulesstr) >= 170) {
+                                               echo $modulesstr."\n";
+                                               $modulesstr = "";
+                                       }
+                               }
+                               if($modulesstr != "")
+                                       echo $modulesstr."\n";
+                       }
+            
+        }
+    }
+}
+
 //SUBCOMMAND: block
 function zncadmin_block() {
     global $argv, $zncservers, $force;
index 674d612cfa091988d5f30a7cd21f6284b96a7bbd..a40f0c6658125d5424139f5b2766e35c6cb2cb44 100644 (file)
@@ -183,52 +183,34 @@ class ZNCServerV1 {
     }
 
     private function parseHTMLFields($html, &$post) {
-        preg_match_all("<input ([^\>]*)>", $html, $matches);
-        foreach($matches[0] as $input) {
-            $fields = array(0 => "");
-            $esc = false; $str = false;
-            $fieldid = 0;
-            for($i = 0; $i < strlen($input); $i++) {
-                if($esc) {
-                    $esc = false;
-                    $fields[$fieldid] .= $input[$i];
-                    continue;
-                }
-                if($input[$i] == "\\") {
-                    $esc = true;
-                }
-                if($input[$i] == "\"") {
-                    $str = !$str;
-                    continue;
-                }
-                if($input[$i] == " " && !$str) {
-                    $fields[++$fieldid] = "";
-                    continue;
-                }
-                $fields[$fieldid] .= $input[$i];
-            }
-            $cfields = array();
-            foreach($fields as $field) {
-                $fieldexp = explode("=", $field, 2);
-                if(count($fieldexp) != 2) continue;
-                $cfields[$fieldexp[0]] = $fieldexp[1];
-            }
-            $name = $cfields['name'];
-            switch($cfields['type']) {
+        preg_match_all("#<input (.*?)/?>#", $html, $matches);
+        foreach($matches[1] as $input) {
+            if(preg_match("#name=\"(.*?)\"#", $input, $match))
+                               $name = $match[1];
+                       else
+                               $name = null;
+                       if(preg_match("#type=\"(.*?)\"#", $input, $match))
+                               $type = $match[1];
+                       else
+                               $type = null;
+                       if(preg_match("#value=\"(.*?)\"#", $input, $match))
+                               $value = $match[1];
+                       else
+                               $value= null;
+            switch($type) {
                 case "text":
                 case "hidden":
-                    $value = $cfields['value'];
                     break;
                 case "checkbox":
-                    if($cfields['checked']) {
-                        $value = ($cfields['value'] ? $cfields['value'] : "1");
+                    if(preg_match("# checked=#", $input, $match)) {
+                        $value = ($value ? $value : "1");
                     } else
                         $value = null;
                     break;
                 default:
                     $value = null;
             }
-            if($name && $value) {
+            if($name && $value !== null) {
                 if(array_key_exists($name, $post)) {
                     if(!is_array($post[$name])) {
                         $cvalue = $post[$name];
@@ -336,9 +318,6 @@ class ZNCServerV1 {
 
     public function blockZNC($username, $block) {
         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
-        $handle = fopen('/home/srvx/neonserv/log.txt', 'a');
-        fwrite($handle, "$html\r\n");
-        fclose($handle);
         if(preg_match("/No such username/i", $html)) return ERR_USER_NOT_FOUND;
         $post = array();
         $post['user'] = $username;
@@ -399,6 +378,45 @@ class ZNCServerV1 {
         $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username.'&network=default', $post);
     }
        
+       public function getUserInfo($username, $loadNetInfos = false) {
+               $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
+        if(preg_match("/No such username/i", $html)) return false;
+        $info = array();
+        $info['loadmod'] = array();
+        $this->parseHTMLFields($html, $info);
+               
+               { // parse network list
+                       $info['networks'] = array();
+                       $netlist = explode("<h3>Networks</h3>", $html);
+                       $netlist = explode("<tbody>", $netlist[1], 2);
+                       $netlist = explode("</tbody>", $netlist[1], 2);
+                       $netlist = explode("</tr>", $netlist[0]);
+                       foreach($netlist as $net) {
+                               if(preg_match_all("#<td>(.*?)</td>#sm", $net, $matches)) {
+                                       $network = array();
+                                       $network['name'] = $matches[1][1];
+                                       $network['clients'] = $matches[1][2];
+                                       $network['server'] = $matches[1][3];
+                                       $network['nick'] = $matches[1][4];
+                                       
+                                       if($loadNetInfos) {
+                                               $html2 = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/editnetwork?user=".$username."&network=".$network['name']);
+                                               if(!preg_match("/No such username/i", $html2)) {
+                                                       $settings = array();
+                                                       $settings['loadmod'] = array();
+                                                       $this->parseHTMLFields($html2, $settings);
+                                                       $network['settings'] = $settings;
+                                               }
+                                       }
+                                       
+                                       $info['networks'][] = $network;
+                               }
+                       }
+               }
+               
+               return $info;
+       }
+       
 }
 
 ?>
\ No newline at end of file