From 6cd66f219f6024ce6400f25b7b0165d37449e49f Mon Sep 17 00:00:00 2001 From: pk910 Date: Wed, 25 Jan 2012 16:08:56 +0100 Subject: [PATCH] added error message for missing modules (send_raw, blockusers) and znc_block, znc_unblock command --- INSTALL | 4 ++ zncadmin.php | 102 +++++++++++++++++++++++++++++++++-- zncadmin/ZNCServer.class.php | 40 +++++++++++--- 3 files changed, 136 insertions(+), 10 deletions(-) diff --git a/INSTALL b/INSTALL index 88e301c..4a04cb6 100644 --- 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 diff --git a/zncadmin.php b/zncadmin.php index 4fffe0e..7893792 100644 --- a/zncadmin.php +++ b/zncadmin.php @@ -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"; + } } } diff --git a/zncadmin/ZNCServer.class.php b/zncadmin/ZNCServer.class.php index 35724a1..d5d4bad 100644 --- a/zncadmin/ZNCServer.class.php +++ b/zncadmin/ZNCServer.class.php @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +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("]*)>", $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) { -- 2.20.1