Class: Spunk::Bot
- Inherits:
-
Object
- Object
- Spunk::Bot
- Defined in:
- lib/spunk/bot.rb
Instance Attribute Summary collapse
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#invite_ok ⇒ Object
Returns the value of attribute invite_ok.
-
#joined_rooms ⇒ Object
Returns the value of attribute joined_rooms.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#nickname ⇒ Object
Returns the value of attribute nickname.
-
#nickserv_password ⇒ Object
Returns the value of attribute nickserv_password.
-
#processors ⇒ Object
readonly
Returns the value of attribute processors.
-
#request_processors ⇒ Object
readonly
Returns the value of attribute request_processors.
-
#response_processors ⇒ Object
readonly
Returns the value of attribute response_processors.
-
#rooms ⇒ Object
Returns the value of attribute rooms.
-
#server ⇒ Object
Returns the value of attribute server.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #add_processor(processor = nil, &block) ⇒ Object
- #add_request_processor(processor = nil, &block) ⇒ Object
- #add_response_processor(processor = nil, &block) ⇒ Object
- #authenticate ⇒ Object
- #connect ⇒ Object
-
#initialize(options = {}) ⇒ Bot
constructor
A new instance of Bot.
- #join_room(room, password = nil) ⇒ Object
- #origin ⇒ Object
- #parse_message(message) ⇒ Object
- #process_request(prefix, command, parameters) ⇒ Object
- #process_response(origin, command, parameters) ⇒ Object
- #run ⇒ Object
- #say(to, message) ⇒ Object
- #send_message(message) ⇒ Object
- #setup_connection ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Bot
Returns a new instance of Bot.
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 |
# File 'lib/spunk/bot.rb', line 10 def initialize( = {}) .each do |option, value| instance_variable_set("@#{option}", value) end @token = [:token] ||= nil @ssl = [:ssl] ||= false if [:invite_ok].nil? [:invite_ok] = true end @invite_ok = [:invite_ok] @nickserv_password = [:nickserv_password] @processors = [] @request_processors = [] @response_processors = [] @joined_rooms = [] @hostname = Socket.gethostname @server ||= { :hostname => [:hostname] || "localhost", :port => [:port] || 6667 } add_request_processor(Spunk::Processor::Ping.new) add_request_processor(Spunk::Processor::Base.new) @rooms = [:rooms] ||= [] if [:logger].class == String @logger = Logger.new([:logger]) @logger.level = Logger::INFO elsif [:logger].nil? @logger = Logger.new(STDOUT) @logger.level = Logger::INFO elsif [:logger].class == Hash @logger = Logger.new([:logger][:file]) @logger.level = [:logger][:level] ||= Logger::INFO elsif [:logger].class == Logger @logger = [:logger] end end |
Instance Attribute Details
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
8 9 10 |
# File 'lib/spunk/bot.rb', line 8 def hostname @hostname end |
#invite_ok ⇒ Object
Returns the value of attribute invite_ok.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def invite_ok @invite_ok end |
#joined_rooms ⇒ Object
Returns the value of attribute joined_rooms.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def joined_rooms @joined_rooms end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def logger @logger end |
#nickname ⇒ Object
Returns the value of attribute nickname.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def nickname @nickname end |
#nickserv_password ⇒ Object
Returns the value of attribute nickserv_password.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def nickserv_password @nickserv_password end |
#processors ⇒ Object (readonly)
Returns the value of attribute processors.
8 9 10 |
# File 'lib/spunk/bot.rb', line 8 def processors @processors end |
#request_processors ⇒ Object (readonly)
Returns the value of attribute request_processors.
8 9 10 |
# File 'lib/spunk/bot.rb', line 8 def request_processors @request_processors end |
#response_processors ⇒ Object (readonly)
Returns the value of attribute response_processors.
8 9 10 |
# File 'lib/spunk/bot.rb', line 8 def response_processors @response_processors end |
#rooms ⇒ Object
Returns the value of attribute rooms.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def rooms @rooms end |
#server ⇒ Object
Returns the value of attribute server.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def server @server end |
#ssl ⇒ Object
Returns the value of attribute ssl.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def ssl @ssl end |
#token ⇒ Object
Returns the value of attribute token.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def token @token end |
Instance Method Details
#add_processor(processor = nil, &block) ⇒ Object
134 135 136 |
# File 'lib/spunk/bot.rb', line 134 def add_processor(processor = nil, &block) @processors << (processor || block) end |
#add_request_processor(processor = nil, &block) ⇒ Object
126 127 128 |
# File 'lib/spunk/bot.rb', line 126 def add_request_processor(processor = nil, &block) @request_processors << (processor || block) end |
#add_response_processor(processor = nil, &block) ⇒ Object
130 131 132 |
# File 'lib/spunk/bot.rb', line 130 def add_response_processor(processor = nil, &block) @response_processors << (processor || block) end |
#authenticate ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/spunk/bot.rb', line 161 def authenticate @logger.debug "Starting authentication section" unless @token.nil? @logger.debug "Token detected.. sending token" "PASS #{@token}" end "NICK #{@nickname}" "USER #{@username} #{@hostname} bla :#{@fullname}" unless @nickserv_password.nil? "PRIVMSG NickServ :identify #{@nickserv_password}" end end |
#connect ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/spunk/bot.rb', line 138 def connect @logger.info "Starting connection to #{@server[:hostname]}:#{@server[:port]}" begin @socket = TCPSocket.new(@server[:hostname], @server[:port]) rescue raise SpunkException::BotException.new "Unable to establish connection" end if @ssl == true @logger.debug "Detected SSL connection" @ssl_context = OpenSSL::SSL::SSLContext.new() @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE @socket = OpenSSL::SSL::SSLSocket.new(@socket, @ssl_context) @socket.sync_close = true @socket.connect end trap("INT") do @socket.close end trap("KILL") do @socket.close end end |
#join_room(room, password = nil) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/spunk/bot.rb', line 174 def join_room(room, password = nil) @logger.debug "Joining room #{room}" @joined_rooms << room @joined_rooms.uniq! ("JOIN #{room}" + (password ? " #{password}" : "")) end |
#origin ⇒ Object
122 123 124 |
# File 'lib/spunk/bot.rb', line 122 def origin @origin ||= Origin.new("#{@nickname}!#{@username}@#{@hostname}") end |
#parse_message(message) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/spunk/bot.rb', line 82 def () prefix, = if =~ /^\:([^\ ]*) (.*)/ .scan(/^\:([^\ ]*) (.*)/)[0] else [ nil, ] end command, parameters = .split(/\:/, 2) [ prefix, command, parameters ].map! { |s| s && s.strip } end |
#process_request(prefix, command, parameters) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/spunk/bot.rb', line 93 def process_request(prefix, command, parameters) origin = if !prefix.nil? && prefix != "" Origin.new(prefix) end hash = Helpers.hashify(self, origin, command, parameters, @logger) @logger.debug "Processing request: origin=#{hash[:origin]} :: command=#{hash[:command]} :: parameters=#{hash[:parameters]}" (@request_processors + @processors).each do |processor| begin processor.call(hash) rescue => e puts e.class.name + ": " + e. puts e.backtrace.join("\n") end end end |
#process_response(origin, command, parameters) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/spunk/bot.rb', line 109 def process_response(origin, command, parameters) (@response_processors + @processors).each do |processor| begin hash = Helpers.hashify(self, origin, command, parameters, @logger) processor.call(hash) rescue => e puts e.class.name + ": " + e. puts e.backtrace.join("\n") end end @socket.print("#{command} :#{parameters}\r\n") end |
#run ⇒ Object
77 78 79 80 |
# File 'lib/spunk/bot.rb', line 77 def run setup_connection start end |
#say(to, message) ⇒ Object
181 182 183 184 185 186 |
# File 'lib/spunk/bot.rb', line 181 def say(to, ) @logger.debug "saying message: PRIVMSG #{to} :#{}" unless "PRIVMSG #{to} :#{}" raise SpunkException::BotException.new "Unable to send message" end end |
#send_message(message) ⇒ Object
188 189 190 191 192 |
# File 'lib/spunk/bot.rb', line 188 def () @logger.debug "Sending message: #{}" command, parameters = .strip.split(/\:/, 2) process_response(origin, command.to_s.strip, parameters) end |
#setup_connection ⇒ Object
71 72 73 74 75 |
# File 'lib/spunk/bot.rb', line 71 def setup_connection connect authenticate @rooms.each { |room| join_room room } end |
#start ⇒ Object
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 |
# File 'lib/spunk/bot.rb', line 44 def start @logger.info "Starting Bot" loop do @buffer ||= "" if @ssl @buffer += @socket.gets else @buffer += @socket.recv(1024) end = @buffer.split(/\r|\n/).collect { |s| s != "" && !s.nil? ? s : nil }.compact if .any? last_character = @buffer[-1..-1] @buffer = if ["\n", "\r"].include?(last_character) "" else .pop.to_s end .each do || .strip! process_request(*()) end end sleep 0.25 end end |