]> jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/blob - lib/User.php
90deaeded05df90985f73a8d3ff078de6c925480
[irc/unrealircd/unrealircd-rpc-php.git] / lib / User.php
1 <?php
2
3 namespace UnrealIRCd;
4
5 use Exception;
6 use stdClass;
7
8 class User
9 {
10
11 public Connection $connection;
12
13 public function __construct(Connection $conn)
14 {
15 $this->connection = $conn;
16 }
17
18 /**
19 * Return a list of all users.
20 *
21 * @throws Exception
22 */
23 public function getAll(): stdClass|array|bool
24 {
25 $response = $this->connection->query('user.list');
26
27 if(!is_bool($response)) {
28 return $response->list;
29 }
30
31 throw new Exception('Invalid JSON Response from UnrealIRCd RPC.');
32 }
33
34 /**
35 * Return a user object
36 *
37 * @return stdClass|array|bool
38 * @throws Exception
39 */
40 public function get(string $nick): stdClass|array|bool
41 {
42 $response = $this->connection->query('user.get', ['nick' => $nick]);
43
44 if (!is_bool($response)) {
45 return $response->client;
46 }
47
48 throw new Exception('Invalid JSON Response from UnrealIRCd RPC.');
49 }
50
51 /**
52 * Set the nickname of a user (changes the nick)
53 *
54 * @return stdClass|array|bool
55 * @throws Exception
56 */
57 public function set_nick(string $nick, string $newnick): stdClass|array|bool
58 {
59 return $this->connection->query('user.set_nick', [
60 'nick' => $nick,
61 'newnick' => $newnick,
62 ]);
63 }
64
65 /**
66 * Set the username/ident of a user
67 *
68 * @return stdClass|array|bool
69 * @throws Exception
70 */
71 public function set_username(string $nick, string $username): stdClass|array|bool
72 {
73 return $this->connection->query('user.set_username', [
74 'nick' => $nick,
75 'username' => $username,
76 ]);
77 }
78
79 /**
80 * Set the realname/gecos of a user
81 *
82 * @return stdClass|array|bool
83 * @throws Exception
84 */
85 public function set_realname(string $nick, string $realname): stdClass|array|bool
86 {
87 return $this->connection->query('user.set_realname', [
88 'nick' => $nick,
89 'realname' => $realname,
90 ]);
91 }
92
93 /**
94 * Set a virtual host (vhost) on the user
95 *
96 * @return stdClass|array|bool
97 * @throws Exception
98 */
99 public function set_vhost(string $nick, string $vhost): stdClass|array|bool
100 {
101 return $this->connection->query('user.set_vhost', [
102 'nick' => $nick,
103 'vhost' => $vhost,
104 ]);
105 }
106
107 /**
108 * Change the user modes of a user.
109 *
110 * @return stdClass|array|bool
111 * @throws Exception
112 */
113 public function set_mode(string $nick, string $mode, bool $hidden = false): stdClass|array|bool
114 {
115 return $this->connection->query('user.set_mode', [
116 'nick' => $nick,
117 'modes' => $mode,
118 'hidden' => $hidden,
119 ]);
120 }
121
122 /**
123 * Change the snomask of a user (oper).
124 *
125 * @return stdClass|array|bool
126 * @throws Exception
127 */
128 public function set_snomask(string $nick, string $snomask, bool $hidden = false): stdClass|array|bool
129 {
130 return $this->connection->query('user.set_snomask', [
131 'nick' => $nick,
132 'snomask' => $snomask,
133 'hidden' => $hidden,
134 ]);
135 }
136
137 /**
138 * Make user an IRC Operator (oper).
139 *
140 * @return stdClass|array|bool
141 * @throws Exception
142 */
143 public function set_oper(string $nick, string $oper_account, string $oper_class,
144 string $class = null, string $modes = null,
145 string $snomask = null, string $vhost = null): stdClass|array|bool
146 {
147 return $this->connection->query('user.set_oper', [
148 'nick' => $nick,
149 'oper_account' => $oper_account,
150 'oper_class' => $oper_class,
151 'class' => $class,
152 'modes' => $modes,
153 'snomask' => $snomask,
154 'vhost' => $vhost,
155 ]);
156 }
157
158 /**
159 * Join a user to a channel.
160 *
161 * @return stdClass|array|bool
162 * @throws Exception
163 */
164 public function join(string $nick, string $channel,
165 string $key = null, bool $force = false): stdClass|array|bool
166 {
167 return $this->connection->query('user.join', [
168 'nick' => $nick,
169 'channel' => $channel,
170 'key' => $key,
171 'force' => $force,
172 ]);
173 }
174
175 /**
176 * Part a user from a channel.
177 *
178 * @return stdClass|array|bool
179 * @throws Exception
180 */
181 public function part(string $nick, string $channel, bool $force = false): stdClass|array|bool
182 {
183 return $this->connection->query('user.part', [
184 'nick' => $nick,
185 'channel' => $channel,
186 'force' => $force,
187 ]);
188 }
189
190 /**
191 * Quit a user from IRC. Pretend it is a normal QUIT.
192 *
193 * @return stdClass|array|bool
194 * @throws Exception
195 */
196 public function quit(string $nick, string $reason): stdClass|array|bool
197 {
198 return $this->connection->query('user.quit', [
199 'nick' => $nick,
200 'reason' => $reason,
201 ]);
202 }
203
204 /**
205 * Kill a user from IRC. Show that the user is forcefully removed.
206 *
207 * @return stdClass|array|bool
208 * @throws Exception
209 */
210 public function kill(string $nick, string $reason): stdClass|array|bool
211 {
212 return $this->connection->query('user.kill', [
213 'nick' => $nick,
214 'reason' => $reason,
215 ]);
216 }
217 }