forked from markolson/linkbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_plugins.rb
More file actions
71 lines (56 loc) · 1.95 KB
/
base_plugins.rb
File metadata and controls
71 lines (56 loc) · 1.95 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
require 'rubygems'
require 'pp'
require 'db'
class MessageType
MESSAGE = :message
DIRECTMESSAGE = :"direct-message"
STARRED = :starred
UNSTARRED = :unstarred
end
#The interface message object between Linkbot::Plugin and the plugins.
#New axiom: the plugins know nothing about the service they're using!
Message = Struct.new(:body, :user_id, :user_name, :type)
class Linkbot
class Plugin
@@plugins = {}
@@message_log = []
def self.handle_message(message)
print "handle_message got called with #{message}"
@@message_log << message
final_message = []
Linkbot::Plugin.plugins.each {|k,v|
if v[:handlers][message.type] && v[:handlers][message.type][:handler]
if ((v[:handlers][message.type][:regex] && v[:handlers][message.type][:regex].match(message.body)) || v[:handlers][message.type][:regex].nil?)
matches = v[:handlers][message.type][:regex] ? v[:handlers][message.type][:regex].match(message.body).to_a.drop(1) : nil
p "#{k} processing message type #{message.type}"
begin
end_msg = v[:ptr].send(v[:handlers][message.type][:handler], message, matches)
rescue => e
end_msg = "the #{k} plugin threw an exception: #{e.inspect}"
puts e.inspect
puts e.backtrace.join("\n")
end
final_message << end_msg if !end_msg.empty?
end
end
}
print "returning msgs from plugins:"
pp final_message
final_message
end
def self.message_history
@@message_log
end
def self.registered_methods
@registered_methods ||= {}
@registered_methods
end
def self.plugins; @@plugins; end;
def self.collect
Dir["plugins/*.rb"].each {|file| load file }
end
def self.register(name, s, handlers)
@@plugins[name] = {:ptr => s, :handlers => handlers}
end
end
end