Permalink
Browse files

Merge pull request #8 from wilkie/feature/tweet_from_url

Feature/tweet from url
  • Loading branch information...
2 parents 3ee507c + 9caed94 commit e36c8d72e6fcc5552f17b235b07465ea5939d55b @LindseyB committed Oct 14, 2012
Showing with 54 additions and 3 deletions.
  1. +2 −1 lib/xombot.rb
  2. +12 −0 lib/xombot/plugin.rb
  3. +15 −1 lib/xombot/plugins/tweet.rb
  4. +25 −1 lib/xombot/plugins/url_announce.rb
View
@@ -38,7 +38,8 @@ class << self
def add_plugin plugin
@plugins = {} if @plugins.nil?
- @plugins[plugin.class.name] = plugin
+ plugin_name = plugin.class.name[/^XOmBot::Plugins::(.*)/, 1]
+ @plugins[plugin_name] = plugin
end
def start
View
@@ -5,9 +5,21 @@ class Plugin
def initialize *args
# Register the plugin with XOmBot
XOmBot.add_plugin self
+
+ # When all plugins are loaded, call setup on each
+ if XOmBot.plugins.count == XOmBot::Plugins.constants.count
+ XOmBot.plugins.each do |k, v|
+ v.setup
+ end
+ end
+
super *args
end
+ # Called after all plugins have been loaded and initialized
+ def setup
+ end
+
module ModuleSet
attr_reader :matches
@@ -7,12 +7,26 @@ class Tweet < XOmBot::Plugin
help "Displays the latest tweet by the given user"
usage "tweet noob -- displays the last tweet by noob"
+ def setup
+ url_plugin = XOmBot.plugins["URLAnnounce"]
+ if url_plugin
+ url_plugin.match_domain("twitter.com") do |m, url|
+ tweet_by_url m, url
+ end
+ end
+ end
+
def tweet_by_id(m, id)
status = Twitter.status(id)
m.reply "@#{status.user.screen_name}: #{HTMLEntities.new.decode status.text}"
end
def tweet_by_username(m, username)
- m.reply "@#{username}: #{HTMLEntities.new.decode Twitter.user_timeline(username).first.text}"
+ m.reply "@#{username}: #{HTMLEntities.new.decode Twitter.user_timeline(username).first.text}"
+ end
+
+ def tweet_by_url(m, url)
+ id = url[/^https?:\/\/.*\/status\/(\d+)/,1]
+ tweet_by_id m, id
end
end
@@ -2,14 +2,38 @@ class URLAnnounce < XOmBot::Plugin
listen_to :channel
def listen(m)
- ignore_plugin = XOmBot.plugins["XOmBot::Plugins::Ignore"]
+ ignore_plugin = XOmBot.plugins["Ignore"]
return if ignore_plugin and ignore_plugin.ignored.include? m.user.nick
m.message.scan /https?:\/\/[\S]+/ do |url|
+ dispatch m, url
+ end
+ end
+
+ def dispatch(m, url)
+ domain = url[/https?:\/\/(?:[^.]+\.)?([^.]+\.[^\/]+)(?:\/|$)/, 1]
+
+ @callback = @callback || {}
+ if @callback[domain]
+ @callback[domain].call m, url
+ else
+ default_announce m, url
+ end
+ end
+
+ def match_domain(domain, &block)
+ @callback = @callback || {}
+ @callback[domain] = block
+ end
+
+ def default_announce(m, url)
+ begin
page = Mechanize.new.get url
+
if page.is_a? Mechanize::Page
m.reply "Title: #{page.title.gsub(/\t|\r|\n/, " ").strip}"
end
+ rescue
end
end
end

0 comments on commit e36c8d7

Please sign in to comment.