ircinch 2.4.4 → 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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.standard.yml +1 -1
  3. data/CHANGELOG.md +13 -0
  4. data/CODE_OF_CONDUCT.md +7 -81
  5. data/README.md +39 -30
  6. data/docs/bot_options.md +207 -314
  7. data/docs/changes.md +293 -282
  8. data/docs/common_mistakes.md +50 -41
  9. data/docs/common_tasks.md +63 -42
  10. data/docs/encodings.md +28 -55
  11. data/docs/events.md +123 -131
  12. data/docs/getting_started.md +97 -102
  13. data/docs/logging.md +62 -70
  14. data/docs/migrating.md +145 -131
  15. data/docs/plugins.md +109 -3
  16. data/docs/readme.md +17 -15
  17. data/examples/README.md +78 -0
  18. data/examples/basic/autovoice.rb +14 -18
  19. data/examples/basic/google.rb +9 -22
  20. data/examples/basic/hello.rb +4 -3
  21. data/examples/basic/join_part.rb +8 -23
  22. data/examples/basic/memo.rb +26 -24
  23. data/examples/basic/msg.rb +6 -6
  24. data/examples/basic/seen.rb +17 -24
  25. data/examples/basic/urban_dict.rb +6 -26
  26. data/examples/basic/url_shorten.rb +8 -26
  27. data/examples/plugins/autovoice.rb +17 -16
  28. data/examples/plugins/custom_prefix.rb +4 -3
  29. data/examples/plugins/dice_roll.rb +13 -20
  30. data/examples/plugins/google.rb +7 -21
  31. data/examples/plugins/hello.rb +4 -3
  32. data/examples/plugins/hooks.rb +5 -6
  33. data/examples/plugins/join_part.rb +8 -21
  34. data/examples/plugins/lambdas.rb +5 -6
  35. data/examples/plugins/last_nick.rb +7 -6
  36. data/examples/plugins/msg.rb +6 -6
  37. data/examples/plugins/multiple_matches.rb +10 -11
  38. data/examples/plugins/own_events.rb +14 -19
  39. data/examples/plugins/seen.rb +23 -23
  40. data/examples/plugins/timer.rb +5 -6
  41. data/examples/plugins/url_shorten.rb +15 -19
  42. data/examples/settings.rb +19 -0
  43. data/examples/tiny_url.rb +41 -0
  44. data/ircinch.gemspec +5 -6
  45. data/lib/cinch/bot.rb +2 -3
  46. data/lib/cinch/channel.rb +4 -4
  47. data/lib/cinch/irc.rb +2 -2
  48. data/lib/cinch/message_queue.rb +2 -2
  49. data/lib/cinch/plugin.rb +1 -1
  50. data/lib/cinch/version.rb +1 -1
  51. metadata +270 -249
  52. data/lib/cinch/rubyext/float.rb +0 -5
@@ -1,14 +1,17 @@
1
- # @title Getting Started
2
- # @markup kramdown
3
-
4
1
  # Getting Started
5
2
 
6
- This short guide will show you how to easily and quickly write your
7
- own IRC bot with Cinch.
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
- # What Cinch really is
12
+ ## What IRCinch really is
10
13
 
11
- First and foremost, it is important to understand that Cinch is more
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 Cinch requires you to be familiar
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
- Channel("#cinch").users.each do |user, modes|
24
- user.send "I am watching you!"
25
- end
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
- # Hello, World
39
+ ## Hello, World
36
40
 
37
41
  The following will describe one of the most basic IRC bots you can
38
- write in Cinch: One that joins a specific channel and responds to
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
- require "cinch"
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
- bot = Cinch::Bot.new do
45
- configure do |c|
46
- c.server = "irc.freenode.net"
47
- c.channels = ["#cinch-bots"]
48
- end
55
+ on :message, "hello" do |m|
56
+ m.reply "Hello, World"
57
+ end
58
+ end
49
59
 
50
- on :message, "hello" do |m|
51
- m.reply "Hello, World"
52
- end
53
- end
60
+ bot.start
61
+ ```
54
62
 
55
- bot.start
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
- Note that this is the entire file and all you need for the basic bot
59
- to work. Save the above example to a file and run it with Ruby.
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
- ({file:docs/bot_options.md#nick c.nick}). For an overview of all
70
- available options, see {file:docs/bot_options.md the list of options}.
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
- {Cinch::Bot#on on} expects two arguments: The kind of message to react
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
- {file:docs/events.md#message :message}, which means that the bot will
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
- {file:docs/events.md the list of events}.
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
- on :message, /^\d{4}$/ do |m|
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
- will match all messages that consist of exactly four digits and
90
- nothing else.
88
+ ```ruby
89
+ on :message, /\Ayear (\d{4})\z/ do |m, year|
90
+ m.reply "You entered #{year}"
91
+ end
92
+ ```
91
93
 
92
- Whenever a message matches the handler we just defined, the block we
93
- provided will be called, with the message object, and optionally
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
- {Cinch::Message#reply reply} method, an easy way of responding to a
99
- message. If the message was sent to a channel, {Cinch::Message#reply
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 then use exactly that {Cinch::Message#reply reply} method to send back "Hello, World"
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
- # on-handlers vs. plugins
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 {Cinch::Plugin specific module} to get
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
- require "cinch"
119
+ ```ruby
120
+ require "ircinch"
121
121
 
122
- class HelloWorld
123
- include Cinch::Plugin
122
+ class HelloWorld
123
+ include Cinch::Plugin
124
124
 
125
- match "hello"
126
- def execute(m)
127
- m.reply "Hello, World"
128
- end
129
- end
125
+ match "hello"
126
+ def execute(m)
127
+ m.reply "Hello, World"
128
+ end
129
+ end
130
130
 
131
- bot = Cinch::Bot.new do
132
- configure do |c|
133
- c.server = "irc.freenode.net"
134
- c.channels = ["#cinch-bots"]
135
- c.plugins.plugins = [HelloWorld]
136
- end
137
- end
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
- bot.start
140
+ bot.start
141
+ ```
140
142
 
141
- The first thing to notice is that we wrote a new class called
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
- First thing after defining a new class is to include {Cinch::Plugin} –
149
- This module will provide methods like
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 {file:docs/events.md#message :message}.
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 Cinch to use our plugin, by
162
- adding it to {file:docs/bot_options.md#pluginsplugins c.plugins.plugins}.
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
- {file:docs/bot_options.md#pluginsprefix prefix}, a string (or pattern)
166
- that gets appended to all patterns you define, and by default this
167
- prefix is `/^!/`. This means that in order to invoke our HelloWorld
168
- plugin, a user has to say "!hello" instead of "hello". This prefix can
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
- # Final Words
167
+ ## Final Words
173
168
 
174
- This short guide only explains the basics of using Cinch, so that you
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
- - {file:docs/plugins.md Plugins}
179
- - {file:docs/bot_options.md A list of all available bot options}
180
- - {file:docs/events.md A list of all available events}
181
- - {file:docs/encodings.md Dealing with encodings}
182
- - {file:docs/logging.md Logging in Cinch}
183
- - {file:docs/common_tasks.md A cookbook for common tasks}
184
- - {file:docs/common_mistakes.md A list of common mistakes and how to avoid them}
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
- # @title Logging
2
- # @markup kramdown
1
+ # Logging
3
2
 
4
- # Using the logger
3
+ ## Using the logger
5
4
 
6
- Plugins can use the logging facility for logging their own messages,
7
- either by using the logging related helper methods (#debug, #info, and
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
- Example:
8
+ ```ruby
9
+ on :join do |m|
10
+ info "#{m.user} joined #{m.channel}"
11
+ end
12
+ ```
12
13
 
13
- class MyPlugin
14
- include Cinch::Plugin
14
+ ## Logger levels
15
15
 
16
- match "foo"
17
- def execute(m)
18
- debug "Starting handler..."
19
- info "Some more important information"
20
- debug "Done."
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
- `:log` will hide debug output but still contain the raw IRC log and
35
- from there on, the levels are rather self-explanatory.
22
+ ### Changing the level
36
23
 
37
- ## Changing the level
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
- The level can be changed for single loggers or all loggers at once, by either using {Cinch::Logger#level=} or {Cinch::LoggerList#level=} respectively.
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
- Example:
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
- bot = Cinch::Bot.new { }
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
- This will set all loggers to the `:debug` level (which actually is the
49
- default already) and the first logger (which is the default STDOUT
50
- logger) to `:info`.
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
- # Log filtering
53
-
54
- Sometimes it is undesirable to log a message unchanged. For example
55
- when identifying to the network, passwords might be sent in plain
56
- text. To prevent such information from appearing in logs, {Cinch::LogFilter log filters}
57
- can be employed.
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
- It is possible to use more than one filter, in which case they will be
64
- called in order, each acting on the previous filter's output.
53
+ message
54
+ end
55
+ end
65
56
 
66
- Filters can be installed by adding them to {Cinch::LoggerList#filters}.
57
+ bot.loggers.filters << AuthenticationFilter.new
58
+ ```
67
59
 
68
- An example (and very simple) password filter might look like this:
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
- class PasswordFilter
71
- def initialize(bot)
72
- @bot = bot
73
- end
64
+ ## Writing your own logger
74
65
 
75
- def filter(message, event)
76
- message.gsub(@bot.config.password, "*" * @bot.config.password.size)
77
- end
78
- end
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
- This filter will replace the password in all log messages (except for
81
- exceptions). It could further discriminate by looking at `event` and
82
- only modify outgoing IRC messages. It could also use the
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
- # Writing your own logger
76
+ def format_info(message)
77
+ "[service] #{message}"
78
+ end
79
+ end
88
80
 
89
- This section will follow soon. For now just look at the code of
90
- already implemented loggers.
81
+ bot.loggers << ServiceLogger.new($stderr, level: :info)
82
+ ```