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.
-
#joined_rooms ⇒ Object
Returns the value of attribute joined_rooms.
-
#nickname ⇒ Object
Returns the value of attribute nickname.
-
#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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/spunk/bot.rb', line 9 def initialize( = {}) .each do |option, value| instance_variable_set("@#{option}", value) end @token = [:token] || nil @ssl = [:ssl] || false @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] ||= [] end |
Instance Attribute Details
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def hostname @hostname end |
#joined_rooms ⇒ Object
Returns the value of attribute joined_rooms.
6 7 8 |
# File 'lib/spunk/bot.rb', line 6 def joined_rooms @joined_rooms end |
#nickname ⇒ Object
Returns the value of attribute nickname.
6 7 8 |
# File 'lib/spunk/bot.rb', line 6 def nickname @nickname end |
#processors ⇒ Object (readonly)
Returns the value of attribute processors.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def processors @processors end |
#request_processors ⇒ Object (readonly)
Returns the value of attribute request_processors.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def request_processors @request_processors end |
#response_processors ⇒ Object (readonly)
Returns the value of attribute response_processors.
7 8 9 |
# File 'lib/spunk/bot.rb', line 7 def response_processors @response_processors end |
#rooms ⇒ Object
Returns the value of attribute rooms.
6 7 8 |
# File 'lib/spunk/bot.rb', line 6 def rooms @rooms end |
#server ⇒ Object
Returns the value of attribute server.
6 7 8 |
# File 'lib/spunk/bot.rb', line 6 def server @server end |
#ssl ⇒ Object
Returns the value of attribute ssl.
6 7 8 |
# File 'lib/spunk/bot.rb', line 6 def ssl @ssl end |
#token ⇒ Object
Returns the value of attribute token.
6 7 8 |
# File 'lib/spunk/bot.rb', line 6 def token @token end |
Instance Method Details
#add_processor(processor = nil, &block) ⇒ Object
113 114 115 |
# File 'lib/spunk/bot.rb', line 113 def add_processor(processor = nil, &block) @processors << (processor || block) end |
#add_request_processor(processor = nil, &block) ⇒ Object
105 106 107 |
# File 'lib/spunk/bot.rb', line 105 def add_request_processor(processor = nil, &block) @request_processors << (processor || block) end |
#add_response_processor(processor = nil, &block) ⇒ Object
109 110 111 |
# File 'lib/spunk/bot.rb', line 109 def add_response_processor(processor = nil, &block) @response_processors << (processor || block) end |
#authenticate ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/spunk/bot.rb', line 134 def authenticate unless @token.nil? "PASS #{@token}" end "NICK #{@nickname}" "USER #{@username} #{@hostname} bla :#{@fullname}" end |
#connect ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/spunk/bot.rb', line 117 def connect @socket = TCPSocket.new(@server[:hostname], @server[:port]) if @ssl == true @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
142 143 144 145 146 |
# File 'lib/spunk/bot.rb', line 142 def join_room(room, password = nil) @joined_rooms << room @joined_rooms.uniq! ("JOIN #{room}" + (password ? " #{password}" : "")) end |
#origin ⇒ Object
101 102 103 |
# File 'lib/spunk/bot.rb', line 101 def origin @origin ||= Origin.new("#{@nickname}!#{@username}@#{@hostname}") end |
#parse_message(message) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/spunk/bot.rb', line 63 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/spunk/bot.rb', line 74 def process_request(prefix, command, parameters) origin = if !prefix.nil? && prefix != "" Origin.new(prefix) end (@request_processors + @processors).each do |processor| begin processor.call(self, origin, command, parameters) rescue => e puts e.class.name + ": " + e. puts e.backtrace.join("\n") end end end |
#process_response(origin, command, parameters) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/spunk/bot.rb', line 89 def process_response(origin, command, parameters) (@response_processors + @processors).each do |processor| begin processor.call(self, origin, command, parameters) rescue => e puts e.class.name + ": " + e. puts e.backtrace.join("\n") end end @socket.print("#{command} :#{parameters}\r\n") end |
#run ⇒ Object
58 59 60 61 |
# File 'lib/spunk/bot.rb', line 58 def run setup_connection start end |
#say(to, message) ⇒ Object
148 149 150 |
# File 'lib/spunk/bot.rb', line 148 def say(to, ) "PRIVMSG #{to} :#{}" end |
#send_message(message) ⇒ Object
152 153 154 155 |
# File 'lib/spunk/bot.rb', line 152 def () command, parameters = .strip.split(/\:/, 2) process_response(origin, command.to_s.strip, parameters) end |
#setup_connection ⇒ Object
52 53 54 55 56 |
# File 'lib/spunk/bot.rb', line 52 def setup_connection connect authenticate @rooms.each { |room| join_room room } end |
#start ⇒ Object
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 |
# File 'lib/spunk/bot.rb', line 26 def start 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 |