aboutsummaryrefslogtreecommitdiff
path: root/lib/IRC/Client.rakumod
blob: 166873c2096b56c01cd62e6d1ed91a10d3fe886f (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#! /usr/bin/env false

use v6.d;

use Log;

use IRC::Client::Core;
use IRC::Client::Handler;
use IRC::Client::Message;
use IRC::Client::Plugin;

#| A simple IRC client, intended for automating interactions.
unit class IRC::Client;

#| The host to connect to.
has Str $.host = '127.0.0.1';

#| The port to connect to.
has Int $.port = 6697;

#| Use SSL. Requires IO::Socket::Async::SSL to be installed.
has Bool $.ssl = True;

#| A list of channels to join on startup.
has Str @.channels is rw;

#| A list of acceptable nicknames to use.
has Str @.nicks;

#| The user to identify as.
has Str $.user = 'raku';

#| The real name to identify as.
has Str $.real-name = 'IRC::Client';

#| A list of plugins to use.
has IRC::Client::Plugin @.plugins;

#| The timeout between liveness checks (a PING to itself).
has Real $.liveness-check-timeout = 30;

#| The timeout between sending individual messages.
has Real $.send-timeout = 0.2;

#| The bot's own full prefix.
has Str $.prefix is rw;

#| The current nick of the bot.
has Str $.nick is rw;

#| Whether the bot is already connected.
has Bool $.connected is rw;

#| The connection with the IRC server.
has $!connection;

#| A supplier for incoming messages.
has Supplier $!in;

#| A Channel for outgoing messages.
has Channel $!out;

submethod TWEAK
{
	# Due to lack of knowledge on how to properly do "protected" variables
	# in Raku, I'm just throwing a lot of warnings when you shouldn't be
	# setting something.
	if ($!connected) { .warning("Don't set connected on .new!") with $Log::instance }
	if ($!prefix) { .warning("Don't set prefix on .new!") with $Log::instance }
	if ($!nick) { .warning("Don't set nick on .new!") with $Log::instance }

	$!connected = False;
}

#| Start the IRC client, connecting to the server and signing on to the
#| network.
method start
{
	# Sanity checks
	if (!@!nicks) {
		.error("You must specify at least one nickname") with $Log::instance;
		return;
	}

	# Include the core functionality plugin
	@!plugins.unshift(IRC::Client::Core);

	# Insert IRC::Client object into plugins
	for @!plugins.grep(* ~~ IRC::Client::Plugin:D) {
		$_.irc = self;
	}

	# Set up suppliers
	$!in  = Supplier.new;
	$!out = Channel.new;

	# Set up a tap to handle incomding messages, outside of the react
	# block. This should help in keeping the react block free of
	# long-running code.
	$!in.Supply.tap(sub ($message) {
		try {
			CATCH {
				default {
					my $exception = $_
						.gist
						.lines
						.map(*.trim-trailing)
						.join("\n")
						;

					.critical($exception) with $Log::instance;
				}
			}

			.debug("Handling $message") with $Log::instance;
			IRC::Client::Handler.handle(IRC::Client::Message.new($message, self));
		}
	});

	# Dispatch an irc-started event. This event should occurr once per run,
	# to perform certain initial setups like an HTTP server.
	IRC::Client::Handler.dispatch(['irc-started' => self], self);

	# Pick the socket class
	my $socket = !$!ssl ?? IO::Socket::Async !! do {
		require ::('IO::Socket::Async::SSL');
	};

	# Connect to the server in a loop, to ensure automatic re-connection
	# upon any issue.
	loop {
		.debug("Connecting to $!host:$!port") with $Log::instance;

		try {
			CATCH {
				default {
					my $exception = $_
						.gist
						.lines
						.map(*.trim-trailing)
						.join("\n")
						;

					.emergency($exception) with $Log::instance;
				}
			}

			$!connection = await $socket.connect($!host, $!port);
			.debug("Connected to $!host:$!port") with $Log::instance;

			# Create a buffer for incoming data
			my $buffer;

			react {
				# Setup handling incoming messages
				whenever $!connection.Supply -> $message {
					for $message.comb -> $character {
						given ($character) {
							# When \r\n is encountered, it signifies the end of a message,
							# so emit whatever is in the $!buffer to the $!in Supply.
							when "\r\n" {
								.info("< $buffer") with $Log::instance;
								$!in.emit($buffer);
								$buffer = '';
							}

							# Otherwise, add the character to the $!buffer.
							default { $buffer ~= $character }
						}
					}
				}

				# Setup handling outgoing messages
				whenever Supply.interval($!send-timeout) {
					if ($!connected) {
						with ($!out.poll) -> $message {
							.notice("> $message") with $Log::instance;
							$!connection.put($message);
						}
					}
				}

				# Simple keep-alive check, to ensure the socket
				# is still open and usable.
				whenever Supply.interval($!liveness-check-timeout).skip {
					if ($!connected) {
						self.privmsg($!nick, "PING {now.to-posix.first.subst('.', ' ')}", :ctcp);
					}
				}

				# Setup handling ^c
				whenever signal(SIGINT) {
					.notice('Caught ^c') with $Log::instance;
					self.stop('Caught ^c');
					exit 0;
				}

				# Log on to the server
				IRC::Client::Handler.dispatch(['irc-setup' => self], self);

				LAST {
					$!connected = False;
					.error('Socket closed') with $Log::instance;
				}
			}
		}

		# Wait a small amount of time, in order to not blast an IRC
		# server with connections when something is wrong.
		sleep(5);
	}
}

#| Stop the IRC client, sending a QUIT to the server and closing the
#| connection.
method stop (
	Str:D $reason = ''
) {
	my $message = "QUIT :$reason";

	.notice("> $message") with $Log::instance;

	$!connected = False;
	$!connection.put($message);
	$!connection.close;
}

#
# Backwards compatability
#

method run (
) is DEPRECATED('IRC::Client.start') {
	self.start()
}

method quit (
) is DEPRECATED('IRC::Client.stop') {
	self.stop()
}

method send (
	:$where!,
	:$text!,
	:$notice
) is DEPRECATED('IRC::Client.privmsg or IRC::Client.notice') {
	self."{$notice ?? 'notice' !! 'privmsg'}"($where, $text)
}

#
# Barebones messaging
#

#| Send a raw line to the IRC server.
method send-raw (
	Str:D $message,
) {
	$!out.send($message);
}

#
# Convenience methods
#

method join (
	Str:D $channel,
) {
	self.send-raw("JOIN $channel");
}

method set-nick (
	Str:D $nick,
) {
	$!nick = $nick;
	self.send-raw("NICK :$nick");
}

method part (
	Str:D $channel,
) {
	self.send-raw("PART $channel");
}

multi method privmsg (
	Str:D $target,
	Str:D $message,
	Bool :$ctcp where { !$_ },
) {
	self.send-raw("PRIVMSG $target :$message");
}

multi method privmsg (
	Str:D $target,
	Str:D $message,
	Bool :$ctcp! where { $_ },
) {
	self.privmsg($target, "\x[1]$message\x[1]");
}

multi method notice (
	Str:D $target,
	Str:D $message,
	Bool :$ctcp where { !$_ },
) {
	self.send-raw("NOTICE $target :$message");
}

multi method notice (
	Str:D $target,
	Str:D $message,
	Bool :$ctcp where { $_ },
) {
	self.notice($target, "\x[1]$message\x[1]");
}

=begin pod

=NAME    IRC::Client
=AUTHOR  Patrick Spek <~tyil/raku-devel@lists.sr.ht>
=VERSION 0.0.0

=head1 Synopsis

=head1 Description

=head1 Examples

=head1 See also

=end pod

# vim: ft=perl6 noet