]>
jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/blob - lib/Connection.php
1b8b01b9bb29a1e3a6f17d0be8892d5d191763f0
10 protected WebSocket\Client
$connection;
15 public function __construct(string $uri, string $api_login, array $options = null)
17 $context = $options["context"] ?? stream_context_create();
19 if (isset($options["tls_verify"]) && !$options["tls_verify"]) {
20 stream_context_set_option($context, 'ssl', 'verify_peer', false);
21 stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
24 $this->connection
= new WebSocket\
Client($uri, [
25 'context' => $context,
27 'Authorization' => sprintf('Basic %s', base64_encode($api_login)),
35 * Encode and send a query to the RPC server.
37 * @note I'm not sure on the response type except that it may be either an object or array.
39 * @param string $method
40 * @param array|null $params
42 * @return object|array|bool
45 public function query(string $method, array|null $params = null): object|array|bool
47 $id = random_int(1, 99999);
56 $json_rpc = json_encode($rpc);
58 $this->connection
->text($json_rpc);
59 $reply = $this->connection
->receive();
61 $reply = json_decode($reply);
63 if (property_exists($reply, 'result')) {
64 if($id !== $reply->id
) {
65 throw new Exception('Invalid ID. This is not the expected reply.');
69 return $reply->result
;
72 if (property_exists($reply, 'error')) {
73 $this->errno
= $reply->error
->code
;
74 $this->error
= $reply->error
->message
;
78 /* This should never happen */
79 throw new Exception('Invalid JSON-RPC response from UnrealIRCd: not an error and not a result.');
82 public function user(): User
84 return new User($this);
87 public function channel(): Channel
89 return new Channel($this);
92 public function serverban(): ServerBan
94 return new ServerBan($this);
97 public function spamfilter(): Spamfilter
99 return new Spamfilter($this);
101 public function nameban(): NameBan
103 return new NameBan($this);
105 public function server(): Server
107 return new Server($this);
109 public function serverbanexception(): BanException
111 return new ServerBanException($this);