Class: IRC::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 8

def initialize(args)
	begin
		@name = args[:setup].name
		@setup = args[:setup]
		@nickname = @setup.config["nickname"]
		@realname = @setup.config["realname"]
		@username = @setup.config["username"]
		@command_char = @setup.config["command_char"]
		@channels = Array.new
		@users = Array.new
		@irc_handlers = Hash.new()
		super
	rescue => err
		log_error(err)
	end
end

Instance Attribute Details

#channelsObject

Returns the value of attribute channels.



5
6
7
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 5

def channels
  @channels
end

#command_charObject (readonly)

Returns the value of attribute command_char.



5
6
7
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 5

def command_char
  @command_char
end

#irc_handlersObject

Returns the value of attribute irc_handlers.



6
7
8
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 6

def irc_handlers
  @irc_handlers
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 5

def name
  @name
end

#nicknameObject

Returns the value of attribute nickname.



6
7
8
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 6

def nickname
  @nickname
end

#realnameObject (readonly)

Returns the value of attribute realname.



5
6
7
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 5

def realname
  @realname
end

#setupObject

Returns the value of attribute setup.



6
7
8
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 6

def setup
  @setup
end

#usernameObject (readonly)

Returns the value of attribute username.



5
6
7
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 5

def username
  @username
end

#usersObject

Returns the value of attribute users.



5
6
7
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 5

def users
  @users
end

Instance Method Details

#action(target, message) ⇒ Object



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

def action(target, message)
	send_ctcp(target, 'ACTION', action);
end

#add_message_handler(event_type, proc = nil, &handler) ⇒ Object



64
65
66
67
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 64

def add_message_handler(event_type, proc=nil, &handler)
	self.irc_handlers[event_type] = Array.new unless self.irc_handlers[event_type].class == Array
	self.irc_handlers[event_type] << proc
end

#connection_completedObject



39
40
41
42
43
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 39

def connection_completed
	logger.debug("Firing connection_completed")
	send_to_server "NICK #{@nickname}"
	send_to_server "USER #{@username} 8 * :#{@realname}"
end

#ctcp(target, type, message) ⇒ Object



106
107
108
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 106

def ctcp(target, type, message)
	send_to_server("PRIVMSG #{target} :\001#{type} #{message}");
end

#deop(channel, target) ⇒ Object



118
119
120
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 118

def deop(channel, target)
	mode(channel, "+o", target)
end

#join(channel) ⇒ Object

Command helpers for easier coding (join, part, quit, kick, ban, etc)



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

def join(channel)
	send_to_server("JOIN #{channel}")
end

#kick(channel, target, message = "Bye!") ⇒ Object



110
111
112
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 110

def kick(channel, target, message="Bye!")
	send_to_server("KICK #{channel} #{target} :#{message}")
end

#logObject



69
70
71
72
73
74
75
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 69

def log
	if @log.nil?
		@log = Logger.new("logs/#{@name}.log")
		@log.level = Logger::INFO
	end
	@log
end

#log_irc(line) ⇒ Object



77
78
79
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 77

def log_irc(line)
	log.info(line)
end

#mode(target, mode, arg = nil) ⇒ Object



122
123
124
125
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 122

def mode(target, mode, arg=nil)
	send_to_server("MODE #{target} #{mode} #{arg}") unless arg.nil?
	send_to_server("MODE #{target} #{mode}")
end

#op(channel, target) ⇒ Object



114
115
116
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 114

def op(channel, target)
	mode(channel, "+o", target)
end

#part(channel) ⇒ Object



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

def part(channel)
	send_to_server("PART #{channel}")
end

#post_initObject

Start handlers



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 26

def post_init
    logger.debug("Firing post_init")
	begin
    	unless setup.startup_handlers.nil?
			setup.startup_handlers.each do |handler|
				handler.call(self) unless handler.nil?
			end
		end
	rescue => err
		log_error(err)
	end
end

#quit(message) ⇒ Object



98
99
100
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 98

def quit(message)
	send_to_server("QUIT :#{message}")
end

#receive_data(data) ⇒ Object



45
46
47
48
49
50
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 45

def receive_data(data)
	data.split("\n").each do |line|
		log_irc(line)
		IRC::Event.new(line, self)
	end
end

#send_message(target, message) ⇒ Object



90
91
92
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 90

def send_message(target, message)
	send_to_server("PRIVMSG #{target} :#{message}")
end

#send_notice(target, message) ⇒ Object



94
95
96
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 94

def send_notice(target, message)
	send_to_server("NOTICE #{target} :#{message}")
end

#send_to_server(message) ⇒ Object



52
53
54
55
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 52

def send_to_server(message)
	log_irc(message)
	send_data "#{message}\n"
end

#unbindObject



57
58
59
60
61
62
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 57

def unbind
	logger.info("[#{self.name}] Connection lost, sleeping 10 seconds")
	sleep 10
	logger.info("[#{self.name}] Reconnecting to: #{setup.config["server_address"]} Port: #{setup.config["server_port"]}")
	EventMachine::reconnect setup.config["server_address"], setup.config["server_port"].to_i, self
end