Class: IRC::Setup

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, name, config) ⇒ Setup

Returns a new instance of Setup.



10
11
12
13
14
15
16
17
18
# File 'lib/em-ruby-irc/IRC.rb', line 10

def initialize(bot, name, config)
  @name = name
  @connection = nil
  @startup_handlers = Array.new
  @config = config
  @memcache = nil
  @bot = bot
  default_handlers
end

Instance Attribute Details

#botObject

Returns the value of attribute bot.



9
10
11
# File 'lib/em-ruby-irc/IRC.rb', line 9

def bot
  @bot
end

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/em-ruby-irc/IRC.rb', line 8

def config
  @config
end

#connectionObject

Returns the value of attribute connection.



9
10
11
# File 'lib/em-ruby-irc/IRC.rb', line 9

def connection
  @connection
end

#memcacheObject

Returns the value of attribute memcache.



9
10
11
# File 'lib/em-ruby-irc/IRC.rb', line 9

def memcache
  @memcache
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/em-ruby-irc/IRC.rb', line 8

def name
  @name
end

#startup_handlersObject

Returns the value of attribute startup_handlers.



9
10
11
# File 'lib/em-ruby-irc/IRC.rb', line 9

def startup_handlers
  @startup_handlers
end

Instance Method Details

#add_startup_handler(proc = nil, &handler) ⇒ Object



20
21
22
# File 'lib/em-ruby-irc/IRC.rb', line 20

def add_startup_handler(proc=nil, &handler)
  startup_handlers << proc
end

#connectObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/em-ruby-irc/IRC.rb', line 28

def connect
  begin
    if not defined?(EventMachine::fork_reactor) or ENV["FORK"] == "false"
      logger.warn("WARNING: Version of eventmachine does not support forking or you used 'FORK=false'.  If you specified multiple connections you will only connect to one.")
      EventMachine::run {
        begin
          self.connection = EventMachine::connect(config["server_address"], config["server_port"].to_i, IRC::Connection, :setup => self)
        rescue => err
          log_error(err)
        end
      }
    else
      logger.warn("Event machine supports forking, attempting to fork.")
      pid = EventMachine::fork_reactor {
        begin
          self.connection = EventMachine::connect(config["server_address"], config["server_port"].to_i, IRC::Connection, :setup => self)
        rescue => err
          log_error(err)
        end
      }

        File.open("#{self.name}.pid", 'w') {|f| f << pid}
        Process.detach(pid)
    end
  rescue => err
    log_error(err)
  end
end

#default_handlersObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/em-ruby-irc/default-handlers.rb', line 3

def default_handlers
  logger.debug("Starting default handlers")
  begin
  @default_join_handler = Proc.new do |event|
    begin
      #Issues a who command on the channel when I join so I can generate the users hostnames
      event.connection.send_to_server "WHO #{event.message}" if event.from.downcase.chomp == event.connection.nickname.downcase.chomp
      #Make sure the bot has this channel in memory
      user = IRC::Utils.channel_user(event.connection, event.message, event.from, event.hostmask) #connection, channel, user
    rescue => err
      log_error(err)
    end
  end
  
  @default_names_reply_handler = Proc.new do |event|
    begin
      users = event.message.split
      users.each do |user|
        channel_user = IRC::Utils.channel_user(event.connection, event.mode, user) #connection, channel, user
      end
    rescue => err
      log_error(err)
    end
  end
  
  @default_part_handler = Proc.new do |event|
    begin
      if event.from.downcase.chomp == event.connection.nickname.downcase.chomp
        IRC::Utils.remove_channel(event.connection, event.channel)
      else           
        IRC::Utils.remove_channel_user(event.connection, event.channel, event.from)
      end
    rescue => err
      log_error(err)
    end
  end

  @default_who_reply_handler = Proc.new do |event|
    begin
      IRC::Utils.update_hostname(event.connection, event.stats[7], "#{event.stats[4]}@#{event.stats[5]}")
    rescue => err
      log_error(err)
    end
  end
  
  @default_nick_in_use_handler = Proc.new do |event|
    begin
      logger.debug("OMG NICKNAME IS BEING USED!")
      new_nickname = event.connection.nickname[0,14] + rand(9).to_s
      event.connection.send_to_server "NICK #{new_nickname}"
      event.connection.send_to_server "USER #{event.connection.username} 8 * :#{event.connection.realname}"
      event.connection.nickname = new_nickname
    rescue => err
      log_error(err)
    end
  end

  logger.debug("Registering handlers")
  IRC::Utils.add_handler('join', @default_join_handler, self)
  IRC::Utils.add_handler('namreply', @default_names_reply_handler, self)
  IRC::Utils.add_handler('part', @default_part_handler, self)
  IRC::Utils.add_handler('whoreply', @default_who_reply_handler, self)
  IRC::Utils.add_handler('nicknameinuse', @default_nick_in_use_handler, self)
  IRC::Utils.add_handler('ping', lambda {|event| event.connection.send_to_server("PONG #{event.message}") }, self)
  rescue => err
    log_error(err)
  end
end

#reset_startup_handlersObject



24
25
26
# File 'lib/em-ruby-irc/IRC.rb', line 24

def reset_startup_handlers
  startup_handlers.clear
end