1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#-- vim:sw=2:et
#++
#
# :title: tumblr interface
#
# Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
# Copyright:: (C) 2009 Giuseppe Bilotta
# License:: GPLv2
#
# Submit URLs to channel-specific tumblr accounts
#
# TODO support other video providers (maybe detect embed codes?)
# TODO support image better (e.g. pages with a single big image)
# TODO customize caption/description format
# TODO do not reblog own posts (maybe?)
require 'rexml/document'
require 'cgi'
class TumblrPlugin < Plugin
RBOT = CGI.escape("rbot #{$version.split.first}")
WRITE_URL = "http://www.tumblr.com/api/write"
REBLOG_URL = "http://www.tumblr.com/api/reblog"
READ_URL = "http://%{user}.tumblr.com/api/read?id=%{id}"
LOGIN = "email=%{email}&password=%{pwd}&group=%{group}&format=markdown&generator=" + RBOT
PHOTO = "&type=photo&source=%{src}&click-through-url=%{src}"
VIDEO = "&type=video&embed=%{src}"
CAPTION = "&caption=%{desc}"
LINK = "&type=link&url=%{src}"
NAME = "&name=%{name}"
DESC = "&description=%{desc}"
REBLOG = "&post-id=%{id}&reblog-key=%{reblog}"
COMMENT = "&comment=%{desc}"
TAGS = "&tags=%{tags}"
def help(plugin, topic="")
case topic
when "configure"
"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"
when "deconfigure"
"tumblr deconfigure [<channel>]: forget credentials for channel <channel> (default: current)."
else
"post links, photos and videos to a channel-specific tumblr. topics: configure, deconfigure"
end
end
def event_url_added(url, options={})
return unless options.key? :channel
chan = options[:channel]
return unless @registry.key? chan
account = @registry[chan]
line = options[:ircline]
line = nil if line and line.empty?
if line and nick = options[:nick]
line = "<#{nick}> #{line}"
end
html_line = line ? CGI.escapeHTML(line) : line
tags = line ? line.scan(/\[([^\]]+)\]/).flatten : []
req = LOGIN % account
ready = false
api_url = WRITE_URL
tumblr = options[:htmlinfo][:headers]['x-tumblr-user'].to_s rescue nil
if tumblr
id = url.match(/\/post\/(\d+)/)
if id
id = id[1]
read_url = READ_URL % { :user => tumblr, :id => id}
# TODO seems to return 503 a little too frequently
xml = @bot.httputil.get(read_url)
if xml
reblog = REXML::Document.new(xml).elements["//post"].attributes["reblog-key"] rescue nil
if reblog and not reblog.empty?
api_url = REBLOG_URL
data = REBLOG
data << COMMENT
html_line = CGI.escapeHTML("(via <a href=%{url}>%{tumblr}</a>" % {
:url => url, :tumblr => tmblr
}) unless html_line
req << (data % {
:id => id,
:reblog => reblog,
:desc => CGI.escape(html_line)
})
ready = true
end
end
end
end
if not ready
type = options[:htmlinfo][:headers]['content-type'].first rescue nil
case type
when /^image\/.*/
data = PHOTO
data << CAPTION if line
else
if url.match(%r{^http://(\w+\.)?(youtube\.com/watch.*|vimeo.com/\d+)})
data = VIDEO
data << CAPTION if line
else
data = LINK
data << NAME if line
end
end
data << TAGS unless tags.empty?
req << (data % {
:src => CGI.escape(url),
:desc => CGI.escape(html_line),
:tags => CGI.escape(tags.join(',')),
:name => CGI.escape(line)
})
end
debug "posting #{req.inspect}"
resp = @bot.httputil.post(api_url, req)
debug "tumblr response: #{resp.inspect}"
end
def configuration(m, params={})
channel = params[:channel] || m.channel
if not channel
m.reply _("Please specify a channel")
return
end
if not @registry.key? channel
m.reply _("No tumblr credentials set for %{chan}" % { :chan => channel })
return false
end
account = @registry[channel]
account[:pwd] = _("<hidden>") if m.public?
account[:chan] = channel
m.reply _("Links on %{chan} will go to %{group} using account %{email} and password %{pwd}" % account)
end
def deconfigure(m, params={})
channel = params[:channel] || m.channel
if not channel
m.reply _("Please specify a channel")
return
end
if not @registry.key? channel
m.reply _("No tumblr credentials set for %{chan}" % { :chan => channel })
return false
end
@registry.delete channel
m.reply _("Links on %{chan} will not be posted to tumblr anymore" % {:chan => channel})
end
def configure(m, params={})
channel = params[:channel] || m.channel
if not channel
m.reply _("Please specify a channel")
return
end
if @registry.key? channel
m.reply _("%{chan} already has credentials configured" % { :chan => channel })
else
group = params[:group] || Channel.npname(channel)
group << ".tumblr.com" unless group.match(/\.tumblr\.com/)
@registry[channel] = {
:email => CGI.escape(params[:email]),
:pwd => CGI.escape(params[:pwd]),
:group => CGI.escape(group)
}
end
return configuration(m, params)
end
end
plugin = TumblrPlugin.new
plugin.default_auth('*', false)
plugin.map 'tumblr configure [:channel]', :action => :configuration
plugin.map 'tumblr deconfigure [:channel]', :action => :deconfigure
plugin.map 'tumblr configure [:channel] :email :pwd [:group]',
:action => :configure,
:requirements => {:channel => Regexp::Irc::GEN_CHAN, :email => /\S+@\S+/, :group => /[A-Za-z\-.]+/}
|