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
24
# 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"]
    @noidprefix = @setup.config["noidprefix"] || false
    @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

#noidprefixObject (readonly)

Returns the value of attribute noidprefix.



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

def noidprefix
  @noidprefix
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



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

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

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



68
69
70
71
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 68

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



40
41
42
43
44
45
46
47
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 40

def connection_completed
  logger.debug("Firing connection_completed")
  send_to_server "NICK #{@nickname}"
  send_to_server "USER #{@username} 8 * :#{@realname}"
  if noidprefix
    send_to_server "CAPAB IDENTIFY-MSG"
  end
end

#ctcp(target, type, message) ⇒ Object



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

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

#deop(channel, target) ⇒ Object



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

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

#join(channel) ⇒ Object

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



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

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

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



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

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

#logObject



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

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

#log_irc(line) ⇒ Object



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

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

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



126
127
128
129
# File 'lib/em-ruby-irc/IRC-Connection.rb', line 126

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



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

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

#part(channel) ⇒ Object



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

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

#post_initObject

Start handlers



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

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



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

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

#receive_data(data) ⇒ Object



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

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



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

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

#send_notice(target, message) ⇒ Object



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

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

#send_to_server(message) ⇒ Object



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

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

#unbindObject



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

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