]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/twitter.rb
twitter plugin: whitespace cleanup
[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
30     @header = {
31       'X-Twitter-Client' => 'rbot twitter plugin'
32     }
33   end
34
35   # return a help string when the bot is asked for help on this plugin
36   def help(plugin, topic="")
37     return "twitter status [status] => updates your status on twitter | twitter identify [username] [password] => ties your nick to your twitter username and password"
38   end
39
40   # update the status on twitter
41   def get_status(m, params)
42
43     nick = params[:nick] || @registry[m.sourcenick + "_username"]
44
45     if not nick
46       m.reply "you should specify the username of the twitter touse, or identify using 'twitter identify [username] [password]'"
47       return false
48     end
49
50
51     # TODO configurable count
52     uri = "http://twitter.com/statuses/user_timeline/#{URI.escape(nick)}.xml?count=3"
53
54     response = @bot.httputil.get(uri, :headers => @header, :cache => false)
55     debug response
56
57     texts = []
58
59     if response
60       begin
61         rex = REXML::Document.new(response)
62         rex.root.elements.each("status") { |st|
63           month, day, hour, min, sec, year = st.elements['created_at'].text.match(/\w+ (\w+) (\d+) (\d+):(\d+):(\d+) \S+ (\d+)/)[1..6]
64           debug [year, month, day, hour, min, sec].inspect
65           time = Time.local(year.to_i, month, day.to_i, hour.to_i, min.to_i, sec.to_i)
66           now = Time.now
67           delta = now - time
68           msg = st.elements['text'].to_s + " (#{Utils.secs_to_string(delta.to_i)} ago via #{st.elements['source'].to_s})"
69           texts << Utils.decode_html_entities(msg).ircify_html
70         }
71       rescue
72         error $!
73         m.reply "could not parse status for #{nick}"
74         return false
75       end
76       m.reply texts.reverse.join("\n")
77       return true
78     else
79       m.reply "could not get status for #{nick}"
80       return false
81     end
82   end
83
84   # update the status on twitter
85   def update_status(m, params)
86
87
88     unless @registry.has_key?(m.sourcenick + "_password") && @registry.has_key?(m.sourcenick + "_username")
89       m.reply "you must identify using 'twitter identify [username] [password]'"
90       return false
91     end
92
93     uri = "http://#{URI.escape(@registry[m.sourcenick + "_username"])}:#{URI.escape(@registry[m.sourcenick + "_password"])}@twitter.com/statuses/update.xml"
94
95     response = @bot.httputil.post(uri, "status=#{CGI.escape(params[:status].to_s)}", :headers => @header)
96     debug response
97
98     if response.class == Net::HTTPOK
99       m.reply "status updated"
100     else
101       m.reply "could not update status"
102     end
103   end
104
105   # ties a nickname to a twitter username and password
106   def identify(m, params)
107     @registry[m.sourcenick + "_username"] = params[:username].to_s
108     @registry[m.sourcenick + "_password"] = params[:password].to_s
109     m.reply "you're all setup!"
110   end
111 end
112
113 # create an instance of our plugin class and register for the "length" command
114 plugin = TwitterPlugin.new
115 plugin.map 'twitter identify :username :password', :action => "identify", :public => false
116 plugin.map 'twitter update *status', :action => "update_status", :threaded => true
117 plugin.map 'twitter status [:nick]', :action => "get_status", :threaded => true
118