]> jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/blame - lib/NameBan.php
Add nameban to Connection.php and various bugfixes
[irc/unrealircd/unrealircd-rpc-php.git] / lib / NameBan.php
CommitLineData
da11ff54
VP
1<?php
2
3namespace UnrealIRCd;
4
5use Exception;
6use stdClass;
7
8class NameBan
9{
10
11 public Connection $connection;
12
13 public function __construct(Connection $conn)
14 {
15 $this->connection = $conn;
16 }
17
18 /**
19 * Add a name ban (QLine).
20 *
21 * @param string $name
22 * @param string $reason
23 * @param string $duration Optional
24 * @param string $set_by Optional
25 * @return stdClass|array|bool
26 * @throws Exception
27 */
28 public function add(string $name, string $reason, string $duration = NULL, $set_by = NULL): stdClass|array|bool
29 {
30 $query = [
31 'name' => $name,
32 'reason' => $reason,
33 'duration_string' => $duration ?? '0',
34 ];
35
36 if ($set_by)
37 $query['set_by'] = $set_by;
38
13ae2ba6 39 $response = $this->connection->query('name_ban.add', $query);
8fd8402e
VP
40
41 if (is_bool($response))
42 return false;
43
13ae2ba6
BM
44 if (property_exists($response, 'tkl'))
45 return $response->tkl;
46 return FALSE;
da11ff54
VP
47 }
48
49 /**
50 * Delete a ban.
51 *
52 * @param string $name
53 * @return stdClass|array|bool
54 * @throws Exception
55 */
56 public function delete(string $name): stdClass|array|bool
57 {
13ae2ba6 58 $response = $this->connection->query('name_ban.del', [
da11ff54
VP
59 'name' => $name,
60 ]);
8fd8402e
VP
61
62 if (is_bool($response))
63 return false;
64
13ae2ba6
BM
65 if (property_exists($response, 'tkl'))
66 return $response->tkl;
67 return FALSE;
da11ff54
VP
68 }
69
70 /**
71 * Return a list of all bans.
72 *
73 * @return stdClass|array|bool
74 * @throws Exception
75 */
76 public function getAll(): stdClass|array|bool
77 {
78 $response = $this->connection->query('name_ban.list');
79
80 if (!is_bool($response)) {
81 return $response->list;
82 }
83
84 throw new Exception('Invalid JSON Response from UnrealIRCd RPC.');
85 }
86
87 /**
88 * Get a specific ban.
89 *
90 * @param string $name
91 * @return stdClass|array|bool
92 * @throws Exception
93 */
94 public function get(string $name): stdClass|array|bool
95 {
96 $response = $this->connection->query('name_ban.get', [
97 'name' => $name,
98 ]);
99
100 if (!is_bool($response)) {
13ae2ba6 101 return $response->tkl;
da11ff54
VP
102 }
103
104 throw new Exception('Invalid JSON Response from UnrealIRCd RPC.');
105 }
106}