Skip to content
Browse files

cleaned up more extension code

  • Loading branch information...
1 parent 696428d commit 4ae1df326c482c697721022ddfce380477c8b440 @andrewhavens committed Feb 18, 2012
View
9 bin/mad_chatter
@@ -14,10 +14,17 @@ module MadChatter
desc "new [name]", "Generates a new Mad Chatter application"
def new(name)
copy_file "templates/config.yml", "#{name}/config.yml"
- copy_file "templates/extensions.rb", "#{name}/extensions.rb"
+ directory "templates/extensions", "#{name}/extensions"
directory "templates/web", "#{name}/web"
end
+ desc "update", "Updates an existing Mad Chatter application"
+ def new(name)
+ copy_file "templates/config.yml", "config.yml"
+ directory "templates/extensions", "extensions"
+ directory "templates/web", "web"
+ end
+
desc 'preview', 'Starts both a web server and the Mad Chatter chat server'
def preview
t1 = Thread.new { require 'mad_chatter/web_server' }
View
4 lib/mad_chatter.rb
@@ -19,15 +19,13 @@
require 'mad_chatter/message_history'
require 'mad_chatter/actions'
+require 'mad_chatter/extension'
require 'mad_chatter/message_listeners/channel_commands'
require 'mad_chatter/message_listeners/markdown'
require 'mad_chatter/message_listeners/nick'
require 'mad_chatter/message_listeners/image'
require 'mad_chatter/message_listeners/code'
-require 'mad_chatter/extension'
-require 'mad_chatter/message_listener'
-
module MadChatter
class << self
View
24 lib/mad_chatter/actions.rb
@@ -5,24 +5,22 @@ def stop_message_handling
raise "Don't call any more message listeners"
end
- # def send_json(json)
- # MadChatter.send_json(json)
- # end
-
- def send_message(text, from)
- MadChatter.send_message MadChatter::Message.new('message', text, from)
+ def send_message(text, from = nil)
+ m = MadChatter::Message.new('message', text)
+ m.username = from || @message.username
+ m.channel = @message.channel
+ MadChatter.send_message(m)
end
def send_status_message(text)
- MadChatter.send_message MadChatter::Message.new('status', text)
+ MadChatter.send_message MadChatter::Message.new('status', text, nil, @message.channel)
end
-
- # def send_users_list
- # send_json MadChatter::Users.to_json
- # end
-
+
def send_action(action, *args)
- MadChatter.send_message MadChatter::Message.new('action', {function: action, args: args})
+ m = MadChatter::Message.new('action')
+ m.json = {function: action, args: args}
+ m.channel = @message.channel
+ MadChatter.send_message(m)
end
end
View
40 lib/mad_chatter/extension.rb
@@ -1,48 +1,16 @@
module MadChatter
class Extension
+ include MadChatter::Actions
- class << self
- def inherited(klass)
- MadChatter.message_listeners << klass.new
- end
+ attr_accessor :message
- def on_message(regex, &block)
- alias_method :old_handle, :handle
- define_method(:handle) {
- if message.text =~ regex
- args = regex.match(message.original_text).captures
- block.call(args)
- end
- }
- end
+ def self.inherited(klass)
+ MadChatter.message_listeners << klass.new
end
-
- attr_accessor :message
def handle(message)
# subclasses should override this function, or use the on_message shortcut method
end
- def stop_message_handling
- raise "Don't call any more message listeners"
- end
-
- def send_message(text, from = nil)
- m = MadChatter::Message.new('message', text)
- m.username = from || @message.username
- m.channel = @message.channel
- MadChatter.send_message(m)
- end
-
- def send_status_message(text)
- MadChatter.send_message MadChatter::Message.new('status', text, nil, @message.channel)
- end
-
- def send_action(action, *args)
- m = MadChatter::Message.new('action')
- m.json = {function: action, args: args}
- m.channel = @message.channel
- MadChatter.send_message(m)
- end
end
end
View
12 lib/mad_chatter/extensions.rb
@@ -1,12 +0,0 @@
-module MadChatter
- module Extensions
-
- # include MadChatter::Actions
-
- # Used for defining simple extensions
- def self.on_message(regex, &block)
- MadChatter.message_listeners << MadChatter::MessageListener.new(regex, block)
- end
-
- end
-end
View
17 lib/mad_chatter/message_listener.rb
@@ -1,17 +0,0 @@
-module MadChatter
- class MessageListener
- include MadChatter::Actions
-
- def initialize(regex = nil, block = nil)
- @regex = regex
- @block = block
- end
-
- def handle(message)
- if message.text =~ @regex
- args = @regex.match(message.original_text).captures
- @block.call(args)
- end
- end
- end
-end
View
7 lib/mad_chatter/message_listeners/code.rb
@@ -4,16 +4,13 @@ class Code
include MadChatter::Actions
- @@regex = %r{^/code (.+)}
-
def handle(msg)
- if msg.original_text =~ @@regex
+ if msg.original_text =~ %r{^/code (.+)}
code = parse(msg.original_text)
message = MadChatter::Message.new('message', nil, msg.token, msg.channel)
message.html = "<pre>" + message.filter(code) + "</pre>"
message.growl = msg.username + ' has shared a code sample' if msg.username
- channel = MadChatter::find_channel_by_id(msg.channel)
- channel.send_message(message)
+ MadChatter.send_message(message)
stop_message_handling
end
end
View
3 lib/mad_chatter/message_listeners/image.rb
@@ -12,8 +12,7 @@ def handle(message)
img_msg = MadChatter::Message.new('message', img_url, message.token, message.channel)
img_msg.html = "<img src='#{img_url}'>"
img_msg.growl = message.username + ' has shared an image'
- channel = MadChatter::find_channel_by_id(message.channel)
- channel.send_message(img_msg)
+ MadChatter.send_message(message)
stop_message_handling
end
end
View
21 templates/extensions.rb
@@ -6,25 +6,14 @@
#
# Example Extension:
#
- on_message %r{ hey andrew: (.+) } do |regex_capture|
- send_status_message "Someone is talking to Andrew"
- send_message "message: #{regex_capture}"
- stop_message_handling
- end
+# on_message %r{ hey andrew: (.+) } do |regex_capture|
+# send_status_message "Someone is talking to Andrew"
+# send_message "message: #{regex_capture}"
+# stop_message_handling
+# end
# on_message %r{^/youtube http://youtu.be/(.*)$} do |youtube_id|
# send_message "<iframe width='560' height='315' src='http://www.youtube.com/embed/#{youtube_id}' frameborder='0' allowfullscreen></iframe>"
# stop_message_handling
-# end
-#
-# on_message %r{^/alert (.*)$} do |alert_message|
-# send_action 'show_alert', alert_message
-# stop_message_handling
-# end
-#
-# on_message %r{^/earthquake$} do
-# send_status_message 'Someone has just caused an earthquake!'
-# send_action 'earthquake'
-# stop_message_handling
# end

0 comments on commit 4ae1df3

Please sign in to comment.
Something went wrong with that request. Please try again.