Class: IRC::Setup
- Inherits:
-
Object
- Object
- IRC::Setup
- Defined in:
- lib/em-ruby-irc/IRC.rb,
lib/em-ruby-irc/default-handlers.rb
Instance Attribute Summary collapse
-
#bot ⇒ Object
Returns the value of attribute bot.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#memcache ⇒ Object
Returns the value of attribute memcache.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#startup_handlers ⇒ Object
Returns the value of attribute startup_handlers.
Instance Method Summary collapse
- #add_startup_handler(proc = nil, &handler) ⇒ Object
- #connect ⇒ Object
- #default_handlers ⇒ Object
-
#initialize(bot, name, config) ⇒ Setup
constructor
A new instance of Setup.
- #reset_startup_handlers ⇒ Object
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
#bot ⇒ Object
Returns the value of attribute bot.
9 10 11 |
# File 'lib/em-ruby-irc/IRC.rb', line 9 def bot @bot end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/em-ruby-irc/IRC.rb', line 8 def config @config end |
#connection ⇒ Object
Returns the value of attribute connection.
9 10 11 |
# File 'lib/em-ruby-irc/IRC.rb', line 9 def connection @connection end |
#memcache ⇒ Object
Returns the value of attribute memcache.
9 10 11 |
# File 'lib/em-ruby-irc/IRC.rb', line 9 def memcache @memcache end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/em-ruby-irc/IRC.rb', line 8 def name @name end |
#startup_handlers ⇒ Object
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 |
#connect ⇒ Object
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_handlers ⇒ Object
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.}" 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., 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..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.}") }, self) rescue => err log_error(err) end end |
#reset_startup_handlers ⇒ Object
24 25 26 |
# File 'lib/em-ruby-irc/IRC.rb', line 24 def reset_startup_handlers startup_handlers.clear end |