ircinch 2.4.5 → 2.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.standard.yml +1 -1
- data/CHANGELOG.md +10 -0
- data/CODE_OF_CONDUCT.md +7 -81
- data/README.md +39 -30
- data/docs/bot_options.md +207 -314
- data/docs/changes.md +293 -282
- data/docs/common_mistakes.md +50 -41
- data/docs/common_tasks.md +63 -42
- data/docs/encodings.md +28 -55
- data/docs/events.md +123 -131
- data/docs/getting_started.md +97 -102
- data/docs/logging.md +62 -70
- data/docs/migrating.md +145 -131
- data/docs/plugins.md +109 -3
- data/docs/readme.md +17 -15
- data/examples/README.md +78 -0
- data/examples/basic/autovoice.rb +14 -18
- data/examples/basic/google.rb +9 -22
- data/examples/basic/hello.rb +4 -3
- data/examples/basic/join_part.rb +8 -23
- data/examples/basic/memo.rb +26 -24
- data/examples/basic/msg.rb +6 -6
- data/examples/basic/seen.rb +17 -24
- data/examples/basic/urban_dict.rb +6 -26
- data/examples/basic/url_shorten.rb +8 -26
- data/examples/plugins/autovoice.rb +17 -16
- data/examples/plugins/custom_prefix.rb +4 -3
- data/examples/plugins/dice_roll.rb +13 -20
- data/examples/plugins/google.rb +7 -21
- data/examples/plugins/hello.rb +4 -3
- data/examples/plugins/hooks.rb +5 -6
- data/examples/plugins/join_part.rb +8 -21
- data/examples/plugins/lambdas.rb +5 -6
- data/examples/plugins/last_nick.rb +7 -6
- data/examples/plugins/msg.rb +6 -6
- data/examples/plugins/multiple_matches.rb +10 -11
- data/examples/plugins/own_events.rb +14 -19
- data/examples/plugins/seen.rb +23 -23
- data/examples/plugins/timer.rb +5 -6
- data/examples/plugins/url_shorten.rb +15 -19
- data/examples/settings.rb +19 -0
- data/examples/tiny_url.rb +41 -0
- data/ircinch.gemspec +5 -6
- data/lib/cinch/bot.rb +2 -3
- data/lib/cinch/channel.rb +4 -4
- data/lib/cinch/message_queue.rb +2 -2
- data/lib/cinch/plugin.rb +1 -1
- data/lib/cinch/version.rb +1 -1
- metadata +270 -249
- data/lib/cinch/rubyext/float.rb +0 -5
data/docs/getting_started.md
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
# @title Getting Started
|
|
2
|
-
# @markup kramdown
|
|
3
|
-
|
|
4
1
|
# Getting Started
|
|
5
2
|
|
|
6
|
-
This
|
|
7
|
-
|
|
3
|
+
This guide builds an IRC bot with IRCinch. Install it with `gem install ircinch`
|
|
4
|
+
or `bundle add ircinch` in an existing Bundler project. The gem loads with
|
|
5
|
+
`require "ircinch"` and exposes the Ruby API under `Cinch`.
|
|
6
|
+
|
|
7
|
+
Choose an IRC server, a unique nickname, and a channel where you can test.
|
|
8
|
+
The examples use the default plain-text port, `6667`, without credentials.
|
|
9
|
+
See the [Libera.Chat connection guide](https://libera.chat/guides/connect)
|
|
10
|
+
and the [bot's TLS and account settings](bot_options.md#ssl) for other setups.
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
## What IRCinch really is
|
|
10
13
|
|
|
11
|
-
First and foremost, it is important to understand that
|
|
14
|
+
First and foremost, it is important to understand that IRCinch is more
|
|
12
15
|
of an API for IRC access than a full-blown bot framework like Autumn
|
|
13
16
|
or Rbot.
|
|
14
17
|
|
|
@@ -16,169 +19,161 @@ There will be no enforced directory structures, no magical places from
|
|
|
16
19
|
which plugins will be loaded and no obscure, "fancy" names. Plugins
|
|
17
20
|
will be plugins and not "leaves".
|
|
18
21
|
|
|
19
|
-
This, however, does not mean that
|
|
22
|
+
This, however, does not mean that IRCinch requires you to be familiar
|
|
20
23
|
with the internals of the IRC protocol. Quite the opposite: A very
|
|
21
24
|
high-level abstraction is provided, allowing things such as
|
|
22
25
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
```ruby
|
|
27
|
+
Channel("#ircinch").users.each do |user, modes|
|
|
28
|
+
user.send "I am watching you!"
|
|
29
|
+
end
|
|
30
|
+
```
|
|
26
31
|
|
|
27
32
|
to work.
|
|
28
33
|
|
|
29
|
-
|
|
30
34
|
Furthermore, the API has been designed in a way that it sticks true to
|
|
31
35
|
the way Ruby looks and behaves. Plugins are normal classes that mix-in
|
|
32
36
|
a module, functions of the bot are implemented as normal methods and
|
|
33
37
|
so on.
|
|
34
38
|
|
|
35
|
-
|
|
39
|
+
## Hello, World
|
|
36
40
|
|
|
37
41
|
The following will describe one of the most basic IRC bots you can
|
|
38
|
-
write in
|
|
42
|
+
write in IRCinch: One that joins a specific channel and responds to
|
|
39
43
|
"hello" by saying "Hello, World".
|
|
40
44
|
|
|
45
|
+
```ruby
|
|
46
|
+
require "ircinch"
|
|
41
47
|
|
|
42
|
-
|
|
48
|
+
bot = Cinch::Bot.new do
|
|
49
|
+
configure do |c|
|
|
50
|
+
c.server = "irc.libera.chat"
|
|
51
|
+
c.nick = "my_example_bot"
|
|
52
|
+
c.channels = ["#ircinch-bots"]
|
|
53
|
+
end
|
|
43
54
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
end
|
|
55
|
+
on :message, "hello" do |m|
|
|
56
|
+
m.reply "Hello, World"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
49
59
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
end
|
|
53
|
-
end
|
|
60
|
+
bot.start
|
|
61
|
+
```
|
|
54
62
|
|
|
55
|
-
|
|
63
|
+
Save the example as `bot.rb`, replace the nickname and channel as needed,
|
|
64
|
+
and run `ruby bot.rb` (or `bundle exec ruby bot.rb` with Bundler). After the
|
|
65
|
+
bot joins, send `hello` from another IRC client in that channel. The bot
|
|
66
|
+
starts its connection loop at `bot.start`. Use Ctrl-C to stop the process.
|
|
56
67
|
|
|
68
|
+
### In Detail
|
|
57
69
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
## In Detail
|
|
62
|
-
|
|
63
|
-
So, what are we actually doing in that short piece of code? First, we
|
|
64
|
-
create a new bot in line 3 and conigure it in lines 4–6 –
|
|
65
|
-
{Cinch::Bot#configure configure} simply yields the configuration
|
|
66
|
-
object, which allows you to configure various things. In this example,
|
|
67
|
-
we only set which server to connect to and which channel to join.
|
|
70
|
+
First, we create a bot. The [`configure`](../lib/cinch/bot.rb) method yields
|
|
71
|
+
the configuration object. In this example, we set which server to connect
|
|
72
|
+
to and which channel to join.
|
|
68
73
|
Another often-used option is the nickname of the bot
|
|
69
|
-
(
|
|
70
|
-
available options, see
|
|
74
|
+
([`c.nick`](bot_options.md#nick)). For an overview of all
|
|
75
|
+
available options, see [the list of options](bot_options.md).
|
|
71
76
|
|
|
72
77
|
Following, we define a basic message handler. In its simplest form,
|
|
73
|
-
|
|
78
|
+
[`on`](../lib/cinch/bot.rb) expects two arguments: The kind of message to react
|
|
74
79
|
to and the pattern to match. In this case, the kind is
|
|
75
|
-
|
|
80
|
+
[`:message`](events.md#message), which means that the bot will
|
|
76
81
|
respond to both messages in channels as well as messages sent directly
|
|
77
82
|
to the bot. For a list of all kinds, called events, see
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
For the pattern we use a basic string, which means that the message
|
|
81
|
-
has to be exactly that string. It mustn't have anything before or
|
|
82
|
-
after the word "hello". Another way of using {Cinch::Bot#on on} is by using
|
|
83
|
-
regular expressions:
|
|
83
|
+
[the list of events](events.md).
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
end
|
|
85
|
+
The string `"hello"` matches the entire message. Use a regular expression
|
|
86
|
+
when you need to match a pattern or extract part of the message:
|
|
88
87
|
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
```ruby
|
|
89
|
+
on :message, /\Ayear (\d{4})\z/ do |m, year|
|
|
90
|
+
m.reply "You entered #{year}"
|
|
91
|
+
end
|
|
92
|
+
```
|
|
91
93
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
capture groups of the regular expression, passed to it.
|
|
94
|
+
For `year 2026`, the handler receives the message and the captured string
|
|
95
|
+
`"2026"` as separate arguments.
|
|
95
96
|
|
|
96
97
|
The message object allows insight into the nature of the message, i.e.
|
|
97
98
|
who sent it, when was it sent etc, and also provides the
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
reply} will respond to the channel, otherwise directly to the user.
|
|
99
|
+
[`reply`](../lib/cinch/message.rb) method. It responds in the channel for
|
|
100
|
+
channel messages and directly to the user for private messages.
|
|
101
101
|
|
|
102
|
-
We
|
|
103
|
-
whenever someone says "hello".
|
|
102
|
+
We use `reply` to send back "Hello, World" whenever someone says "hello".
|
|
104
103
|
|
|
105
104
|
That's it!
|
|
106
105
|
|
|
107
|
-
|
|
106
|
+
## on-handlers vs. plugins
|
|
108
107
|
|
|
109
108
|
Using `on` might be nice and handy for writing simple bots, but if you
|
|
110
109
|
want to write a more complex bot, providing lots of different features
|
|
111
110
|
to its users, then using plugins might be a better solution.
|
|
112
111
|
|
|
113
112
|
But what are plugins, exactly? Technically, plugins are implemented as
|
|
114
|
-
Ruby classes that mix-in a
|
|
113
|
+
Ruby classes that mix-in a [specific module](../lib/cinch/plugin.rb) to get
|
|
115
114
|
access to various methods.
|
|
116
115
|
|
|
117
116
|
To have an example to work with, we'll convert our "Hello, World" bot
|
|
118
117
|
to using the plugin API:
|
|
119
118
|
|
|
120
|
-
|
|
119
|
+
```ruby
|
|
120
|
+
require "ircinch"
|
|
121
121
|
|
|
122
|
-
|
|
123
|
-
|
|
122
|
+
class HelloWorld
|
|
123
|
+
include Cinch::Plugin
|
|
124
124
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
125
|
+
match "hello"
|
|
126
|
+
def execute(m)
|
|
127
|
+
m.reply "Hello, World"
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
130
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
131
|
+
bot = Cinch::Bot.new do
|
|
132
|
+
configure do |c|
|
|
133
|
+
c.server = "irc.libera.chat"
|
|
134
|
+
c.nick = "my_example_bot"
|
|
135
|
+
c.channels = ["#ircinch-bots"]
|
|
136
|
+
c.plugins.plugins = [HelloWorld]
|
|
137
|
+
end
|
|
138
|
+
end
|
|
138
139
|
|
|
139
|
-
|
|
140
|
+
bot.start
|
|
141
|
+
```
|
|
140
142
|
|
|
141
|
-
The
|
|
142
|
-
`HelloWorld`, and that we use {Cinch::Plugin::ClassMethods#match
|
|
143
|
-
match} instead of `on` to define our handler. Furthermore, we didn't
|
|
144
|
-
specify a message type nor did we provide any blocks.
|
|
143
|
+
The `HelloWorld` class defines its handler with [`match`](../lib/cinch/plugin.rb).
|
|
145
144
|
|
|
146
145
|
But let's back up and proceed in smaller steps to see how plugins are built.
|
|
147
146
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
{Cinch::Plugin::ClassMethods#match match} and also allows Cinch to
|
|
151
|
-
control the class in specific ways required for plugins to work.
|
|
147
|
+
Including [`Cinch::Plugin`](../lib/cinch/plugin.rb) gives the class methods
|
|
148
|
+
like `match` and lets Cinch manage it as a plugin.
|
|
152
149
|
|
|
153
150
|
Then we use aforementioned `match`, instead of `on`, to specify what
|
|
154
151
|
messages we want to react to. We didn't have to specify the message
|
|
155
|
-
type because plugins default to
|
|
152
|
+
type because plugins default to [`:message`](events.md#message).
|
|
156
153
|
|
|
157
154
|
We then define a method called `execute`, which is pretty much the
|
|
158
155
|
same as blocks are to on-handlers. And from here on, everything is the
|
|
159
156
|
same.
|
|
160
157
|
|
|
161
|
-
The only thing left to do is to tell
|
|
162
|
-
adding it to
|
|
158
|
+
The only thing left to do is to tell IRCinch to use our plugin, by
|
|
159
|
+
adding it to [`c.plugins.plugins`](bot_options.md#pluginsplugins).
|
|
163
160
|
|
|
164
161
|
One important thing remains to note: Plugins have a
|
|
165
|
-
|
|
166
|
-
that gets
|
|
167
|
-
prefix is `/^!/`.
|
|
168
|
-
|
|
169
|
-
be configured on a per-plugin or global basis, but that's not in the
|
|
170
|
-
scope of this document.
|
|
162
|
+
[`prefix`](bot_options.md#pluginsprefix), a string (or pattern)
|
|
163
|
+
that gets prepended to all patterns you define, and by default this
|
|
164
|
+
prefix is `/^!/`. Send `!hello` to invoke the plugin. The [plugin guide](plugins.md#matching-messages)
|
|
165
|
+
shows how to change the prefix for a bot, plugin, or matcher.
|
|
171
166
|
|
|
172
|
-
|
|
167
|
+
## Final Words
|
|
173
168
|
|
|
174
|
-
This short guide only explains the basics of using
|
|
169
|
+
This short guide only explains the basics of using IRCinch, so that you
|
|
175
170
|
can get started as quickly as possible. For more advanced topics, you
|
|
176
171
|
will want to read the specific documents:
|
|
177
172
|
|
|
178
|
-
-
|
|
179
|
-
-
|
|
180
|
-
-
|
|
181
|
-
-
|
|
182
|
-
-
|
|
183
|
-
-
|
|
184
|
-
-
|
|
173
|
+
- [Plugins](plugins.md)
|
|
174
|
+
- [A list of all available bot options](bot_options.md)
|
|
175
|
+
- [A list of all available events](events.md)
|
|
176
|
+
- [Dealing with encodings](encodings.md)
|
|
177
|
+
- [Logging in IRCinch](logging.md)
|
|
178
|
+
- [A cookbook for common tasks](common_tasks.md)
|
|
179
|
+
- [A list of common mistakes and how to avoid them](common_mistakes.md)
|
data/docs/logging.md
CHANGED
|
@@ -1,90 +1,82 @@
|
|
|
1
|
-
#
|
|
2
|
-
# @markup kramdown
|
|
1
|
+
# Logging
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
## Using the logger
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
so on) or by directly interfacing with {Cinch::LoggerList}, which is
|
|
9
|
-
available via `@bot.loggers`.
|
|
5
|
+
Use `debug`, `info`, `warn`, `error`, and `fatal` in plugins and `on` handlers.
|
|
6
|
+
The helpers send messages to [`bot.loggers`](../lib/cinch/logger_list.rb).
|
|
10
7
|
|
|
11
|
-
|
|
8
|
+
```ruby
|
|
9
|
+
on :join do |m|
|
|
10
|
+
info "#{m.user} joined #{m.channel}"
|
|
11
|
+
end
|
|
12
|
+
```
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
include Cinch::Plugin
|
|
14
|
+
## Logger levels
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Logger levels
|
|
25
|
-
|
|
26
|
-
Cinch uses a priority-based logging system, using the types `:debug`,
|
|
27
|
-
`:log`, `:info`, `:warn`, `:error` and `:fatal`, each of them
|
|
28
|
-
displaying less information than the previous.
|
|
29
|
-
|
|
30
|
-
By default, the logging level to display is set to `:debug`, which
|
|
31
|
-
will include all possible kinds of log events, including the rather
|
|
32
|
-
verbose debug output caused by plugins.
|
|
16
|
+
From most verbose to least verbose, the levels are `:debug`, `:log`,
|
|
17
|
+
`:info`, `:warn`, `:error`, and `:fatal`. A logger emits messages at or above
|
|
18
|
+
its configured level. `:debug` is the initial level. `:log` still includes
|
|
19
|
+
raw incoming and outgoing IRC traffic. Use `:info` or higher to omit that
|
|
20
|
+
traffic from normal logging.
|
|
33
21
|
|
|
34
|
-
|
|
35
|
-
from there on, the levels are rather self-explanatory.
|
|
22
|
+
### Changing the level
|
|
36
23
|
|
|
37
|
-
|
|
24
|
+
The bot starts with one formatted logger on standard error. Assign `level`
|
|
25
|
+
on a logger to change its threshold. Assign `bot.loggers.level` to change
|
|
26
|
+
all loggers already in the list. Set the level of each logger when you add it.
|
|
38
27
|
|
|
39
|
-
|
|
28
|
+
```ruby
|
|
29
|
+
bot.loggers.first.level = :info
|
|
30
|
+
file = File.open("ircinch.log", "a")
|
|
31
|
+
bot.loggers << Cinch::Logger::FormattedLogger.new(file, level: :warn)
|
|
32
|
+
```
|
|
40
33
|
|
|
41
|
-
|
|
34
|
+
Keep the file open while the bot is using it and close it after the bot stops.
|
|
35
|
+
The [formatted logger](../lib/cinch/logger/formatted_logger.rb) adds timestamps
|
|
36
|
+
and uses color on a terminal. For incoming IRC lines in pisg's Zcbot format,
|
|
37
|
+
load `require "cinch/logger/zcbot_logger"` and add a
|
|
38
|
+
[`Cinch::Logger::ZcbotLogger`](../lib/cinch/logger/zcbot_logger.rb) to the list.
|
|
42
39
|
|
|
43
|
-
|
|
44
|
-
bot.loggers << Cinch::Logger::FormattedLogger.new(File.open("/tmp/log.log", "a"))
|
|
45
|
-
bot.loggers.level = :debug
|
|
46
|
-
bot.loggers.first.level = :info
|
|
40
|
+
## Log filtering
|
|
47
41
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
Add an object with a `filter(message, event)` method to `bot.loggers.filters`.
|
|
43
|
+
Return the message to keep it, a replacement string to modify it, or `nil`
|
|
44
|
+
to drop it. Filters run in order. Calls to `bot.loggers.exception` bypass them.
|
|
51
45
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
Log filters take a log message as input and return a new message. This
|
|
60
|
-
allows removing/masking out passwords or other undesired information.
|
|
61
|
-
Additionally, messages can be dropped entirely by returning nil.
|
|
46
|
+
```ruby
|
|
47
|
+
class AuthenticationFilter
|
|
48
|
+
def filter(message, event)
|
|
49
|
+
if event == :outgoing && message.match?(/\A(?:PASS|OPER|AUTHENTICATE)(?:\s|\z)/)
|
|
50
|
+
return "[outgoing authentication command redacted]"
|
|
51
|
+
end
|
|
62
52
|
|
|
63
|
-
|
|
64
|
-
|
|
53
|
+
message
|
|
54
|
+
end
|
|
55
|
+
end
|
|
65
56
|
|
|
66
|
-
|
|
57
|
+
bot.loggers.filters << AuthenticationFilter.new
|
|
58
|
+
```
|
|
67
59
|
|
|
68
|
-
|
|
60
|
+
This filter covers the listed outgoing commands. Other events, including
|
|
61
|
+
handler errors and failed writes, can still contain sensitive text. Avoid
|
|
62
|
+
logging secrets from application code.
|
|
69
63
|
|
|
70
|
-
|
|
71
|
-
def initialize(bot)
|
|
72
|
-
@bot = bot
|
|
73
|
-
end
|
|
64
|
+
## Writing your own logger
|
|
74
65
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
66
|
+
Subclass [`Cinch::Logger`](../lib/cinch/logger.rb) and override private
|
|
67
|
+
formatters such as `format_general(message)` for all messages or
|
|
68
|
+
`format_info(message)` for a particular event. The base class applies the
|
|
69
|
+
level threshold, serializes writes, and sends the result to the supplied
|
|
70
|
+
output object. An event-specific formatter can return `nil` to skip a message.
|
|
79
71
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
{Cinch::Message} class to parse the message and only operate on the
|
|
84
|
-
actual message component, not channel names and similar. How fancy
|
|
85
|
-
your filtering needs to be depends on you.
|
|
72
|
+
```ruby
|
|
73
|
+
class ServiceLogger < Cinch::Logger
|
|
74
|
+
private
|
|
86
75
|
|
|
87
|
-
|
|
76
|
+
def format_info(message)
|
|
77
|
+
"[service] #{message}"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
88
80
|
|
|
89
|
-
|
|
90
|
-
|
|
81
|
+
bot.loggers << ServiceLogger.new($stderr, level: :info)
|
|
82
|
+
```
|