]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/tumblr.rb
hangman: ensure 'hangman define' always answers
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / tumblr.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: tumblr interface
5 #
6 # Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2009 Giuseppe Bilotta
8 # License:: GPLv2
9 #
10 # Submit URLs to channel-specific tumblr accounts
11 #
12 # TODO reblog tumblr URLs
13 # TODO support video better (e.g. Vimeo or anything else with embed)
14 # TODO support image better (e.g. pages with a single big image)
15 # TODO customize caption/description format
16
17 require 'rexml/document'
18 require 'cgi'
19
20 class TumblrPlugin < Plugin
21   RBOT = CGI.escape("rbot #{$version.split.first}")
22   WRITE_URL = "http://www.tumblr.com/api/write"
23   LOGIN = "email=%{email}&password=%{pwd}&group=%{group}&format=markdown&generator=" + RBOT
24   PHOTO = "&type=photo&source=%{src}&click-through-url=%{src}"
25   VIDEO = "&type=video&embed=%{src}"
26   CAPTION = "&caption=%{desc}"
27   LINK = "&type=link&url=%{src}"
28   DESC = "&name=%{desc}"
29
30   def help(plugin, topic="")
31     case topic
32     when "configure"
33       "tumblr configure [<channel>]: show credentials used for channel <channel> (default: current).    tumblr configure [<channel>] <email> <password> [<group>] => post links from channel <channel> (default: current) to group <group> (default: name of channel) using the given tumblr credentials"
34     when "deconfigure"
35       "tumblr deconfigure [<channel>]: forget credentials for channel <channel> (default: current)."
36     else
37       "post links, photos and videos to a channel-specific tumblr. topics: configure, deconfigure"
38     end
39   end
40
41   def event_url_added(url, options={})
42     return unless options.key? :channel
43     chan = options[:channel]
44     return unless @registry.key? chan
45
46     account = @registry[chan]
47
48     line = options[:ircline]
49     line = nil if line and line.empty?
50     if line and nick = options[:nick]
51       line = "<#{nick}> #{line}"
52     end
53
54     req = LOGIN % account
55     type = options[:htmlinfo][:headers]['content-type'].first rescue nil
56     case type
57     when /^image\/.*/
58       data = PHOTO
59       data << CAPTION if line
60     else
61       if url.match(%r{^http://(\w+\.)?youtube\.com/watch.*})
62         data = VIDEO
63         data << CAPTION if line
64       else
65         data = LINK
66         data << DESC if line
67       end
68     end
69     req << (data % { :src => CGI.escape(url), :desc => CGI.escape(line) })
70     debug "posting #{req.inspect}"
71     resp  = @bot.httputil.post(WRITE_URL, req)
72     debug "tumblr response: #{resp.inspect}"
73   end
74
75   def configuration(m, params={})
76     channel = params[:channel] || m.channel
77     if not channel
78       m.reply _("Please specify a channel")
79       return
80     end
81     if not @registry.key? channel
82       m.reply _("No tumblr credentials set for %{chan}" % { :chan => channel })
83       return false
84     end
85
86     account = @registry[channel]
87
88     account[:pwd] = _("<hidden>") if m.public?
89     account[:chan] = channel
90
91     m.reply _("Links on %{chan} will go to %{group} using account %{email} and password %{pwd}" % account)
92   end
93
94   def deconfigure(m, params={})
95     channel = params[:channel] || m.channel
96     if not channel
97       m.reply _("Please specify a channel")
98       return
99     end
100     if not @registry.key? channel
101       m.reply _("No tumblr credentials set for %{chan}" % { :chan => channel })
102       return false
103     end
104
105     @registry.delete channel
106
107     m.reply _("Links on %{chan} will not be posted to tumblr anymore" % {:chan => channel})
108   end
109
110   def configure(m, params={})
111     channel = params[:channel] || m.channel
112     if not channel
113       m.reply _("Please specify a channel")
114       return
115     end
116     if @registry.key? channel
117       m.reply _("%{chan} already has credentials configured" % { :chan => channel })
118     else
119       group = params[:group] || Channel.npname(channel)
120       group << ".tumblr.com" unless group.match(/\.tumblr\.com/)
121       @registry[channel] = {
122         :email => CGI.escape(params[:email]),
123         :pwd => CGI.escape(params[:pwd]),
124         :group => CGI.escape(group)
125       }
126
127     end
128
129     return configuration(m, params)
130   end
131
132 end
133
134 plugin = TumblrPlugin.new
135
136 plugin.default_auth('*', false)
137
138 plugin.map 'tumblr configure [:channel]', :action => :configuration
139 plugin.map 'tumblr deconfigure [:channel]', :action => :deconfigure
140 plugin.map 'tumblr configure [:channel] :email :pwd [:group]',
141   :action => :configure,
142   :requirements => {:channel => Regexp::Irc::GEN_CHAN, :email => /.+@.+/, :group => /[A-Za-z-]+/}
143