added error message for missing modules (send_raw, blockusers) and znc_block, znc_unb...
authorpk910 <philipp@zoelle1.de>
Wed, 25 Jan 2012 15:08:56 +0000 (16:08 +0100)
committerpk910 <philipp@zoelle1.de>
Wed, 25 Jan 2012 15:08:56 +0000 (16:08 +0100)
INSTALL
zncadmin.php
zncadmin/ZNCServer.class.php

diff --git a/INSTALL b/INSTALL
index 88e301c659fbe24aa33345f7c9bd10cc4023166a..4a04cb6a69baded8cd2802d7d22026bdd601c1fb 100644 (file)
--- a/INSTALL
+++ b/INSTALL
@@ -30,6 +30,8 @@ ZNCAdmin is made to be used as external scripts from NeonServV5.
        +bind znc_whois NeonServ.extscript php scripts/zncadmin.php search $1 $2- %1+
        +bind znc_seen NeonServ.extscript php scripts/zncadmin.php seen $1 $2 %1+
        +bind znc_simul NeonServ.extscript php scripts/zncadmin.php simul $1 $2 $3- %1+
+       +bind znc_block NeonServ.extscript php scripts/zncadmin.php block $1 $2 %1+
+       +bind znc_unblock NeonServ.extscript php scripts/zncadmin.php unblock $1 $2 %1+
        +bind znc_stats NeonServ.extscript php scripts/zncadmin.php stats $1 %1+
 
      You may also bind the "admin commands" (override the protected option)
@@ -37,6 +39,8 @@ ZNCAdmin is made to be used as external scripts from NeonServV5.
        +bind znc_admin_del NeonServ.extscript php scripts/zncadmin.php force del $1 $2 %1+
        +bind znc_admin_resetpass NeonServ.extscript php scripts/zncadmin.php force resetpass $1 $2 %1+
        +bind znc_admin_simul NeonServ.extscript php scripts/zncadmin.php force simul $1 $2 $3- %1+
+       +bind znc_admin_block NeonServ.extscript php scripts/zncadmin.php force block $1 $2 %1+
+       +bind znc_admin_unblock NeonServ.extscript php scripts/zncadmin.php force unblock $1 $2 %1+
 
   4) DO NOT FORGET TO SET THE PERMISSIONS!
      External Scripts do not have own permission requirements. You need to set them manually using +modcmd
index 4fffe0e22ca993503b5e095432da45b9e7819ea4..78937925953b5c40ba70e4cfc35f10bebecceef9 100644 (file)
@@ -56,6 +56,12 @@ switch(strtolower($argv[1])) {
     case "simul":
         zncadmin_simul();
         break;
+    case "block":
+        zncadmin_block();
+        break;
+    case "unblock":
+        zncadmin_unblock();
+        break;
     case "stats":
         zncadmin_stats();
         break;
@@ -453,9 +459,99 @@ function zncadmin_simul() {
         error("Access denied\n");
     } else {
         $deluser = $delusers[0];
-        $deluser['server']['conn']->simulZNC($deluser['user']['user'], $raw);
-        echo "Simuled \002".$deluser['user']['user']."\002 on Server ".$deluser['server']['name'].": ".$raw."\n";
-        echo"/log\n";
+        $ret = $deluser['server']['conn']->simulZNC($deluser['user']['user'], $raw);
+        if($ret == ERR_MODULE_NOT_FOUND) {
+            error("send_raw module is not installed or not activated for user ".$deluser['server']['auser']." on ".$deluser['server']['name']);
+        } else {
+            echo "Simuled \002".$deluser['user']['user']."\002 on Server ".$deluser['server']['name'].": ".$raw."\n";
+            echo"/log\n";
+        }
+    }
+}
+
+//SUBCOMMAND: block
+function zncadmin_block() {
+    global $argv, $zncservers, $force;
+    $username = strtolower($argv[2]);
+    $server = strtolower($argv[3]);
+    if(!$username) {
+        error("missing username");
+        return;
+    }
+    $delusers = 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']);
+        $zncserver['conn']->login($zncserver['auser'], $zncserver['apass']);
+        foreach($zncserver['conn']->getUserList() as $user) {
+            if(strtolower($user['user']) == $username) {
+                $delusers[] = array("server" => $zncserver, "user" => $user);
+            }
+        }
+    }
+    if(count($delusers) > 1) {
+        error($argv[2]." exists on multiple servers! please add the server name the user should be blocked on.");
+        echo"Found User on following Servers:\n";
+        foreach($delusers as $server) {
+            echo "\002".$server['server']['name']."\002  Server: ".$server['user']['server']."  Nick: ".$server['user']['nick']."  Clients: ".$server['user']['clients']."\n";
+        }
+    } else if(count($delusers) == 0) {
+        error("Couldn't find an user called \002".$argv[2]."\002.");
+    } else if($delusers[0]['server']['protected'] && !$force) {
+        error("Access denied\n");
+    } else {
+        $deluser = $delusers[0];
+        $ret = $deluser['server']['conn']->blockZNC($deluser['user']['user'], true);
+        if($ret == ERR_MODULE_NOT_FOUND) {
+            error("blockuser module is not installed or not activated on ".$deluser['server']['name']);
+        } else {
+            echo "Blocked \002".$deluser['user']['user']."\002 on Server ".$deluser['server']['name'].".\n";
+            echo"/log\n";
+        }
+    }
+}
+
+//SUBCOMMAND: unblock
+function zncadmin_unblock() {
+    global $argv, $zncservers, $force;
+    $username = strtolower($argv[2]);
+    $server = strtolower($argv[3]);
+    if(!$username) {
+        error("missing username");
+        return;
+    }
+    $delusers = 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']);
+        $zncserver['conn']->login($zncserver['auser'], $zncserver['apass']);
+        foreach($zncserver['conn']->getUserList() as $user) {
+            if(strtolower($user['user']) == $username) {
+                $delusers[] = array("server" => $zncserver, "user" => $user);
+            }
+        }
+    }
+    if(count($delusers) > 1) {
+        error($argv[2]." exists on multiple servers! please add the server name the user should be unblocked on.");
+        echo"Found User on following Servers:\n";
+        foreach($delusers as $server) {
+            echo "\002".$server['server']['name']."\002  Server: ".$server['user']['server']."  Nick: ".$server['user']['nick']."  Clients: ".$server['user']['clients']."\n";
+        }
+    } else if(count($delusers) == 0) {
+        error("Couldn't find an user called \002".$argv[2]."\002.");
+    } else if($delusers[0]['server']['protected'] && !$force) {
+        error("Access denied\n");
+    } else {
+        $deluser = $delusers[0];
+        $ret = $deluser['server']['conn']->blockZNC($deluser['user']['user'], false);
+        if($ret == ERR_MODULE_NOT_FOUND) {
+            error("blockuser module is not installed or not activated on ".$deluser['server']['name']);
+        } else {
+            echo "Unblocked \002".$deluser['user']['user']."\002 on Server ".$deluser['server']['name'].".\n";
+            echo"/log\n";
+        }
     }
 }
 
index 35724a1c1994628cfa283a2a8c4b84872a71ce1c..d5d4badbc6ff85050cb1eaf12abfebaaada96d7d 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
  */
 
+define("ERR_OK", 0);
+define("ERR_UNKNOWN", 1);
+define("ERR_MODULE_NOT_FOUND", 2);
+define("ERR_USER_NOT_FOUND", 3);
+
 class ZNCServer {
        private $host, $port;
        private $connector;
@@ -135,11 +140,7 @@ class ZNCServer {
         $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/deluser", $post);
     }
     
-    public function editZNC($username, $password = NULL, $new_servers = NULL, $add_modules = NULL, $del_modules = NULL, $others = NULL) {
-        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username);
-        if(preg_match("/No such username/i", $html)) return false;
-        $post = array();
-        $post['loadmod'] = array();
+    private function parseHTMLFields($html, &$post) {
         preg_match_all("<input ([^\>]*)>", $html, $matches);
         foreach($matches[0] as $input) {
             $fields = array(0 => "");
@@ -208,6 +209,14 @@ class ZNCServer {
             $content = $content[0];
             $post[$name] = $content;
         }
+    }
+    
+    public function editZNC($username, $password = NULL, $new_servers = NULL, $add_modules = NULL, $del_modules = NULL, $others = NULL) {
+        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username);
+        if(preg_match("/No such username/i", $html)) return false;
+        $post = array();
+        $post['loadmod'] = array();
+        $this->parseHTMLFields($html, $post);
         if($password) {
             $post['password'] = $password;
             $post['password2'] = $password;
@@ -233,9 +242,25 @@ class ZNCServer {
         return !preg_match("/Invalid Submission/i", $html);
     }
     
-    public function simulZNC($username, $raw, $server = true) {
+    public function blockZNC($username, $block) {
+        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username);
+        if(preg_match("/No such username/i", $html)) return ERR_USER_NOT_FOUND;
+        $post = array();
+        $post['loadmod'] = array();
+        $this->parseHTMLFields($html, $post);
+        if(!$post['embed_blockuser_presented']) return ERR_MODULE_NOT_FOUND;
+        if(!($post['embed_blockuser_block'] xor $block)) return ERR_OK;
+        $post['embed_blockuser_block'] = ($block ? "1" : null);
+        if(!$block) {
+            $post['doconnect'] = 1;
+        }
+        $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username, $post);
+        return (preg_match("/Invalid Submission/i", $html) ? ERR_UNKNOWN : ERR_OK);
+    }
+    
+    public function simulZNC($username, $raw, &$errmsg, $server = true) {
         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/send_raw/");
-        if(preg_match("/Not Found/i", $html)) return false;
+        if(preg_match("/Not Found/i", $html)) return ERR_MODULE_NOT_FOUND;
                $exp = explode('name="_CSRF_Check" value="', $html);
         $exp = explode('"', $exp[1]);
         $csrf = $exp[0];
@@ -246,6 +271,7 @@ class ZNCServer {
         $post['send_to'] = ($server ? "server" : "client");
         $post['line'] = $raw;
         $this->connector->post("http://".$this->host.":".$this->port."/mods/send_raw/", $post);
+        return ERR_OK;
     }
     
     public function addChan($username, $channel, $key = NULL, $buffer = NULL) {