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,159 @@
|
|
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: bot.rb 85 2006-08-13 11:42:07Z roman $
|
25
|
+
#
|
26
|
+
require 'irc/models/user'
|
27
|
+
require 'irc/models/packet'
|
28
|
+
|
29
|
+
module IRC
|
30
|
+
|
31
|
+
module Models
|
32
|
+
|
33
|
+
class Bot < User
|
34
|
+
|
35
|
+
attr_accessor :bot_type
|
36
|
+
|
37
|
+
attr_accessor :bandwidth_capacity
|
38
|
+
attr_accessor :bandwidth_current
|
39
|
+
attr_accessor :bandwidth_record
|
40
|
+
|
41
|
+
attr_accessor :command_details
|
42
|
+
attr_accessor :command_list
|
43
|
+
attr_accessor :command_request
|
44
|
+
|
45
|
+
attr_accessor :queue_position
|
46
|
+
attr_accessor :queue_size
|
47
|
+
|
48
|
+
attr_accessor :slots_open
|
49
|
+
attr_accessor :slots_total
|
50
|
+
|
51
|
+
attr_accessor :transfer_max
|
52
|
+
attr_accessor :transfer_min
|
53
|
+
attr_accessor :transfer_record
|
54
|
+
|
55
|
+
def initialize(attributes)
|
56
|
+
|
57
|
+
super(attributes)
|
58
|
+
|
59
|
+
self.bot_type = attributes[:bot_type]
|
60
|
+
|
61
|
+
self.bandwidth_capacity = attributes[:bandwidth_capacity]
|
62
|
+
self.bandwidth_current = attributes[:bandwidth_current]
|
63
|
+
self.bandwidth_record = attributes[:bandwidth_record]
|
64
|
+
|
65
|
+
self.command_details = attributes[:command_details]
|
66
|
+
self.command_list = attributes[:command_list]
|
67
|
+
self.command_request = attributes[:command_request]
|
68
|
+
|
69
|
+
self.queue_position = attributes[:queue_position]
|
70
|
+
self.queue_size = attributes[:queue_size]
|
71
|
+
|
72
|
+
self.slots_open = attributes[:slots_open]
|
73
|
+
self.slots_total = attributes[:slots_total]
|
74
|
+
|
75
|
+
self.transfer_max = attributes[:transfer_max]
|
76
|
+
self.transfer_min = attributes[:transfer_min]
|
77
|
+
self.transfer_record = attributes[:transfer_record]
|
78
|
+
|
79
|
+
@channels = Hash.new
|
80
|
+
@packets = Array.new
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
def add_channel(channel)
|
85
|
+
@channels[channel.hash] = channel
|
86
|
+
return channel
|
87
|
+
end
|
88
|
+
|
89
|
+
def channels
|
90
|
+
return @channels.values
|
91
|
+
end
|
92
|
+
|
93
|
+
def create_packet(number, description, size_in_bytes = nil, number_of_downloads = nil)
|
94
|
+
raise Exception.new("Can't create packet. A packet with the same number does already exist.") if packet(number) != nil
|
95
|
+
packet = Packet.new(:bot => self, :number => number, :description => description, :size_in_bytes => size_in_bytes, :number_of_downloads => number_of_downloads)
|
96
|
+
@packets[number - 1] = packet
|
97
|
+
return packet
|
98
|
+
end
|
99
|
+
|
100
|
+
def merge(other)
|
101
|
+
|
102
|
+
super(other)
|
103
|
+
|
104
|
+
return if !other.kind_of?(Bot) && !other.kind_of?(IRC::Bot)
|
105
|
+
|
106
|
+
self.bot_type = other.bot_type
|
107
|
+
|
108
|
+
self.bandwidth_capacity = other.bandwidth_capacity
|
109
|
+
self.bandwidth_current = other.bandwidth_current
|
110
|
+
self.bandwidth_record = other.bandwidth_record
|
111
|
+
|
112
|
+
self.command_details = other.command_details
|
113
|
+
self.command_list = other.command_list
|
114
|
+
self.command_request = other.command_request
|
115
|
+
|
116
|
+
self.queue_position = other.queue_position
|
117
|
+
self.queue_size = other.queue_size
|
118
|
+
|
119
|
+
self.slots_open = other.slots_open
|
120
|
+
self.slots_total = other.slots_total
|
121
|
+
|
122
|
+
self.transfer_max = other.transfer_max
|
123
|
+
self.transfer_min = other.transfer_min
|
124
|
+
self.transfer_record = other.transfer_record
|
125
|
+
|
126
|
+
other.packets do |packet|
|
127
|
+
@packets[packet.number - 1] = packet
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
def update_packet(number, description, size_in_bytes = nil, number_of_downloads = nil)
|
133
|
+
|
134
|
+
# Find existing packet by number, or return with a new packet.
|
135
|
+
packet = packet(number)
|
136
|
+
return create_packet(number, description, size_in_bytes, number_of_downloads) if packet == nil
|
137
|
+
|
138
|
+
# Update already existing packet.
|
139
|
+
packet.description = description
|
140
|
+
packet.size_in_bytes = size_in_bytes
|
141
|
+
packet.number_of_downloads = number_of_downloads
|
142
|
+
|
143
|
+
return packet
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
def packet(number)
|
148
|
+
@packets[number - 1]
|
149
|
+
end
|
150
|
+
|
151
|
+
def packets
|
152
|
+
return @packets.compact
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
@@ -0,0 +1,158 @@
|
|
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: channel.rb 85 2006-08-13 11:42:07Z roman $
|
25
|
+
#
|
26
|
+
module IRC
|
27
|
+
|
28
|
+
module Models
|
29
|
+
|
30
|
+
class Channel
|
31
|
+
|
32
|
+
attr_reader :network
|
33
|
+
attr_reader :name
|
34
|
+
|
35
|
+
attr_accessor :last_ban
|
36
|
+
attr_accessor :last_join
|
37
|
+
attr_accessor :last_join_failure
|
38
|
+
attr_accessor :last_join_failure_reason
|
39
|
+
attr_accessor :last_kick
|
40
|
+
attr_accessor :last_kick_reason
|
41
|
+
attr_accessor :last_part
|
42
|
+
attr_accessor :password
|
43
|
+
attr_accessor :topic
|
44
|
+
attr_accessor :topic_author
|
45
|
+
attr_accessor :last_topic_change
|
46
|
+
|
47
|
+
# Creates a new channel object with a name and an optional
|
48
|
+
# topic, that belongs to a network.
|
49
|
+
def initialize(attributes)
|
50
|
+
|
51
|
+
raise ArgumentError.new("Can't create a new channel object. The :network attribute is missing.") if attributes[:network] == nil
|
52
|
+
@network = attributes[:network]
|
53
|
+
|
54
|
+
raise ArgumentError.new("Can't create a new user object. The :name attribute is missing.") if attributes[:name] == nil
|
55
|
+
@name = attributes[:name].strip
|
56
|
+
|
57
|
+
self.last_ban = attributes[:last_ban]
|
58
|
+
self.last_join = attributes[:last_join]
|
59
|
+
self.last_join_failure = attributes[:last_join_failure]
|
60
|
+
self.last_join_failure_reason = attributes[:last_join_failure_reason]
|
61
|
+
self.last_kick = attributes[:last_kick]
|
62
|
+
self.last_kick_reason = attributes[:last_kick_reason]
|
63
|
+
self.last_part = attributes[:last_part]
|
64
|
+
self.password = attributes[:password]
|
65
|
+
self.topic = attributes[:topic]
|
66
|
+
self.topic_author = attributes[:topic_author]
|
67
|
+
self.last_topic_change = attributes[:last_topic_change]
|
68
|
+
|
69
|
+
@users = Hash.new
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
def add_user(user)
|
74
|
+
@users[user] = user
|
75
|
+
end
|
76
|
+
|
77
|
+
def ==(other)
|
78
|
+
return eql?(other)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns true if the other Channel object is equal to the
|
82
|
+
# current.
|
83
|
+
def eql?(other)
|
84
|
+
return other.instance_of?(Channel) && @network == other.network && @name == other.name
|
85
|
+
end
|
86
|
+
|
87
|
+
def hash
|
88
|
+
return key.hash
|
89
|
+
end
|
90
|
+
|
91
|
+
def key
|
92
|
+
return "#{network.name}|#{name}".downcase
|
93
|
+
end
|
94
|
+
|
95
|
+
def merge(other)
|
96
|
+
|
97
|
+
# @network = other.network
|
98
|
+
@name = other.name
|
99
|
+
|
100
|
+
self.last_ban = other.last_ban
|
101
|
+
|
102
|
+
self.last_join = other.last_join
|
103
|
+
self.last_join_failure = other.last_join_failure
|
104
|
+
self.last_join_failure_reason = other.last_join_failure_reason
|
105
|
+
|
106
|
+
self.last_kick = other.last_kick
|
107
|
+
self.last_kick_reason = other.last_kick_reason
|
108
|
+
|
109
|
+
self.last_part = other.last_part
|
110
|
+
|
111
|
+
self.password = other.password
|
112
|
+
|
113
|
+
self.topic = other.topic
|
114
|
+
self.topic_author = other.topic_author
|
115
|
+
self.last_topic_change = other.last_topic_change
|
116
|
+
|
117
|
+
other.users.each do |other_user|
|
118
|
+
user = network.lookup_or_create_user(other_user.nick, other_user.login, other_user.hostname)
|
119
|
+
add_user(other_user)
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
def remove_user(user)
|
125
|
+
return @users.delete(user)
|
126
|
+
end
|
127
|
+
|
128
|
+
def users
|
129
|
+
return @users.values
|
130
|
+
end
|
131
|
+
|
132
|
+
# Returns true
|
133
|
+
def self.is_valid?(channel_name)
|
134
|
+
return Regexp.new('\s*#|&.*').match(channel_name) != nil
|
135
|
+
end
|
136
|
+
|
137
|
+
# Returns the name of the channel.
|
138
|
+
def to_s
|
139
|
+
return @name
|
140
|
+
end
|
141
|
+
|
142
|
+
# Returns the name of the channel.
|
143
|
+
def to_str
|
144
|
+
return to_s
|
145
|
+
end
|
146
|
+
|
147
|
+
def to_xml
|
148
|
+
channel = REXML::Element.new("Channel")
|
149
|
+
channel.add_element("Name").add_text(REXML::CData.new(name))
|
150
|
+
channel.add_element("Password").add_text(REXML::CData.new(password)) if password != nil
|
151
|
+
return channel
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
@@ -0,0 +1,252 @@
|
|
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: network.rb 85 2006-08-13 11:42:07Z roman $
|
25
|
+
#
|
26
|
+
require 'irc/models/bot'
|
27
|
+
require 'irc/models/channel'
|
28
|
+
require 'irc/models/server'
|
29
|
+
require 'irc/models/user'
|
30
|
+
require 'rexml/document'
|
31
|
+
|
32
|
+
module IRC
|
33
|
+
|
34
|
+
module Models
|
35
|
+
|
36
|
+
class Network
|
37
|
+
|
38
|
+
attr_reader :name
|
39
|
+
|
40
|
+
attr_accessor :description
|
41
|
+
attr_accessor :last_connect
|
42
|
+
attr_accessor :last_disconnect
|
43
|
+
attr_accessor :last_observation_start
|
44
|
+
attr_accessor :last_observation_stop
|
45
|
+
|
46
|
+
def initialize(attributes)
|
47
|
+
|
48
|
+
raise ArgumentError.new("Can't create a new network object. The :name attribute is missing.") if attributes[:name] == nil
|
49
|
+
@name = attributes[:name].strip
|
50
|
+
|
51
|
+
self.description = attributes[:description]
|
52
|
+
self.last_connect = attributes[:last_connect]
|
53
|
+
self.last_disconnect = attributes[:last_disconnect]
|
54
|
+
self.last_observation_start = attributes[:last_observation_start]
|
55
|
+
self.last_observation_stop = attributes[:last_observation_stop]
|
56
|
+
|
57
|
+
@bots = Hash.new
|
58
|
+
@channels = Hash.new
|
59
|
+
@servers = Hash.new
|
60
|
+
@users = Hash.new
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
# Adds a bot to the network and returns the bot.
|
65
|
+
def add_bot(bot)
|
66
|
+
add_user(bot)
|
67
|
+
return @bots[bot.key] = bot
|
68
|
+
end
|
69
|
+
|
70
|
+
# Adds a channel to the network and returns the channel.
|
71
|
+
def add_channel(channel)
|
72
|
+
return @channels[channel.key] = channel
|
73
|
+
end
|
74
|
+
|
75
|
+
# Adds a server to the network and returns the server.
|
76
|
+
def add_server(server)
|
77
|
+
return @servers[server.key] = server
|
78
|
+
end
|
79
|
+
|
80
|
+
# Adds an user to the network and returns the user.
|
81
|
+
def add_user(user)
|
82
|
+
return @users[user.key] = user
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns all bots that are associated with this network.
|
86
|
+
def bots
|
87
|
+
return @bots.values
|
88
|
+
end
|
89
|
+
|
90
|
+
# Returns all channels that are associated with this network.
|
91
|
+
def channels
|
92
|
+
return @channels.values
|
93
|
+
end
|
94
|
+
|
95
|
+
# Creates a bot, adds it to the network and returns the new bot.
|
96
|
+
def create_bot(nick, login = nil, hostname = nil)
|
97
|
+
return add_bot(Bot.new(:network => self, :nick => nick, :login => login, :hostname => hostname))
|
98
|
+
end
|
99
|
+
|
100
|
+
# Creates a channel, adds it to the network and returns the new channel.
|
101
|
+
def create_channel(name , password = nil)
|
102
|
+
return add_channel(Channel.new(:network => self, :name => name, :password => password))
|
103
|
+
end
|
104
|
+
|
105
|
+
# Creates a server, adds it to the network and returns the new server.
|
106
|
+
def create_server(hostname, port = Server::DEFAULT_PORT)
|
107
|
+
return add_server(Server.new(:network => self, :hostname => hostname, :port => port))
|
108
|
+
end
|
109
|
+
|
110
|
+
# Creates a user, adds it to the network and returns the new user.
|
111
|
+
def create_user(nick, login = nil, hostname = nil)
|
112
|
+
return add_user(User.new(:network => self, :nick => nick, :login => login, :hostname => hostname))
|
113
|
+
end
|
114
|
+
|
115
|
+
# Returns true if the other Network object is equal to the current.
|
116
|
+
def eql?(other)
|
117
|
+
return other.instance_of?(Network) && name == other.name
|
118
|
+
end
|
119
|
+
|
120
|
+
# Looks up or creates a new bot object and adds that bot to the
|
121
|
+
# internal hash table of bots.
|
122
|
+
def lookup_or_create_bot(nick, login = nil, hostname = nil)
|
123
|
+
return lookup_bot(nick) || create_bot(nick, login, hostname)
|
124
|
+
end
|
125
|
+
|
126
|
+
def hash
|
127
|
+
return key.hash
|
128
|
+
end
|
129
|
+
|
130
|
+
def key
|
131
|
+
return name.downcase
|
132
|
+
end
|
133
|
+
|
134
|
+
# Looks up or creates a new channel. The method returns the channel
|
135
|
+
# that was found or created.
|
136
|
+
def lookup_or_create_channel(name, password = nil)
|
137
|
+
return lookup_channel(name) || create_channel(name, password)
|
138
|
+
end
|
139
|
+
|
140
|
+
# Looks up or creates a new server. The method returns the server
|
141
|
+
def lookup_or_create_server(hostname, port = Server::DEFAULT_PORT)
|
142
|
+
return lookup_server(hostname, port) || create_server(hostname, port)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Looks up or creates a new user object and adds that user to the
|
146
|
+
# internal hash table of users.
|
147
|
+
def lookup_or_create_user(nick, login = nil, hostname = nil)
|
148
|
+
return lookup_user(nick) || create_user(nick, login, hostname)
|
149
|
+
end
|
150
|
+
|
151
|
+
# Returns a bot object by its nick name or nil if there is no
|
152
|
+
# such bot associated with the network.
|
153
|
+
def lookup_bot(nick)
|
154
|
+
return @bots[Bot.new(:network => self, :nick => nick).key]
|
155
|
+
end
|
156
|
+
|
157
|
+
# Returns a channel object by its name or nil if there is no
|
158
|
+
# such channel associated with the network.
|
159
|
+
def lookup_channel(name)
|
160
|
+
return @channels[Channel.new(:network => self, :name => name).key]
|
161
|
+
end
|
162
|
+
|
163
|
+
# Returns a server object by its name and port number or nil
|
164
|
+
# if there is no such server associated with the network.
|
165
|
+
def lookup_server(hostname, port = Server::DEFAULT_PORT)
|
166
|
+
return @servers[Server.new(:network => self, :hostname => hostname, :port => port).key]
|
167
|
+
end
|
168
|
+
|
169
|
+
# Returns a user object by its nick name or nil if there is no
|
170
|
+
# such user associated with the network.
|
171
|
+
def lookup_user(nick)
|
172
|
+
return @users[User.new(:network => self, :nick => nick).key] || lookup_bot(nick)
|
173
|
+
end
|
174
|
+
|
175
|
+
def merge(other)
|
176
|
+
|
177
|
+
@name = other.name
|
178
|
+
self.description = other.description
|
179
|
+
|
180
|
+
self.last_connect = other.last_connect
|
181
|
+
self.last_disconnect = other.last_disconnect
|
182
|
+
|
183
|
+
self.last_observation_start = other.last_observation_start
|
184
|
+
self.last_observation_stop = other.last_observation_stop
|
185
|
+
|
186
|
+
# other.bots.each do |bot|
|
187
|
+
# add_bot(bot)
|
188
|
+
# add_user(bot)
|
189
|
+
# end
|
190
|
+
|
191
|
+
other.channels.each do |other_channel|
|
192
|
+
channel = lookup_or_create_channel(other_channel.name)
|
193
|
+
channel.merge(other_channel)
|
194
|
+
end
|
195
|
+
|
196
|
+
other.servers.each do |other_server|
|
197
|
+
server = lookup_or_create_server(other_server.hostname, other_server.port)
|
198
|
+
server.merge(other_server)
|
199
|
+
end
|
200
|
+
#
|
201
|
+
# other.users do.each |user|
|
202
|
+
# add_user(user)
|
203
|
+
# end
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
# Returns all servers that are associated with this network.
|
208
|
+
def servers
|
209
|
+
return @servers.values
|
210
|
+
end
|
211
|
+
|
212
|
+
# Returns all users that are associated with this network.
|
213
|
+
def users
|
214
|
+
return (@users.values + bots).uniq
|
215
|
+
end
|
216
|
+
|
217
|
+
# Returns the name of the network converted to upper case.
|
218
|
+
def to_s
|
219
|
+
return name.upcase
|
220
|
+
end
|
221
|
+
|
222
|
+
# Returns the name of the network converted to upper case.
|
223
|
+
def to_str
|
224
|
+
return to_s
|
225
|
+
end
|
226
|
+
|
227
|
+
# Returns a XML representation of the network.
|
228
|
+
def to_xml
|
229
|
+
|
230
|
+
network = REXML::Element.new("Network")
|
231
|
+
network.add_element("Name").add_text(REXML::CData.new(name))
|
232
|
+
network.add_element("Description").add_text(REXML::CData.new(description)) if description != nil
|
233
|
+
|
234
|
+
channels_element = network.add_element("Channels")
|
235
|
+
channels.each { |channel| channels_element.add_element(channel.to_xml) }
|
236
|
+
|
237
|
+
servers_element = network.add_element("Servers")
|
238
|
+
servers.each { |server| servers_element.add_element(server.to_xml) }
|
239
|
+
|
240
|
+
return network
|
241
|
+
|
242
|
+
end
|
243
|
+
|
244
|
+
def ==(other)
|
245
|
+
return eql?(other)
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
251
|
+
|
252
|
+
end
|