Class: Sprinkles::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/sprinkles/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bot



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sprinkles/bot.rb', line 7

def initialize(options = {})
  options.each do |option, value|
    instance_variable_set("@#{option}", value)
  end
  @processors = []
  @request_processors = []
  @response_processors = []
  @hostname = Socket.gethostname
  @server ||= { :hostname => options[:hostname] || "localhost", :port => options[:port] || 6667 }
  add_request_processor(Sprinkles::Processor::Ping.new)
  @rooms = options[:rooms] || []
end

Instance Attribute Details

#nicknameObject

Returns the value of attribute nickname



5
6
7
# File 'lib/sprinkles/bot.rb', line 5

def nickname
  @nickname
end

#serverObject

Returns the value of attribute server



5
6
7
# File 'lib/sprinkles/bot.rb', line 5

def server
  @server
end

Instance Method Details

#add_processor(processor = nil, &block) ⇒ Object



95
96
97
# File 'lib/sprinkles/bot.rb', line 95

def add_processor(processor = nil, &block)
  @processors << (processor || block)
end

#add_request_processor(processor = nil, &block) ⇒ Object



87
88
89
# File 'lib/sprinkles/bot.rb', line 87

def add_request_processor(processor = nil, &block)
  @request_processors << (processor || block)
end

#add_response_processor(processor = nil, &block) ⇒ Object



91
92
93
# File 'lib/sprinkles/bot.rb', line 91

def add_response_processor(processor = nil, &block)
  @response_processors << (processor || block)
end

#authenticateObject



109
110
111
112
# File 'lib/sprinkles/bot.rb', line 109

def authenticate
  send_message "NICK #{@nickname}"
  send_message "USER #{@username} #{@hostname} bla :#{@fullname}"
end

#connectObject



99
100
101
102
103
104
105
106
107
# File 'lib/sprinkles/bot.rb', line 99

def connect
  @socket = TCPSocket.new(@server[:hostname], @server[:port])
  trap("INT") do
    @socket.close
  end
  trap("KILL") do
    @socket.close
  end
end

#join_room(room, password = nil) ⇒ Object



114
115
116
# File 'lib/sprinkles/bot.rb', line 114

def join_room(room, password = nil)
  send_message("JOIN #{room}" + (password ? " #{password}" : ""))
end

#originObject



83
84
85
# File 'lib/sprinkles/bot.rb', line 83

def origin
  @origin ||= Origin.new("#{@nickname}!#{@username}@#{@hostname}")
end

#parse_message(message) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/sprinkles/bot.rb', line 45

def parse_message(message)
  prefix, message = if message =~ /^\:([^\ ]*) (.*)/
    message.scan(/^\:([^\ ]*) (.*)/)[0]
  else
    [ nil, message ]
  end

  command, parameters = message.split(/\:/, 2)
  [ prefix, command, parameters ].map! { |s| s && s.strip }
end

#process_request(prefix, command, parameters) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sprinkles/bot.rb', line 56

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.message
      puts e.backtrace.join("\n")
    end
  end
end

#process_response(origin, command, parameters) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sprinkles/bot.rb', line 71

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.message
      puts e.backtrace.join("\n")
    end
  end
  @socket.print("#{command} :#{parameters}\r\n")
end

#say(to, message) ⇒ Object



118
119
120
# File 'lib/sprinkles/bot.rb', line 118

def say(to, message)
  send_message "PRIVMSG #{to} :#{message}"
end

#send_message(message) ⇒ Object



122
123
124
125
# File 'lib/sprinkles/bot.rb', line 122

def send_message(message)
  command, parameters = message.strip.split(/\:/, 2)
  process_response(origin, command.to_s.strip, parameters)
end

#startObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sprinkles/bot.rb', line 20

def start
  connect
  authenticate
  @rooms.each { |room| join_room room }
  loop do
    @buffer ||= ""
    @buffer += @socket.recv(1024)
    messages = @buffer.split(/\r|\n/).collect { |s| s != "" && !s.nil? ? s : nil }.compact
    if messages.any?
      last_character = @buffer[-1..-1]
      @buffer = if ["\n", "\r"].include?(last_character)
        ""
      else
        messages.pop.to_s
      end

      messages.each do |message|
        message.strip!
        process_request(*parse_message(message))
      end
    end
    sleep 0.25
  end
end