Class: Isaac::Bot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&b) ⇒ Bot

Returns a new instance of Bot.



12
13
14
15
16
17
# File 'lib/isaac/bot.rb', line 12

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

  instance_eval(&b) if block_given?
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



9
10
11
# File 'lib/isaac/bot.rb', line 9

def channel
  @channel
end

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/isaac/bot.rb', line 9

def config
  @config
end

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/isaac/bot.rb', line 9

def error
  @error
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/isaac/bot.rb', line 9

def host
  @host
end

#ircObject

Returns the value of attribute irc.



9
10
11
# File 'lib/isaac/bot.rb', line 9

def irc
  @irc
end

#matchObject

Returns the value of attribute match.



9
10
11
# File 'lib/isaac/bot.rb', line 9

def match
  @match
end

#messageObject

Returns the value of attribute message.



9
10
11
# File 'lib/isaac/bot.rb', line 9

def message
  @message
end

#nickObject

Returns the value of attribute nick.



9
10
11
# File 'lib/isaac/bot.rb', line 9

def nick
  @nick
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/isaac/bot.rb', line 9

def user
  @user
end

Instance Method Details

#action(recipient, text) ⇒ Object



44
45
46
# File 'lib/isaac/bot.rb', line 44

def action(recipient, text)
  raw("PRIVMSG #{recipient} :\001ACTION #{text}\001")
end

#configure(&b) ⇒ Object



19
20
21
# File 'lib/isaac/bot.rb', line 19

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

#dispatch(event, msg = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/isaac/bot.rb', line 83

def dispatch(event, msg=nil)
  if msg
    @nick, @user, @host, @channel, @error, @message = 
      msg.nick, msg.user, msg.host, msg.channel, msg.error, msg.message
  end

  if handler = find(event, message)
    regexp, block = *handler
    self.match = message.match(regexp).captures
    invoke block
  end
end

#haltObject



32
33
34
# File 'lib/isaac/bot.rb', line 32

def halt
  throw :halt
end

#helpers(&b) ⇒ Object



28
29
30
# File 'lib/isaac/bot.rb', line 28

def helpers(&b)
  instance_eval(&b)
end

#join(*channels) ⇒ Object



48
49
50
# File 'lib/isaac/bot.rb', line 48

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

#kick(channel, user, reason = nil) ⇒ Object



64
65
66
# File 'lib/isaac/bot.rb', line 64

def kick(channel, user, reason=nil)
  raw("KICK #{channel} #{user} :#{reason}")
end

#mode(channel, option) ⇒ Object



60
61
62
# File 'lib/isaac/bot.rb', line 60

def mode(channel, option)
  raw("MODE #{channel} #{option}")
end

#msg(recipient, text) ⇒ Object



40
41
42
# File 'lib/isaac/bot.rb', line 40

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

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



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

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

#part(*channels) ⇒ Object



52
53
54
# File 'lib/isaac/bot.rb', line 52

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

#quit(message = nil) ⇒ Object



68
69
70
71
# File 'lib/isaac/bot.rb', line 68

def quit(message=nil)
  command = message ? "QUIT :#{message}" : "QUIT"
  raw command
end

#raw(command) ⇒ Object



36
37
38
# File 'lib/isaac/bot.rb', line 36

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

#startObject



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

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



56
57
58
# File 'lib/isaac/bot.rb', line 56

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