]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - Classes/class-rpc.php
Merge branch 'main' of https://github.com/ValwareIRC/unrealircd-webpanel
[irc/unrealircd/unrealircd-webpanel.git] / Classes / class-rpc.php
index 76bdb653b9985b74a914baacaec32767e0f18f67..7e2b80dd0582f4a98c216a8aa751e0de3940ce65 100644 (file)
@@ -1,28 +1,14 @@
 <?php
+/**
+ * RPC Functionality for UnrealIRCd Admin Webpanel
+ * License: GPLv3 or later
+ * Author: ValwareIRC
+ * GitHub URI: ValwareIRC/unrealircd-webpanel
+ * 2023
+ */
 
 if (!defined('UPATH'))
-    die("Access denied");
-
-
-/** The RPC User name as defined in your unrealircd.conf */
-define( 'UNREALIRCD_RPC_USER', 'apiuser' );
-
-/** The RPC User password as defined in your unrealircd.conf */
-define( 'UNREALIRCD_RPC_PASSWORD', 'securepassword' );
-
-/** The host of the RPC server */
-define( 'UNREALIRCD_HOST', '127.0.0.1' );
-
-/** The port of your RPC server as defined in your unrealircd.conf */
-define( 'UNREALIRCD_PORT', '8000' );
-
-/* You should only really uncomment this if you are running on 
- * localhost and for some reason don't have a fully valid cert
-*/
-define( 'UNREALIRCD_SSL_VERIFY', false );
-
-/* You should only really need this if you're developing something. */
-define( 'UNREALIRCD_DEBUG', true );
+       die("Access denied");
 
 class RPC
 {
@@ -30,7 +16,7 @@ class RPC
        public $errcount = 0; // more of a bool check
        public $content = [];
        public $body = [];
-    public $result = NULL;
+       public $result = NULL;
        function __construct()
        {
                if (!defined('UNREALIRCD_RPC_USER') ||
@@ -70,18 +56,20 @@ class RPC
         */
        function set_method(String $method) : void
        {
+               do_log("Set method:", $method);
                $this->body['method'] = $method;
        }
 
        function set_params(array $params) : void
        {
-               array_merge($this->body['params'], $params);
+               do_log("Set params:", $params);
+               $this->body['params'] = $params;
        }
 
        function execute()
        {
                $this->content['body'] = json_encode($this->body);
-        if (!$this->content['body'])
+               if (!$this->content['body'])
                        return;
                $url = "https://".UNREALIRCD_HOST.":".UNREALIRCD_PORT."/api";
                $curl = curl_init($url);
@@ -92,10 +80,13 @@ class RPC
                $headers = array(
                        "Accept: application/json",
                        "Content-Type: application/json",
-            "Authorization: Basic ". base64_encode(UNREALIRCD_RPC_USER.":".UNREALIRCD_RPC_PASSWORD),
+                       "Authorization: Basic ". base64_encode(UNREALIRCD_RPC_USER.":".UNREALIRCD_RPC_PASSWORD),
                );
                curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 
+               if (UNREALIRCD_DEBUG)
+                       do_log("SENDING JSON:", $this->content['body']);
+
                curl_setopt($curl, CURLOPT_POSTFIELDS, $this->content['body']);
 
                //for debug only!
@@ -110,6 +101,7 @@ class RPC
 
        function fetch_assoc()
        {
+               do_log("RPC::fetch_assoc()", $this->result);
                return json_decode($this->result, true);
        }
 
@@ -122,10 +114,10 @@ class RPC
 
 class RPC_List
 {
-    static $user = [];
-    static $channel = [];
-    static $tkl = [];
-    static $spamfilter = [];
+       static $user = [];
+       static $channel = [];
+       static $tkl = [];
+       static $spamfilter = [];
 
        static $opercount = 0;
        static $services_count = 0;
@@ -151,6 +143,7 @@ function rpc_pop_lists()
                if ($key == "result")
                        foreach($value['list'] as $r)
                        {
+                               
                                RPC_List::$user[] = $r;
                                if (strpos($r['user']['modes'],"o") !== false && strpos($r['user']['modes'],"S") == false)
                                        RPC_List::$opercount++;
@@ -215,4 +208,77 @@ function rpc_pop_lists()
                                RPC_List::$spamfilter[] = $r;
        }
 
+}
+
+
+/** RPC TKL Add */
+function rpc_tkl_add($name, $type, $expiry, $reason) : bool
+{
+       $rpc = new RPC();
+       $rpc->set_method("server_ban.add");
+       $rpc->set_params(["name" => $name, "type" => $type, "reason" => $reason, "duration_string" => $expiry]);
+       $rpc->execute();
+       $result = $rpc->fetch_assoc();
+       if (isset($result['error']))
+       {
+               $msg = "The $type could not be added: $name - ".$result['error']['message'] . " (" . $result['error']['code'] . ")";
+               Message::Fail($msg);
+               return false;
+       }
+       return true;
+}
+
+
+/** RPC TKL Delete */
+function rpc_tkl_del($name, $type) : bool
+{
+       $rpc = new RPC();
+       $rpc->set_method("server_ban.del");
+       $rpc->set_params(["name" => $name, "type" => $type]);
+       $rpc->execute();
+       $result = $rpc->fetch_assoc();
+       if (isset($result['error']))
+       {
+               $msg = "The $type could not be deleted: $name - ".$result['error']['message'] . " (" . $result['error']['code'] . ")";
+               Message::Fail($msg);
+               return false;
+       }
+       return true;
+}
+
+/** RPC Spamfilter Delete
+ * 
+ */
+function rpc_sf_del($name, $mtype, $targets, $action) : bool
+{
+       $rpc = new RPC();
+       $rpc->set_method("spamfilter.del");
+       $rpc->set_params(["name" => $name, "match_type" => $mtype, "spamfilter_targets" => $targets, "ban_action" => $action, "set_by" => "YoMama"]);
+       $rpc->execute();
+       $result = $rpc->fetch_assoc();
+       if (isset($result['error']))
+       {
+               $msg = "The spamfilter entry could not be deleted: $name - ".$result['error']['message'] . " (" . $result['error']['code'] . ")";
+               Message::Fail($msg);
+               return false;
+       }
+       else
+       {
+               $r = $result['result']['tkl']; 
+               Message::Success("Deleted spamfilter entry: ".$r['name']." [type: ".$r['match_type']."] [targets: ".$r['spamfilter_targets']. "] [action: ".$r['ban_action']."] [reason: ".$r['reason']."] [set by: ".$r['set_by']."]");
+       }
+       return true;
+}
+
+/** Convert the duration_string */
+function rpc_convert_duration_string($str)
+{
+       $tok = explode("w", $str);
+       $weeks = $tok[0];
+       $tok = explode("d", $tok[1]);
+       $days = $tok[0];
+       $tok = explode("h", $tok[1]);
+       $hours = $tok[0];
+       return "$weeks weeks, $days days and $hours hours";
+       
 }
\ No newline at end of file