Class: Isaac::Bot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&b) ⇒ Bot

Returns a new instance of Bot.



16
17
18
19
20
21
# File 'lib/isaac.rb', line 16

def initialize(&b)
  @events = {}
  @config = Config.new("localhost", 6667, nil, "isaac", "Isaac", 'isaac', :production, false)

  instance_eval(&b) if block_given?
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



13
14
15
# File 'lib/isaac.rb', line 13

def channel
  @channel
end

#configObject

Returns the value of attribute config.



13
14
15
# File 'lib/isaac.rb', line 13

def config
  @config
end

#errorObject

Returns the value of attribute error.



13
14
15
# File 'lib/isaac.rb', line 13

def error
  @error
end

#ircObject

Returns the value of attribute irc.



13
14
15
# File 'lib/isaac.rb', line 13

def irc
  @irc
end

#matchObject

Returns the value of attribute match.



13
14
15
# File 'lib/isaac.rb', line 13

def match
  @match
end

#messageObject

Returns the value of attribute message.



13
14
15
# File 'lib/isaac.rb', line 13

def message
  @message
end

#nickObject

Returns the value of attribute nick.



13
14
15
# File 'lib/isaac.rb', line 13

def nick
  @nick
end

#userhostObject

Returns the value of attribute userhost.



13
14
15
# File 'lib/isaac.rb', line 13

def userhost
  @userhost
end

Instance Method Details

#configure(&b) ⇒ Object



38
39
40
# File 'lib/isaac.rb', line 38

def configure(&b)
  b.call(@config)
end

#dispatch(event, env = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/isaac.rb', line 42

def dispatch(event, env={})
  self.nick, self.userhost, self.channel, self.error =
    env[:nick], env[:userhost], env[:channel], env[:error]
  self.message = env[:message] || ""

  event = @events[event] && @events[event].detect do |regexp,_|
    message.match(regexp)
  end

  if event
    regexp, block = *event
    self.match = message.match(regexp).captures
    catch(:halt) { instance_eval(&block) }
  end
end

#haltObject



58
59
60
# File 'lib/isaac.rb', line 58

def halt
  throw :halt
end

#helpers(&b) ⇒ Object



34
35
36
# File 'lib/isaac.rb', line 34

def helpers(&b)
  instance_eval &b
end

#join(*channels) ⇒ Object



70
71
72
# File 'lib/isaac.rb', line 70

def join(*channels)
  channels.each {|channel| raw("JOIN #{channel}")}
end

#msg(recipient, m) ⇒ Object



66
67
68
# File 'lib/isaac.rb', line 66

def msg(recipient, m)
  raw("PRIVMSG #{recipient} :#{m}")
end

#on(event, match = //, &b) ⇒ Object



29
30
31
32
# File 'lib/isaac.rb', line 29

def on(event, match=//, &b)
  match = match.to_s if match.is_a? Integer
  (@events[event] ||= []) << [Regexp.new(match), b]
end

#part(*channels) ⇒ Object



74
75
76
# File 'lib/isaac.rb', line 74

def part(*channels)
  channels.each {|channel| raw("PART #{channel}")}
end

#raw(m) ⇒ Object



62
63
64
# File 'lib/isaac.rb', line 62

def raw(m)
  @irc.message(m)
end

#startObject



23
24
25
26
27
# File 'lib/isaac.rb', line 23

def start
  puts "Connecting to #{@config.server}:#{@config.port}" unless @config.environment == :test
  @irc = IRC.new(self, @config)
  @irc.connect
end

#topic(channel, text) ⇒ Object



78
79
80
# File 'lib/isaac.rb', line 78

def topic(channel, text)
  raw("TOPIC #{channel} :#{text}")
end