-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
188 lines (164 loc) · 4.16 KB
/
app.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const IRC = require('irc-framework')
const eol = require('eol');
const core = require('@actions/core');
const github = require('@actions/github');
const client = new IRC.Client();
const throttled_interval = 1000;
const truncated_max_lines = 3;
function toBool(str) {
return !!JSON.parse(str);
}
const inputs = {
server: core.getInput('server'),
port: core.getInput('port'),
nickname: core.getInput('nickname'),
sasl_password: core.getInput('sasl_password'),
tls: toBool(core.getInput('tls')),
message: core.getInput('message'),
notice: toBool(core.getInput('notice')),
channel: core.getInput('channel'),
channel_key: core.getInput('channel_key'),
response_allow_from: core.getInput('response_allow_from'),
response_timeout: core.getInput('response_timeout'),
debug: core.getInput('debug'),
excess_flood: core.getInput('excess_flood'),
}
//const inputs = {
// server: 'irc.libera.chat',
// port: '6697',
// nickname: 'gottox-test',
// sasl_password: undefined,
// tls: true,
// message: "a\n".repeat(20),
// notice: false,
// channel: '##gottox-channel',
// channel_key: undefined,
// response_allow_from: "",
// response_timeout: 10,
// debug: true,
// excess_flood: "throttle"
//}
process.exitCode = 1;
const excess_flood_func = (() => {
switch (inputs.excess_flood) {
case "throttle":
return throttled;
case "truncate":
return truncated;
default:
console.error(`excess_flood must either be "truncate" or "throttle"`);
return process.exit();
}
})();
client.connect({
host: inputs.server,
port: inputs.port,
nick: inputs.nickname,
password: inputs.sasl_password,
tls: inputs.tls,
});
function throttled(array, cb) {
return new Promise(resolve => {
let timeout = 0;
let i = 0;
const f = () => {
cb(array[i]);
i++;
if (i >= array.length) {
resolve();
} else {
setTimeout(f, i < truncated_max_lines ? 0 : throttled_interval);
}
}
f();
});
}
function truncated(array, cb) {
return new Promise(resolve => {
let i = 0;
for (i = 0; i < array.length && i < truncated_max_lines; i++) {
if (i + 1 == truncated_max_lines && i + 1 != array.length) {
cb(array[i] + " [...]");
} else {
cb(array[i]);
}
}
resolve();
});
}
function sync(cb) {
client.raw('VERSION');
client.on('raw', (raw) => {
let i = 1;
const msg = raw.line.split(' ');
// if the line has message-tags, the command index will be offset by 1
if (msg[0][0] == '@') {
i++;
}
if (msg[i] === '351') {
return cb();
}
});
}
function finish_client() {
if (inputs.notice === false) {
client.part(inputs.channel);
}
client.part(inputs.channel);
client.quit();
process.exitCode = 0;
}
function handle_response() {
const allow_from = inputs.response_allow_from
.split(',').map(x => x.trim())
console.log(`Waiting ${inputs.response_timeout} seconds for response`);
const timeout = setTimeout(() => {
console.log("Timeout waiting for response")
finish_client()
}, inputs.response_timeout * 1000);
client.on('privmsg', (msg) => {
let prefix = inputs.nickname + ": ";
if (!msg.message.startsWith(prefix)) {
return;
}
console.log(`Mention from ${msg.nick}. Check authentication of the account`)
client.whois(msg.nick, (nick) => {
if (!allow_from.includes(nick.account)) {
return console.log(`${msg.nick}: account ${nick.account} is not in response_allow_from`)
}
core.setOutput("response", msg.message.substr(prefix.length));
core.setOutput("response_from", nick.account)
clearTimeout(timeout);
finish_client();
})
})
}
if (inputs.debug) {
client.on('debug', (ev) => {
console.log('#', ev)
});
client.on('raw', (ev) => {
console.log(ev.from_server ? '<' : '>', ev.line.trim());
});
}
client.on('registered', () => {
const messages = eol.split(inputs.message);
let promise = null;
if (inputs.notice) {
promise = excess_flood_func(messages, (message) => {
client.notice(inputs.channel, message);
});
} else {
client.join(inputs.channel);
promise = excess_flood_func(messages, (message) => {
client.say(inputs.channel, message);
});
}
promise.then(() => {
if (inputs.response_allow_from && inputs.notice === false) {
sync(handle_response)
} else {
sync(finish_client)
}
})
})