public
Description: An extensible bot for the Campfire web-based chat system #crc
Homepage:
Clone URL: git://github.com/timriley/campfire-bot.git
make the restructure work
timriley (author)
Mon Oct 27 05:25:15 -0700 2008
commit  c66dc88de53eeac256d86b45d29f08f5b45e1cbe
tree    cbab5f26c7d1db0d1e38d473a89f6c4a90e31a5a
parent  1071a20f1028292ffce00180bb55ee8250ed5891
...
8
9
10
11
 
 
12
13
14
...
19
20
21
22
 
23
24
25
...
43
44
45
46
47
 
 
48
49
50
...
54
55
56
57
 
 
 
 
 
 
58
59
60
61
62
63
64
65
66
 
 
 
67
68
69
...
8
9
10
 
11
12
13
14
15
...
20
21
22
 
23
24
25
26
...
44
45
46
 
 
47
48
49
50
51
...
55
56
57
 
58
59
60
61
62
63
64
65
66
67
68
69
 
 
 
70
71
72
73
74
75
0
@@ -8,7 +8,8 @@ require "#{BOT_ROOT}/lib/event"
0
 require "#{BOT_ROOT}/lib/plugin"
0
 
0
 # This requires my fork of tinder for the time being
0
-require "#{BOT_ROOT}/../tinder/lib/tinder"
0
+# http://github.com/timriley/tinder/tree
0
+require 'tinder'
0
 
0
 module CampfireBot
0
   class Bot
0
@@ -19,7 +20,7 @@ module CampfireBot
0
     attr_reader :campfire, :room, :config
0
   
0
     def initialize
0
-      @config   = YAML::load(File.read(File.join(File.dirname(__FILE__), 'config.yml')))[BOT_ENVIRONMENT]
0
+      @config   = YAML::load(File.read("#{BOT_ROOT}/config.yml"))[BOT_ENVIRONMENT]
0
     end
0
   
0
     def connect
0
@@ -43,8 +44,8 @@ module CampfireBot
0
           # EventHanlder.handle_time(optional_arg = Time.now)
0
         
0
           # Run time-oriented events
0
-          PluginBase.registered_intervals.each        { |handler| handler.run }
0
-          PluginBase.registered_times.each_with_index { |handler, index| PluginBase.registered_times.delete_at(index) if handler.run }
0
+          Plugin.registered_intervals.each        { |handler| handler.run }
0
+          Plugin.registered_times.each_with_index { |handler, index| Plugin.registered_times.delete_at(index) if handler.run }
0
         
0
           sleep interval
0
         end
0
@@ -54,16 +55,21 @@ module CampfireBot
0
     private
0
   
0
     def load_plugins
0
-      Dir["#{File.dirname(__FILE__)}/plugins/*.rb"].each{|x| load x }
0
+      Dir["#{BOT_ROOT}/plugins/*.rb"].each{|x| load x }
0
+      
0
+      # And instantiate them
0
+      Plugin.registered_plugins.each_pair do |name, klass|
0
+        Plugin.registered_plugins[name] = klass.new
0
+      end
0
     end
0
   
0
     def handle_message(msg)
0
       puts
0
       puts msg.inspect
0
     
0
-      PluginBase.registered_commands.each { |handler| handler.run(msg) }
0
-      PluginBase.registered_speakers.each { |handler| handler.run(msg) }
0
-      PluginBase.registered_messages.each { |handler| handler.run(msg) }
0
+      Plugin.registered_commands.each { |handler| handler.run(msg) }
0
+      Plugin.registered_speakers.each { |handler| handler.run(msg) }
0
+      Plugin.registered_messages.each { |handler| handler.run(msg) }
0
     end
0
   end
0
 end
...
17
18
19
20
 
21
22
23
...
80
81
82
83
 
84
85
86
...
102
103
104
105
 
106
107
108
...
17
18
19
 
20
21
22
23
...
80
81
82
 
83
84
85
86
...
102
103
104
 
105
106
107
108
0
@@ -17,7 +17,7 @@ module CampfireBot
0
     
0
       def run(msg, force = false)
0
         if force || match?(msg)
0
-          PluginBase.registered_plugins[@plugin].send(@method, filter_message(msg))
0
+          Plugin.registered_plugins[@plugin].send(@method, filter_message(msg))
0
         else
0
           false
0
         end
0
@@ -80,7 +80,7 @@ module CampfireBot
0
   
0
       def run(force = false)
0
         if match?
0
-          PluginBase.registered_plugins[@plugin].send(@method)
0
+          Plugin.registered_plugins[@plugin].send(@method)
0
           @last_run = Time.now
0
         else
0
           false
0
@@ -102,7 +102,7 @@ module CampfireBot
0
   
0
       def run(force = false)
0
         if match?
0
-          PluginBase.registered_plugins[@plugin].send(@method)
0
+          Plugin.registered_plugins[@plugin].send(@method)
0
           @run = true
0
         else
0
           false
...
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
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
0
@@ -1,84 +1,74 @@
0
-module CampfireBot
0
-  module Plugin
0
-  
0
-    module PluginSugar
0
-      def def_field(*names)
0
-        class_eval do 
0
-          names.each do |name|
0
-            define_method(name) do |*args| 
0
-              case args.size
0
-                when 0: instance_variable_get("@#{name}")
0
-                else    instance_variable_set("@#{name}", *args)
0
-              end
0
-            end
0
-          end
0
-        end
0
+module CampfireBot  
0
+  class Plugin
0
+    @registered_plugins   = {}
0
+    
0
+    @registered_commands  = []
0
+    @registered_messages  = []
0
+    @registered_speakers  = []
0
+    @registered_intervals = []
0
+    @registered_times     = []
0
+    
0
+    class << self
0
+      attr_reader :registered_plugins,
0
+                  :registered_commands,
0
+                  :registered_messages,
0
+                  :registered_speakers,
0
+                  :registered_intervals,
0
+                  :registered_times
0
+
0
+      # Registering plugins
0
+
0
+      def inherited(child)
0
+        Plugin.registered_plugins[child.to_s] = child
0
       end
0
-    end
0
 
0
-    class Base
0
-      @registered_plugins   = {}
0
-      
0
-      class << self
0
-        extend PluginSugar
0
-        def_field :author, :version
0
-    
0
-        attr_reader :registered_plugins
0
+      # Event handlers
0
 
0
-        # Registering plugins
0
-  
0
-        def inherited(child)
0
-          PluginBase.registered_plugins[child.to_s] = child.new
0
+      def on_command(command, *methods)
0
+        methods.each do |method|
0
+          Plugin.registered_commands << Event::Command.new(command, self.to_s, method)
0
         end
0
+      end
0
 
0
-        # Event handlers
0
-  
0
-        def on_command(command, *methods)
0
-          methods.each do |method|
0
-            registered_handlers << CampfireBot::Event::Command.new(command, self.to_s, method)
0
-          end
0
-        end
0
-  
0
-        def on_message(regexp, *methods)
0
-          methods.each do |method|
0
-            registered_handlers << CampfireBot::Event::Message.new(regexp, self.to_s, method)
0
-          end
0
+      def on_message(regexp, *methods)
0
+        methods.each do |method|
0
+          Plugin.registered_messages << Event::Message.new(regexp, self.to_s, method)
0
         end
0
-  
0
-        def on_speaker(speaker, *methods)
0
-          methods.each do |method|
0
-            registered_handlers << CampfireBot::Event::Speaker.new(speaker, self.to_s, method)
0
-          end
0
+      end
0
+
0
+      def on_speaker(speaker, *methods)
0
+        methods.each do |method|
0
+          Plugin.registered_speakers << Event::Speaker.new(speaker, self.to_s, method)
0
         end
0
-  
0
-        def at_interval(interval, *methods)
0
-          methods.each do |method|
0
-            registered_handlers << CampfireBot::Event::Interval.new(interval, self.to_s, method)
0
-          end
0
+      end
0
+
0
+      def at_interval(interval, *methods)
0
+        methods.each do |method|
0
+          Plugin.registered_intervals << Event::Interval.new(interval, self.to_s, method)
0
         end
0
+      end
0
 
0
-        def at_time(timestamp, *methods)
0
-          methods.each do |method|
0
-            registered_handlers << CampfireBot::Event::Time.new(timestamp, self.to_s, method)
0
-          end
0
+      def at_time(timestamp, *methods)
0
+        methods.each do |method|
0
+          Plugin.registered_times << Event::Time.new(timestamp, self.to_s, method)
0
         end
0
       end
0
+    end
0
 
0
-      protected
0
+    protected
0
 
0
-      # Shortcuts to access the room
0
-  
0
-      def speak(words)
0
-        CampfireBot::Bot.instance.room.speak(words)
0
-      end
0
-  
0
-      def paste(words)
0
-        CampfireBot::Bot.instance.room.paste(words)
0
-      end
0
-  
0
-      def upload(file_path)
0
-        CampfireBot::Bot.instance.room.upload(file_path)
0
-      end
0
+    # Shortcuts to access the room
0
+
0
+    def speak(words)
0
+      CampfireBot::Bot.instance.room.speak(words)
0
+    end
0
+
0
+    def paste(words)
0
+      CampfireBot::Bot.instance.room.paste(words)
0
+    end
0
+
0
+    def upload(file_path)
0
+      CampfireBot::Bot.instance.room.upload(file_path)
0
     end
0
   end
0
 end
0
\ No newline at end of file
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 require 'open-uri'
0
 require 'hpricot'
0
 
0
-class Austin < PluginBase
0
+class Austin < CampfireBot::Plugin
0
   BASE_URL = 'http://www.imdb.com/character/ch0002425/quotes'
0
   
0
   on_command 'austin', :austin
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 require 'open-uri'
0
 require 'hpricot'
0
 
0
-class BeijingTally < CampfireBot::Plugin::Base
0
+class BeijingTally < CampfireBot::Plugin
0
   
0
   on_command 'tally', :tally
0
   
...
11
12
13
14
 
15
16
17
...
11
12
13
 
14
15
16
17
0
@@ -11,7 +11,7 @@ Tempfile.class_eval do
0
   end
0
 end
0
 
0
-class Calvin < CampfireBot::Plugin::Base
0
+class Calvin < CampfireBot::Plugin
0
   BASE_URL    = 'http://www.marcellosendos.ch/comics/ch/'
0
   START_DATE  = Date.parse('1984-08-14')
0
   END_DATE    = Date.parse('1995-12-31') # A sad day
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 require 'open-uri'
0
 require 'hpricot'
0
 
0
-class Chuck < CampfireBot::Plugin::Base
0
+class Chuck < CampfireBot::Plugin
0
   on_command 'chuck', :chuck
0
   
0
   def chuck(msg)
...
11
12
13
14
 
15
16
17
...
11
12
13
 
14
15
16
17
0
@@ -11,7 +11,7 @@ Tempfile.class_eval do
0
   end
0
 end
0
 
0
-class Dilbert < CampfireBot::Plugin::Base
0
+class Dilbert < CampfireBot::Plugin
0
   BASE_URL   = 'http://dilbert.com/'
0
   START_DATE = Date.parse('1996-01-01')
0
   
...
1
 
2
3
 
4
5
6
...
 
1
2
 
3
4
5
6
0
@@ -1,6 +1,6 @@
0
-class Fun < CampfireBot::Plugin::Base
0
+class Fun < CampfireBot::Plugin
0
   on_command    'say',              :say
0
-  on_message    Regexp.new("^#{Bot.instance.config['nickname']},\\s+(should|can|will|shall) (i|he|she|we|they) do it\\?", Regexp::IGNORECASE), :do_or_do_not
0
+  on_message    Regexp.new("^#{CampfireBot::Bot.instance.config['nickname']},\\s+(should|can|will|shall) (i|he|she|we|they) do it\\?", Regexp::IGNORECASE), :do_or_do_not
0
   on_message    /^(good morning|morning|m0ink).$/i, :greet
0
   # on_speaker    'Tim R.',           :agree_with_tim
0
   # on_message    /undo it/i,         :do_it
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 require 'open-uri'
0
 require 'hpricot'
0
 
0
-class Quote < CampfireBot::Plugin::Base
0
+class Quote < CampfireBot::Plugin
0
   on_command 'quote', :quote
0
   
0
   def quote(msg)
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 require 'open-uri'
0
 require 'hpricot'
0
 
0
-class Schneier < PluginBase
0
+class Schneier < CampfireBot::Plugin
0
   BASE_URL   = 'http://geekz.co.uk/schneierfacts/'
0
   
0
   on_command 'schneier', :schneier
...
1
2
3
 
4
5
6
...
1
2
 
3
4
5
6
0
@@ -1,6 +1,6 @@
0
 require 'yahoo-weather'
0
 
0
-class Weather < CampfireBot::Plugin::Base
0
+class Weather < CampfireBot::Plugin
0
   on_command 'weather', :weather
0
   
0
   def weather(msg)
...
11
12
13
14
 
15
16
17
...
11
12
13
 
14
15
16
17
0
@@ -11,7 +11,7 @@ Tempfile.class_eval do
0
   end
0
 end
0
 
0
-class Xkcd < CampfireBot::Plugin::Base
0
+class Xkcd < CampfireBot::Plugin
0
   BASE_URL = 'http://xkcd.com/'
0
   
0
   on_command 'xkcd', :xkcd
...
1
2
3
4
5
6
 
 
 
7
8
9
10
...
1
 
2
3
4
 
5
6
7
8
9
10
11
0
@@ -1,9 +1,10 @@
0
 #!/usr/bin/env ruby
0
-require File.dirname(__FILE__) + '/../lib/bot'
0
 
0
 # Run this script with the environment as the only argument. eg. ./script/bot development
0
 BOT_ENVIRONMENT = ARGV.first
0
-BOT_ROOT        = File.dirname(__FILE__)
0
+BOT_ROOT        = File.join(File.dirname(__FILE__), '..')
0
+
0
+require File.join(BOT_ROOT, 'lib', 'bot')
0
 
0
 bot.connect
0
 bot.run
0
\ No newline at end of file

Comments