4 # :title: tumblr interface
6 # Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2009 Giuseppe Bilotta
10 # Submit URLs to channel-specific tumblr accounts
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
17 require 'rexml/document'
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}"
30 def help(plugin, topic="")
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"
35 "tumblr deconfigure [<channel>]: forget credentials for channel <channel> (default: current)."
37 "post links, photos and videos to a channel-specific tumblr. topics: configure, deconfigure"
41 def event_url_added(url, options={})
42 return unless options.key? :channel
43 chan = options[:channel]
44 return unless @registry.key? chan
46 account = @registry[chan]
48 line = options[:ircline]
49 line = nil if line and line.empty?
50 if line and nick = options[:nick]
51 line = "<#{nick}> #{line}"
55 type = options[:htmlinfo][:headers]['content-type'].first rescue nil
59 data << CAPTION if line
61 if url.match(%r{^http://(\w+\.)?youtube\.com/watch.*})
63 data << CAPTION if line
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}"
75 def configuration(m, params={})
76 channel = params[:channel] || m.channel
78 m.reply _("Please specify a channel")
81 if not @registry.key? channel
82 m.reply _("No tumblr credentials set for %{chan}" % { :chan => channel })
86 account = @registry[channel]
88 account[:pwd] = _("<hidden>") if m.public?
89 account[:chan] = channel
91 m.reply _("Links on %{chan} will go to %{group} using account %{email} and password %{pwd}" % account)
94 def deconfigure(m, params={})
95 channel = params[:channel] || m.channel
97 m.reply _("Please specify a channel")
100 if not @registry.key? channel
101 m.reply _("No tumblr credentials set for %{chan}" % { :chan => channel })
105 @registry.delete channel
107 m.reply _("Links on %{chan} will not be posted to tumblr anymore" % {:chan => channel})
110 def configure(m, params={})
111 channel = params[:channel] || m.channel
113 m.reply _("Please specify a channel")
116 if @registry.key? channel
117 m.reply _("%{chan} already has credentials configured" % { :chan => channel })
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)
129 return configuration(m, params)
134 plugin = TumblrPlugin.new
136 plugin.default_auth('*', false)
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-]+/}