ircguerilla-irc 1.2.0 → 1.3.0

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 (53) hide show
  1. data/lib/irc/client/command_handler.rb +2 -2
  2. data/lib/irc/client/connected_state.rb +53 -8
  3. data/lib/irc/client/connection.rb +41 -6
  4. data/lib/irc/client/connection_listener.rb +26 -11
  5. data/lib/irc/client/connection_state.rb +15 -2
  6. data/lib/irc/client/context.rb +49 -11
  7. data/lib/irc/client/disconnected_state.rb +28 -16
  8. data/lib/irc/client/message_handler.rb +40 -16
  9. data/lib/irc/client/registered_state.rb +18 -0
  10. data/lib/irc/client/unregistered_state.rb +51 -16
  11. data/lib/irc/commands/nick_command.rb +14 -0
  12. data/lib/irc/messages/error_join_message.rb +1 -1
  13. data/lib/irc/messages/error_message.rb +53 -24
  14. data/lib/irc/messages/error_nick_name_in_use_message.rb +70 -0
  15. data/lib/irc/messages/factory.rb +16 -0
  16. data/lib/irc/messages/isupport_message.rb +86 -0
  17. data/lib/irc/messages/kick_message.rb +114 -0
  18. data/lib/irc/messages/message.rb +21 -1
  19. data/lib/irc/messages/nick_message.rb +17 -2
  20. data/lib/irc/messages/pong_message.rb +1 -1
  21. data/lib/irc/messages/welcome_message.rb +72 -0
  22. data/lib/irc/util/color.rb +77 -0
  23. data/lib/irc/util/nick_generator.rb +58 -0
  24. data/test/functional/irc/client/connection_test.rb +34 -1
  25. data/test/functional/irc/client/context_test.rb +5 -2
  26. data/test/mocks/test/irc/client/connection.rb +128 -0
  27. data/test/mocks/test/irc/client/disconnected_state.rb +44 -0
  28. data/test/test_helper.rb +1 -1
  29. data/test/unit/irc/client/command_handler_test.rb +12 -12
  30. data/test/unit/irc/client/message_handler_test.rb +136 -0
  31. data/test/unit/irc/commands/command_test.rb +87 -84
  32. data/test/unit/irc/commands/join_command_test.rb +3 -1
  33. data/test/unit/irc/commands/nick_command_test.rb +7 -1
  34. data/test/unit/irc/commands/ping_command_test.rb +12 -16
  35. data/test/unit/irc/commands/pong_command_test.rb +2 -1
  36. data/test/unit/irc/messages/error_join_message_test.rb +23 -2
  37. data/test/unit/irc/messages/error_message_test.rb +56 -0
  38. data/test/unit/irc/messages/error_nick_name_in_use_message_test.rb +56 -0
  39. data/test/unit/irc/messages/factory_test.rb +25 -0
  40. data/test/unit/irc/messages/isupport_message_test.rb +138 -0
  41. data/test/unit/irc/messages/join_message_test.rb +22 -1
  42. data/test/unit/irc/messages/kick_message_test.rb +76 -0
  43. data/test/unit/irc/messages/message_test.rb +33 -0
  44. data/test/unit/irc/messages/nick_message_test.rb +22 -1
  45. data/test/unit/irc/messages/notice_message_test.rb +22 -1
  46. data/test/unit/irc/messages/part_message_test.rb +22 -1
  47. data/test/unit/irc/messages/ping_message_test.rb +21 -1
  48. data/test/unit/irc/messages/pong_message_test.rb +21 -1
  49. data/test/unit/irc/messages/private_message_test.rb +22 -1
  50. data/test/unit/irc/messages/welcome_message_test.rb +59 -0
  51. data/test/unit/irc/util/color_test.rb +97 -0
  52. data/test/unit/irc/util/nick_generator_test.rb +52 -0
  53. metadata +69 -52
@@ -23,42 +23,29 @@
23
23
  #
24
24
  # $Id$
25
25
  #
26
+ require 'irc/client/connection'
26
27
  require 'irc/client/connection_listener'
28
+ require 'irc/client/context'
27
29
  require 'irc/commands/command'
30
+ require 'irc/messages/message'
28
31
  require 'irc/messages/nick_message'
32
+ require 'irc/models/network'
29
33
  require 'timeout'
30
34
  require 'test/unit'
35
+ require File.dirname(__FILE__) + '/../../../test_helper'
31
36
 
32
37
  class IRC::Commands::CommandTest < Test::Unit::TestCase
33
38
 
34
39
  include IRC::Client::ConnectionListener
35
40
 
36
- class ConnectionMock
41
+ attr_reader :connection
42
+ attr_reader :server
37
43
 
38
- attr_reader :commands
39
- attr_reader :connection_listeners
44
+ def setup
40
45
 
41
- def initialize
42
- @commands = StringIO.new
43
- @connection_listeners = Array.new
44
- end
45
-
46
- # Saves the command for later inspection in a string io object.
47
- def <<(command)
48
- @commands<<(command)
49
- end
50
-
51
- # Adds a new connection listener. The connection listener gets notified when a
52
- # message was received from the IRC server.
53
- def add_connection_listener(connection_listener)
54
- connection_listeners << connection_listener unless connection_listeners.include?(connection_listener)
55
- end
56
-
57
- # Removes a previously added connection listener. The connection listener will not
58
- # get notified any longer when a message was received from the IRC server.
59
- def remove_connection_listener(connection_listener)
60
- connection_listeners.delete(connection_listener)
61
- end
46
+ # The connection mock & the server.
47
+ @connection = IRC::Client::Connection.new
48
+ @server = IRC::Models::Network.new(:name => "Efnet").create_server("irc.efnet.pl")
62
49
 
63
50
  end
64
51
 
@@ -67,71 +54,87 @@ class IRC::Commands::CommandTest < Test::Unit::TestCase
67
54
  assert_equal "NICK Wiz", command.command
68
55
  end
69
56
 
70
- def test_execute
71
-
72
- # The connection mock.
73
- connection = ConnectionMock.new
74
-
75
- # Construct and execute the nick message.
76
- command = IRC::Commands::Command.new("NICK Wiz")
77
- command.execute(connection)
78
-
79
- # The command should be registered as a connection listener.
80
- assert connection.connection_listeners.include?(command)
81
-
82
- # Send the response message.
83
- command.on_server_response(connection, IRC::Messages::NickMessage.new(":WiZ NICK Kilroy"))
84
-
85
- # Should return immediately.
86
- command.wait
87
-
88
- # The command should no longer be registered as a connection listener.
89
- assert !connection.connection_listeners.include?(command)
90
-
91
- # The command should be available at the connection object.
92
- connection.commands.rewind
93
- assert_equal command.command, connection.commands.readline
94
-
95
- end
96
-
97
57
  def test_to_s
98
58
  command = IRC::Commands::Command.new("NICK Wiz")
99
59
  assert_equal "NICK Wiz", command.to_s
100
- end
101
-
102
- def test_wait
60
+ end
103
61
 
104
- # The connection mock.
105
- connection = ConnectionMock.new
106
-
107
- # Construct and execute the nick message.
62
+ def test_valid_response
108
63
  command = IRC::Commands::Command.new("NICK Wiz")
109
- command.execute(connection)
110
-
111
- # The command should be registered as a connection listener.
112
- assert connection.connection_listeners.include?(command)
113
-
114
- # The wait method should block until a response was received. Because the VALID response
115
- # hasn't been received yet this method should block forever.
116
- assert_raise(Timeout::Error) do
117
- Timeout::timeout(0.1) { command.wait }
118
- end
119
-
120
- # Send the response message.
121
- command.on_server_response(connection, IRC::Messages::NickMessage.new(":WiZ NICK Kilroy"))
122
-
123
- # Now the methos shouldn't block anymore.
124
- Timeout::timeout(0.1) do
125
- command.wait
126
- end
127
-
128
- # The command should no longer be registered as a connection listener.
129
- assert !connection.connection_listeners.include?(command)
130
-
131
- # The command should be available at the connection object.
132
- connection.commands.rewind
133
- assert_equal command.command, connection.commands.readline
134
-
64
+ assert command.valid_response?(IRC::Messages::Message.new(":WiZ NICK Kilroy"))
135
65
  end
136
66
 
67
+ # def test_on_server_response
68
+ #
69
+ # # Simulate a connection.
70
+ # connection.connect(server)
71
+ #
72
+ # # Register the response message.
73
+ # message = IRC::Messages::Message.new(":WiZ NICK Kilroy")
74
+ # connection.register_raw_message(message.to_s)
75
+ #
76
+ # command = IRC::Commands::Command.new("NICK Wiz")
77
+ # command.execute(connection)
78
+ #
79
+ # sleep 1
80
+ #
81
+ # assert_equal message, command.response
82
+ #
83
+ # end
84
+
85
+
86
+ #
87
+ # def test_execute
88
+ #
89
+ # # Construct and execute the nick message.
90
+ # command = IRC::Commands::Command.new("NICK Wiz")
91
+ # command.execute(connection)
92
+ #
93
+ # # The command should be registered as a connection listener.
94
+ # assert connection.connection_listeners.include?(command)
95
+ #
96
+ # # Send the response message.
97
+ # command.on_server_response(connection, IRC::Messages::NickMessage.new(":WiZ NICK Kilroy"))
98
+ #
99
+ # # Should return immediately.
100
+ # command.wait
101
+ #
102
+ # # The command should no longer be registered as a connection listener.
103
+ # assert !connection.connection_listeners.include?(command)
104
+ #
105
+ # end
106
+ #
107
+ # def test_to_s
108
+ # command = IRC::Commands::Command.new("NICK Wiz")
109
+ # assert_equal "NICK Wiz", command.to_s
110
+ # end
111
+ #
112
+ # def test_wait
113
+ #
114
+ # # Construct and execute the nick message.
115
+ # command = IRC::Commands::Command.new("NICK Wiz")
116
+ # command.execute(connection)
117
+ #
118
+ # # The command should be registered as a connection listener.
119
+ # assert connection.connection_listeners.include?(command)
120
+ #
121
+ # # The wait method should block until a response was received. Because the VALID response
122
+ # # hasn't been received yet this method should block forever.
123
+ # assert_raise(Timeout::Error) do
124
+ # Timeout::timeout(0.1) { command.wait }
125
+ # end
126
+ #
127
+ # # Send the response message.
128
+ # command.on_server_response(connection, IRC::Messages::NickMessage.new(":WiZ NICK Kilroy"))
129
+ #
130
+ # # Now the methos shouldn't block anymore.
131
+ # Timeout::timeout(0.1) do
132
+ # command.wait
133
+ # end
134
+ #
135
+ # # The command should no longer be registered as a connection listener.
136
+ # assert !connection.connection_listeners.include?(command)
137
+ #
138
+ # end
139
+
137
140
  end
@@ -23,11 +23,13 @@
23
23
  #
24
24
  # $Id: join_command_test.rb 85 2006-08-13 11:42:07Z roman $
25
25
  #
26
+ require 'irc/commands/command_test'
26
27
  require 'irc/commands/join_command'
28
+ require 'irc/messages/join_message'
27
29
  require 'irc/models/network'
28
30
  require 'test/unit'
29
31
 
30
- class IRC::Commands::JoinCommandTest < Test::Unit::TestCase
32
+ class IRC::Commands::JoinCommandTest < IRC::Commands::CommandTest
31
33
 
32
34
  def test_command_with_name
33
35
  assert_equal "JOIN #ircguerilla", IRC::Commands::JoinCommand.new("#ircguerilla").command
@@ -23,10 +23,16 @@
23
23
  #
24
24
  # $Id: nick_command_test.rb 85 2006-08-13 11:42:07Z roman $
25
25
  #
26
+ require 'irc/messages/codes'
27
+ require 'irc/client/unregistered_state'
28
+ require 'irc/commands/command_test'
26
29
  require 'irc/commands/nick_command'
30
+ require 'irc/commands/user_command'
31
+ require 'stringio'
32
+ require 'timeout'
27
33
  require 'test/unit'
28
34
 
29
- class IRC::Commands::NickCommandTest < Test::Unit::TestCase
35
+ class IRC::Commands::NickCommandTest < IRC::Commands::CommandTest
30
36
 
31
37
  def test_command
32
38
  assert_equal "NICK fumanshu 12", IRC::Commands::NickCommand.new("fumanshu", 12).command
@@ -37,8 +37,8 @@ class IRC::Commands::PingCommandTest < IRC::Commands::CommandTest
37
37
 
38
38
  def test_execute
39
39
 
40
- # The connection mock.
41
- connection = ConnectionMock.new
40
+ # Simulate a connection.
41
+ connection.connect(server)
42
42
 
43
43
  # Construct and execute the nick message.
44
44
  command = IRC::Commands::PingCommand.new("tolsun.oulu.fi")
@@ -55,10 +55,6 @@ class IRC::Commands::PingCommandTest < IRC::Commands::CommandTest
55
55
 
56
56
  # The command should no longer be registered as a connection listener.
57
57
  assert !connection.connection_listeners.include?(command)
58
-
59
- # The command should be available at the connection object.
60
- connection.commands.rewind
61
- assert_equal command.command, connection.commands.readline
62
58
 
63
59
  end
64
60
 
@@ -69,8 +65,8 @@ class IRC::Commands::PingCommandTest < IRC::Commands::CommandTest
69
65
 
70
66
  def test_wait_for_pong
71
67
 
72
- # The connection mock.
73
- connection = ConnectionMock.new
68
+ # Simulate a connection.
69
+ connection.connect(server)
74
70
 
75
71
  # Construct and execute the nick message.
76
72
  command = IRC::Commands::PingCommand.new("tolsun.oulu.fi")
@@ -101,9 +97,9 @@ class IRC::Commands::PingCommandTest < IRC::Commands::CommandTest
101
97
 
102
98
  def test_wait_for_error_no_origin
103
99
 
104
- # The connection mock.
105
- connection = ConnectionMock.new
106
-
100
+ # Simulate a connection.
101
+ connection.connect(server)
102
+
107
103
  # Construct and execute the nick message.
108
104
  command = IRC::Commands::PingCommand.new("tolsun.oulu.fi")
109
105
  command.execute(connection)
@@ -114,7 +110,7 @@ class IRC::Commands::PingCommandTest < IRC::Commands::CommandTest
114
110
  # The wait method should block until a response was received. Because the VALID response
115
111
  # hasn't been received yet this method should block forever.
116
112
  assert_raise(Timeout::Error) do
117
- command.on_server_response(connection, IRC::Messages::ErrorMessage.new(":irc.easynews.com 4XX"))
113
+ command.on_server_response(connection, IRC::Messages::Message.new(":irc.easynews.com 4XX"))
118
114
  Timeout::timeout(0.1) { command.wait }
119
115
  end
120
116
 
@@ -132,9 +128,9 @@ class IRC::Commands::PingCommandTest < IRC::Commands::CommandTest
132
128
  end
133
129
 
134
130
  def test_wait_for_error_no_such_server
135
-
136
- # The connection mock.
137
- connection = ConnectionMock.new
131
+
132
+ # Simulate a connection.
133
+ connection.connect(server)
138
134
 
139
135
  # Construct and execute the nick message.
140
136
  command = IRC::Commands::PingCommand.new("tolsun.oulu.fi")
@@ -146,7 +142,7 @@ class IRC::Commands::PingCommandTest < IRC::Commands::CommandTest
146
142
  # The wait method should block until a response was received. Because the VALID response
147
143
  # hasn't been received yet this method should block forever.
148
144
  assert_raise(Timeout::Error) do
149
- command.on_server_response(connection, IRC::Messages::ErrorMessage.new(":irc.easynews.com 4XX"))
145
+ command.on_server_response(connection, IRC::Messages::Message.new(":irc.easynews.com 4XX"))
150
146
  Timeout::timeout(0.1) { command.wait }
151
147
  end
152
148
 
@@ -23,10 +23,11 @@
23
23
  #
24
24
  # $Id: pong_command_test.rb 85 2006-08-13 11:42:07Z roman $
25
25
  #
26
+ require 'irc/commands/command_test'
26
27
  require 'irc/commands/pong_command'
27
28
  require 'test/unit'
28
29
 
29
- class IRC::Commands::PongCommandTest < Test::Unit::TestCase
30
+ class IRC::Commands::PongCommandTest < IRC::Commands::CommandTest
30
31
 
31
32
  def test_command
32
33
  assert_equal "PONG csd.bu.edu", IRC::Commands::PongCommand.new("csd.bu.edu").command
@@ -25,11 +25,16 @@
25
25
  # $Id$
26
26
  #
27
27
  require 'irc/messages/invalid_message'
28
+ require 'irc/messages/message_test'
28
29
  require 'irc/messages/error_join_message'
29
30
  require 'test/unit'
30
31
 
31
- class IRC::Messages::ErrorJoinMessageTest < Test::Unit::TestCase
32
-
32
+ class IRC::Messages::ErrorJoinMessageTest < IRC::Messages::MessageTest
33
+
34
+ # This message gets called when a channel join attempt failed.
35
+ def on_join_failure(connection, channel, code, reason)
36
+ @channel, @code, @reason = channel, code, reason
37
+ end
33
38
 
34
39
  def test_error_no_such_channel
35
40
 
@@ -42,4 +47,20 @@ class IRC::Messages::ErrorJoinMessageTest < Test::Unit::TestCase
42
47
 
43
48
  end
44
49
 
50
+ def test_handle_error_no_such_channel
51
+
52
+ # Simulate a connection registration.
53
+ connection.add_connection_listener(self)
54
+ connection.connect(server)
55
+ connection.register
56
+
57
+ message = IRC::Messages::ErrorJoinMessage.new(":irc.easynews.com 403 fumanshu invalid :No such channel")
58
+ message.handle(connection.context)
59
+
60
+ assert_equal "invalid", @channel.name
61
+ assert_equal 403, @code
62
+ assert_equal "No such channel", @reason
63
+
64
+ end
65
+
45
66
  end
@@ -0,0 +1,56 @@
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: nick_message_test.rb 89 2006-08-13 14:03:35Z roman $
25
+ #
26
+ require 'irc/messages/invalid_message'
27
+ require 'irc/messages/message_test'
28
+ require 'irc/messages/error_message'
29
+ require 'test/unit'
30
+
31
+ class IRC::Messages::ErrorMessageTest < IRC::Messages::MessageTest
32
+
33
+ # This method gets called an error message was received from the IRC server.
34
+ def on_error(connection, reason)
35
+ @reason = reason
36
+ end
37
+
38
+ def test_message
39
+ message = IRC::Messages::ErrorMessage.new("ERROR :Closing Link: 85.178.111.249 (*** Banned (cache))")
40
+ assert_equal "Closing Link: 85.178.111.249 (*** Banned (cache))", message.reason
41
+ end
42
+
43
+ def test_handle
44
+
45
+ # Simulate a connection registration.
46
+ connection.add_connection_listener(self)
47
+ connection.connect(server)
48
+ connection.register
49
+
50
+ message = IRC::Messages::ErrorMessage.new("ERROR :Closing Link: 85.178.111.249 (*** Banned (cache))")
51
+ message.handle(connection.context)
52
+ assert_equal "Closing Link: 85.178.111.249 (*** Banned (cache))", @reason
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,56 @@
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: nick_message_test.rb 89 2006-08-13 14:03:35Z roman $
25
+ #
26
+ require 'irc/messages/invalid_message'
27
+ require 'irc/messages/message_test'
28
+ require 'irc/messages/error_nick_name_in_use_message'
29
+ require 'test/unit'
30
+
31
+ class IRC::Messages::ErrorNickNameInUseMessageTest < IRC::Messages::MessageTest
32
+
33
+ # This method gets called when the chosen nick name is already in use.
34
+ def on_nick_already_in_use(connection, nick_name_in_use)
35
+ @nick_name_in_use = nick_name_in_use
36
+ end
37
+
38
+ def test_nick_change
39
+ message = IRC::Messages::ErrorNickNameInUseMessage.new(":irc.easynews.com 433 * fumanshu :Nickname is already in use.")
40
+ assert_equal "fumanshu", message.nick_name_in_use
41
+ end
42
+
43
+ def test_handle
44
+
45
+ # Simulate a connection registration.
46
+ connection.add_connection_listener(self)
47
+ connection.connect(server)
48
+ connection.register
49
+
50
+ message = IRC::Messages::ErrorNickNameInUseMessage.new(":irc.easynews.com 433 * fumanshu :Nickname is already in use.")
51
+ message.handle(connection.context)
52
+ assert_equal "fumanshu", @nick_name_in_use
53
+
54
+ end
55
+
56
+ end