]>
jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/blob - lib/Connection.php
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)),
32 /* Start the connection now */
33 $this->connection
->ping();
37 * Encode and send a query to the RPC server.
39 * @note I'm not sure on the response type except that it may be either an object or array.
41 * @param string $method
42 * @param array|null $params
44 * @return object|array|bool
47 public function query(string $method, array|null $params = null): object|array|bool
49 $id = random_int(1, 99999);
58 $json_rpc = json_encode($rpc);
60 $this->connection
->text($json_rpc);
61 $reply = $this->connection
->receive();
63 $reply = json_decode($reply);
65 if (property_exists($reply, 'result')) {
66 if($id !== $reply->id
) {
67 throw new Exception('Invalid ID. This is not the expected reply.');
71 return $reply->result
;
74 if (property_exists($reply, 'error')) {
75 $this->errno
= $reply->error
->code
;
76 $this->error
= $reply->error
->message
;
80 /* This should never happen */
81 throw new Exception('Invalid JSON-RPC response from UnrealIRCd: not an error and not a result.');
84 public function user(): User
86 return new User($this);
89 public function channel(): Channel
91 return new Channel($this);
94 public function serverban(): ServerBan
96 return new ServerBan($this);
99 public function spamfilter(): Spamfilter
101 return new Spamfilter($this);
103 public function nameban(): NameBan
105 return new NameBan($this);
107 public function server(): Server
109 return new Server($this);
111 public function serverbanexception(): ServerBanException
113 return new ServerBanException($this);