ircguerilla-irc 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/irc/client/client_error.rb +35 -0
- data/lib/irc/client/command_handler.rb +113 -0
- data/lib/irc/client/connected_state.rb +95 -0
- data/lib/irc/client/connection.rb +119 -0
- data/lib/irc/client/connection_listener.rb +97 -0
- data/lib/irc/client/connection_state.rb +88 -0
- data/lib/irc/client/context.rb +250 -0
- data/lib/irc/client/disconnected_state.rb +132 -0
- data/lib/irc/client/message_handler.rb +87 -0
- data/lib/irc/client/registered_state.rb +90 -0
- data/lib/irc/client/unregistered_state.rb +113 -0
- data/lib/irc/commands/command.rb +57 -0
- data/lib/irc/commands/invalid_command.rb +35 -0
- data/lib/irc/commands/join_command.rb +122 -0
- data/lib/irc/commands/nick_command.rb +86 -0
- data/lib/irc/commands/part_command.rb +84 -0
- data/lib/irc/commands/password_command.rb +72 -0
- data/lib/irc/commands/ping_command.rb +79 -0
- data/lib/irc/commands/pong_command.rb +68 -0
- data/lib/irc/commands/quit_command.rb +77 -0
- data/lib/irc/commands/user_command.rb +105 -0
- data/lib/irc/messages/codes.rb +186 -0
- data/lib/irc/messages/error_join_message.rb +72 -0
- data/lib/irc/messages/error_message.rb +101 -0
- data/lib/irc/messages/factory.rb +122 -0
- data/lib/irc/messages/invalid_message.rb +35 -0
- data/lib/irc/messages/join_message.rb +130 -0
- data/lib/irc/messages/message.rb +110 -0
- data/lib/irc/messages/nick_message.rb +93 -0
- data/lib/irc/messages/notice_message.rb +87 -0
- data/lib/irc/messages/part_message.rb +95 -0
- data/lib/irc/messages/ping_message.rb +101 -0
- data/lib/irc/messages/pong_message.rb +89 -0
- data/lib/irc/messages/private_message.rb +121 -0
- data/lib/irc/models/bot.rb +159 -0
- data/lib/irc/models/channel.rb +158 -0
- data/lib/irc/models/network.rb +252 -0
- data/lib/irc/models/packet.rb +84 -0
- data/lib/irc/models/server.rb +116 -0
- data/lib/irc/models/user.rb +112 -0
- data/test/functional/irc/client/connection_test.rb +118 -0
- data/test/functional/irc/client/context_test.rb +126 -0
- data/test/test_helper.rb +32 -0
- data/test/unit/irc/client/command_handler_test.rb +137 -0
- data/test/unit/irc/commands/join_command_test.rb +72 -0
- data/test/unit/irc/commands/nick_command_test.rb +35 -0
- data/test/unit/irc/commands/part_command_test.rb +52 -0
- data/test/unit/irc/commands/password_command_test.rb +35 -0
- data/test/unit/irc/commands/ping_command_test.rb +36 -0
- data/test/unit/irc/commands/pong_command_test.rb +36 -0
- data/test/unit/irc/commands/quit_command_test.rb +35 -0
- data/test/unit/irc/commands/user_command_test.rb +35 -0
- data/test/unit/irc/messages/error_join_message_test.rb +45 -0
- data/test/unit/irc/messages/factory_test.rb +62 -0
- data/test/unit/irc/messages/join_message_test.rb +46 -0
- data/test/unit/irc/messages/message_test.rb +56 -0
- data/test/unit/irc/messages/nick_message_test.rb +48 -0
- data/test/unit/irc/messages/notice_message_test.rb +43 -0
- data/test/unit/irc/messages/part_message_test.rb +47 -0
- data/test/unit/irc/messages/ping_message_test.rb +57 -0
- data/test/unit/irc/messages/pong_message_test.rb +57 -0
- data/test/unit/irc/messages/private_message_test.rb +42 -0
- data/test/unit/irc/models/bot_test.rb +85 -0
- data/test/unit/irc/models/channel_test.rb +90 -0
- data/test/unit/irc/models/network_test.rb +210 -0
- data/test/unit/irc/models/packet_test.rb +38 -0
- data/test/unit/irc/models/server_test.rb +54 -0
- data/test/unit/irc/models/user_test.rb +67 -0
- metadata +111 -0
@@ -0,0 +1,87 @@
|
|
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: message_handler.rb 85 2006-08-13 11:42:07Z roman $
|
25
|
+
#
|
26
|
+
require 'irc/messages/factory'
|
27
|
+
require 'log4r'
|
28
|
+
|
29
|
+
module IRC
|
30
|
+
|
31
|
+
module Client
|
32
|
+
|
33
|
+
class MessageHandlerError < Exception
|
34
|
+
end
|
35
|
+
|
36
|
+
class MessageHandler
|
37
|
+
|
38
|
+
def initialize(context)
|
39
|
+
@log = Log4r::Logger.new('IRC::Client::MessageHandler')
|
40
|
+
@context = context
|
41
|
+
end
|
42
|
+
|
43
|
+
def start
|
44
|
+
|
45
|
+
# Don't start the command handler if already started.
|
46
|
+
raise MessageHandlerError.new("Can't start message handler. Already started.") if @thread != nil
|
47
|
+
|
48
|
+
# Start the message handler thread.
|
49
|
+
@thread = Thread.new do
|
50
|
+
|
51
|
+
loop do
|
52
|
+
|
53
|
+
# Read the raw message from the socket.
|
54
|
+
raw_message = @context.socket.gets("\r\n")
|
55
|
+
|
56
|
+
# Log the raw message.
|
57
|
+
@log.debug("[#{@context.network.to_s.upcase}] <<< #{raw_message.chomp}")
|
58
|
+
|
59
|
+
# Create the message object & ask the message object to handle it.
|
60
|
+
message = message = IRC::Messages::Factory.create(raw_message)
|
61
|
+
message.handle(@context)
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
@log.debug("[#{@context.network.to_s.upcase}] Message handler successfully started.")
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
def stop
|
72
|
+
|
73
|
+
# Don't stop the command handler if already stopped.
|
74
|
+
raise CommandHandlerError.new("Can't stop message handler. Already stopped.") if @thread == nil
|
75
|
+
|
76
|
+
# Kill the thread.
|
77
|
+
@thread.kill
|
78
|
+
|
79
|
+
@log.debug("[#{@context.network.to_s.upcase}] Message handler successfully stopped.")
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,90 @@
|
|
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: registered_state.rb 94 2006-08-13 21:40:40Z roman $
|
25
|
+
#
|
26
|
+
require 'irc/client/client_error'
|
27
|
+
require 'irc/client/connected_state'
|
28
|
+
require 'irc/commands/join_command'
|
29
|
+
require 'irc/commands/part_command'
|
30
|
+
require 'log4r'
|
31
|
+
|
32
|
+
module IRC
|
33
|
+
|
34
|
+
module Client
|
35
|
+
|
36
|
+
class RegisteredState < ConnectedState
|
37
|
+
|
38
|
+
def initialize
|
39
|
+
@log = Log4r::Logger.new('IRC::Client::RegisteredState')
|
40
|
+
end
|
41
|
+
|
42
|
+
# Connects to the server.
|
43
|
+
def connect(context, server)
|
44
|
+
raise ClientError.new("Can't connect to server. Already connected to a server.")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Joins the given channels.
|
48
|
+
def join(context, channels)
|
49
|
+
IRC::Commands::JoinCommand.new(channels).send(context)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Leaves the given channels.
|
53
|
+
def part(context, channels)
|
54
|
+
IRC::Commands::PartCommand.new(channels).send(context)
|
55
|
+
end
|
56
|
+
|
57
|
+
# ConnectionListener
|
58
|
+
|
59
|
+
# This method gets called when a user (possibly us) joins a channel.
|
60
|
+
def on_join(context, channel, user)
|
61
|
+
return unless context.nick.strip.downcase == user.nick.strip.downcase
|
62
|
+
context.join_succeeded(channel)
|
63
|
+
@log.debug("#{network_name(context)} Successfully joined channel #{channel}.")
|
64
|
+
end
|
65
|
+
|
66
|
+
# This message gets called when a channel join attempt failed.
|
67
|
+
def on_join_failure(context, channel, code, reason)
|
68
|
+
@log.debug("#{network_name(context)} Failed to join channel #{channel}. #{reason.capitalize}.")
|
69
|
+
end
|
70
|
+
|
71
|
+
# This method gets called when a user gets kicked from a channel.
|
72
|
+
def on_kick(context, channel, kick_user, kicked_user, reason)
|
73
|
+
end
|
74
|
+
|
75
|
+
# This method gets called when a user (possibly us) leaves a channel.
|
76
|
+
def on_part(context, channel, user)
|
77
|
+
return unless context.nick.strip.downcase == user.nick.strip.downcase
|
78
|
+
context.part_succeeded(channel)
|
79
|
+
@log.debug("#{network_name(context)} Successfully left channel #{channel}.")
|
80
|
+
end
|
81
|
+
|
82
|
+
# This method gets called when a channel part attempt failed.
|
83
|
+
def on_part_failure(connection, channel)
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
@@ -0,0 +1,113 @@
|
|
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: unregistered_state.rb 90 2006-08-13 14:45:04Z roman $
|
25
|
+
#
|
26
|
+
require 'irc/client/client_error'
|
27
|
+
require 'irc/client/connected_state'
|
28
|
+
require 'irc/client/registered_state'
|
29
|
+
require 'irc/messages/error_message'
|
30
|
+
require 'log4r'
|
31
|
+
|
32
|
+
module IRC
|
33
|
+
|
34
|
+
module Client
|
35
|
+
|
36
|
+
class UnregisteredState < ConnectedState
|
37
|
+
|
38
|
+
def initialize
|
39
|
+
@log = Log4r::Logger.new('IRC::Client::UnregisteredState')
|
40
|
+
end
|
41
|
+
|
42
|
+
# Joins the given channels.
|
43
|
+
def join(context, channels)
|
44
|
+
raise ClientError.new("Can't join any channel. The connection is not yet registered.")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Leaves the given channels.
|
48
|
+
def part(context, channels)
|
49
|
+
raise ClientError.new("Can't leave any channel. The connection is not yet registered.")
|
50
|
+
end
|
51
|
+
|
52
|
+
# Sends a private message to the target (a nick or a channel).
|
53
|
+
def private_message(context, target, message)
|
54
|
+
raise ClientError.new("Can't send a private message. The connection is not yet registered.")
|
55
|
+
end
|
56
|
+
|
57
|
+
# ConnectionListener
|
58
|
+
|
59
|
+
# This method gets called when a message from the server is received.
|
60
|
+
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)
|
81
|
+
|
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)
|
89
|
+
|
90
|
+
# Everything ok. Change to the registered state.
|
91
|
+
else
|
92
|
+
change_state(context, RegisteredState.instance)
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
protected
|
99
|
+
|
100
|
+
# Notify all connection listeners that an error occured during the connection registration.
|
101
|
+
def registration_failure(context, message)
|
102
|
+
|
103
|
+
context.connection_listeners.each do |connection_listener|
|
104
|
+
connection_listener.on_registration_failure(context, message)
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
@@ -0,0 +1,57 @@
|
|
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: command.rb 85 2006-08-13 11:42:07Z roman $
|
25
|
+
#
|
26
|
+
require 'irc/commands/invalid_command'
|
27
|
+
|
28
|
+
module IRC
|
29
|
+
|
30
|
+
module Commands
|
31
|
+
|
32
|
+
class Command
|
33
|
+
|
34
|
+
def initialize(raw_command = "")
|
35
|
+
@raw_command = raw_command
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns the command as a string.
|
39
|
+
def command
|
40
|
+
return @raw_command
|
41
|
+
end
|
42
|
+
|
43
|
+
# Sends the command to the io object.
|
44
|
+
def send(io)
|
45
|
+
io << self
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns the command as a string.
|
49
|
+
def to_s
|
50
|
+
return command
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,35 @@
|
|
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: invalid_command.rb 85 2006-08-13 11:42:07Z roman $
|
25
|
+
#
|
26
|
+
module IRC
|
27
|
+
|
28
|
+
module Commands
|
29
|
+
|
30
|
+
class InvalidCommand < Exception
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,122 @@
|
|
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: join_command.rb 85 2006-08-13 11:42:07Z roman $
|
25
|
+
#
|
26
|
+
require 'irc/commands/command'
|
27
|
+
require 'irc/commands/invalid_command'
|
28
|
+
|
29
|
+
module IRC
|
30
|
+
|
31
|
+
module Commands
|
32
|
+
|
33
|
+
# 4.2.1 Join message
|
34
|
+
#
|
35
|
+
# Command: JOIN
|
36
|
+
# Parameters: <channel>{,<channel>} [<key>{,<key>}]
|
37
|
+
#
|
38
|
+
# The JOIN command is used by client to start listening a specific channel.
|
39
|
+
# Whether or not a client is allowed to join a channel is checked only by
|
40
|
+
# the server the client is connected to; all other servers automatically add
|
41
|
+
# the user to the channel when it is received from other servers. The conditions
|
42
|
+
# which affect this are as follows:
|
43
|
+
#
|
44
|
+
# 1. the user must be invited if the channel is invite-only;
|
45
|
+
# 2. the user's nick/username/hostname must not match any active bans;
|
46
|
+
# 3. the correct key (password) must be given if it is set.
|
47
|
+
#
|
48
|
+
# These are discussed in more detail under the MODE command (see section 4.2.3
|
49
|
+
# for more details).
|
50
|
+
#
|
51
|
+
# Once a user has joined a channel, they receive notice about all commands their
|
52
|
+
# server receives which affect the channel. This includes MODE, KICK, PART, QUIT
|
53
|
+
# and of course PRIVMSG/NOTICE. The JOIN command needs to be broadcast to all
|
54
|
+
# servers so that each server knows where to find the users who are on the channel.
|
55
|
+
# This allows optimal delivery of PRIVMSG/NOTICE messages to the channel.
|
56
|
+
#
|
57
|
+
# If a JOIN is successful, the user is then sent the channel's topic (using
|
58
|
+
# RPL_TOPIC) and the list of users who are on the channel (using RPL_NAMREPLY),
|
59
|
+
# which must include the user joining.
|
60
|
+
#
|
61
|
+
# Numeric Replies:
|
62
|
+
#
|
63
|
+
# * ERR_NEEDMOREPARAMS
|
64
|
+
# * ERR_BANNEDFROMCHAN
|
65
|
+
# * ERR_INVITEONLYCHAN
|
66
|
+
# * ERR_BADCHANNELKEY
|
67
|
+
# * ERR_CHANNELISFULL
|
68
|
+
# * ERR_BADCHANMASK
|
69
|
+
# * ERR_NOSUCHCHANNEL
|
70
|
+
# * ERR_TOOMANYCHANNELS
|
71
|
+
# * RPL_TOPIC
|
72
|
+
#
|
73
|
+
# Examples:
|
74
|
+
#
|
75
|
+
# JOIN #foobar ; join channel #foobar.
|
76
|
+
#
|
77
|
+
# JOIN &foo fubar ; join channel &foo using key "fubar".
|
78
|
+
#
|
79
|
+
# JOIN #foo,&bar fubar ; join channel #foo using key "fubar"
|
80
|
+
# ; and &bar using no key.
|
81
|
+
#
|
82
|
+
# JOIN #foo,#bar fubar,foobar ; join channel #foo using key "fubar".
|
83
|
+
# ; and channel #bar using key "foobar".
|
84
|
+
#
|
85
|
+
# JOIN #foo,#bar ; join channels #foo and #bar.
|
86
|
+
#
|
87
|
+
# :WiZ JOIN #Twilight_zone ; JOIN message from WiZ
|
88
|
+
#
|
89
|
+
class JoinCommand < Command
|
90
|
+
|
91
|
+
attr_reader :channels
|
92
|
+
attr_reader :passwords
|
93
|
+
|
94
|
+
def initialize(channels, passwords = [])
|
95
|
+
|
96
|
+
raise InvalidCommand.new("Can't create join command. No channels.") unless channels
|
97
|
+
|
98
|
+
if channels.instance_of?(Array)
|
99
|
+
@channels = channels
|
100
|
+
else
|
101
|
+
@channels = Array.new
|
102
|
+
@channels << channels
|
103
|
+
end
|
104
|
+
|
105
|
+
if passwords.instance_of?(Array)
|
106
|
+
@passwords = passwords
|
107
|
+
else
|
108
|
+
@passwords = Array.new
|
109
|
+
@passwords << passwords
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
def command
|
115
|
+
return "JOIN #{channels.join(',')} #{passwords.join(',')}".strip
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|