]> jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/commitdiff
Create NameBan class for adding QLines
authorValerie Pond <redacted>
Mon, 9 Jan 2023 03:30:54 +0000 (03:30 +0000)
committerValerie Pond <redacted>
Mon, 9 Jan 2023 03:30:54 +0000 (03:30 +0000)
lib/NameBan.php [new file with mode: 0644]

diff --git a/lib/NameBan.php b/lib/NameBan.php
new file mode 100644 (file)
index 0000000..7218a5d
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+
+namespace UnrealIRCd;
+
+use Exception;
+use stdClass;
+
+class NameBan
+{
+
+    public Connection $connection;
+
+    public function __construct(Connection $conn)
+    {
+        $this->connection = $conn;
+    }
+
+    /**
+     * Add a name ban (QLine).
+     *
+     * @param string  $name
+     * @param string $reason
+     * @param string $duration Optional
+     * @param string $set_by Optional
+     * @return stdClass|array|bool
+     * @throws Exception
+     */
+    public function add(string $name, string $reason, string $duration = NULL, $set_by = NULL): stdClass|array|bool
+    {
+        $query = [
+            'name' => $name,
+            'reason' => $reason,
+            'duration_string' => $duration ?? '0',
+        ];
+
+        if ($set_by)
+            $query['set_by'] = $set_by;
+
+        return $this->connection->query('name_ban.add', $query);
+    }
+
+    /**
+     * Delete a ban.
+     *
+     * @param  string  $name
+     * @return stdClass|array|bool
+     * @throws Exception
+     */
+    public function delete(string $name): stdClass|array|bool
+    {
+        return $this->connection->query('name_ban.del', [
+            'name' => $name,
+        ]);
+    }
+
+    /**
+     * Return a list of all bans.
+     *
+     * @return stdClass|array|bool
+     * @throws Exception
+     */
+    public function getAll(): stdClass|array|bool
+    {
+        $response = $this->connection->query('name_ban.list');
+
+        if (!is_bool($response)) {
+            return $response->list;
+        }
+
+        throw new Exception('Invalid JSON Response from UnrealIRCd RPC.');
+    }
+
+    /**
+     * Get a specific ban.
+     *
+     * @param string $name
+     * @return stdClass|array|bool
+     * @throws Exception
+     */
+    public function get(string $name): stdClass|array|bool
+    {
+        $response = $this->connection->query('name_ban.get', [
+            'name' => $name,
+        ]);
+
+        if (!is_bool($response)) {
+            return $response;
+        }
+
+        throw new Exception('Invalid JSON Response from UnrealIRCd RPC.');
+    }
+}