]> jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/blob - lib/Server.php
Add $rpc->rpc()->add_timer(); and ->del_timer()
[irc/unrealircd/unrealircd-rpc-php.git] / lib / Server.php
1 <?php
2
3 namespace UnrealIRCd;
4
5 use Exception;
6 use stdClass;
7
8 class Server
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 servers.
20 *
21 * @throws Exception
22 */
23 public function getAll(): stdClass|array|bool
24 {
25 $response = $this->connection->query('server.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 server object
36 *
37 * @return stdClass|array|bool
38 * @throws Exception
39 */
40 public function get(string $server = null): stdClass|array|bool
41 {
42 $response = $this->connection->query('server.get', ['server' => $server]);
43
44 if (!is_bool($response)) {
45 return $response->server;
46 }
47
48 return false; // not found
49 }
50
51 /**
52 * Rehash a server
53 *
54 * @return stdClass|array|bool
55 * @throws Exception
56 */
57 public function rehash(string $serv): stdClass|array|bool
58 {
59 return $this->connection->query('server.rehash', ["server" => $serv]);
60 }
61
62 /**
63 * Connect to a server
64 *
65 * @param string $name The name of the server, e.g; irc.example.com
66 * @return stdClass|array|bool
67 * @throws Exception
68 */
69 public function connect(string $name): stdClass|array|bool
70 {
71 return $this->connection->query('server.connect', [
72 'link' => $name,
73 ]);
74 }
75
76 /**
77 * Disconnects a server
78 *
79 * @param string $name The name of the server, e.g; irc.example.com
80 * @return stdClass|array|bool
81 * @throws Exception
82 */
83 public function disconnect(string $name, string $reason = "No reason"): stdClass|array|bool
84 {
85 return $this->connection->query('server.disconnect', [
86 'link' => $name,
87 'reason' => $reason
88 ]);
89 }
90
91
92 /**
93 * List modules on the server
94 *
95 * @return stdClass|array|bool
96 * @throws Exception
97 */
98 public function module_list($name = NULL): stdClass|array|bool
99 {
100 $arr = [];
101 if ($name)
102 $arr['server'] = $name;
103
104 return $this->connection->query('server.module_list', $arr);
105 }
106
107 }