ircguerilla-irc 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,16 +70,6 @@ module IRC
70
70
 
71
71
  end
72
72
 
73
- # Sends the command to the server bypassing the command queue.
74
- def send_command(context, command)
75
- context.command_handler.send_command(command)
76
- end
77
-
78
- # Sends the command to the server using the command queue.
79
- def send_command_via_queue(context, command)
80
- context.command_handler.send_command_via_queue(command)
81
- end
82
-
83
73
  # ConnectionListener
84
74
 
85
75
  # This method gets called when disconnected from a server.
@@ -87,6 +77,11 @@ module IRC
87
77
  log.debug("#{network_name(connection)} Successfully disconnected from #{server}. #{reason != nil ? reason + '.' : ''}")
88
78
  end
89
79
 
80
+ # This method gets called when a ping message was received from the server.
81
+ def on_ping(connection, servers)
82
+ IRC::Commands::PongCommand.new(servers[0], servers[1]).execute(connection)
83
+ end
84
+
90
85
 
91
86
  end
92
87
 
@@ -23,8 +23,6 @@
23
23
  #
24
24
  # $Id: connection_listener.rb 90 2006-08-13 14:45:04Z roman $
25
25
  #
26
- require 'irc/commands/pong_command'
27
- require 'irc/models/server'
28
26
  require 'log4r'
29
27
 
30
28
  module IRC
@@ -57,10 +57,12 @@ module IRC
57
57
 
58
58
  # Sends the command to the server bypassing the command queue.
59
59
  def send_command(context, command)
60
+ context.command_handler.send_command(command)
60
61
  end
61
62
 
62
63
  # Sends the command to the server using the command queue.
63
64
  def send_command_via_queue(context, command)
65
+ context.command_handler.send_command_via_queue(command)
64
66
  end
65
67
 
66
68
  # ConnectionListener
@@ -209,7 +209,7 @@ module IRC
209
209
 
210
210
  # Sends the command to the server bypassing the command queue.
211
211
  def send_command(command)
212
- state.send_command(self, command)
212
+ state.send_command(self, command)
213
213
  end
214
214
 
215
215
  # Sends the command to the server using the command queue.
@@ -51,7 +51,7 @@ module IRC
51
51
  loop do
52
52
 
53
53
  # Read the raw message from the socket.
54
- raw_message = @context.socket.gets("\r\n")
54
+ raw_message = socket.gets("\r\n")
55
55
 
56
56
  # Log the raw message.
57
57
  @log.debug("[#{@context.network.to_s.upcase}] <<< #{raw_message.chomp}")
@@ -80,6 +80,12 @@ module IRC
80
80
 
81
81
  end
82
82
 
83
+ private
84
+
85
+ def socket
86
+ return @context.socket
87
+ end
88
+
83
89
  end
84
90
 
85
91
  end
@@ -46,12 +46,12 @@ module IRC
46
46
 
47
47
  # Joins the given channels.
48
48
  def join(context, channels)
49
- IRC::Commands::JoinCommand.new(channels).send(context)
49
+ IRC::Commands::JoinCommand.new(channels).execute(context)
50
50
  end
51
51
 
52
52
  # Leaves the given channels.
53
53
  def part(context, channels)
54
- IRC::Commands::PartCommand.new(channels).send(context)
54
+ IRC::Commands::PartCommand.new(channels).execute(context)
55
55
  end
56
56
 
57
57
  # ConnectionListener
@@ -27,6 +27,9 @@ require 'irc/client/client_error'
27
27
  require 'irc/client/connected_state'
28
28
  require 'irc/client/registered_state'
29
29
  require 'irc/messages/error_message'
30
+ require 'irc/commands/nick_command'
31
+ require 'irc/messages/notice_message'
32
+ require 'irc/commands/user_command'
30
33
  require 'log4r'
31
34
 
32
35
  module IRC
@@ -58,40 +61,42 @@ module IRC
58
61
 
59
62
  # This method gets called when a message from the server is received.
60
63
  def on_server_response(context, message)
61
-
62
- # The given nick name is not valid.
63
- if message.instance_of?(IRC::Messages::ErrorErroneusNickNameMessage)
64
- registration_failure(context, message)
65
-
66
- # We didn't send a valid command. There are some parameters missing.
67
- elsif message.instance_of?(IRC::Messages::ErrorNeedMoreParametersMessage)
68
- registration_failure(context, message)
69
-
70
- # Nick name collision.
71
- elsif message.instance_of?(IRC::Messages::ErrorNickCollisionMessage)
72
- registration_failure(context, message)
73
-
74
- # The chosen nick name is already in use.
75
- elsif message.instance_of?(IRC::Messages::ErrorNickNameInUseMessage)
76
- registration_failure(context, message)
77
-
78
- # We didn't send a valid nick command. The nick name was missing.
79
- elsif message.instance_of?(IRC::Messages::ErrorNoNickNameGivenMessage)
80
- registration_failure(context, message)
64
+
65
+ case message.code
81
66
 
82
- # Ignore notice messages.
83
- elsif message.instance_of?(IRC::Messages::NoticeMessage)
84
- return
85
-
86
- # We are already registered. Did we miss something?
87
- elsif message.instance_of?(IRC::Messages::ErrorAlreadyRegisteredMessage)
88
- change_state(context, RegisteredState.instance)
67
+ # Ignore notice messages.
68
+ when IRC::Messages::NoticeMessage::CODE
69
+ return
89
70
 
90
- # Everything ok. Change to the registered state.
91
- else
92
- change_state(context, RegisteredState.instance)
93
-
94
- end
71
+ # The given nick name is not valid.
72
+ when IRC::Messages::ErrorErroneusNickNameMessage::CODE
73
+ registration_failure(context, message)
74
+
75
+ # We didn't send a valid command. There are some parameters missing.
76
+ when IRC::Messages::ErrorNeedMoreParametersMessage::CODE
77
+ registration_failure(context, message)
78
+
79
+ # Nick name collision.
80
+ when IRC::Messages::ErrorNickCollisionMessage::CODE
81
+ registration_failure(context, message)
82
+
83
+ # The chosen nick name is already in use.
84
+ when IRC::Messages::ErrorNickNameInUseMessage::CODE
85
+ registration_failure(context, message)
86
+
87
+ # We didn't send a valid nick command. The nick name was missing.
88
+ when IRC::Messages::ErrorNoNickNameGivenMessage::CODE
89
+ registration_failure(context, message)
90
+
91
+ # We are already registered. Did we miss something?
92
+ when IRC::Messages::ErrorAlreadyRegisteredMessage::CODE
93
+ change_state(context, RegisteredState.instance)
94
+
95
+ # Everything ok. Change to the registered state.
96
+ else
97
+ change_state(context, RegisteredState.instance)
98
+
99
+ end
95
100
 
96
101
  end
97
102
 
@@ -103,6 +108,9 @@ module IRC
103
108
  context.connection_listeners.each do |connection_listener|
104
109
  connection_listener.on_registration_failure(context, message)
105
110
  end
111
+
112
+ IRC::Commands::NickCommand.new("au_lasa").execute(context)
113
+ IRC::Commands::UserCommand.new("au_lasa", Socket.gethostname, server.hostname, context.realname).execute(context)
106
114
 
107
115
  end
108
116
 
@@ -23,15 +23,23 @@
23
23
  #
24
24
  # $Id: command.rb 85 2006-08-13 11:42:07Z roman $
25
25
  #
26
+ require 'irc/client/connection_listener'
26
27
  require 'irc/commands/invalid_command'
28
+ require 'monitor'
27
29
 
28
30
  module IRC
29
31
 
30
32
  module Commands
31
33
 
32
- class Command
34
+ class Command < Monitor
35
+
36
+ include IRC::Client::ConnectionListener
37
+
38
+ # The message that was sent by the IRC server as a response to the execution of the command.
39
+ attr_reader :response
33
40
 
34
41
  def initialize(raw_command = "")
42
+ super()
35
43
  @raw_command = raw_command
36
44
  end
37
45
 
@@ -40,15 +48,65 @@ module IRC
40
48
  return @raw_command
41
49
  end
42
50
 
43
- # Sends the command to the io object.
44
- def send(io)
45
- io << self
51
+ # Execute the command. The method registers the command as an connection listener
52
+ # at the connection object and sends the command. The method returns the command.
53
+ def execute(connection)
54
+
55
+ # Register the command as a connection listener, so we can receive response messages
56
+ # from the server.
57
+ connection.add_connection_listener(self)
58
+
59
+ # Send the message.
60
+ connection << self
61
+
62
+ # Create a conditinal monitor variable.
63
+ self.synchronize do
64
+ @response_received ||= self.new_cond
65
+ end
66
+
67
+ return self
68
+
46
69
  end
47
70
 
48
71
  # Returns the command as a string.
49
72
  def to_s
50
73
  return command
51
74
  end
75
+
76
+ # Returns true, if the message is a valid response to the command. This method should get
77
+ # overriddn in subclasses. For this class every response is valid.
78
+ def valid_response?(message)
79
+ return true
80
+ end
81
+
82
+ # Waits until a response for the command was sent by the IRC server. This method expects,
83
+ # that the command has already been sent to the server by the execute method. A call to
84
+ # this method blocks until a response was reseived from the server.
85
+ def wait
86
+
87
+ # If the command has not been sent, or a response was already received. a
88
+ # call to this method doesn't make any sense.
89
+ raise Exception.new("Can't wait for response. The command was not send yet, or a response was already received.") if @response_received == nil
90
+
91
+ # Wait until a response was received from the server.
92
+ synchronize do
93
+ @response_received.wait_until { response != nil }
94
+ end
95
+
96
+ end
97
+
98
+ # CONNECTION LISTENER
99
+
100
+ # This method gets called when a message from the server is received. This method should be
101
+ # overridden in subclasses to handle the command specific response messages from the server.
102
+ def on_server_response(connection, message)
103
+
104
+ if valid_response?(message)
105
+ connection.remove_connection_listener(self)
106
+ @response = message
107
+ end
108
+
109
+ end
52
110
 
53
111
  end
54
112
 
@@ -25,6 +25,7 @@
25
25
  #
26
26
  require 'irc/commands/command'
27
27
  require 'irc/commands/invalid_command'
28
+ require 'irc/messages/codes'
28
29
 
29
30
  module IRC
30
31
 
@@ -109,12 +110,26 @@ module IRC
109
110
  @passwords << passwords
110
111
  end
111
112
 
113
+ super(command)
114
+
112
115
  end
113
116
 
114
117
  def command
115
118
  return "JOIN #{channels.join(',')} #{passwords.join(',')}".strip
116
119
  end
117
120
 
121
+ # Returns true, if the message is a valid response to the command.
122
+ def valid_response?(message)
123
+
124
+ valid_reponse_codes = [IRC::Messages::Codes::ERR_NEEDMOREPARAMS, IRC::Messages::Codes::ERR_BANNEDFROMCHAN,
125
+ IRC::Messages::Codes::ERR_INVITEONLYCHAN, IRC::Messages::Codes::ERR_BADCHANNELKEY, IRC::Messages::Codes::ERR_CHANNELISFULL,
126
+ IRC::Messages::Codes::ERR_BADCHANMASK, IRC::Messages::Codes::ERR_NOSUCHCHANNEL, IRC::Messages::Codes::ERR_TOOMANYCHANNELS,
127
+ IRC::Messages::Codes::RPL_TOPIC]
128
+
129
+ return valid_reponse_codes.include?(message.code)
130
+
131
+ end
132
+
118
133
  end
119
134
 
120
135
  end
@@ -73,6 +73,7 @@ module IRC
73
73
  raise InvalidCommand.new("Can't create nick command. No nick name.") unless nick
74
74
  @nick = nick
75
75
  @hopcount = hopcount
76
+ super(command)
76
77
  end
77
78
 
78
79
  def command
@@ -71,6 +71,8 @@ module IRC
71
71
 
72
72
  end
73
73
 
74
+ super(command)
75
+
74
76
  end
75
77
 
76
78
  def command
@@ -59,6 +59,7 @@ module IRC
59
59
  def initialize(password)
60
60
  raise InvalidCommand.new("Can't create password command. No password.") unless password
61
61
  @password = password
62
+ super(command)
62
63
  end
63
64
 
64
65
  def command
@@ -25,6 +25,8 @@
25
25
  #
26
26
  require 'irc/commands/command'
27
27
  require 'irc/commands/invalid_command'
28
+ require 'irc/messages/error_message'
29
+ require 'irc/messages/pong_message'
28
30
 
29
31
  module IRC
30
32
 
@@ -66,12 +68,29 @@ module IRC
66
68
  def initialize(first_server, second_server = nil)
67
69
  raise InvalidCommand.new("Can't create ping command. No server.") unless first_server
68
70
  @first_server, @second_server = first_server, second_server
71
+ super(command)
69
72
  end
70
73
 
74
+ # Returns the command as a string.
71
75
  def command
72
76
  return "PING #{first_server} #{second_server != nil ? second_server.to_s : ''}".strip
73
77
  end
74
78
 
79
+ # Returns true, if the message is a valid response to the command.
80
+ def valid_response?(message)
81
+
82
+ if message.kind_of?(IRC::Messages::PongMessage)
83
+ return first_server == message.daemons[0] && second_server == message.daemons[1]
84
+
85
+ elsif message.kind_of?(IRC::Messages::ErrorNoOriginMessage) || message.kind_of?(IRC::Messages::ErrorNoSuchServerMessage)
86
+ return true
87
+
88
+ end
89
+
90
+ return false
91
+
92
+ end
93
+
75
94
  end
76
95
 
77
96
  end
@@ -32,7 +32,7 @@ module IRC
32
32
 
33
33
  # 4.6.3 Pong message
34
34
  #
35
- # Command: PONG
35
+ # Command: PONG
36
36
  # Parameters: <daemon> [<daemon2>]
37
37
  #
38
38
  # PONG message is a reply to ping message. If parameter <daemon2> is given this message must
@@ -53,8 +53,11 @@ module IRC
53
53
  attr_reader :second_daemon
54
54
 
55
55
  def initialize(first_daemon, second_daemon = nil)
56
+
56
57
  raise InvalidCommand.new("Can't create ping command. No daemon.") unless first_daemon
57
58
  @first_daemon, @second_daemon = first_daemon, second_daemon
59
+ super(command)
60
+
58
61
  end
59
62
 
60
63
  def command
@@ -62,8 +62,12 @@ module IRC
62
62
  attr_reader :message
63
63
 
64
64
  def initialize(message)
65
+
65
66
  raise InvalidCommand.new("Can't create quit command. No quit message.") unless message
66
67
  @message = message
68
+
69
+ super(command)
70
+
67
71
  end
68
72
 
69
73
  def command
@@ -58,8 +58,8 @@ module IRC
58
58
  #
59
59
  # Numeric Replies:
60
60
  #
61
- # ERR_NEEDMOREPARAMS
62
- # ERR_ALREADYREGISTRED
61
+ # * ERR_NEEDMOREPARAMS
62
+ # * ERR_ALREADYREGISTRED
63
63
  #
64
64
  # Examples:
65
65
  #
@@ -80,18 +80,20 @@ module IRC
80
80
 
81
81
  def initialize(user, host, server, realname)
82
82
 
83
- raise InvalidCommand.new("Can't create user command. No user name.") unless user
83
+ raise InvalidCommand.new("Can't create user command. User name is missing.") unless user
84
84
  @user = user
85
85
 
86
- raise InvalidCommand.new("Can't create user command. No host.") unless host
86
+ raise InvalidCommand.new("Can't create user command. Host is missing.") unless host
87
87
  @host = host
88
88
 
89
- raise InvalidCommand.new("Can't create user command. No server.") unless server
89
+ raise InvalidCommand.new("Can't create user command. Server is missing.") unless server
90
90
  @server = server
91
91
 
92
- raise InvalidCommand.new("Can't create user command. No real name.") unless realname
92
+ raise InvalidCommand.new("Can't create user command. Real name is missing.") unless realname
93
93
  @realname = realname
94
-
94
+
95
+ super(command)
96
+
95
97
  end
96
98
 
97
99
  def command
@@ -77,17 +77,25 @@ module IRC
77
77
  end
78
78
 
79
79
  class ErrorNickNameInUseMessage < ErrorMessage
80
- CODE = ERR_ERRONEUSNICKNAME
80
+ CODE = ERR_NICKNAMEINUSE
81
81
  end
82
82
 
83
83
  class ErrorNoNickNameGivenMessage < ErrorMessage
84
84
  CODE = ERR_NONICKNAMEGIVEN
85
85
  end
86
86
 
87
+ class ErrorNoOriginMessage < ErrorMessage
88
+ CODE = ERR_NOORIGIN
89
+ end
90
+
87
91
  class ErrorNoSuchChannelMessage < ErrorJoinMessage
88
92
  CODE = ERR_NOSUCHCHANNEL
89
93
  end
90
94
 
95
+ class ErrorNoSuchServerMessage < ErrorMessage
96
+ CODE = ERR_NOSUCHSERVER
97
+ end
98
+
91
99
  class ErrorNotOnChannelMessage < ErrorMessage
92
100
  CODE = ERR_NOTONCHANNEL
93
101
  end
@@ -31,6 +31,7 @@ require 'irc/messages/notice_message'
31
31
  require 'irc/messages/part_message'
32
32
  require 'irc/messages/ping_message'
33
33
  require 'irc/messages/pong_message'
34
+ require 'irc/messages/private_message'
34
35
 
35
36
  module IRC
36
37
 
@@ -81,9 +82,15 @@ module IRC
81
82
 
82
83
  when ErrorNoNickNameGivenMessage::CODE
83
84
  return ErrorNoNickNameGivenMessage.new(raw_message)
85
+
86
+ when ErrorNoOriginMessage::CODE
87
+ return ErrorNoOriginMessage.new(raw_message)
84
88
 
85
89
  when ErrorNoSuchChannelMessage::CODE
86
90
  return ErrorNoSuchChannelMessage.new(raw_message)
91
+
92
+ when ErrorNoSuchServerMessage::CODE
93
+ return ErrorNoSuchServerMessage.new(raw_message)
87
94
 
88
95
  when ErrorNotOnChannelMessage::CODE
89
96
  return ErrorNotOnChannelMessage.new(raw_message)
@@ -108,6 +115,9 @@ module IRC
108
115
 
109
116
  when PongMessage::CODE
110
117
  return PongMessage.new(raw_message)
118
+
119
+ when PrivateMessage::CODE
120
+ return PrivateMessage.new(raw_message)
111
121
 
112
122
  end
113
123
 
@@ -96,7 +96,10 @@ module IRC
96
96
  @host = match_data[6]
97
97
 
98
98
  # Extract the IRC message code. If the code is a number, convert it to an integer.
99
- @code = (match_data[7] =~ /\d+/ ? match_data[7].to_i : match_data[7])
99
+ @code = (match_data[7].match(/\d+/) ? match_data[7].to_i : match_data[7])
100
+
101
+ # puts @code
102
+ # puts @code.class
100
103
 
101
104
  # Extract the rest of the message. The message is everything after the IRC message code.
102
105
  @message = match_data[8]
@@ -75,7 +75,7 @@ module IRC
75
75
 
76
76
  # Extract the message specific fields.
77
77
  match_data = Regexp.new(':?([^\s:]+)(\s+:?(.+))?').match(message)
78
- raise InvalidMessage.new("Can't parse ping message. Invalid message format.") if match_data == nil || code != CODE
78
+ raise InvalidMessage.new("Can't parse pong message. Invalid message format.") if match_data == nil || code != CODE
79
79
 
80
80
  # Extract the servers and strip white spaces.
81
81
  @daemons = [match_data[1], match_data[3]].compact.map! { |element| element.strip }
@@ -42,7 +42,7 @@ class IRC::Client::ConnectionTest < Test::Unit::TestCase
42
42
  # Initialize the network, channel & server that is used for testing.
43
43
  network = IRC::Models::Network.new(:name => "Efnet")
44
44
  @channel = network.create_channel("#MP3_RAP_N_REGGAE")
45
- @server = network.create_server("irc.efnet.net")
45
+ @server = network.create_server("irc.efnet.pl")
46
46
 
47
47
  # Initialize the connection connection.
48
48
  @connection = IRC::Client::Connection::Context.new("fumanshu")
@@ -58,20 +58,17 @@ class IRC::Client::ConnectionTest < Test::Unit::TestCase
58
58
  Timeout::timeout(IRC::Client::Connection::MAX_CONNECTION_TIMEOUT_IN_SEC) do
59
59
  @connection.connect(@server)
60
60
  while !@connection.connected? do sleep 0.1; end
61
- assert @connection.connected?
62
61
  end
63
62
 
64
63
  # Wait until the connection is registered.
65
64
  Timeout::timeout(5) do
66
65
  while !@connection.registered? do sleep 0.1; end
67
- assert @connection.registered?
68
66
  end
69
67
 
70
68
  # Disconnect and wait until the connection is in the disconnected state.
71
69
  Timeout::timeout(5) do
72
70
  @connection.disconnect
73
71
  while @connection.connected? do sleep 0.1; end
74
- assert !@connection.connected?
75
72
  end
76
73
 
77
74
  end
@@ -85,13 +82,11 @@ class IRC::Client::ConnectionTest < Test::Unit::TestCase
85
82
  Timeout::timeout(IRC::Client::Connection::MAX_CONNECTION_TIMEOUT_IN_SEC) do
86
83
  @connection.connect(@server)
87
84
  while !@connection.connected? do sleep 0.1; end
88
- assert @connection.connected?
89
85
  end
90
86
 
91
87
  # Wait until the connection is registered.
92
88
  Timeout::timeout(5) do
93
89
  while !@connection.registered? do sleep 0.1; end
94
- assert @connection.registered?
95
90
  end
96
91
 
97
92
  # Join a channel & wait until the channel is joined and the block was called.
@@ -110,7 +105,6 @@ class IRC::Client::ConnectionTest < Test::Unit::TestCase
110
105
  Timeout::timeout(5) do
111
106
  @connection.disconnect
112
107
  while @connection.connected? do sleep 0.1; end
113
- assert !@connection.connected?
114
108
  end
115
109
 
116
110
  end
@@ -42,7 +42,7 @@ class IRC::Client::ContextTest < Test::Unit::TestCase
42
42
  # Initialize the network, channel & server that is used for testing.
43
43
  network = IRC::Models::Network.new(:name => "Efnet")
44
44
  @channel = network.create_channel("#ircguerilla")
45
- @server = network.create_server("irc.efnet.net")
45
+ @server = network.create_server("irc.efnet.pl")
46
46
 
47
47
  # Initialize the connection context.
48
48
  @context = IRC::Client::Connection::Context.new("fumanshu")
@@ -60,8 +60,7 @@ class IRC::Client::ContextTest < Test::Unit::TestCase
60
60
  # Connect to the server and wait until the context is in the unregistered state.
61
61
  Timeout::timeout(IRC::Client::Connection::MAX_CONNECTION_TIMEOUT_IN_SEC) do
62
62
  @context.connect(@server)
63
- while @context.state.instance_of?(IRC::Client::DisconnectedState) do sleep 0.1; end
64
- assert @context.state.instance_of?(IRC::Client::UnregisteredState)
63
+ while !@context.connected? do sleep 0.1; end
65
64
  end
66
65
 
67
66
  # The context should be connected now.
@@ -69,17 +68,13 @@ class IRC::Client::ContextTest < Test::Unit::TestCase
69
68
 
70
69
  # Wait until the connection is registered.
71
70
  Timeout::timeout(5) do
72
- while @context.state.instance_of?(IRC::Client::UnregisteredState) do sleep 0.1; end
73
- assert @context.state.instance_of?(IRC::Client::RegisteredState)
71
+ while !@context.registered? do sleep 0.1; end
74
72
  end
75
-
76
- IRC::Commands::Command.new("JOIN a").send(@context)
77
- sleep 5
78
73
 
79
74
  # Disconnect and wait until the context is in the disconnected state.
80
75
  Timeout::timeout(5) do
81
76
  @context.disconnect
82
- assert @context.state.instance_of?(IRC::Client::DisconnectedState)
77
+ while @context.connected? do sleep 0.1; end
83
78
  end
84
79
 
85
80
  end
@@ -92,14 +87,12 @@ class IRC::Client::ContextTest < Test::Unit::TestCase
92
87
  # Connect to the server and wait until the context is in the unregistered state.
93
88
  Timeout::timeout(IRC::Client::Connection::MAX_CONNECTION_TIMEOUT_IN_SEC) do
94
89
  @context.connect(@server)
95
- while @context.state.instance_of?(IRC::Client::DisconnectedState) do sleep 0.1; end
96
- assert @context.state.instance_of?(IRC::Client::UnregisteredState)
90
+ while !@context.connected? do sleep 0.1; end
97
91
  end
98
92
 
99
93
  # Wait until the connection is registered.
100
94
  Timeout::timeout(5) do
101
- while @context.state.instance_of?(IRC::Client::UnregisteredState) do sleep 0.1; end
102
- assert @context.state.instance_of?(IRC::Client::RegisteredState)
95
+ while !@context.registered? do sleep 0.1; end
103
96
  end
104
97
 
105
98
  # Join a channel & wait until the channel is joined and the block was called.
@@ -117,10 +110,11 @@ class IRC::Client::ContextTest < Test::Unit::TestCase
117
110
  # Disconnect and wait until the context is in the disconnected state.
118
111
  Timeout::timeout(5) do
119
112
  @context.disconnect
120
- assert @context.state.instance_of?(IRC::Client::DisconnectedState)
113
+ while @context.connected? do sleep 0.1; end
121
114
  end
122
115
 
123
116
  end
124
117
 
125
-
118
+ def test_truth
119
+ end
126
120
  end
@@ -0,0 +1,137 @@
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/connection_listener'
27
+ require 'irc/commands/command'
28
+ require 'irc/messages/nick_message'
29
+ require 'timeout'
30
+ require 'test/unit'
31
+
32
+ class IRC::Commands::CommandTest < Test::Unit::TestCase
33
+
34
+ include IRC::Client::ConnectionListener
35
+
36
+ class ConnectionMock
37
+
38
+ attr_reader :commands
39
+ attr_reader :connection_listeners
40
+
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
62
+
63
+ end
64
+
65
+ def test_command
66
+ command = IRC::Commands::Command.new("NICK Wiz")
67
+ assert_equal "NICK Wiz", command.command
68
+ end
69
+
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
+ def test_to_s
98
+ command = IRC::Commands::Command.new("NICK Wiz")
99
+ assert_equal "NICK Wiz", command.to_s
100
+ end
101
+
102
+ def test_wait
103
+
104
+ # The connection mock.
105
+ connection = ConnectionMock.new
106
+
107
+ # Construct and execute the nick message.
108
+ 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
+
135
+ end
136
+
137
+ end
@@ -23,14 +23,144 @@
23
23
  #
24
24
  # $Id: ping_command_test.rb 85 2006-08-13 11:42:07Z roman $
25
25
  #
26
+ require 'irc/commands/command_test'
26
27
  require 'irc/commands/ping_command'
28
+ require 'irc/messages/pong_message'
27
29
  require 'test/unit'
28
30
 
29
- class IRC::Commands::PingCommandTest < Test::Unit::TestCase
31
+ class IRC::Commands::PingCommandTest < IRC::Commands::CommandTest
30
32
 
31
33
  def test_command
32
34
  assert_equal "PING WiZ", IRC::Commands::PingCommand.new("WiZ").command
33
35
  assert_equal "PING WiZ irc.efnet.pl", IRC::Commands::PingCommand.new("WiZ", "irc.efnet.pl").command
34
36
  end
35
37
 
38
+ def test_execute
39
+
40
+ # The connection mock.
41
+ connection = ConnectionMock.new
42
+
43
+ # Construct and execute the nick message.
44
+ command = IRC::Commands::PingCommand.new("tolsun.oulu.fi")
45
+ command.execute(connection)
46
+
47
+ # The command should be registered as a connection listener.
48
+ assert connection.connection_listeners.include?(command)
49
+
50
+ # Send the response message.
51
+ command.on_server_response(connection, IRC::Messages::PongMessage.new("PONG tolsun.oulu.fi"))
52
+
53
+ # Should return immediately.
54
+ command.wait
55
+
56
+ # The command should no longer be registered as a connection listener.
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
+
63
+ end
64
+
65
+ def test_to_s
66
+ command = IRC::Commands::PingCommand.new("WiZ")
67
+ assert_equal "PING WiZ", command.to_s
68
+ end
69
+
70
+ def test_wait_for_pong
71
+
72
+ # The connection mock.
73
+ connection = ConnectionMock.new
74
+
75
+ # Construct and execute the nick message.
76
+ command = IRC::Commands::PingCommand.new("tolsun.oulu.fi")
77
+ command.execute(connection)
78
+
79
+ # The command should be registered as a connection listener.
80
+ assert connection.connection_listeners.include?(command)
81
+
82
+ # The wait method should block until a response was received. Because the VALID response
83
+ # hasn't been received yet this method should block forever.
84
+ assert_raise(Timeout::Error) do
85
+ command.on_server_response(connection, IRC::Messages::NickMessage.new(":WiZ NICK Kilroy"))
86
+ Timeout::timeout(0.1) { command.wait }
87
+ end
88
+
89
+ # Send the response message.
90
+ command.on_server_response(connection, IRC::Messages::PongMessage.new("PONG tolsun.oulu.fi"))
91
+
92
+ # Now the methos shouldn't block anymore.
93
+ Timeout::timeout(0.1) do
94
+ command.wait
95
+ end
96
+
97
+ # The command should no longer be registered as a connection listener.
98
+ assert !connection.connection_listeners.include?(command)
99
+
100
+ end
101
+
102
+ def test_wait_for_error_no_origin
103
+
104
+ # The connection mock.
105
+ connection = ConnectionMock.new
106
+
107
+ # Construct and execute the nick message.
108
+ command = IRC::Commands::PingCommand.new("tolsun.oulu.fi")
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
+ command.on_server_response(connection, IRC::Messages::ErrorMessage.new(":irc.easynews.com 4XX"))
118
+ Timeout::timeout(0.1) { command.wait }
119
+ end
120
+
121
+ # Send the response message.
122
+ command.on_server_response(connection, IRC::Messages::ErrorNoOriginMessage.new(":irc.easynews.com 4XX"))
123
+
124
+ # Now the methos shouldn't block anymore.
125
+ Timeout::timeout(0.1) do
126
+ command.wait
127
+ end
128
+
129
+ # The command should no longer be registered as a connection listener.
130
+ assert !connection.connection_listeners.include?(command)
131
+
132
+ end
133
+
134
+ def test_wait_for_error_no_such_server
135
+
136
+ # The connection mock.
137
+ connection = ConnectionMock.new
138
+
139
+ # Construct and execute the nick message.
140
+ command = IRC::Commands::PingCommand.new("tolsun.oulu.fi")
141
+ command.execute(connection)
142
+
143
+ # The command should be registered as a connection listener.
144
+ assert connection.connection_listeners.include?(command)
145
+
146
+ # The wait method should block until a response was received. Because the VALID response
147
+ # hasn't been received yet this method should block forever.
148
+ assert_raise(Timeout::Error) do
149
+ command.on_server_response(connection, IRC::Messages::ErrorMessage.new(":irc.easynews.com 4XX"))
150
+ Timeout::timeout(0.1) { command.wait }
151
+ end
152
+
153
+ # Send the response message.
154
+ command.on_server_response(connection, IRC::Messages::ErrorNoSuchServerMessage.new(":irc.easynews.com 4XX"))
155
+
156
+ # Now the methos shouldn't block anymore.
157
+ Timeout::timeout(0.1) do
158
+ command.wait
159
+ end
160
+
161
+ # The command should no longer be registered as a connection listener.
162
+ assert !connection.connection_listeners.include?(command)
163
+
164
+ end
165
+
36
166
  end
@@ -57,6 +57,10 @@ class IRC::Messages::FactoryTest < Test::Unit::TestCase
57
57
  message = IRC::Messages::Factory.create("PONG tolsun.oulu.fi")
58
58
  assert message.instance_of?(IRC::Messages::PongMessage)
59
59
  end
60
-
60
+
61
+ def test_private_message
62
+ message = IRC::Messages::Factory.create(":Angel PRIVMSG Wiz :Hello are you receiving this message ?")
63
+ assert message.instance_of?(IRC::Messages::PrivateMessage)
64
+ end
61
65
 
62
66
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: ircguerilla-irc
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.0
7
- date: 2006-08-15 00:00:00 +02:00
6
+ version: 1.2.0
7
+ date: 2006-08-20 00:00:00 +02:00
8
8
  summary: A Ruby framework for the Internet Relay Chat (IRC) protocol.
9
9
  require_paths:
10
10
  - lib
@@ -70,8 +70,9 @@ files:
70
70
  - lib/irc/client/command_handler.rb
71
71
  test_files:
72
72
  - test/test_helper.rb
73
- - test/unit/irc/commands/password_command_test.rb
74
73
  - test/unit/irc/commands/quit_command_test.rb
74
+ - test/unit/irc/commands/command_test.rb
75
+ - test/unit/irc/commands/password_command_test.rb
75
76
  - test/unit/irc/commands/pong_command_test.rb
76
77
  - test/unit/irc/commands/nick_command_test.rb
77
78
  - test/unit/irc/commands/part_command_test.rb