1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
// SPDX-License-Identifier: CC0-1.0
"use strict";
/** Implements methods for accessing an Anope JSON-RPC server. */
class AnopeRPC {
/**
* Initializes a new AnopeRPC instance with the specified RPC host.
*
* @param {string} The RPC host base URL.
*/
constructor(host) {
this.host = host;
}
/**
* Executes an arbitrary RPC query.
*
* @param {string} method The name of the method to execute.
* @param {...*} params The parameters pass to the method.
* @returns {*} The result of the RPC query.
*/
async run(method, ...params) {
const request = JSON.stringify({
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": Math.random().toString(36).slice(2)
});
const response = await fetch(this.host, {
method: 'POST',
body: request
});
if (!response.ok) {
throw new Error(`HTTP returned ${response.status}`)
}
const json = await response.json();
if ("error" in json) {
throw new Error(`JSON-RPC returned ${json.error.code}: ${json.error.message}`)
}
if ("result" in json) {
return json.result;
}
return null;
}
/**
* Retrieves a list of channels.
*
* @returns {array} An array of channel names.
*/
listChannels() {
return this.run("anope.listChannels");
}
/**
* Retrieves information about the specified channel.
*
* @param {string} name The name of the channel.
* @returns {object} An object containing information about the channel.
*/
channel(name) {
return this.run("anope.channel", name);
}
/**
* Retrieves a list of services operators.
*
* @returns {array} An array of channel names.
*/
listOpers() {
return this.run("anope.listOpers");
}
/**
* Retrieves information about the specified services operator.
*
* @param {string} name The name of the services operator.
* @returns {object} An object containing information about the services operator.
*/
oper(name) {
return this.run("anope.oper", name);
}
/**
* Retrieves a list of servers.
*
* @returns {array} An array of servers names.
*/
listServers() {
return this.run("anope.listServers");
}
/**
* Retrieves information about the specified server.
*
* @param {string} name The name of the server.
* @returns {object} An object containing information about the server.
*/
server(name) {
return this.run("anope.server", name);
}
/**
* Retrieves a list of users.
*
* @returns {array} An array of channel names.
*/
listUsers() {
return this.run("anope.listUsers");
}
/**
* Retrieves information about the specified user.
*
* @param {string} nick The nick of the user.
* @returns {object} An object containing information about the user.
*/
user(nick) {
return this.run("anope.user", nick);
}
/**
* Sends a message to every user on the network.
* @param {...*} messages One or more messages to send.
*/
messageNetwork(...messages) {
return this.run("anope.messageNetwork", ...messages);
}
/**
* Sends a message to every user on the specified server.
* @param {string} name The name of the server.
* @param {...*} messages One or more messages to send.
*/
messageServer(server, ...messages) {
return this.run("anope.messageServer", server, ...messages);
}
/**
* Sends a message to the specified user.
* @param {string} source The source pseudoclient to send the message from.
* @param {string} target The target user to send the message to.
* @param {...*} messages One or more messages to send.
*/
messageUser(source, target, ...messages) {
return this.run("anope.messageServer", source, target, ...messages);
}
}
/*
const arpc = new AnopeRPC("http://127.0.0.1:8080/jsonrpc");
arpc.listServers().then(servers => {
console.log(servers);
}).catch (error => {
console.log(error);
});
*/
|