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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.standard.yml +1 -1
  3. data/CHANGELOG.md +10 -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/message_queue.rb +2 -2
  48. data/lib/cinch/plugin.rb +1 -1
  49. data/lib/cinch/version.rb +1 -1
  50. metadata +270 -249
  51. data/lib/cinch/rubyext/float.rb +0 -5
@@ -1,60 +1,69 @@
1
- # @title Common mistakes
2
- # @markup kramdown
1
+ # Common Mistakes
3
2
 
4
- # I defined an initialize method in my plugin and now it doesn't work properly anymore
3
+ ## I defined an initialize method in my plugin and now it doesn't work properly anymore
5
4
 
6
- Cinch requires plugins to set certain states for them to work
7
- properly. This is done by the initialize method of the Plugin module.
8
- If you define your own initializer, make sure to accept an arbitrary
9
- number of arguments (`*args`) and to call `super` as soon as possible.
5
+ IRCinch passes the bot to the plugin initializer. Call `super` with that
6
+ argument so `Cinch::Plugin` can initialize the instance and register its
7
+ handlers.
10
8
 
11
- ## Example
9
+ ### Example
12
10
 
13
- class MyPlugin
14
- include Cinch::Plugin
11
+ ```ruby
12
+ class MyPlugin
13
+ include Cinch::Plugin
15
14
 
16
- def initialize(*args)
17
- super
18
- my own stuff here
19
- end
20
- end
15
+ def initialize(bot)
16
+ super
17
+ @seen = {}
18
+ end
19
+ end
20
+ ```
21
21
 
22
+ ## I am trying to use plugin options, but Ruby says it cannot find the `config` method
22
23
 
23
- # I am trying to use plugin options, but Ruby says it cannot find the `config` method
24
+ `config` belongs to the plugin instance. Class-level declarations run before
25
+ the class is registered with a bot, so `match(config[:pattern])` raises.
26
+ Define a static matcher and check the configured value in the handler, as
27
+ shown in [plugin configuration](plugins.md#configuration).
24
28
 
25
- A common mistake is to try and use plugin options on class level, for example to manipulate matches. This cannot work because the class itself is not connected to any bot. Plugin options only work in instance methods of plugins.
29
+ ## My handlers won't run, even though the regular expression matches
26
30
 
27
- The following is not possible:
31
+ IRCinch plugins have a default prefix (`/^!/`). This allows for flexible prefixes
32
+ across the whole bot. The default prefix can be changed in `Bot#configure`:
28
33
 
29
- class MyPlugin
30
- include Cinch::Plugin
34
+ ```ruby
35
+ bot = Cinch::Bot.new do
36
+ configure do |c|
37
+ # ...
38
+ c.plugins.prefix = /^%/
39
+ end
40
+ end
41
+ ```
31
42
 
32
- match(config[:pattern])
33
- def execute(m)
34
- # ...
35
- end
36
- end
43
+ Alternatively, you can set the prefix for specific plugins only, by calling `set`:
37
44
 
38
- # My handlers won't run, even though the Regexp matches.
45
+ ```ruby
46
+ class MyPlugin
47
+ include Cinch::Plugin
39
48
 
40
- Cinch plugins have a default prefix (`/^!/`). This allows for flexible prefixes
41
- across the whole bot. The default prefix can be changed in `Bot#configure`:
49
+ set :prefix, /^%/
50
+ end
51
+ ```
42
52
 
43
- x = Cinch::Bot.new do
44
- configure do |c|
45
- # ...
46
- c.plugins.prefix = /^%/
47
- end
48
- end
53
+ You can also choose to not use a prefix for specific matchers:
49
54
 
50
- Alternatively, you can set the prefix for specific plugins only, by calling `set`:
55
+ ```ruby
56
+ match /hi/, use_prefix: false
57
+ ```
51
58
 
52
- class MyPlygin
53
- include Cinch::Plugin
59
+ ## My bot hasn't joined a channel when `:connect` runs
54
60
 
55
- set :prefix, /^%/
56
- end
61
+ `:connect` signals IRC registration. The channel joins happen afterward.
62
+ Listen for `:join` and check `m.user == bot` before relying on membership.
63
+ See [`:connect`](events.md#connect) and [`delay_joins`](bot_options.md#delay_joins).
57
64
 
58
- You can also choose to not use a prefix for specific matchers:
65
+ ## Setting `c.default_logger_level` didn't change my log output
59
66
 
60
- match /hi/, use_prefix: false
67
+ The default logger already exists when the configuration block runs. Set
68
+ `bot.loggers.first.level = :info` before `bot.start` to change its level.
69
+ See [Logging](logging.md#changing-the-level).
data/docs/common_tasks.md CHANGED
@@ -1,57 +1,78 @@
1
- # @title Common Tasks
2
- # @markup kramdown
1
+ # Common Tasks
3
2
 
4
- # Checking if a user is online
3
+ ## Checking if a user is online
5
4
 
6
- Cinch by itself tries to keep track of the online state of users.
7
- Whenever it sees someone speak, change their nick or be in a channel the
8
- bot is also in, it'll set the user to being online. And when a user
9
- quits, gets killed or cannot be whoised/contacted, its state will be
10
- set to offline.
5
+ IRCinch tracks presence from messages it receives. That state can get stale
6
+ when the bot stops sharing channels with a user. Call
7
+ [`User(nick).monitor`](../lib/cinch/user.rb) to use the server's `MONITOR`
8
+ support or poll with `WHOIS` every 30 seconds. Call `unmonitor` to stop.
11
9
 
12
- A problem with that information is that it can quickly become out of
13
- sync. Consider the following example:
10
+ The `:online` and `:offline` handlers receive `nil` and then the user.
11
+ Set up monitoring once per connection, since server subscriptions are
12
+ connection-specific. This example guards against repeated `:connect`
13
+ events on the same connection.
14
14
 
15
- The bot joins a channel, sees someone in the name list and thus marks
16
- him as online. The bot then leaves the channel and at some later
17
- point, the user disconnects. The bot won't know that and still track
18
- the user as online.
15
+ ```ruby
16
+ class Presence
17
+ include Cinch::Plugin
19
18
 
20
- If (near-)realtime information about this state is required, one can
21
- use {Cinch::User#monitor} to automatically monitor the state.
22
- {Cinch::User#monitor #monitor} uses either the _MONITOR_ feature of modern IRCds or, if
23
- that's not available, periodically runs _WHOIS_ to update the
24
- information.
19
+ listen_to :connect, method: :on_connect
20
+ listen_to :online, method: :on_online
21
+ listen_to :offline, method: :on_offline
25
22
 
26
- Whenever a user's state changes, either the `:online` or the
27
- `:offline` event will be fired, as can be seen in the following
28
- example:
23
+ def on_connect(_message)
24
+ synchronize(:presence_monitor) do
25
+ return if @monitored_connection.equal?(bot.irc)
29
26
 
30
- class SomePlugin
31
- include Cinch::Plugin
27
+ User("example_user").monitor
28
+ @monitored_connection = bot.irc
29
+ end
30
+ end
32
31
 
33
- listen_to :connect, method: :on_connect
34
- listen_to :online, method: :on_online
35
- listen_to :offline, method: :on_offline
32
+ def on_online(_message, user)
33
+ info "#{user} is online"
34
+ end
36
35
 
37
- def on_connect(m)
38
- User("my_master").monitor
39
- end
36
+ def on_offline(_message, user)
37
+ info "#{user} is offline"
38
+ end
39
+ end
40
+ ```
40
41
 
41
- def on_online(m, user)
42
- user.send "Hello master"
43
- end
42
+ ## Sending messages to users and channels beside `m.user` and `m.channel`
44
43
 
45
- def on_offline(m, user)
46
- @bot.loggers.info "I miss my master :("
47
- end
48
- end
44
+ Use the [`User`, `Channel`, and `Target` helpers](../lib/cinch/helpers.rb)
45
+ inside a plugin or `on` handler to look up another recipient.
46
+
47
+ ```ruby
48
+ User("example_user").send("Hello!")
49
+ Channel("#example").send("Hello, channel!")
50
+ ```
51
+
52
+ Use `m.reply` to answer the current conversation. If the message came from a
53
+ channel, `m.reply(text, true)` prefixes the sender's nickname. A reply to a
54
+ `STATUSMSG` uses the same channel audience. Use `safe_reply` or `safe_send`
55
+ for text from users or feeds to strip nonprinting characters.
56
+
57
+ ## Formatting messages
49
58
 
50
- # Sending messages to users and channels beside `m.user` and `m.channel`
59
+ Use `Format` with a style or color followed by the text. For example,
60
+ `m.reply(Format(:bold, "Ready"))` sends bold text to clients that support IRC
61
+ formatting. Use `Unformat(text)` to remove IRC formatting codes or
62
+ `Sanitize(text)` to remove nonprintable characters. See the
63
+ [formatting options](../lib/cinch/formatting.rb) and [helper methods](../lib/cinch/helpers.rb).
51
64
 
52
- Cinch provides {Cinch::Helpers helper methods} to get instances of Channel
53
- and User objects that you can work with:
65
+ ## DCC file transfers
54
66
 
55
- User('user').send("Hello!") # Sends a message to user 'user'
56
- Channel('#channel').send("Hello!") # Sends a message to channel '#channel'
67
+ DCC sends the offer over IRC and transfers file data directly between clients.
68
+ Open an outgoing file in binary mode and pass it to
69
+ [`User(nick).dcc_send(io)`](../lib/cinch/user.rb) from a handler. The call blocks
70
+ while waiting for the recipient and transferring the data. The advertised
71
+ address can be configured with [`dcc.own_ip`](bot_options.md#dccown_ip).
57
72
 
73
+ For incoming offers, listen to [`:dcc_send`](events.md#dcc_send). The offer
74
+ exposes `user`, `filename`, `size`, `ip`, and `port`. Calling `accept(output)`
75
+ connects to the offered address and writes to an object that supports `<<`.
76
+ Review the sender and destination before accepting an offer. Choose the local
77
+ output path yourself and enforce your own transfer limits. See the
78
+ [incoming transfer API](../lib/cinch/dcc/incoming/send.rb).
data/docs/encodings.md CHANGED
@@ -1,69 +1,42 @@
1
- # @title Encodings
2
- # @markup kramdown
3
-
4
1
  # Encodings
5
2
 
6
- The IRC protocol doesn't define a specific encoding that should be
7
- used, nor does it provide any information on which encodings _are_
8
- being used.
9
-
10
- At the same time, lots of different encodings have become popular on
11
- IRC. This presents a big problem, because, if you're using a different
12
- encoding than someone else on IRC, you'll receive their text as
13
- garbage.
14
-
15
- Cinch tries to work around this issue in two ways, while also keeping
16
- the usual Ruby behaviour.
3
+ IRC messages carry bytes without an encoding label. Configure
4
+ [`c.encoding`](bot_options.md#encoding) to control how IRCinch decodes
5
+ incoming bytes and encodes outgoing text.
17
6
 
18
7
  ## The `encoding` option
19
- By setting {file:docs/bot_options.md#encoding the `encoding` option}, you
20
- set your expectations on what encoding other users will use. Allowed
21
- values are instances of Encoding, names of valid encodings (as
22
- strings) and the special `:irc` encoding, which will be explained
23
- further down.
24
8
 
9
+ Use an encoding name such as `"UTF-8"`, a Ruby `Encoding` object such as
10
+ `Encoding::UTF_8`, or the special value `:irc` (the default).
25
11
 
26
- ## Encoding.default_internal
27
- If set, Cinch will automatically convert incoming messages to the
28
- encoding defined by `Encoding.default_internal`, unless the special
29
- encoding `:irc` is being used as the {file:docs/bot_options.md#encoding
30
- `encoding option`}
12
+ ```ruby
13
+ bot.configure do |c|
14
+ c.encoding = Encoding::UTF_8
15
+ end
16
+ ```
31
17
 
32
- ## The `:irc` encoding
33
- As mentioned earlier, people couldn't decide on a single encoding to
34
- use. As such, specifying a single encoding would most likely lead to
35
- problems, especially if the bot is in more than one channel.
18
+ An explicit encoding applies in both directions. For example, use `"Windows-1252"`
19
+ for a network that expects that encoding on the wire.
36
20
 
37
- Luckily, even though people cannot decide on a single encoding,
38
- western countries usually either use CP1252 (Windows Latin-1) or
39
- UTF-8. Since text encoded in CP1252 fails validation as UTF-8, it is
40
- easy to tell the two apart. Additionally it is possible to losslessly
41
- re-encode CP1252 in UTF-8 and as such, a small subset of UTF-8 is also
42
- representable in CP1252.
21
+ ## Encoding.default_internal
43
22
 
44
- If incoming text is valid UTF-8, it will be interpreted as such. If it
45
- fails validation, a CP1252 → UTF-8 conversion is performed. This
46
- ensures that you will always deal with UTF-8 in your code, even if
47
- other people use CP1252. Note, however, that we ignore
48
- `Encoding.default_internal` in this case and always present you with
49
- UTF-8.
23
+ For explicit encodings, IRCinch passes incoming strings through Ruby's
24
+ `String#encode!`, which converts to `Encoding.default_internal` when it is
25
+ set. This Ruby setting is process-wide. `:irc` always returns incoming text
26
+ as UTF-8, regardless of `Encoding.default_internal`.
50
27
 
51
- If text you send contains only characters that fit inside the
52
- CP1252 code page, the entire line will be sent that way.
28
+ ## The `:irc` encoding
53
29
 
54
- If the text doesn't fit inside the CP1252 code page, (for example if
55
- you type Eastern European characters, or Russian) it will be sent as
56
- UTF-8. Only UTF-8 capable clients will be able to see these characters
57
- correctly.
30
+ - **Incoming** Keep valid UTF-8. If the byte sequence is invalid UTF-8,
31
+ decode the whole line as Windows-1252 (CP1252) and convert it to UTF-8.
32
+ - **Outgoing** Encode as UTF-8, including text that could fit in CP1252.
58
33
 
59
34
  ## Invalid bytes and unsupported translations
60
- If Cinch receives text in an encoding other than the one assumed, it
61
- can happen that the message contains bytes that are not valid in the
62
- assumed encoding. Instead of dropping the complete message, Cinch will
63
- replace offending bytes with question marks.
64
35
 
65
- Also, if you expect messages in e.g. UTF-8 but re-encode them in
66
- CP1252 (by setting `Encoding.default_internal` to CP1252), it can
67
- happen that some characters cannot be represented in CP1252. In such a
68
- case, Cinch will too replace the offending characters with question
69
- marks.
36
+ IRCinch asks Ruby to replace invalid bytes or characters that cannot be
37
+ converted when it transcodes. The replacement depends on the destination
38
+ encoding. For an explicit incoming encoding, IRCinch discards characters
39
+ that are still invalid after the conversion step.
40
+
41
+ See the [encoding implementation](../lib/cinch/utilities/encoding.rb) and
42
+ [encoding tests](../test/lib/cinch/utilities/encoding.rb).