ircbgb 0.0.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +19 -0
- data/.gitignore +7 -0
- data/Gemfile +20 -0
- data/Rakefile +31 -0
- data/ircbgb.gemspec +25 -0
- data/lib/ircbgb.rb +15 -0
- data/lib/ircbgb/behaviors.rb +17 -0
- data/lib/ircbgb/behaviors/negotiates_connection.rb +38 -0
- data/lib/ircbgb/behaviors/provides_commands.rb +20 -0
- data/lib/ircbgb/client.rb +103 -0
- data/lib/ircbgb/errors.rb +5 -0
- data/lib/ircbgb/matcher.rb +13 -0
- data/lib/ircbgb/message.rb +17 -0
- data/lib/ircbgb/message_parser.rb +567 -0
- data/lib/ircbgb/message_parser.rl +84 -0
- data/lib/ircbgb/server.rb +13 -0
- data/lib/ircbgb/user.rb +32 -0
- data/lib/ircbgb/version.rb +3 -0
- data/spec/ircbgb/client_spec.rb +171 -0
- data/spec/ircbgb/matcher_spec.rb +25 -0
- data/spec/ircbgb/message_parser_spec.rb +100 -0
- data/spec/ircbgb/message_spec.rb +39 -0
- data/spec/ircbgb/user_spec.rb +72 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/support/dummy_io.rb +70 -0
- data/spec/support/stubz.rb +49 -0
- data/spec/support/unblocked_tcp_socket.rb +60 -0
- metadata +93 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Ircbgb::User do
|
4
|
+
describe "parsing" do
|
5
|
+
it "parses a `nick!user@host` string" do
|
6
|
+
user = Ircbgb::User.parse('my_nick!~stuffit@some.exotic.host')
|
7
|
+
user.nick.must_equal 'my_nick'
|
8
|
+
user.user.must_equal '~stuffit'
|
9
|
+
user.host.must_equal 'some.exotic.host'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "parses a wildcarded string" do
|
13
|
+
user = Ircbgb::User.parse('*!?stuffy@*.example.???')
|
14
|
+
user.nick.must_equal '*'
|
15
|
+
user.user.must_equal '?stuffy'
|
16
|
+
user.host.must_equal '*.example.???'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "parses missing bits, wildcarding what's left out" do
|
20
|
+
user = Ircbgb::User.parse('beeboz')
|
21
|
+
user.nick.must_equal 'beeboz'
|
22
|
+
user.user.must_equal '*'
|
23
|
+
user.host.must_equal '*'
|
24
|
+
user = Ircbgb::User.parse('booboz!@host')
|
25
|
+
user.nick.must_equal 'booboz'
|
26
|
+
user.user.must_equal '*'
|
27
|
+
user.host.must_equal 'host'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "parses a user as itself" do
|
31
|
+
user = Ircbgb::User.parse('my_nick!~stuffit@some.exotic.host')
|
32
|
+
Ircbgb::User.parse(user).must_equal user
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "methods" do
|
37
|
+
it "converts to a string" do
|
38
|
+
user = Ircbgb::User.new('a_nick', 'a_username', 'a.host.example.org')
|
39
|
+
user.to_s.must_equal 'a_nick!a_username@a.host.example.org'
|
40
|
+
Ircbgb::User.parse('nick').to_s.must_equal 'nick!*@*'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "matching" do
|
45
|
+
def match_me; @match_me ||= Ircbgb::User.new('someNick', '~stuffy', 'britches.co.uk'); end
|
46
|
+
|
47
|
+
it "matches against IRC wildcard strings" do
|
48
|
+
'someNick!~stuffy@britches.co.uk'.must_match match_me
|
49
|
+
'*o*!*stuffy@*i*.?o.u?'.must_match match_me
|
50
|
+
'S???nick!*~stuffy@britches.co.**'.must_match match_me
|
51
|
+
'*!*@*'.must_match match_me
|
52
|
+
'?*'.must_match match_me
|
53
|
+
'someNick?!*@*'.wont_match match_me
|
54
|
+
'*b*!*@*'.wont_match match_me
|
55
|
+
'*q*'.wont_match match_me
|
56
|
+
end
|
57
|
+
|
58
|
+
it "matches against regexs" do
|
59
|
+
/\Asome/.must_match match_me
|
60
|
+
/stuffy@bri/.must_match match_me
|
61
|
+
/\AsomeNick.!/.wont_match match_me
|
62
|
+
/.*b.*!.*@.*/.wont_match match_me
|
63
|
+
/q/.wont_match match_me
|
64
|
+
end
|
65
|
+
|
66
|
+
it "matches against matchers" do
|
67
|
+
Ircbgb::Matcher.new('*o*!*stuffy@*i*.?o.u?').must_match match_me
|
68
|
+
Ircbgb::Matcher.new('someNick?!*@*').wont_match match_me
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
if ENV['SCOV']
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter "/spec/"
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'minitest/autorun'
|
10
|
+
require 'minitest/emoji'
|
11
|
+
rescue LoadError
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir[File.expand_path('../support/*.rb', __FILE__)].each do |r|
|
15
|
+
require r
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'ircbgb'
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
|
3
|
+
# Used to test IO stuff, using stringio objects for the write and
|
4
|
+
# read stream.
|
5
|
+
class DummyIO
|
6
|
+
attr_reader :closed, :w_stream, :r_stream, :raised_write, :raised_read
|
7
|
+
alias :closed? :closed
|
8
|
+
alias :raised_write? :raised_write
|
9
|
+
alias :raised_read? :raised_read
|
10
|
+
attr_accessor :readable, :writeable
|
11
|
+
attr_accessor :write_delay, :read_delay
|
12
|
+
attr_accessor :max_write, :max_read
|
13
|
+
attr_accessor :raise_read, :raise_write
|
14
|
+
alias :readable? :readable
|
15
|
+
alias :writeable? :writeable
|
16
|
+
|
17
|
+
def initialize *args, &block
|
18
|
+
@r_stream = StringIO.new
|
19
|
+
@w_stream = StringIO.new
|
20
|
+
@readable = @writeable = true
|
21
|
+
@read_delay = @write_delay = 0
|
22
|
+
@max_write = 0
|
23
|
+
@max_read = 0
|
24
|
+
@closed = false
|
25
|
+
@raise_read = nil
|
26
|
+
@raise_write = nil
|
27
|
+
@raised_write = false
|
28
|
+
@raised_read = false
|
29
|
+
end
|
30
|
+
|
31
|
+
def close
|
32
|
+
@closed = true
|
33
|
+
@w_stream.close
|
34
|
+
@r_stream.close
|
35
|
+
end
|
36
|
+
|
37
|
+
def write_nonblock bytes
|
38
|
+
sleep(@write_delay) if @write_delay > 0
|
39
|
+
do_raise_write
|
40
|
+
if @max_write > 0 && bytes.size > @max_write
|
41
|
+
@w_stream.write bytes[0...@max_write]
|
42
|
+
else
|
43
|
+
@w_stream.write bytes
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def read_nonblock len
|
48
|
+
sleep(@read_delay) if @read_delay > 0
|
49
|
+
do_raise_read
|
50
|
+
if @max_read > 0 && len > @max_read
|
51
|
+
@r_stream.read @max_read
|
52
|
+
else
|
53
|
+
@r_stream.read len
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def do_raise_write
|
58
|
+
if @raise_write
|
59
|
+
@raised_write = true
|
60
|
+
raise @raise_write
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def do_raise_read
|
65
|
+
if @raise_read
|
66
|
+
@raised_read = true
|
67
|
+
raise @raise_read
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Stubz
|
2
|
+
def self.included base
|
3
|
+
base.__send__(:before) { _ensure_stubs_ }
|
4
|
+
base.__send__(:after) { _reset_stubs_ }
|
5
|
+
end
|
6
|
+
|
7
|
+
def stub_instance(klass, meth, *val, &block)
|
8
|
+
_do_stub_ klass, meth, *val, &block
|
9
|
+
end
|
10
|
+
|
11
|
+
def stub(obj, meth, *val, &block)
|
12
|
+
_do_stub_ get_meta_class(obj), meth, *val, &block
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_meta_class klass
|
16
|
+
class << klass; self; end
|
17
|
+
end
|
18
|
+
|
19
|
+
def _do_stub_ klass, meth, *val, &block
|
20
|
+
if klass.method_defined? meth
|
21
|
+
aliased = "_stub_#{meth}_#{Time.now.to_i}_"
|
22
|
+
klass.send(:alias_method, aliased, meth)
|
23
|
+
else
|
24
|
+
aliased = nil
|
25
|
+
end
|
26
|
+
if block
|
27
|
+
klass.send(:define_method, meth, &block)
|
28
|
+
else
|
29
|
+
klass.send(:define_method, meth) do |*_|
|
30
|
+
val.first
|
31
|
+
end
|
32
|
+
end
|
33
|
+
@stubbies << [klass, meth, aliased]
|
34
|
+
end
|
35
|
+
|
36
|
+
def _ensure_stubs_
|
37
|
+
@stubbies = []
|
38
|
+
end
|
39
|
+
|
40
|
+
def _reset_stubs_
|
41
|
+
@stubbies.each do |(klass, meth, aliased)|
|
42
|
+
klass.send(:remove_method, meth)
|
43
|
+
if aliased
|
44
|
+
klass.send(:alias_method, meth, aliased)
|
45
|
+
klass.send(:remove_method, aliased)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Rather than using IoUnblock with a mock TCPSocket, we'll trust IoUnblock to
|
2
|
+
# test itself properly, and mock out IoUnblock::TcpSocket instead.
|
3
|
+
class UnblockedTcpSocket
|
4
|
+
attr_reader :host, :port, :callbacks, :written
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@written = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def init_with host, port, callbacks=nil
|
11
|
+
@callbacks = callbacks || {}
|
12
|
+
@host = host
|
13
|
+
@port = port
|
14
|
+
@callbacks[:callback_failed] ||= lambda { |ex|
|
15
|
+
warn "Default callback_failed handler: #{ex}"
|
16
|
+
}
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def start; self; end
|
21
|
+
def stop; self; end
|
22
|
+
def running?; true; end
|
23
|
+
|
24
|
+
def server_write_raw str
|
25
|
+
@callbacks[:read] && @callbacks[:read].call(str)
|
26
|
+
end
|
27
|
+
|
28
|
+
def server_write cmd, max_len=nil
|
29
|
+
server_write_raw ":#{@host} #{cmd}\r\n"
|
30
|
+
end
|
31
|
+
|
32
|
+
def write str
|
33
|
+
if str[-2..-1] != "\r\n"
|
34
|
+
raise "You forgot to CRLF your message"
|
35
|
+
end
|
36
|
+
@written << str[0..-3]
|
37
|
+
end
|
38
|
+
|
39
|
+
def trigger_start
|
40
|
+
trigger_ev :started, :start
|
41
|
+
end
|
42
|
+
|
43
|
+
def trigger_loop
|
44
|
+
trigger_ev :looped, self
|
45
|
+
end
|
46
|
+
|
47
|
+
def trigger_stop
|
48
|
+
trigger_ev :stopped, :stop
|
49
|
+
end
|
50
|
+
|
51
|
+
def trigger_ev ev, *args
|
52
|
+
@callbacks[ev] && @callbacks[ev].call(*args)
|
53
|
+
rescue Exception
|
54
|
+
if ev != :callback_failed
|
55
|
+
@callbacks[:callback_failed].call($!)
|
56
|
+
else
|
57
|
+
warn "Your callback failure callback is raising errors: #{$!}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ircbgb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.pre
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ian D. Eccles
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-14 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &2156987900 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2156987900
|
25
|
+
description: An IRC client library geared toward writing custom clients and bots
|
26
|
+
email:
|
27
|
+
- ian.eccles@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .autotest
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- Rakefile
|
36
|
+
- ircbgb.gemspec
|
37
|
+
- lib/ircbgb.rb
|
38
|
+
- lib/ircbgb/behaviors.rb
|
39
|
+
- lib/ircbgb/behaviors/negotiates_connection.rb
|
40
|
+
- lib/ircbgb/behaviors/provides_commands.rb
|
41
|
+
- lib/ircbgb/client.rb
|
42
|
+
- lib/ircbgb/errors.rb
|
43
|
+
- lib/ircbgb/matcher.rb
|
44
|
+
- lib/ircbgb/message.rb
|
45
|
+
- lib/ircbgb/message_parser.rb
|
46
|
+
- lib/ircbgb/message_parser.rl
|
47
|
+
- lib/ircbgb/server.rb
|
48
|
+
- lib/ircbgb/user.rb
|
49
|
+
- lib/ircbgb/version.rb
|
50
|
+
- spec/ircbgb/client_spec.rb
|
51
|
+
- spec/ircbgb/matcher_spec.rb
|
52
|
+
- spec/ircbgb/message_parser_spec.rb
|
53
|
+
- spec/ircbgb/message_spec.rb
|
54
|
+
- spec/ircbgb/user_spec.rb
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
- spec/support/dummy_io.rb
|
57
|
+
- spec/support/stubz.rb
|
58
|
+
- spec/support/unblocked_tcp_socket.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses: []
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>'
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 1.3.1
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project: ircbgb
|
79
|
+
rubygems_version: 1.8.12
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: An IRC client library
|
83
|
+
test_files:
|
84
|
+
- spec/ircbgb/client_spec.rb
|
85
|
+
- spec/ircbgb/matcher_spec.rb
|
86
|
+
- spec/ircbgb/message_parser_spec.rb
|
87
|
+
- spec/ircbgb/message_spec.rb
|
88
|
+
- spec/ircbgb/user_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/support/dummy_io.rb
|
91
|
+
- spec/support/stubz.rb
|
92
|
+
- spec/support/unblocked_tcp_socket.rb
|
93
|
+
has_rdoc:
|