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,19 +1,19 @@
1
- require "ircinch"
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../settings"
2
4
 
3
5
  class Messenger
4
6
  include Cinch::Plugin
5
7
 
6
- match(/msg (.+?) (.+)/)
8
+ match(/msg ([^\s,#&]+) (.+)\z/)
7
9
  def execute(m, receiver, message)
8
- User(receiver).send(message)
10
+ User(receiver).safe_send(message) if ExampleSettings.admin?(m)
9
11
  end
10
12
  end
11
13
 
12
14
  bot = Cinch::Bot.new do
13
15
  configure do |c|
14
- c.server = "irc.libera.chat"
15
- c.nick = "IrCinchBot"
16
- c.channels = ["#ircinch-bots"]
16
+ ExampleSettings.configure(c, "irc_pmsg")
17
17
  c.plugins.plugins = [Messenger]
18
18
  end
19
19
  end
@@ -1,31 +1,30 @@
1
- require "ircinch"
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../settings"
2
4
 
3
5
  class MultiCommands
4
6
  include Cinch::Plugin
5
7
 
6
- match(/command1 (.+)/, method: :command1)
7
- match(/command2 (.+)/, method: :command2)
8
- match(/^command3 (.+)/, use_prefix: false)
8
+ match(/command1 (.+)\z/, method: :command1)
9
+ match(/command2 (.+)\z/, method: :command2)
10
+ match(/\Acommand3 (.+)\z/, use_prefix: false)
9
11
 
10
12
  def command1(m, arg)
11
- m.reply "command1, arg: #{arg}"
13
+ m.safe_reply "command1, arg: #{arg}"
12
14
  end
13
15
 
14
16
  def command2(m, arg)
15
- m.reply "command2, arg: #{arg}"
17
+ m.safe_reply "command2, arg: #{arg}"
16
18
  end
17
19
 
18
20
  def execute(m, arg)
19
- m.reply "command3, arg: #{arg}"
21
+ m.safe_reply "command3, arg: #{arg}"
20
22
  end
21
23
  end
22
24
 
23
25
  bot = Cinch::Bot.new do
24
26
  configure do |c|
25
- c.nick = "ircinch_multi"
26
- c.server = "irc.libera.chat"
27
- c.channels = ["#ircinch-bots"]
28
- c.verbose = true
27
+ ExampleSettings.configure(c, "irc_multi")
29
28
  c.plugins.plugins = [MultiCommands]
30
29
  end
31
30
  end
@@ -1,37 +1,32 @@
1
- require "ircinch"
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../settings"
2
4
 
3
5
  class RandomNumberGenerator
4
- def initialize(bot)
5
- @bot = bot
6
- end
6
+ include Cinch::Plugin
7
7
 
8
- def start
9
- loop do
10
- sleep 5 # pretend that we are waiting for some kind of entropy
11
- @bot.handlers.dispatch(:random_number, nil, Kernel.rand)
12
- end
8
+ timer 5, method: :publish
9
+
10
+ def publish
11
+ bot.handlers.dispatch(:random_number, nil, rand)
13
12
  end
14
13
  end
15
14
 
16
- class DoSomethingRandom
15
+ class RandomNumberReporter
17
16
  include Cinch::Plugin
18
17
 
19
18
  listen_to :random_number
20
- def listen(m, number)
21
- Channel("#ircinch-bots").send "I got a random number: #{number}"
19
+
20
+ def listen(_message, number)
21
+ bot.channels.first&.send("I got a random number: #{number}")
22
22
  end
23
23
  end
24
24
 
25
25
  bot = Cinch::Bot.new do
26
26
  configure do |c|
27
- c.nick = "ircinch_events"
28
- c.server = "irc.libera.chat"
29
- c.channels = ["#ircinch-bots"]
30
- c.verbose = true
31
- c.plugins.plugins = [DoSomethingRandom]
27
+ ExampleSettings.configure(c, "irc_event")
28
+ c.plugins.plugins = [RandomNumberGenerator, RandomNumberReporter]
32
29
  end
33
30
  end
34
31
 
35
- Thread.new { RandomNumberGenerator.new(bot).start }
36
-
37
32
  bot.start
@@ -1,43 +1,43 @@
1
- require "ircinch"
1
+ # frozen_string_literal: true
2
2
 
3
- class Seen
4
- class SeenStruct < Struct.new(:who, :where, :what, :time)
5
- def to_s
6
- "[#{time.asctime}] #{who} was seen in #{where} saying #{what}"
7
- end
8
- end
3
+ require_relative "../settings"
9
4
 
5
+ class Seen
10
6
  include Cinch::Plugin
11
7
 
12
- listen_to :channel
13
- match(/seen (.+)/)
8
+ listen_to :message
9
+ match(/seen (\S+)\z/)
14
10
 
15
- def initialize(*args)
11
+ def initialize(bot)
16
12
  super
17
- @users = {}
13
+ @last_messages = {}
18
14
  end
19
15
 
20
16
  def listen(m)
21
- @users[m.user.nick] = SeenStruct.new(m.user, m.channel, m.message, Time.now)
17
+ return unless m.channel && m.user && !m.ctcp? && !m.statusmsg_mode
18
+
19
+ nick = m.user.nick
20
+ entry = "[#{m.time.asctime}] #{nick} said #{m.message}"
21
+ synchronize(:seen) { @last_messages[seen_key(m.channel, nick)] = entry }
22
22
  end
23
23
 
24
24
  def execute(m, nick)
25
- if nick == @bot.nick
26
- m.reply "That's me!"
27
- elsif nick == m.user.nick
28
- m.reply "That's you!"
29
- elsif @users.key?(nick)
30
- m.reply @users[nick].to_s
31
- else
32
- m.reply "I haven't seen #{nick}"
33
- end
25
+ return unless m.channel
26
+
27
+ entry = synchronize(:seen) { @last_messages[seen_key(m.channel, nick)] }
28
+ m.safe_reply(entry || "I haven't seen #{nick} in this channel.")
29
+ end
30
+
31
+ private
32
+
33
+ def seen_key(channel, nick)
34
+ [channel, nick.irc_downcase(bot.irc.isupport["CASEMAPPING"])]
34
35
  end
35
36
  end
36
37
 
37
38
  bot = Cinch::Bot.new do
38
39
  configure do |c|
39
- c.server = "irc.libera.chat"
40
- c.channels = ["#ircinch-bots"]
40
+ ExampleSettings.configure(c, "irc_pseen")
41
41
  c.plugins.plugins = [Seen]
42
42
  end
43
43
  end
@@ -1,20 +1,19 @@
1
- require "ircinch"
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../settings"
2
4
 
3
5
  class TimedPlugin
4
6
  include Cinch::Plugin
5
7
 
6
8
  timer 5, method: :timed
7
9
  def timed
8
- Channel("#ircinch-bots").send "5 seconds have passed"
10
+ bot.channels.first&.send("5 seconds have passed")
9
11
  end
10
12
  end
11
13
 
12
14
  bot = Cinch::Bot.new do
13
15
  configure do |c|
14
- c.nick = "ircinch_timer"
15
- c.server = "irc.libera.chat"
16
- c.channels = ["#ircinch-bots"]
17
- c.verbose = true
16
+ ExampleSettings.configure(c, "irc_timer")
18
17
  c.plugins.plugins = [TimedPlugin]
19
18
  end
20
19
  end
@@ -1,33 +1,29 @@
1
- require "cgi"
2
- require "ircinch"
3
- require "open-uri"
1
+ # frozen_string_literal: true
4
2
 
5
- class TinyURL
3
+ require_relative "../settings"
4
+ require_relative "../tiny_url"
5
+
6
+ class URLShortener
6
7
  include Cinch::Plugin
7
8
 
8
- listen_to :channel
9
+ set :required_options, [:token]
10
+ match(/shorten (\S+)\z/)
9
11
 
10
- def shorten(url)
11
- url = URI.parse("http://tinyurl.com/api-create.php?url=#{CGI.escape(url)}").open
12
- (url == "Error") ? nil : url
13
- rescue OpenURI::HTTPError
14
- nil
12
+ def initialize(bot)
13
+ super
14
+ @shortener = TinyURLClient.new(config.fetch(:token))
15
15
  end
16
16
 
17
- def listen(m)
18
- urls = URI.extract(m.message, "http")
19
- short_urls = urls.map { |url| shorten(url) }.compact
20
- unless short_urls.empty?
21
- m.reply short_urls.join(", ")
22
- end
17
+ def execute(m, url)
18
+ m.safe_reply(@shortener.shorten(url))
23
19
  end
24
20
  end
25
21
 
26
22
  bot = Cinch::Bot.new do
27
23
  configure do |c|
28
- c.server = "irc.libera.chat"
29
- c.channels = ["#ircinch-bots"]
30
- c.plugins.plugins = [TinyURL]
24
+ ExampleSettings.configure(c, "irc_purl")
25
+ c.plugins.plugins = [URLShortener]
26
+ c.plugins.options[URLShortener] = {token: ENV.fetch("TINYURL_TOKEN")}
31
27
  end
32
28
  end
33
29
 
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ircinch"
4
+
5
+ module ExampleSettings
6
+ def self.configure(config, nick)
7
+ config.server = ENV.fetch("IRC_SERVER", "localhost")
8
+ config.ssl.use = ENV["IRC_TLS"] == "1"
9
+ config.ssl.verify = config.ssl.use
10
+ config.port = Integer(ENV.fetch("IRC_PORT", config.ssl.use ? "6697" : "6667"), 10)
11
+ config.nick = ENV.fetch("IRC_NICK", nick)
12
+ config.channels = [ENV.fetch("IRC_CHANNEL", "#ircinch-bots")]
13
+ end
14
+
15
+ def self.admin?(message)
16
+ admin = ENV["IRC_ADMIN_PREFIX"]
17
+ admin && !admin.empty? && message.user && message.prefix == admin
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "openssl"
6
+ require "uri"
7
+
8
+ class TinyURLClient
9
+ ENDPOINT = URI("https://api.tinyurl.com/create")
10
+
11
+ def initialize(token)
12
+ @token = token
13
+ end
14
+
15
+ def shorten(url)
16
+ target = URI.parse(url)
17
+ return "Enter a valid HTTP or HTTPS URL." unless target.is_a?(URI::HTTP) && target.host && !target.userinfo
18
+
19
+ request = Net::HTTP::Post.new(ENDPOINT)
20
+ request["Authorization"] = "Bearer #{@token}"
21
+ request["Content-Type"] = "application/json"
22
+ request["Accept"] = "application/json"
23
+ request.body = JSON.generate(url: url)
24
+
25
+ response = Net::HTTP.start(ENDPOINT.host, ENDPOINT.port, use_ssl: true, open_timeout: 5, read_timeout: 5) do |http|
26
+ http.request(request)
27
+ end
28
+ if response.is_a?(Net::HTTPSuccess)
29
+ body = JSON.parse(response.body)
30
+ data = body["data"] if body.is_a?(Hash)
31
+ result = data["tiny_url"] if data.is_a?(Hash)
32
+ return result if result.is_a?(String) && !result.empty?
33
+ end
34
+
35
+ "TinyURL could not shorten that URL."
36
+ rescue URI::InvalidURIError
37
+ "Enter a valid HTTP or HTTPS URL."
38
+ rescue JSON::ParserError, IOError, SystemCallError, SocketError, Timeout::Error, OpenSSL::SSL::SSLError
39
+ "TinyURL is unavailable. Try again later."
40
+ end
41
+ end
data/ircinch.gemspec CHANGED
@@ -12,14 +12,13 @@ Gem::Specification.new do |spec|
12
12
  spec.description = "A simple, friendly Ruby DSL for creating IRC bots"
13
13
  spec.homepage = "https://github.com/ircinchrb/ircinch"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = ">= 3.2.0"
15
+ spec.required_ruby_version = ">= 3.3.0"
16
16
 
17
17
  spec.metadata = {
18
- "bug_tracker_uri" => "https://github.com/ircinchrb/ircinch/issues",
19
- "changelog_uri" => "https://github.com/ircinchrb/ircinch/blob/main/CHANGELOG.md",
18
+ "bug_tracker_uri" => "#{spec.homepage}/issues",
19
+ "changelog_uri" => "#{spec.homepage}/blob/main/CHANGELOG.md",
20
20
  "documentation_uri" => "https://rubydoc.info/gems/ircinch",
21
- "homepage_uri" => spec.homepage,
22
- "source_code_uri" => "https://github.com/ircinchrb/ircinch",
21
+ "source_code_uri" => spec.homepage,
23
22
  "rubygems_mfa_required" => "true"
24
23
  }
25
24
 
@@ -35,10 +34,10 @@ Gem::Specification.new do |spec|
35
34
  spec.require_paths = ["lib"]
36
35
 
37
36
  # Register runtime dependencies (gems required to run this gem)
37
+ spec.add_dependency "base64"
38
38
  spec.add_dependency "ostruct"
39
39
 
40
40
  # Register development dependencies (gems required to program this gem)
41
- spec.add_development_dependency "base64"
42
41
  spec.add_development_dependency "bundler-audit"
43
42
  spec.add_development_dependency "bundler-integrity"
44
43
  spec.add_development_dependency "irb"
data/lib/cinch/bot.rb CHANGED
@@ -5,7 +5,6 @@ require "socket"
5
5
 
6
6
  require_relative "rubyext/module"
7
7
  require_relative "rubyext/string"
8
- require_relative "rubyext/float"
9
8
 
10
9
  require_relative "exceptions"
11
10
 
@@ -285,8 +284,8 @@ module Cinch
285
284
  wait = 2**@reconnects
286
285
  wait = @config.max_reconnect_delay if wait > @config.max_reconnect_delay
287
286
  @loggers.info "Waiting #{wait} seconds before reconnecting"
288
- start_time = Time.now
289
- while !@quitting && (Time.now - start_time) < wait
287
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
288
+ while !@quitting && (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) < wait
290
289
  sleep 1
291
290
  end
292
291
  end
data/lib/cinch/channel.rb CHANGED
@@ -128,25 +128,25 @@ module Cinch
128
128
  # @return [Array<User>] All ops in the channel
129
129
  # @since 2.0.0
130
130
  def ops
131
- @users.select { |user, modes| modes.include?("o") }.keys
131
+ @users.filter_map { |user, modes| user if modes.include?("o") }
132
132
  end
133
133
 
134
134
  # @return [Array<User>] All half-ops in the channel
135
135
  # @since 2.0.0
136
136
  def half_ops
137
- @users.select { |user, modes| modes.include?("h") }.keys
137
+ @users.filter_map { |user, modes| user if modes.include?("h") }
138
138
  end
139
139
 
140
140
  # @return [Array<User>] All voiced users in the channel
141
141
  # @since 2.0.0
142
142
  def voiced
143
- @users.select { |user, modes| modes.include?("v") }.keys
143
+ @users.filter_map { |user, modes| user if modes.include?("v") }
144
144
  end
145
145
 
146
146
  # @return [Array<User>] All admins in the channel
147
147
  # @since 2.0.0
148
148
  def admins
149
- @users.select { |user, modes| modes.include?("a") }.keys
149
+ @users.filter_map { |user, modes| user if modes.include?("a") }
150
150
  end
151
151
  # @endgroup
152
152
 
data/lib/cinch/irc.rb CHANGED
@@ -13,7 +13,7 @@ module Cinch
13
13
  include Helpers
14
14
 
15
15
  # @api private
16
- REGISTRATION_COMMANDS = %w[001 002 003 004 422].freeze
16
+ REGISTRATION_COMMANDS = %w[001 002 003 004 376 422].freeze
17
17
  # @api private
18
18
  PRIVMSG_NOTICE_COMMANDS = %w[PRIVMSG NOTICE].freeze
19
19
 
@@ -284,7 +284,7 @@ module Cinch
284
284
 
285
285
  # @return [Boolean] true if we successfully registered yet
286
286
  def registered?
287
- (("001".."004").to_a - @registration).empty? || @registration.include?("422")
287
+ (("001".."004").to_a - @registration).empty? || @registration.include?("376") || @registration.include?("422")
288
288
  end
289
289
 
290
290
  # Send a message to the server.
@@ -92,10 +92,10 @@ module Cinch
92
92
  begin
93
93
  to_send = Cinch::Utilities::Encoding.encode_outgoing(message, @bot.config.encoding)
94
94
  @socket.write(to_send + "\r\n")
95
- @log << Time.now
95
+ @log << Process.clock_gettime(Process::CLOCK_MONOTONIC)
96
96
  @bot.loggers.outgoing(message)
97
97
 
98
- @time_since_last_send = Time.now
98
+ @time_since_last_send = Process.clock_gettime(Process::CLOCK_MONOTONIC)
99
99
  rescue IOError
100
100
  @bot.loggers.error "Could not send message (connectivity problems): #{message}"
101
101
  end
data/lib/cinch/plugin.rb CHANGED
@@ -312,7 +312,7 @@ module Cinch
312
312
  if hooks.is_a?(Hash)
313
313
  hooks = hooks.map { |k, v| v }
314
314
  end
315
- hooks = hooks.select { |hook| (events & hook.for).size > 0 }
315
+ hooks = hooks.select { |hook| events.intersect?(hook.for) }
316
316
  end
317
317
 
318
318
  hooks.select { |hook| hook.group.nil? || hook.group == group }
data/lib/cinch/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Cinch
4
4
  # Version of the library
5
- VERSION = "2.4.4"
5
+ VERSION = "2.4.6"
6
6
  end