3 Url = Struct.new("Url", :channel, :nick, :time, :url)
4 TITLE_RE = /<\s*title\s*>(.+)<\s*\/title\s*>/im
6 class UrlPlugin < Plugin
7 BotConfig.register BotConfigIntegerValue.new('url.max_urls',
8 :default => 100, :validate => Proc.new{|v| v > 0},
9 :desc => "Maximum number of urls to store. New urls replace oldest ones.")
10 BotConfig.register BotConfigBooleanValue.new('url.say_titles',
12 :desc => "Get the title of any links pasted to the channel and display it (Also, tells if the link is broken)")
16 @registry.set_default(Array.new)
19 def help(plugin, topic="")
20 "urls [<max>=4] => list <max> last urls mentioned in current channel, urls search [<max>=4] <regexp> => search for matching urls. In a private message, you must specify the channel to query, eg. urls <channel> [max], urls search <channel> [max] <regexp>"
23 def get_title_for_url(url)
25 pagedata = open(url).read
26 return unless TITLE_RE.match(pagedata)
27 title = $1.strip.gsub(/\s*\n\s*/, " ")
28 title = title[0..255] if title.length > 255
31 rescue SocketError => e
32 "Title: ^- Error connecting to site (#{e.message})"
33 rescue OpenURI::HTTPError => e
34 "Title: ^- Error getting page (#{e.message})"
36 "Title: ^- Error: #{e.inspect}"
41 return unless m.kind_of?(PrivMessage)
43 # TODO support multiple urls in one line
44 if m.message =~ /(f|ht)tps?:\/\//
45 if m.message =~ /((f|ht)tps?:\/\/.*?)(?:\s+|$)/
47 list = @registry[m.target]
49 if @bot.config['url.say_titles']
50 debug "Getting title for #{urlstr}..."
51 title = get_title_for_url urlstr
56 debug "Title not found!"
60 # check to see if this url is already listed
61 return if list.find {|u| u.url == urlstr }
63 url = Url.new(m.target, m.sourcenick, Time.new, urlstr)
64 debug "#{list.length} urls so far"
65 if list.length > @bot.config['url.max_urls']
68 debug "storing url #{url.url}"
70 debug "#{list.length} urls now"
71 @registry[m.target] = list
77 channel = params[:channel] ? params[:channel] : m.target
78 max = params[:limit].to_i
81 list = @registry[channel]
83 m.reply "no urls seen yet for channel #{channel}"
85 list[0..(max-1)].each do |url|
86 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
92 channel = params[:channel] ? params[:channel] : m.target
93 max = params[:limit].to_i
94 string = params[:string]
97 regex = Regexp.new(string)
98 list = @registry[channel].find_all {|url|
99 regex.match(url.url) || regex.match(url.nick)
102 m.reply "no matches for channel #{channel}"
104 list[0..(max-1)].each do |url|
105 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
110 plugin = UrlPlugin.new
111 plugin.map 'urls search :channel :limit :string', :action => 'search',
112 :defaults => {:limit => 4},
113 :requirements => {:limit => /^\d+$/},
115 plugin.map 'urls search :limit :string', :action => 'search',
116 :defaults => {:limit => 4},
117 :requirements => {:limit => /^\d+$/},
119 plugin.map 'urls :channel :limit', :defaults => {:limit => 4},
120 :requirements => {:limit => /^\d+$/},
122 plugin.map 'urls :limit', :defaults => {:limit => 4},
123 :requirements => {:limit => /^\d+$/},