]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/twitter.rb
94a1374edfa63412325e11cd9ea5f35b4a173395
[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 # Copyright:: (C) 2007 Giuseppe Bilotta
11 #
12 # Users can setup their twitter username and password and then begin updating
13 # twitter whenever
14
15 require 'rexml/rexml'
16 require 'cgi'
17
18 class TwitterPlugin < Plugin
19   Config.register Config::IntegerValue.new('twitter.status_count',
20     :default => 1, :validate => Proc.new { |v| v > 0 && v <= 10},
21     :desc => "Maximum number of status updates shown by 'twitter status'")
22   Config.register Config::IntegerValue.new('twitter.friends_status_count',
23     :default => 3, :validate => Proc.new { |v| v > 0 && v <= 10},
24     :desc => "Maximum number of status updates shown by 'twitter friends status'")
25
26   def initialize
27     super
28
29     class << @registry
30       def store(val)
31         val
32       end
33       def restore(val)
34         val
35       end
36     end
37
38     @header = {
39       'X-Twitter-Client' => 'rbot twitter plugin'
40     }
41   end
42
43   # return a help string when the bot is asked for help on this plugin
44   def help(plugin, topic="")
45     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 identify [username] [password] => ties your nick to your twitter username and password | twitter actions [on|off] => enable/disable twitting of actions (/me does ...)"
46   end
47
48   # update the status on twitter
49   def get_status(m, params)
50
51     nick = params[:nick] || @registry[m.sourcenick + "_username"]
52
53     if not nick
54       m.reply "you should specify the username of the twitter touse, or identify using 'twitter identify [username] [password]'"
55       return false
56     end
57
58     user = URI.escape(nick)
59
60     count = @bot.config['twitter.status_count']
61     unless params[:friends]
62       uri = "http://twitter.com/statuses/user_timeline/#{user}.xml?count=#{count}"
63     else
64       count = @bot.config['twitter.friends_status_count']
65       auth = ""
66       if m.private?
67         auth << URI.escape(@registry[m.sourcenick + "_username"])
68         auth << ":"
69         auth << URI.escape(@registry[m.sourcenick + "_password"])
70         auth << "@"
71       end
72       uri = "http://#{auth}twitter.com/statuses/friends_timeline/#{user}.xml"
73     end
74
75     response = @bot.httputil.get(uri, :headers => @header, :cache => false)
76     debug response
77
78     texts = []
79
80     if response
81       begin
82         rex = REXML::Document.new(response)
83         rex.root.elements.each("status") { |st|
84           # month, day, hour, min, sec, year = st.elements['created_at'].text.match(/\w+ (\w+) (\d+) (\d+):(\d+):(\d+) \S+ (\d+)/)[1..6]
85           # debug [year, month, day, hour, min, sec].inspect
86           # time = Time.local(year.to_i, month, day.to_i, hour.to_i, min.to_i, sec.to_i)
87           time = Time.parse(st.elements['created_at'].text)
88           now = Time.now
89           # Sometimes, time can be in the future; invert the relation in this case
90           delta = ((time > now) ? time - now : now - time)
91           msg = st.elements['text'].to_s + " (#{Utils.secs_to_string(delta.to_i)} ago via #{st.elements['source'].to_s})"
92           author = ""
93           if params[:friends]
94             author = Utils.decode_html_entities(st.elements['user'].elements['name'].text) + ": " rescue ""
95           end
96           texts << author+Utils.decode_html_entities(msg).ircify_html
97         }
98         if params[:friends]
99           # friends always return the latest 20 updates, so we clip the count
100           texts[count..-1]=nil
101         end
102       rescue
103         error $!
104         m.reply "could not parse status for #{nick}"
105         return false
106       end
107       m.reply texts.reverse.join("\n")
108       return true
109     else
110       m.reply "could not get status for #{nick}"
111       return false
112     end
113   end
114
115   # update the status on twitter
116   def update_status(m, params)
117
118
119     unless @registry.has_key?(m.sourcenick + "_password") && @registry.has_key?(m.sourcenick + "_username")
120       m.reply "you must identify using 'twitter identify [username] [password]'"
121       return false
122     end
123
124     user = URI.escape(@registry[m.sourcenick + "_username"])
125     pass = URI.escape(@registry[m.sourcenick + "_password"])
126     uri = "http://#{user}:#{pass}@twitter.com/statuses/update.xml"
127
128     msg = params[:status].to_s
129
130     if msg.length > 160
131       m.reply "your status message update is too long, please keep it under 140 characters if possible, 160 characters maximum"
132       return
133     end
134
135     if msg.length > 140
136       m.reply "your status message is longer than 140 characters, which is not optimal, but I'm going to update anyway"
137     end
138
139     source = "source=rbot"
140     msg = "status=#{CGI.escape(msg)}"
141     body = [source,msg].join("&")
142
143     response = @bot.httputil.post(uri, body, :headers => @header)
144     debug response
145
146     reply_method = params[:notify] ? :notify : :reply
147     if response.class == Net::HTTPOK
148       m.__send__(reply_method, "status updated")
149     else
150       m.__send__(reply_method, "could not update status")
151     end
152   end
153
154   # ties a nickname to a twitter username and password
155   def identify(m, params)
156     @registry[m.sourcenick + "_username"] = params[:username].to_s
157     @registry[m.sourcenick + "_password"] = params[:password].to_s
158     m.reply "you're all setup!"
159   end
160
161   # update on ACTION if the user has enabled the option
162   def ctcp_listen(m)
163     return unless m.action?
164     return unless @registry[m.sourcenick + "_actions"]
165     update_status(m, :status => m.message, :notify => true)
166   end
167
168   # show or toggle action twitting
169   def actions(m, params)
170     case params[:toggle]
171     when 'on'
172       @registry[m.sourcenick + "_actions"] = true
173       m.okay
174     when 'off'
175       @registry.delete(m.sourcenick + "_actions")
176       m.okay
177     else
178       if @registry[m.sourcenick + "_actions"]
179         m.reply _("actions will be twitted")
180       else
181         m.reply _("actions will not be twitted")
182       end
183     end
184   end
185 end
186
187 # create an instance of our plugin class and register for the "length" command
188 plugin = TwitterPlugin.new
189 plugin.map 'twitter identify :username :password', :action => "identify", :public => false
190 plugin.map 'twitter update *status', :action => "update_status", :threaded => true
191 plugin.map 'twitter status [:nick]', :action => "get_status", :threaded => true
192 plugin.map 'twitter actions [:toggle]', :action => "actions", :requirements => { :toggle => /^on|off$/ }
193 plugin.map 'twitter :friends [status] [:nick]', :action => "get_status", :requirements => { :friends => /^friends?$/ }, :threaded => true
194