]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/twitter.rb
chucknorris: typo
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / twitter.rb
index 1c8f61dd0556b7064e3e21a2b1a9a26659bbd527..8725ac2cd21de783cbad33c30709de7849aa580b 100644 (file)
@@ -39,6 +39,29 @@ class TwitterPlugin < Plugin
       :default => 3, :validate => Proc.new { |v| v > 0 && v <= 10},
       :desc => "Maximum number of status updates shown by 'twitter friends status'")
 
+  def twitter_filter(s)
+    loc = Utils.check_location(s, Regexp.new('twitter\.com/#!/.*/status/\d+'))
+    return nil unless loc
+    id = loc.first.match(/\/status\/(\d+)/)[1]
+    xml = @bot.httputil.get('http://api.twitter.com/1/statuses/show.xml?id=' + id)
+    return nil unless xml
+    root = REXML::Document.new(xml).root
+    status = {
+      :date => (Time.parse(root.elements["created_at"].text) rescue "<unknown>"),
+      :id => (root.elements["id"].text rescue "<unknown>"),
+      :text => (root.elements["text"].text.ircify_html rescue "<error>"),
+      :source => (root.elements["source"].text rescue "<unknown>"),
+      :user => (root.elements["user/name"].text rescue "<unknown>"),
+      :user_nick => (root.elements["user/screen_name"] rescue "<unknown>")
+      # TODO other entries
+    }
+    status[:nicedate] = String === status[:date] ? status[:date] : Utils.timeago(status[:date])
+    return {
+      :title => "#{status[:user]}/#{status[:id]}",
+      :content => "#{status[:text]} (#{status[:nicedate]} via #{status[:source]})"
+    }
+  end
+
   def initialize
     super
 
@@ -52,6 +75,8 @@ class TwitterPlugin < Plugin
         val
       end
     end
+
+    @bot.register_filter(:twitter, :htmlinfo) { |s| twitter_filter(s) }
   end
 
   def report_oauth_missing(m, failed_action)
@@ -90,21 +115,17 @@ class TwitterPlugin < Plugin
       return false
     end
 
-    count = @bot.config['twitter.friends_status_count']
+    count = friends ? @bot.config['twitter.friends_status_count'] : @bot.config['twitter.status_count']
     user = URI.escape(nick)
+    # receive the public timeline per default (this works even without an access_token)
+    uri = "https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=#{user}&count=#{count}&include_rts=true"
     if @has_oauth and @registry.has_key?(m.sourcenick + "_access_token")
         if friends
           #no change to count variable
-          uri = "https://api.twitter.com/1/statuses/friends_timeline.xml?count=#{count}"
-          response = @access_token.get(uri).body
-        else
-          count = @bot.config['twitter.status_count']
-          uri = "https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=#{user}&count=#{count}"
-          response = @access_token.get(uri).body
+          uri = "https://api.twitter.com/1/statuses/friends_timeline.xml?count=#{count}&include_rts=true"
         end
+        response = @access_token.get(uri).body
     else
-       #unauthorized user, will try to get from public timeline the old way
-       uri = "http://twitter.com/statuses/user_timeline/#{user}.xml?count=#{count}"
        response = @bot.httputil.get(uri, :cache => false)
     end
     debug response
@@ -192,14 +213,19 @@ class TwitterPlugin < Plugin
       return false
     end
 
-    @consumer = OAuth::Consumer.new(key, secret, {   
+    @consumer = OAuth::Consumer.new(key, secret, {
       :site => "https://api.twitter.com",
       :request_token_path => "/oauth/request_token",
       :access_token_path => "/oauth/access_token",
       :authorize_path => "/oauth/authorize"
-      } )
-    @request_token = @consumer.get_request_token
-    @registry[m.sourcenick + "_request_token"] = YAML::dump(@request_token)        
+    } )
+    begin
+      @request_token = @consumer.get_request_token
+    rescue OAuth::Unauthorized
+      m.reply _("My authorization failed! Did you block me? Or is my Twitter Consumer Key/Secret pair incorrect?")
+      return false
+    end
+    @registry[m.sourcenick + "_request_token"] = YAML::dump(@request_token)
     m.reply "Go to this URL to get your authorization PIN, then use 'twitter pin <pin>' to finish authorization: " + @request_token.authorize_url
   end