ircguerilla-irc 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/irc/client/command_handler.rb +2 -2
- data/lib/irc/client/connected_state.rb +53 -8
- data/lib/irc/client/connection.rb +41 -6
- data/lib/irc/client/connection_listener.rb +26 -11
- data/lib/irc/client/connection_state.rb +15 -2
- data/lib/irc/client/context.rb +49 -11
- data/lib/irc/client/disconnected_state.rb +28 -16
- data/lib/irc/client/message_handler.rb +40 -16
- data/lib/irc/client/registered_state.rb +18 -0
- data/lib/irc/client/unregistered_state.rb +51 -16
- data/lib/irc/commands/nick_command.rb +14 -0
- data/lib/irc/messages/error_join_message.rb +1 -1
- data/lib/irc/messages/error_message.rb +53 -24
- data/lib/irc/messages/error_nick_name_in_use_message.rb +70 -0
- data/lib/irc/messages/factory.rb +16 -0
- data/lib/irc/messages/isupport_message.rb +86 -0
- data/lib/irc/messages/kick_message.rb +114 -0
- data/lib/irc/messages/message.rb +21 -1
- data/lib/irc/messages/nick_message.rb +17 -2
- data/lib/irc/messages/pong_message.rb +1 -1
- data/lib/irc/messages/welcome_message.rb +72 -0
- data/lib/irc/util/color.rb +77 -0
- data/lib/irc/util/nick_generator.rb +58 -0
- data/test/functional/irc/client/connection_test.rb +34 -1
- data/test/functional/irc/client/context_test.rb +5 -2
- data/test/mocks/test/irc/client/connection.rb +128 -0
- data/test/mocks/test/irc/client/disconnected_state.rb +44 -0
- data/test/test_helper.rb +1 -1
- data/test/unit/irc/client/command_handler_test.rb +12 -12
- data/test/unit/irc/client/message_handler_test.rb +136 -0
- data/test/unit/irc/commands/command_test.rb +87 -84
- data/test/unit/irc/commands/join_command_test.rb +3 -1
- data/test/unit/irc/commands/nick_command_test.rb +7 -1
- data/test/unit/irc/commands/ping_command_test.rb +12 -16
- data/test/unit/irc/commands/pong_command_test.rb +2 -1
- data/test/unit/irc/messages/error_join_message_test.rb +23 -2
- data/test/unit/irc/messages/error_message_test.rb +56 -0
- data/test/unit/irc/messages/error_nick_name_in_use_message_test.rb +56 -0
- data/test/unit/irc/messages/factory_test.rb +25 -0
- data/test/unit/irc/messages/isupport_message_test.rb +138 -0
- data/test/unit/irc/messages/join_message_test.rb +22 -1
- data/test/unit/irc/messages/kick_message_test.rb +76 -0
- data/test/unit/irc/messages/message_test.rb +33 -0
- data/test/unit/irc/messages/nick_message_test.rb +22 -1
- data/test/unit/irc/messages/notice_message_test.rb +22 -1
- data/test/unit/irc/messages/part_message_test.rb +22 -1
- data/test/unit/irc/messages/ping_message_test.rb +21 -1
- data/test/unit/irc/messages/pong_message_test.rb +21 -1
- data/test/unit/irc/messages/private_message_test.rb +22 -1
- data/test/unit/irc/messages/welcome_message_test.rb +59 -0
- data/test/unit/irc/util/color_test.rb +97 -0
- data/test/unit/irc/util/nick_generator_test.rb +52 -0
- metadata +69 -52
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright (c) 2006 Roman Scherer | IRC Guerilla | Rapid Packet Movement
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
#
|
24
|
+
# $Id$
|
25
|
+
#
|
26
|
+
module IRC
|
27
|
+
|
28
|
+
module Util
|
29
|
+
|
30
|
+
class NickGenerator
|
31
|
+
|
32
|
+
attr_reader :names
|
33
|
+
|
34
|
+
def initialize(nick, nick_length = nil, max_nicks = 10)
|
35
|
+
|
36
|
+
nick_length = !nick_length.nil? ? nick_length : nick.size + max_nicks.to_s.length
|
37
|
+
|
38
|
+
@names = Array.new
|
39
|
+
|
40
|
+
1.upto(max_nicks) do |number|
|
41
|
+
@names << nick[0, nick_length - number.to_s.size] + number.to_s
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def each
|
47
|
+
@names.each { |name| yield name }
|
48
|
+
end
|
49
|
+
|
50
|
+
def pop
|
51
|
+
@names.pop
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -45,7 +45,7 @@ class IRC::Client::ConnectionTest < Test::Unit::TestCase
|
|
45
45
|
@server = network.create_server("irc.efnet.pl")
|
46
46
|
|
47
47
|
# Initialize the connection connection.
|
48
|
-
@connection = IRC::Client::Connection
|
48
|
+
@connection = IRC::Client::Connection.new("fumanshu")
|
49
49
|
|
50
50
|
end
|
51
51
|
|
@@ -62,6 +62,7 @@ class IRC::Client::ConnectionTest < Test::Unit::TestCase
|
|
62
62
|
|
63
63
|
# Wait until the connection is registered.
|
64
64
|
Timeout::timeout(5) do
|
65
|
+
@connection.register
|
65
66
|
while !@connection.registered? do sleep 0.1; end
|
66
67
|
end
|
67
68
|
|
@@ -73,6 +74,37 @@ class IRC::Client::ConnectionTest < Test::Unit::TestCase
|
|
73
74
|
|
74
75
|
end
|
75
76
|
|
77
|
+
def test_nick_change
|
78
|
+
|
79
|
+
# The connection shouldn't be connected now.
|
80
|
+
assert !@connection.connected?
|
81
|
+
|
82
|
+
# Connect to the server and wait until the connection is in the unregistered state.
|
83
|
+
Timeout::timeout(IRC::Client::Connection::MAX_CONNECTION_TIMEOUT_IN_SEC) do
|
84
|
+
@connection.connect(@server)
|
85
|
+
while !@connection.connected? do sleep 0.1; end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Wait until the connection is registered.
|
89
|
+
Timeout::timeout(5) do
|
90
|
+
@connection.register
|
91
|
+
while !@connection.registered? do sleep 0.1; end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Try to change the nick name & wait until changed.
|
95
|
+
Timeout::timeout(5) do
|
96
|
+
@connection.nick = "N1CKCHANGE"
|
97
|
+
while @connection.nick != "N1CKCHANGE"[0..@connection.nick.size-1] do sleep 0.1; end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Disconnect and wait until the connection is in the disconnected state.
|
101
|
+
Timeout::timeout(5) do
|
102
|
+
@connection.disconnect
|
103
|
+
while @connection.connected? do sleep 0.1; end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
76
108
|
def test_join_and_part
|
77
109
|
|
78
110
|
# The connection shouldn't be connected now.
|
@@ -86,6 +118,7 @@ class IRC::Client::ConnectionTest < Test::Unit::TestCase
|
|
86
118
|
|
87
119
|
# Wait until the connection is registered.
|
88
120
|
Timeout::timeout(5) do
|
121
|
+
@connection.register
|
89
122
|
while !@connection.registered? do sleep 0.1; end
|
90
123
|
end
|
91
124
|
|
@@ -68,6 +68,7 @@ class IRC::Client::ContextTest < Test::Unit::TestCase
|
|
68
68
|
|
69
69
|
# Wait until the connection is registered.
|
70
70
|
Timeout::timeout(5) do
|
71
|
+
@context.register
|
71
72
|
while !@context.registered? do sleep 0.1; end
|
72
73
|
end
|
73
74
|
|
@@ -92,6 +93,7 @@ class IRC::Client::ContextTest < Test::Unit::TestCase
|
|
92
93
|
|
93
94
|
# Wait until the connection is registered.
|
94
95
|
Timeout::timeout(5) do
|
96
|
+
@context.register
|
95
97
|
while !@context.registered? do sleep 0.1; end
|
96
98
|
end
|
97
99
|
|
@@ -115,6 +117,7 @@ class IRC::Client::ContextTest < Test::Unit::TestCase
|
|
115
117
|
|
116
118
|
end
|
117
119
|
|
118
|
-
|
119
|
-
|
120
|
+
def test_truth
|
121
|
+
end
|
122
|
+
|
120
123
|
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright (c) 2006 Roman Scherer | IRC Guerilla | Rapid Packet Movement
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
#
|
24
|
+
# $Id$
|
25
|
+
#
|
26
|
+
|
27
|
+
require File.dirname(__FILE__) + '/../../../../../lib/irc/client/connection'
|
28
|
+
|
29
|
+
module IRC
|
30
|
+
|
31
|
+
module Client
|
32
|
+
|
33
|
+
class Connection
|
34
|
+
|
35
|
+
attr_reader :context
|
36
|
+
|
37
|
+
def connect(server)
|
38
|
+
|
39
|
+
if context.input_socket == nil
|
40
|
+
|
41
|
+
context.server = server
|
42
|
+
|
43
|
+
register_connection_messages
|
44
|
+
register_registration_messages
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
context.connect(server)
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
def register_raw_message(raw_message)
|
53
|
+
|
54
|
+
# Create an input socket if not done already.
|
55
|
+
context.input_socket ||= StringIO.new
|
56
|
+
|
57
|
+
# Save the input socket position and seek to the end.
|
58
|
+
position = context.input_socket.pos
|
59
|
+
context.input_socket.seek(0, IO::SEEK_END)
|
60
|
+
|
61
|
+
# Append the raw message to the end of the string io object.
|
62
|
+
context.input_socket << "#{IRC::Messages::Message.new(raw_message).to_s}\r\n"
|
63
|
+
|
64
|
+
# Seek back to the saved position.
|
65
|
+
context.input_socket.seek(position, IO::SEEK_SET)
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
def register_raw_messages(raw_messages)
|
70
|
+
|
71
|
+
raw_messages.each do |raw_message|
|
72
|
+
register_raw_message(raw_message)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
def register_connection_messages
|
78
|
+
|
79
|
+
raw_messages = [
|
80
|
+
"NOTICE AUTH :*** Looking up your hostname...",
|
81
|
+
"NOTICE AUTH :*** Checking Ident",
|
82
|
+
"NOTICE AUTH :*** Found your hostname",
|
83
|
+
"NOTICE AUTH :*** Got Ident response"
|
84
|
+
]
|
85
|
+
|
86
|
+
register_raw_messages(raw_messages)
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
def register_registration_messages
|
91
|
+
|
92
|
+
raw_messages = [
|
93
|
+
":#{context.server.hostname} 001 #{context.nick} :Welcome to the EFNet Internet Relay Chat Network #{context.nick}",
|
94
|
+
":#{context.server.hostname} 002 #{context.nick} :Your host is #{context.server.hostname}[#{context.server.hostname}/6667], running version ircd-ratbox-1.5-3",
|
95
|
+
":#{context.server.hostname} 003 #{context.nick} :This server was created Wto 30 Lis CET at 18:36:52 2004",
|
96
|
+
":#{context.server.hostname} 004 #{context.nick} #{context.server.hostname} ircd-ratbox-1.5-3 oiwszcerkfydnxbauglZ biklmnopstveI bkloveI",
|
97
|
+
":#{context.server.hostname} 005 #{context.nick} STD=i-d STATUSMSG=@+ KNOCK EXCEPTS INVEX MODES=4 MAXCHANNELS=25 MAXBANS=100 MAXTARGETS=4 NICKLEN=9 TOPICLEN=120 KICKLEN=120 :are supported by this server",
|
98
|
+
":#{context.server.hostname} 005 #{context.nick} CHANTYPES=#& PREFIX=(ov)@+ CHANMODES=eIb,k,l,imnpst NETWORK=EFNet CASEMAPPING=rfc1459 CHARSET=ascii CALLERID ETRACE WALLCHOPS SAFELIST ELIST=U :are supported by this server",
|
99
|
+
":#{context.server.hostname} 251 #{context.nick} :There are 8205 users and 63039 invisible on 59 servers",
|
100
|
+
":#{context.server.hostname} 252 #{context.nick} 437 :IRC Operators online",
|
101
|
+
":#{context.server.hostname} 254 #{context.nick} 33951 :channels formed",
|
102
|
+
":#{context.server.hostname} 255 #{context.nick} :I have 5548 clients and 1 servers",
|
103
|
+
":#{context.server.hostname} 265 #{context.nick} :Current local users: 5548 Max: 6470",
|
104
|
+
":#{context.server.hostname} 266 #{context.nick} :Current global users: 71244 Max: 80036",
|
105
|
+
":#{context.server.hostname} 250 #{context.nick} :Highest connection count: 6471 (6470 clients) (217198 connections received)",
|
106
|
+
":#{context.server.hostname} 375 #{context.nick} :- #{context.server.hostname} Message of the Day -",
|
107
|
+
":#{context.server.hostname} 372 #{context.nick} :-",
|
108
|
+
":#{context.server.hostname} 372 #{context.nick} :- w w w w w w a ttt m m ccc ooo m m ppp l",
|
109
|
+
":#{context.server.hostname} 372 #{context.nick} :- w w w w w w a a t mm mm c o o mm mm p p l",
|
110
|
+
":#{context.server.hostname} 372 #{context.nick} :- w w w w w w w w w aaaaa t m m m c o o m m m ppp l",
|
111
|
+
":#{context.server.hostname} 372 #{context.nick} :- w w w w w w .a a t m m .ccc ooo m m.p llll",
|
112
|
+
":#{context.server.hostname} 372 #{context.nick} :-",
|
113
|
+
":#{context.server.hostname} 372 #{context.nick} :- WWW.ATM.COM.PL",
|
114
|
+
":#{context.server.hostname} 372 #{context.nick} :-",
|
115
|
+
":#{context.server.hostname} 376 #{context.nick} :End of /MOTD command.",
|
116
|
+
":#{context.nick} MODE #{context.nick} :+i"
|
117
|
+
]
|
118
|
+
|
119
|
+
register_raw_messages(raw_messages)
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright (c) 2006 Roman Scherer | IRC Guerilla | Rapid Packet Movement
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
#
|
24
|
+
# $Id$
|
25
|
+
#
|
26
|
+
require File.dirname(__FILE__) + '/../../../../../lib/irc/client/disconnected_state'
|
27
|
+
|
28
|
+
module IRC
|
29
|
+
|
30
|
+
module Client
|
31
|
+
|
32
|
+
class DisconnectedState < ConnectionState
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def establish_connection(context, server)
|
37
|
+
context.output_socket = StringIO.new
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -38,7 +38,7 @@ class IRC::Client::CommandHandlerTest < Test::Unit::TestCase
|
|
38
38
|
# Set up a connection context.
|
39
39
|
@context = IRC::Client::Connection::Context.new
|
40
40
|
@context.network = IRC::Models::Network.new(:name => "Efnet")
|
41
|
-
@context.
|
41
|
+
@context.output_socket = StringIO.new("", "w+")
|
42
42
|
|
43
43
|
# Initialize the command handler with the mock connection context.
|
44
44
|
@command_handler = IRC::Client::CommandHandler.new(@context)
|
@@ -84,14 +84,14 @@ class IRC::Client::CommandHandlerTest < Test::Unit::TestCase
|
|
84
84
|
@command_handler.send_command(nick_command)
|
85
85
|
|
86
86
|
# Rewind the stringio object, so we can read from it.
|
87
|
-
@context.
|
87
|
+
@context.output_socket.rewind
|
88
88
|
|
89
89
|
# Both commands should be available immediately, since they were not sent through the queue.
|
90
|
-
assert_equal password_command.command, @context.
|
91
|
-
assert_equal nick_command.command, @context.
|
90
|
+
assert_equal password_command.command, @context.output_socket.readline.chomp
|
91
|
+
assert_equal nick_command.command, @context.output_socket.readline.chomp
|
92
92
|
|
93
93
|
# There should be no more commands available.
|
94
|
-
assert_raise(EOFError) { @context.
|
94
|
+
assert_raise(EOFError) { @context.output_socket.readline }
|
95
95
|
|
96
96
|
# Stop the command handler.
|
97
97
|
@command_handler.stop
|
@@ -112,22 +112,22 @@ class IRC::Client::CommandHandlerTest < Test::Unit::TestCase
|
|
112
112
|
@command_handler << nick_command
|
113
113
|
|
114
114
|
# Rewind the stringio object, so we can read from it.
|
115
|
-
@context.
|
115
|
+
@context.output_socket.rewind
|
116
116
|
|
117
117
|
# The first command got sent but the second is still in the queue.
|
118
|
-
assert_equal password_command.command, @context.
|
119
|
-
assert_raise(EOFError) { @context.
|
118
|
+
assert_equal password_command.command, @context.output_socket.readline.chomp
|
119
|
+
assert_raise(EOFError) { @context.output_socket.readline }
|
120
120
|
|
121
121
|
# Wait for the 2nd command.
|
122
122
|
sleep @command_handler.delay * 1.5
|
123
123
|
|
124
124
|
# Rewind again and start reading all commands from scratch.
|
125
|
-
@context.
|
126
|
-
assert_equal password_command.command, @context.
|
127
|
-
assert_equal nick_command.command, @context.
|
125
|
+
@context.output_socket.rewind
|
126
|
+
assert_equal password_command.command, @context.output_socket.readline.chomp
|
127
|
+
assert_equal nick_command.command, @context.output_socket.readline.chomp
|
128
128
|
|
129
129
|
# There should be no more commands available.
|
130
|
-
assert_raise(EOFError) { @context.
|
130
|
+
assert_raise(EOFError) { @context.output_socket.readline }
|
131
131
|
|
132
132
|
# Stop the command handler.
|
133
133
|
@command_handler.stop
|
@@ -0,0 +1,136 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright (c) 2006 Roman Scherer | IRC Guerilla | Rapid Packet Movement
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
#
|
24
|
+
# $Id$
|
25
|
+
#
|
26
|
+
require 'irc/client/message_handler'
|
27
|
+
require 'irc/client/context'
|
28
|
+
require 'irc/commands/nick_command'
|
29
|
+
require 'irc/commands/password_command'
|
30
|
+
require 'irc/models/network'
|
31
|
+
require 'stringio'
|
32
|
+
require 'test/unit'
|
33
|
+
|
34
|
+
class IRC::Client::MessageHandlerTest < Test::Unit::TestCase
|
35
|
+
|
36
|
+
def setup
|
37
|
+
|
38
|
+
# Set up a connection context.
|
39
|
+
@context = IRC::Client::Connection::Context.new
|
40
|
+
@context.network = IRC::Models::Network.new(:name => "Efnet")
|
41
|
+
@context.input_socket = StringIO.new("", "w+")
|
42
|
+
|
43
|
+
# Initialize the command handler with the mock connection context.
|
44
|
+
@message_handler = IRC::Client::MessageHandler.new(@context)
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_start
|
49
|
+
|
50
|
+
# Start the command handler.
|
51
|
+
@message_handler.start
|
52
|
+
|
53
|
+
# Starting the command handler again should raise an exception.
|
54
|
+
assert_raise(IRC::Client::MessageHandlerError) { @message_handler.start }
|
55
|
+
|
56
|
+
# Stop the command handler.
|
57
|
+
@message_handler.stop
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_stop
|
62
|
+
|
63
|
+
# Stopping a not started command handler should raise an exception.
|
64
|
+
assert_raise(IRC::Client::MessageHandlerError) { @message_handler.stop }
|
65
|
+
|
66
|
+
# Stopping a command handler after it was started should work fine.
|
67
|
+
@message_handler.start
|
68
|
+
@message_handler.stop
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
# def test_send_command
|
73
|
+
#
|
74
|
+
# # Start the command handler.
|
75
|
+
# @command_handler.start
|
76
|
+
#
|
77
|
+
# # Send the password command.
|
78
|
+
# password_command = IRC::Commands::PasswordCommand.new("secret")
|
79
|
+
# @command_handler.send_command(password_command)
|
80
|
+
#
|
81
|
+
# # Send the nick command.
|
82
|
+
# nick_command = IRC::Commands::NickCommand.new("fumanshu")
|
83
|
+
# @command_handler.send_command(nick_command)
|
84
|
+
#
|
85
|
+
# # Rewind the stringio object, so we can read from it.
|
86
|
+
# @context.output_socket.rewind
|
87
|
+
#
|
88
|
+
# # Both commands should be available immediately, since they were not sent through the queue.
|
89
|
+
# assert_equal password_command.command, @context.output_socket.readline.chomp
|
90
|
+
# assert_equal nick_command.command, @context.output_socket.readline.chomp
|
91
|
+
#
|
92
|
+
# # There should be no more commands available.
|
93
|
+
# assert_raise(EOFError) { @context.output_socket.readline }
|
94
|
+
#
|
95
|
+
# # Stop the command handler.
|
96
|
+
# @command_handler.stop
|
97
|
+
#
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# def test_send_command_via_queue
|
101
|
+
#
|
102
|
+
# # Start the command handler.
|
103
|
+
# @command_handler.start
|
104
|
+
#
|
105
|
+
# # Send the password command.
|
106
|
+
# password_command = IRC::Commands::PasswordCommand.new("secret")
|
107
|
+
# @command_handler.send_command_via_queue(password_command)
|
108
|
+
#
|
109
|
+
# # Send the nick command.
|
110
|
+
# nick_command = IRC::Commands::NickCommand.new("fumanshu")
|
111
|
+
# @command_handler << nick_command
|
112
|
+
#
|
113
|
+
# # Rewind the stringio object, so we can read from it.
|
114
|
+
# @context.output_socket.rewind
|
115
|
+
#
|
116
|
+
# # The first command got sent but the second is still in the queue.
|
117
|
+
# assert_equal password_command.command, @context.output_socket.readline.chomp
|
118
|
+
# assert_raise(EOFError) { @context.output_socket.readline }
|
119
|
+
#
|
120
|
+
# # Wait for the 2nd command.
|
121
|
+
# sleep @command_handler.delay * 1.5
|
122
|
+
#
|
123
|
+
# # Rewind again and start reading all commands from scratch.
|
124
|
+
# @context.output_socket.rewind
|
125
|
+
# assert_equal password_command.command, @context.output_socket.readline.chomp
|
126
|
+
# assert_equal nick_command.command, @context.output_socket.readline.chomp
|
127
|
+
#
|
128
|
+
# # There should be no more commands available.
|
129
|
+
# assert_raise(EOFError) { @context.output_socket.readline }
|
130
|
+
#
|
131
|
+
# # Stop the command handler.
|
132
|
+
# @command_handler.stop
|
133
|
+
#
|
134
|
+
# end
|
135
|
+
|
136
|
+
end
|