]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/twitter.rb
f04ca6735f5cd6f602a5c302a427a048f9ccb112
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / twitter.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Twitter Status Update for rbot
5 #
6 # Author:: Carter Parks (carterparks) <carter@carterparks.com>
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 #
9 # Copyright:: (C) 2007 Carter Parks
10 #
11 # Users can setup their twitter username and password and then begin updating
12 # twitter whenever
13
14 require 'rexml/rexml'
15 require 'cgi'
16
17 class TwitterPlugin < Plugin
18   def initialize
19     super
20     
21     class << @registry
22       def store(val)
23         val
24       end
25       def restore(val)
26         val
27       end
28     end
29   end
30   
31   # return a help string when the bot is asked for help on this plugin
32   def help(plugin, topic="")
33     return "twitter status [status] => updates your status on twitter | twitter identify [username] [password] => ties your nick to your twitter username and password"
34   end
35   
36   # update the status on twitter
37   def get_status(m, params)
38   
39     nick = params[:nick] || @registry[m.sourcenick + "_username"]
40
41     if not nick
42       m.reply "you should specify the username of the twitter touse, or identify using 'twitter identify [username] [password]'"
43       return false
44     end
45       
46     # TODO configurable count
47     uri = "http://twitter.com/statuses/user_timeline/#{URI.escape(nick)}.xml?count=3"
48     
49     response = @bot.httputil.get(uri, :cache => false)
50     debug response
51
52     texts = []
53     
54     if response
55       begin
56         rex = REXML::Document.new(response)
57         rex.root.elements.each("status") { |st|
58           month, day, hour, min, sec, year = st.elements['created_at'].text.match(/\w+ (\w+) (\d+) (\d+):(\d+):(\d+) \S+ (\d+)/)[1..6]
59           debug [year, month, day, hour, min, sec].inspect
60           time = Time.local(year.to_i, month, day.to_i, hour.to_i, min.to_i, sec.to_i)
61           now = Time.now
62           delta = now - time
63           msg = st.elements['text'].to_s + " (#{Utils.secs_to_string(delta.to_i)} ago via #{st.elements['source'].to_s})"
64           texts << Utils.decode_html_entities(msg).ircify_html
65         }
66       rescue
67         error $!
68         m.reply "could not parse status for #{nick}"
69         return false
70       end
71       m.reply texts.reverse.join("\n")
72       return true
73     else
74       m.reply "could not get status for #{nick}"
75       return false
76     end
77   end
78   
79   # update the status on twitter
80   def update_status(m, params)
81   
82
83     unless @registry.has_key?(m.sourcenick + "_password") && @registry.has_key?(m.sourcenick + "_username")
84       m.reply "you must identify using 'twitter identify [username] [password]'"
85       return false
86     end
87       
88     uri = "http://#{URI.escape(@registry[m.sourcenick + "_username"])}:#{URI.escape(@registry[m.sourcenick + "_password"])}@twitter.com/statuses/update.xml"
89     
90     response = @bot.httputil.post(uri, "status=#{CGI.escape(params[:status].to_s)}")
91     debug response
92     
93     if response.class == Net::HTTPOK
94       m.reply "status updated"
95     else
96       m.reply "could not update status"
97     end
98   end
99   
100   # ties a nickname to a twitter username and password
101   def identify(m, params)
102     @registry[m.sourcenick + "_username"] = params[:username].to_s
103     @registry[m.sourcenick + "_password"] = params[:password].to_s
104     m.reply "you're all setup!"
105   end
106 end
107
108 # create an instance of our plugin class and register for the "length" command
109 plugin = TwitterPlugin.new
110 plugin.map 'twitter identify :username :password', :action => "identify", :public => false
111 plugin.map 'twitter update *status', :action => "update_status", :threaded => true
112 plugin.map 'twitter status [:nick]', :action => "get_status", :threaded => true
113