Class: IRC::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/em-ruby-irc/IRC-Utils.rb

Class Method Summary collapse

Class Method Details

.add_handler(eventname, proc, network) ⇒ Object



100
101
102
103
104
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 100

def self.add_handler(eventname, proc, network)
 network.add_startup_handler(lambda {|bot|
  bot.add_message_handler(eventname, proc)
 })
end

.channel(connection, channel_name) ⇒ Object

Adds the channel to the global channel list (IRC::Connection#channels) and then returns it



4
5
6
7
8
9
10
11
12
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 4

def self.channel(connection, channel_name)
	channel_name = channel_name.downcase.chomp
	channel = connection.channels.select { |obj| obj.name == channel_name }.first
	if channel.nil?
		channel = IRC::Channel.new(channel_name)
		connection.channels << channel
	end
	return channel
end

.channel_user(connection, channel_name, user_name, hostmask = nil) ⇒ Object

Creates the user in the channel userlist (IRC::Channel#users) and then returns the user



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 40

def self.channel_user(connection, channel_name, user_name, hostmask=nil)
  return false if channel_name.nil? or user_name.nil?
	user_name = sanitize_nickname(user_name)
	channel_name = channel_name.downcase.chomp
	user = global_user(connection, user_name, hostmask)
	channel = channel(connection, channel_name)
	channel_user = channel.users.select { |obj| obj.name == user_name }.first
	if channel_user.nil?
		channel.users << user
	end
	return user
end

.get_channel_user_from_event(event, user = nil) ⇒ Object

Returns a user from an event.



54
55
56
57
58
59
60
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 54

def self.get_channel_user_from_event(event, user=nil)
	if user.nil?
		channel_user(event.connection, event.channel, event.from)
	else
		channel_user(event.connection, event.channel, user)
	end
end

.global_user(connection, user_name, hostmask = nil) ⇒ Object

Creates the user in the global userlist (IRC::Connection#users) and then returns the user



63
64
65
66
67
68
69
70
71
72
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 63

def self.global_user(connection, user_name, hostmask=nil)
	user_name = sanitize_nickname(user_name)
	user = connection.users.select { |obj| obj.name == user_name }.first
	if user.nil?
		user = IRC::User.new(user_name, hostmask)
		connection.users << user
	end
	user.hostmask = hostmask unless hostmask.nil?
	return user
end

.regex_mask(hostmask) ⇒ Object

Converts a hostmask like brian@.google.com to .*brian@.*google.com so it can be properly used in a regex



87
88
89
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 87

def self.regex_mask(hostmask)
	hostmask.gsub(/([\[\]\(\)\?\^\$])\\/, '\\1').gsub(/\./, '\.').gsub(/\[/, '\[').gsub(/\]/, '\]').gsub(/\*/, '.*').sub(/^/, '^').sub(/$/, '$')
end

.remove_channel(connection, channel_name) ⇒ Object

Removes the channel from the global channel list (IRC::Connection#channels)



33
34
35
36
37
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 33

def self.remove_channel(connection, channel_name)
	channel_name = channel_name.downcase.chomp
	channel = connection.channels.select { |obj| obj.name == channel_name }.first
	connection.channels.delete(channel) unless channel.nil?
end

.remove_channel_user(connection, channel_name, user_name) ⇒ Object

Removes the user from the channel userlist, and if the user is not on any other channel userlists, it deletes it from global as well.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 15

def self.remove_channel_user(connection, channel_name, user_name)
	user_name = sanitize_nickname(user_name)
	channel_name = channel_name.downcase.chomp
	channel = channel(connection, channel_name)
	user = channel.users.select { |obj| obj.name == user_name }.first
	unless user.nil?
		channel.users.delete(user)
		keep_user = false
		connection.channels.each do |thischannel|
			unless thischannel.users.select { |obj| obj.name == user_name }.first.nil?
				keep_user = true
			end
		end
		connection.users.delete(user) unless keep_user
	end
end

.sanitize_nickname(nickname) ⇒ Object



82
83
84
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 82

def self.sanitize_nickname(nickname)
	return nickname.downcase.chomp.match(/(?![\@\%\&\+])([\-\_\[\]\{\}\\\|\`\^a-zA-Z0-9]*)/)[0]
end

.setup_connections(bot, config) ⇒ Object

Sets up all connections into global @@connections



92
93
94
95
96
97
98
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 92

def self.setup_connections(bot, config)
  connections = {}
	config['networks'].each do |network, server_setup|
	  connections[network] = IRC::Setup.new(bot, network, server_setup)
	end
	return connections
end

.update_hostname(connection, user_name, hostmask) ⇒ Object

Update the users hostmask



75
76
77
78
79
80
# File 'lib/em-ruby-irc/IRC-Utils.rb', line 75

def self.update_hostname(connection, user_name, hostmask)
	user_name = sanitize_nickname(user_name)
	user = connection.users.select { |obj| obj.name == user_name }.first
	user.hostmask = hostmask unless user.nil? or hostmask.nil?
	return user
end