~tyil/raku-local-musashi

ref: 2eceb29fb9cbb1a2ff85d02e58052ce3f3a0ef54 raku-local-musashi/lib/Local/Musashi.pm6 -rw-r--r-- 4.0 KiB View raw
2eceb29f — Patrick Spek Add IRC::Client::Plugin::Reminders a month ago
                                                                                
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
#! /usr/bin/env false

use v6.d;

use Config;
use IO::Glob;
use IO::Path::XDG;
use IRC::Client;
use IRC::Client::Plugin::DiceRolls;
use IRC::Client::Plugin::NickServ;
use IRC::Client::Plugin::Reminders;
use IRC::TextColor;
use Log;
use Log::Level;

unit module Local::Musashi;

#| Run the musashi IRC bot.
unit sub MAIN (
) is export {
	# Set up logger
	my $*LOG;

	if (%*ENV<RAKU_LOG_CLASS>:exists) {
		$*LOG = (require ::(%*ENV<RAKU_LOG_CLASS>)).new;
		$*LOG.add-output($*OUT, %*ENV<RAKU_LOG_LEVEL> // Log::Level::Info);
	}

	# Create $bot early to use it in traps
	my IRC::Client $bot;

	# Play nice with Kubernetes
	$*ERR.out-buffer = False;
	$*OUT.out-buffer = False;

	# Trap signals
	signal(SIGTERM).tap({ .quit with $bot });

	# Load config
	my IO::Path $config-file = xdg-config-dirs()
		.map(*.add('musashi.toml'))
		.grep(*.f)
		.first
		;

	die "Missing configuration file: $config-file.absolute()." unless $config-file;

	my $*CONFIG = Config.new.read($config-file.absolute);

	if (!$*CONFIG.get('irc.opers')) {
		.warning('No opers defined in irc.opers') with $*LOG;
	}

	# Set up database connection
	my $*DB;

	given ($*CONFIG.get('database.driver', '').fc) {
		when 'postgresql' {
			try require DB::Pg;

			if (DB::Pg ~~ Failure) {
				.emergency('Failed to load DB::Pg') with $*LOG;
				return;
			}

			my $conninfo = %(
				host => $*CONFIG.get('database.host', 'localhost'),
				dbname => $*CONFIG.get('database.database', 'musashi'),
				user => $*CONFIG.get('database.user', ~$*USER),
				password => $*CONFIG.get('database.password'),
				port => $*CONFIG.get('database.port', 5432),
			).pairs.grep(*.value).map({ "{$_.key}={$_.value}" }).join(' ');

			.info("Using Postgres database ($conninfo)") with $*LOG;

			$*DB = DB::Pg.new(:$conninfo);
		}
		default {
			.warning("Invalid database driver") with $*LOG;
		}
	}

	# Prepare plugins
	my @plugins = (
		IRC::Client::Plugin::NickServ.new(config => $*CONFIG),
		IRC::Client::Plugin::DiceRolls.new(config => $*CONFIG),
		IRC::Client::Plugin::Reminders.new(config => $*CONFIG),

		# Musashi-specific behaviour
		class {
			multi method irc-privmsg-channel($e where /^hi$/)
			{
				"Hi {$e.nick}!"
			}

			multi method irc-privmsg-channel($ where /^o\/$/)
			{
				"\\o"
			}

			multi method irc-privmsg-channel($e where /^[good]?morn[ing]?$/)
			{
				"And a good morning to you too, {$e.nick}"
			}

			multi method irc-privmsg-channel($e where /black/)
			{
				$e.text.subst('black', ircstyle('african-american', :bold), :g)
			}

			multi method irc-privmsg-channel($e where *.text.contains('twitter.com'))
			{
				$e.text.subst('twitter.com', ircstyle('nitter.net', :bold), :g)
			}

			#| Allow reloading configuration while the bot is
			#| running.
			multi method irc-to-me (
				$event where 'reload',
			) {
				if (!self!is-oper($event.usermask)) {
					return "I'm sorry, I can't let you do that";
				}

				.info('Reloading configuration') with $*LOG;
				$*CONFIG.=read($config-file.absolute);

				for @plugins -> $plugin {
					next unless $plugin.^can('reload-config');

					.info("Reloading plugin $plugin") with $*LOG;
					$plugin.reload-config($*CONFIG);
				}

				'Reloaded configuration!';
			}

			#| Check if a usermask has oper privileges for the bot.
			method !is-oper (
				Str() $usermask,
				--> Bool:D
			) {
				for $*CONFIG.get('irc.opers', []).List -> $oper {
					return True if $usermask ~~ glob($oper);
				}

				False;
			}
		},
	);

	# Set up the bot
	$bot .= new(
		:nick($*CONFIG.get("irc.nickname", "musashi"))
		:username($*CONFIG.get("irc.username", "musashi"))
		:realuser($*CONFIG.get("irc.realname", "Yet another tachikoma AI"))
		:host($*CONFIG.get("irc.host", "irc.darenet.org"))
		:port($*CONFIG.get("irc.port", 6667))
		:ssl($*CONFIG.get("irc.ssl", False))
		:channels($*CONFIG.get("irc.channels", "#scriptkitties"))
		:debug($*CONFIG.get("debug", True))
		:!autoprefix
		:@plugins
	);

	# Start
	$bot.run;
}

=begin pod

=NAME    Local::Musashi
=AUTHOR  Patrick Spek <p.spek@tyil.work>
=VERSION 0.0.1

=head1 Synopsis

=head1 Description

=head1 Examples

=head1 See also

=end pod

# vim: ft=perl6 noet