]> jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/commitdiff
Add $rpc->rpc()->add_timer(); and ->del_timer()
authorBram Matthys <redacted>
Wed, 12 Apr 2023 08:48:27 +0000 (10:48 +0200)
committerBram Matthys <redacted>
Wed, 12 Apr 2023 08:48:27 +0000 (10:48 +0200)
Add a timer. Requires UnrealIRCd 6.1.0+
@param timer_id         Name of the timer (so you can .del_timer later)
@param every_msec       Every -this- milliseconds the command must be executed
@param method           The JSON-RPC method to execute (lowlevel name, eg "stats.get")
@param params           Parameters to the JSON-RPC call that will be executed, can be NULL
@param id               Set JSON-RPC id to be used in the timer, leave NULL for auto id.
@return stdClass|array|bool

Example usage would be:
$rpc->rpc()->add_timer("timer", 1000, "stats.get");

lib/Rpc.php

index 721ba4568e577614e0287ce385f9b68787ea3a30..81e3e67a801101f094211900251e86451836971f 100644 (file)
@@ -36,4 +36,44 @@ class Rpc
             'name' => $name,
         ]);
     }
+
+    /**
+     * Add a timer. Requires UnrealIRCd 6.1.0+
+     * @param timer_id         Name of the timer (so you can .del_timer later)
+     * @param every_msec       Every -this- milliseconds the command must be executed
+     * @param method           The JSON-RPC method to execute (lowlevel name, eg "stats.get")
+     * @param params           Parameters to the JSON-RPC call that will be executed, can be NULL
+     * @param id               Set JSON-RPC id to be used in the timer, leave NULL for auto id.
+     * @return stdClass|array|bool
+     */
+    public function add_timer(string $timer_id, int $every_msec, string $method, array|null $params = null, $id = null): stdClass|array|bool
+    {
+        if ($id === null)
+            $id = random_int(100000, 999999); /* above the regular query() ids */
+
+        $request = [
+            "jsonrpc" => "2.0",
+            "method" => $method,
+            "params" => $params,
+            "id" => $id
+        ];
+
+        return $this->connection->query('rpc.add_timer', [
+            'timer_id' => $timer_id,
+            'every_msec' => $every_msec,
+            'request' => $request,
+        ]);
+    }
+
+    /**
+     * Delete a timer. Requires UnrealIRCd 6.1.0+
+     * @param timer_id         Name of the timer that was added through del_timer earlier.
+     * @return stdClass|array|bool
+     */
+    public function del_timer(string $timer_id)
+    {
+        return $this->connection->query('rpc.del_timer', [
+            'timer_id' => $timer_id,
+        ]);
+    }
 }