public
Description: An extensible bot for the Campfire web-based chat system #crc
Homepage:
Clone URL: git://github.com/timriley/campfire-bot.git
campfire-bot / lib / bot.rb
100755 81 lines (64 sloc) 2.223 kb
1
2
3
4
5
6
7
8
9
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
43
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
70
71
72
73
74
75
76
77
78
79
80
81
# External Libs
require 'rubygems'
require 'activesupport'
require 'yaml'
 
# Local Libs
require "#{BOT_ROOT}/lib/event"
require "#{BOT_ROOT}/lib/plugin"
 
# This requires my fork of tinder for the time being
# gem sources -a http://gems.github.com
# sudo gem install timriley-tinder
require 'tinder'
 
module CampfireBot
  class Bot
    # this is necessary so the room and campfire objects can be accessed by plugins.
    include Singleton
 
    # FIXME - these will be unaccessible if disconnected. handle this.
    attr_reader :campfire, :room, :config
  
    def initialize
      @config = YAML::load(File.read("#{BOT_ROOT}/config.yml"))[BOT_ENVIRONMENT]
    end
  
    def connect
      load_plugins
    
      @campfire = Tinder::Campfire.new(@config['site'])
      @campfire.login(@config['username'], @config['password'])
      @room = @campfire.find_room_by_name(@config['room'])
      @room.join
      puts "Ready."
    end
  
    def run(interval = 5)
      catch(:stop_listening) do
        trap('INT') { throw :stop_listening }
        loop do
          @room.ping
          @room.listen.each { |msg| handle_message(msg) }
        
          # Here's how I want it to look
          # @room.listen.each { |m| EventHandler.handle_message(m) }
          # EventHanlder.handle_time(optional_arg = Time.now)
        
          # Run time-oriented events
          Plugin.registered_intervals.each { |handler| handler.run }
          Plugin.registered_times.each_with_index { |handler, index| Plugin.registered_times.delete_at(index) if handler.run }
        
          sleep interval
        end
      end
    end
  
    private
  
    def load_plugins
      Dir["#{BOT_ROOT}/plugins/*.rb"].each{|x| load x }
      
      # And instantiate them
      Plugin.registered_plugins.each_pair do |name, klass|
        Plugin.registered_plugins[name] = klass.new
      end
    end
  
    def handle_message(msg)
      puts
      puts msg.inspect
    
      Plugin.registered_commands.each { |handler| handler.run(msg) }
      Plugin.registered_speakers.each { |handler| handler.run(msg) }
      Plugin.registered_messages.each { |handler| handler.run(msg) }
    end
  end
end
 
def bot
  CampfireBot::Bot.instance
end