]> 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 045b6172ff39a0fdbc6cb19b13e29e2a0793da1a..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,12 +75,18 @@ class TwitterPlugin < Plugin
         val
       end
     end
+
+    @bot.register_filter(:twitter, :htmlinfo) { |s| twitter_filter(s) }
   end
 
   def report_oauth_missing(m, failed_action)
     m.reply [failed_action, "I cannot authenticate to Twitter (OAuth not available)"].join(' because ')
   end
 
+  def report_key_missing(m, failed_action)
+    m.reply [failed_action, "no Twitter Consumer Key/Secret is defined"].join(' because ')
+  end
+
   def help(plugin, topic="")
     return "twitter status [nick] => show nick's (or your) status, use 'twitter friends status [nick]' to also show the friends' timeline | twitter update [status] => updates your status on twitter | twitter authorize => Generates an authorization URL which will give you a PIN to authorize the bot to use your twitter account. | twitter pin [pin] => Finishes bot authorization using the PIN provided by the URL from twitter authorize. | twitter deauthorize => Makes the bot forget your Twitter account. | twitter actions [on|off] => enable/disable twitting of actions (/me does ...)"
   end
@@ -86,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
@@ -167,8 +192,9 @@ class TwitterPlugin < Plugin
   end
 
   def authorize(m, params)
+    failed_action = "we can't complete the authorization process"
     unless @has_oauth
-      report_oauth_missing(m, "we can't complete the authorization process")
+      report_oauth_missing(m, failed_action)
       return false
     end
 
@@ -182,14 +208,24 @@ class TwitterPlugin < Plugin
 
     key = @bot.config['twitter.key']
     secret = @bot.config['twitter.secret']
-    @consumer = OAuth::Consumer.new(key, secret, {   
+    if key.empty? or secret.empty?
+      report_key_missing(m, failed_action)
+      return false
+    end
+
+    @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