This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
forked from eebrah/zipper-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_info.rb
93 lines (83 loc) · 2.85 KB
/
link_info.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
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
# -*- coding: utf-8 -*-
#
# = Cinch Link Info plugin
# Inspects any links that are posted into a channel Cinch
# is currently in and prints out the value of the title
# and description meta tags, if any.
#
# == Dependencies
# * Gem: nokogiri
#
# == Configuration
# Add the following to your bot’s configure.do stanza:
#
# config.plugins.options[Cinch::LinkInfo] = {
# :blacklist => [/\.xz$/],
# :no_description => false,
# }
#
# [blacklist]
# If a URL matches any of the regular expressions defined
# in this array, it will not be inspected. This plugin
# alraedy ignores URLs ending in common image file
# extensions, so you don’t have to specify .png, .jpeg,
# etc.
# [no_description]
# Set this to true if you want Cinch to not print the
# content of the Meta description tag.
#
# == Author
# Marvin Gülker (Quintus)
#
# == License
# A link info plugin for Cinch.
# Copyright © 2012 Marvin Gülker
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require "open-uri"
require "nokogiri"
# Plugin for inspecting links pasted into channels.
class LinkInfo
include Cinch::Plugin
# Default list of URL regexps to ignore.
DEFAULT_BLACKLIST = [/\.png$/i, /\.jpe?g$/i, /\.bmp$/i, /\.gif$/i, /\.pdf$/i].freeze
set :help, <<-HELP
http[s]://...
I’ll fire a GET request at any link I encounter, parse the HTML
meta tags, and paste the result back into the channel.
HELP
match %r{(https?://.*?)(?:\s|$|,|\.\s|\.$)}, :use_prefix => false
def execute(msg, url)
blacklist = DEFAULT_BLACKLIST.dup
blacklist.concat(config[:blacklist]) if config[:blacklist]
return if blacklist.any?{|entry| url =~ entry}
debug "URL matched: #{url}"
html = Nokogiri::HTML(open(url))
if node = html.at_xpath("html/head/title")
msg.reply("Title: #{node.text}")
end
if !config[:no_description]
node = html.at_xpath('html/head/meta[@name="description"]') || html.at_xpath('html/head/meta[@name="Description"]')
if node
if node[:content].chars.count > 255
msg.reply("Description: #{node[:content].lines.first(3).join("").gsub("\n", " ")[0..255] + "..."}")
else
msg.reply("Description: #{node[:content]}")
end
end
end
rescue => e
error "#{e.class.name}: #{e.message}"
end
end