iirc 0.2.0 → 0.2.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fe307700ed349facc2bfced87e9f2081534b3aa8f5807e104e923c29970d63a
4
- data.tar.gz: 53f7ed40fc8fc50f0f10cfb05baed481b0e9cea39158f28ccf4bc3d2f672f4b9
3
+ metadata.gz: ea1143ad079f11338ac80bd0b0e903e5fdfa16e37f112796d5b9e0856f5b48b4
4
+ data.tar.gz: 70d6519b3f27e2c61fa3e12b74612a56e505500e92f270c3f4ac71081e422dc3
5
5
  SHA512:
6
- metadata.gz: b7505abc4e38ecf5b78c3f3159841b06250399457012e8d7c94415e56ef0a3f439a3e6e3523fceed31c5dcb9454288807d30a52f7377c3126e20a1417b7960b2
7
- data.tar.gz: d1a53a1d19cd9da9db10eada940432597638dab3c5b07a61aa34fe04f3d02a90ac813e700d5cacdeb9497b6d57914364412e2fa6853f8209da02456be06055cc
6
+ metadata.gz: 4ab6beadf68e22e90aebc25421743406c18154da15e35e30bf02cebed3dceef804e7cd6783fa75cc48dfcf4edc528ea2d079887a2ce60062af218579591c63bb
7
+ data.tar.gz: 0666efaf1442141df7a4b7929a4435e070b8ce46f94a3031b1c934ff47fd48513da961bb1477039df213c8a8aa243ec2b1f4636a92e511717fed2170a0166fcd
data/CHANGELOG.md CHANGED
@@ -1,4 +1,36 @@
1
- ## [Unreleased]
1
+ ## [0.2.5] - 2022-03-16
2
+
3
+ - Add dial method. Uses SSL by default, with configurable context
4
+ - Add Bot.run method. Dials, yields an instance, then calls #run
5
+ - Add IIRC() method to top-level for DSL shorthand
6
+ - See examples/facts.rb
7
+ - Add examples/ (Facts, Greeter, Wolfram)
8
+
9
+ ## [0.2.4] - 2022-03-15
10
+
11
+ - [Bot::Hooks] Run catch-all hooks before on_#{verb} ones
12
+ - Include Bot::AmbientVerbs in Batteries
13
+ - Lets you write `say "Hello"` in event handlers instead of `say evt.target, "Hello"`
14
+ - Defaults the first arg of say,act,join,part,cycle,mode to the target of the current event
15
+ - Add Bot::PrintIO module
16
+
17
+ ## [0.2.3] - 2022-03-14
18
+
19
+ - Use new configure style for Bot::OperUp
20
+ - Use new configure style for Bot::TrackOwnNick
21
+ - Update own nick from welcome message (001)
22
+
23
+ ## [0.2.2] - 2022-03-13
24
+
25
+ - Use escape sequences in formatting.rb for readability
26
+
27
+ - Fix Event reference in parse()
28
+ - Bot::Event was moved to Event during refactor
29
+ - Missed as my code uses `include IIRC` at the top level
30
+
31
+ ## [0.2.1] - 2022-03-12
32
+
33
+ - Add User.from_source(str) alias
2
34
 
3
35
  ## [0.2.0] - 2022-03-12
4
36
 
data/examples/facts.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "iirc"
2
+
3
+ IIRC {
4
+ include IIRC::Bot::PrintIO
5
+
6
+ def facts
7
+ @facts ||= {}
8
+ end
9
+
10
+ def on_privmsg(evt)
11
+ case evt.message
12
+ when /^=list\b/
13
+ say "I know about #{facts.empty? ? 'nothing' : facts.keys.join(', ')}"
14
+ when /^=tell ([[:alnum:]]+)/
15
+ say facts[$1] ? "#{$1} is #{facts[$1]}" : "I don't know what that is"
16
+ when /^=add ([[:alnum:]]+) (.+)/
17
+ say "Okay, #{$1} is #{$2}"
18
+ facts[$1] = $2
19
+ when /^=del ([[:alnum:]]+)/
20
+ say "Okay, forgetting #{$1}"
21
+ facts.delete $1
22
+ end
23
+ end
24
+
25
+ def autojoin_channels
26
+ ['##facts', '##iirc']
27
+ end
28
+
29
+ run "irc.libera.chat", nick: "FactBot[#{rand(100)}]"
30
+ }
@@ -0,0 +1,16 @@
1
+ require "iirc"
2
+
3
+ class Greeter < IIRC::Bot
4
+ include AutoJoin
5
+ include Verbs
6
+
7
+ def on_join(evt)
8
+ say evt.target, "Hello #{evt.sender.nick}!"
9
+ end
10
+
11
+ def autojoin_channels
12
+ ['##iirc']
13
+ end
14
+ end
15
+
16
+ Greeter.run 'irc.libera.chat', nick: 'GreeterBot' if __FILE__ == $0
@@ -0,0 +1,39 @@
1
+ require "net/https"
2
+ require "iirc"
3
+
4
+ class Wolfram < IIRC::Batteries
5
+ include PrintIO
6
+
7
+ def on_privmsg(evt)
8
+ case evt.message
9
+ when /^!(what .+)/, /^!(define .+)/
10
+ answer($1).then { |answer|
11
+ if answer == "Wolfram|Alpha did not understand your input"
12
+ case rand(3)
13
+ when 0
14
+ say "I have no #{%w(earthly stinkin bloody).sample} idea :)"
15
+ when 1
16
+ say "If only I knew..."
17
+ when 2
18
+ say "#{evt.sender.nick}: If you ever find out, will you tell me?"
19
+ end
20
+ else
21
+ say answer.capitalize
22
+ end
23
+ }
24
+ end
25
+ end
26
+
27
+ def answer(query)
28
+ url = URI.parse("https://api.wolframalpha.com/v1/result")
29
+ url.query = URI.encode_www_form(i: query, appid: ENV['WOLFRAM_APP_ID'])
30
+
31
+ Net::HTTP.get(url)
32
+ end
33
+
34
+ def autojoin_channels
35
+ ['##wolframbot', '##iirc']
36
+ end
37
+ end
38
+
39
+ Wolfram.run 'irc.libera.chat', nick: 'WolframBot'+rand(100).to_s
@@ -1,11 +1,11 @@
1
1
  module IIRC
2
2
  module Bot::Formatting
3
3
  module_function
4
- def bold s; "#{s}"; end
5
- def italic s; "#{s}"; end
6
- def strike s; "#{s}"; end
7
- def underline s; "#{s}"; end
8
- def strip s; s.gsub /\x03(?:\d{1,2}(?:,\d{1,2})?)?/, ''; end
4
+ def bold(s) "\x02#{s}\x02" end
5
+ def italic(s) "\x1d#{s}\x1d" end
6
+ def strike(s) "\x1e#{s}\x1e" end
7
+ def underline(s) "\x1f#{s}\x1f" end
8
+ def strip(s) s.gsub(/\x03(?:\d{1,2}(?:,\d{1,2})?)?/, '') end
9
9
  alias :strip_formatting :strip
10
10
  end
11
11
  end
@@ -31,10 +31,13 @@ module IIRC
31
31
  end
32
32
 
33
33
  def fire! verb, *args, **kwargs
34
+ for action in hooks[nil]
35
+ call action, *args, **kwargs
36
+ end
34
37
  if respond_to? :"on_#{verb}"
35
38
  send(:"on_#{verb}", *args, **kwargs)
36
39
  end
37
- for action in [*hooks[nil], *hooks[verb]]
40
+ for action in hooks[verb]
38
41
  call action, *args, **kwargs
39
42
  end
40
43
  rescue StopIteration
@@ -1,8 +1,6 @@
1
1
  module IIRC
2
2
  module Bot::IRCv3
3
3
  class Batch < Event
4
- attr_accessor :events
5
-
6
4
  def events
7
5
  @events ||= []
8
6
  end
@@ -6,7 +6,7 @@ module IIRC
6
6
  def parse(line)
7
7
  msg = IRCParser::Message.parse(line.chomp)
8
8
 
9
- Bot::Event.new.tap { |evt|
9
+ Event.new.tap { |evt|
10
10
  evt.sender = IRCParser::RFCWireFormat.__stringify_prefix(msg.prefix) if msg.prefix
11
11
  evt.tags = msg.tags
12
12
  evt.verb = msg.command.downcase.to_sym
@@ -1,15 +1,12 @@
1
1
  module IIRC
2
2
  module Bot::OperUp
3
- def self.configure_actions
4
- [:add_oper_up_hook]
5
- end
6
-
7
- def add_oper_up_hook
8
- on :'001', :oper_up!
9
- end
10
-
11
3
  def oper_up!
12
4
  self << "OPER #{ENV['IRC_OPER']}" if ENV['IRC_OPER']
13
5
  end
6
+
7
+ private
8
+ def configure_oper_up
9
+ on :'001', :oper_up!
10
+ end
14
11
  end
15
12
  end
@@ -1,7 +1,7 @@
1
1
  module IIRC
2
2
  module Bot::Parsing
3
3
  def parse(line)
4
- Bot::Event.new.tap { |evt|
4
+ Event.new.tap { |evt|
5
5
  words = line.chomp.split " "
6
6
 
7
7
  if words[0] and words[0].start_with? ":"
@@ -0,0 +1,12 @@
1
+ module IIRC
2
+ module Bot::PrintIO
3
+ def lines
4
+ super { |line| puts ">> #{line}"; yield line }
5
+ end
6
+
7
+ def <<(line)
8
+ puts "<< #{line}"
9
+ super
10
+ end
11
+ end
12
+ end
@@ -1,9 +1,18 @@
1
1
  module IIRC
2
2
  module Bot::TrackOwnNick
3
- def on_nick evt
4
- if me === evt.sender
5
- self.user.nick = evt.args[0]
3
+ private
4
+
5
+ def configure_own_nick_tracking
6
+ on :'001', :track_own_nick_from_welcome
7
+ on :nick, :track_own_nick_change
8
+ end
9
+
10
+ def track_own_nick_from_welcome evt
11
+ me.nick = evt.args[0]
12
+ end
13
+
14
+ def track_own_nick_change evt
15
+ me.nick = evt.args[0] if me === evt.sender
6
16
  end
7
- end
8
17
  end
9
18
  end
data/lib/iirc/user.rb CHANGED
@@ -19,6 +19,8 @@ module IIRC
19
19
 
20
20
  new(nick: nick, username: username, host: host)
21
21
  end
22
+
23
+ alias :from_source :from_sender
22
24
  end
23
25
 
24
26
  def initialize(nick:nil,username:nil,realname:nil,host:nil)
@@ -54,4 +56,4 @@ module IIRC
54
56
  end
55
57
  end
56
58
  end
57
- end
59
+ end
data/lib/iirc/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IIRC
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.5"
5
5
  end
data/lib/iirc.rb CHANGED
@@ -15,6 +15,7 @@ require_relative "iirc/bot/members"
15
15
  require_relative "iirc/bot/oper_up"
16
16
  require_relative "iirc/bot/parsing"
17
17
  require_relative "iirc/bot/pong"
18
+ require_relative "iirc/bot/print_io"
18
19
  require_relative "iirc/bot/reading"
19
20
  require_relative "iirc/bot/redis"
20
21
  require_relative "iirc/bot/regex_hooks"
@@ -36,6 +37,16 @@ module IIRC
36
37
  include Configure
37
38
  include Pong
38
39
  include TrackOwnNick
40
+
41
+ def self.run(host, port=6697, ssl_context: SSL.default_context, **user_params, &blk)
42
+ socket = IIRC.dial(host, port, ssl_context: ssl_context)
43
+
44
+ new(socket, **user_params).tap { |bot|
45
+ bot.register!
46
+ bot.tap(&blk) if blk
47
+ bot.run
48
+ }
49
+ end
39
50
  end
40
51
 
41
52
  class IRCv3Bot < Bot
@@ -46,10 +57,63 @@ module IIRC
46
57
  end
47
58
 
48
59
  class Batteries < IRCv3Bot
49
- include Channels
50
- include Members
51
- include Formatting
52
- include AutoJoin
53
- include Verbs
60
+ include Bot::Channels
61
+ include Bot::Members
62
+ include Bot::Formatting
63
+ include Bot::AutoJoin
64
+ include Bot::Verbs
65
+ include Bot::AmbientVerbs
66
+ end
67
+
68
+ module SSL
69
+ module_function
70
+
71
+ def default_context
72
+ verify_peer_and_hostname
73
+ end
74
+
75
+ def verify_peer_and_hostname
76
+ verify_peer.tap { |context|
77
+ context.verify_hostname = true
78
+ }
79
+ end
80
+
81
+ def verify_peer
82
+ no_verify.tap { |context|
83
+ context.verify_mode = OpenSSL::SSL::VERIFY_PEER
84
+ context.cert_store = OpenSSL::X509::Store.new.tap(&:set_default_paths)
85
+ }
86
+ end
87
+
88
+ def verify_hostname_only
89
+ no_verify.tap { |context|
90
+ context.verify_hostname = true
91
+ }
92
+ end
93
+
94
+ def no_verify
95
+ require 'openssl'
96
+ OpenSSL::SSL::SSLContext.new
97
+ end
54
98
  end
99
+
100
+ module_function
101
+ def dial(host, port=6697, ssl_context: SSL.default_context)
102
+ require 'socket'
103
+
104
+ Socket.tcp(host, port).then { |socket|
105
+ if ssl_context
106
+ OpenSSL::SSL::SSLSocket.new(socket, ssl_context).tap { |socket|
107
+ socket.hostname = host
108
+ socket.connect
109
+ }
110
+ else
111
+ socket
112
+ end
113
+ }
114
+ end
55
115
  end
116
+
117
+ def IIRC(*args, **kwargs, &blk)
118
+ Class.new(IIRC::Batteries, &blk)
119
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iirc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - mooff
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-12 00:00:00.000000000 Z
11
+ date: 2022-03-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A composable toolkit for IRC bots
14
14
  email:
@@ -25,6 +25,9 @@ files:
25
25
  - Rakefile
26
26
  - bin/console
27
27
  - bin/setup
28
+ - examples/facts.rb
29
+ - examples/greeter.rb
30
+ - examples/wolfram.rb
28
31
  - iirc.gemspec
29
32
  - lib/iirc.rb
30
33
  - lib/iirc/bot.rb
@@ -42,6 +45,7 @@ files:
42
45
  - lib/iirc/bot/oper_up.rb
43
46
  - lib/iirc/bot/parsing.rb
44
47
  - lib/iirc/bot/pong.rb
48
+ - lib/iirc/bot/print_io.rb
45
49
  - lib/iirc/bot/reading.rb
46
50
  - lib/iirc/bot/redis.rb
47
51
  - lib/iirc/bot/regex_hooks.rb