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 / event.rb
100644 114 lines (91 sloc) 2.581 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
module CampfireBot
  module Event
    
    # This is an abstract base class for all event types, not to be used directly.
    class EventHandler
      
      attr_reader :kind, :matcher, :plugin, :method
    
      def self.handles(event_type)
        @kind = event_type
      end
  
      def initialize(matcher, plugin_name, method_name)
        @matcher = matcher
        @plugin = plugin_name
        @method = method_name
      end
    
      def run(msg, force = false)
        if force || match?(msg)
          Plugin.registered_plugins[@plugin].send(@method, filter_message(msg))
        else
          false
        end
      end
  
      protected
  
      def filter_message(msg)
        msg
      end
    end
    
    class Command < EventHandler
      handles :messages
  
      def match?(msg)
        (
          msg[:message][0..0] == '!' ||
          msg[:message] =~ Regexp.new("^#{bot.config['nickname']},", Regexp::IGNORECASE)
        ) &&
        msg[:message].gsub(/^!/, '').gsub(Regexp.new("#{bot.config['nickname']}(,|:)?\\s*", Regexp::IGNORECASE), '').split(' ')[0].to_s.downcase == @matcher.downcase
        # FIXME - the above should be just done with one regexp to pull out the first non-! non-<bot name> word.
      end
  
      protected
  
      def filter_message(msg)
        msg[:message] = msg[:message].gsub(Regexp.new("^(!|#{bot.config['nickname']},)\\s*#{@matcher}\\s*", Regexp::IGNORECASE), '')
        msg
      end
    end
 
    class Speaker < EventHandler
      handles :messages
  
      def match?(msg)
        msg[:person] == @matcher
      end
    end
 
    class Message < EventHandler
      handles :messages
  
      def match?(msg)
        msg[:message] =~ @matcher
      end
    end
 
    class Interval < EventHandler
      handles :times
  
      def initialize(*args)
        @last_run = ::Time.now
        super(*args)
      end
  
      def match?
        @last_run < ::Time.now - @matcher
      end
  
      def run(msg, force = false)
        if match?
          Plugin.registered_plugins[@plugin].send(@method, msg)
          @last_run = ::Time.now
        else
          false
        end
      end
    end
 
    class Time < EventHandler
      handles :times
  
      def initialize(*args)
        @run = false
        super(*args)
      end
  
      def match?
        @matcher <= ::Time.now && !@run
      end
  
      def run(force = false)
        if match?
          Plugin.registered_plugins[@plugin].send(@method)
          @run = true
        else
          false
        end
      end
    end
  end
end