/
reupload.rb
56 lines (47 loc) · 1.7 KB
/
reupload.rb
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
# failirc, a fail IRC library.
#
# Copyleft meh. [http://meh.doesntexist.org | meh@paranoici.org]
#
# This file is part of failirc.
#
# failirc is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# failirc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with failirc. If not, see <http://www.gnu.org/licenses/>.
require 'net/http'
require 'uri'
version '0.0.1'
def reupload (url, time=5, service=:imageshack)
result = url
begin
case service
when :imageshack
result = timeout time do
response = Net::HTTP.get_response(URI.parse("http://imageshack.us/transload.php?url=#{URI.escape(url)}"))
Net::HTTP.get(URI.parse(response['location'])).match(%r{<textarea.*?>(.*?img.*?\..*?)</textarea>})[1] rescue nil
end
end
rescue Timeout::Error
rescue Exception => e
IRC.debug e
result = url
end
return result || url
end
on :message, priorty: -102 do |chain=:input, from, to, message|
return unless chain == :input
message.scan(%r{https?://\S+}).uniq.each {|uri|
options[:matches].each {|name, regex|
next unless uri.match(/#{regex}/)
message.gsub!(/#{Regexp.escape(uri)}/, "#{reupload(uri, (options[:timeout] || 5).to_f, (options[:service] || 'imageshack').to_sym)} (#{name})")
}
}
end