2 # Web services plugin. Google and more.
12 @brief_help = 'Performs various web services.'
18 def cmd_title(irc, line)
19 if !line or line.empty?
20 irc.reply 'USAGE: title <http-url>'
21 elsif @requests >= @max_requests
22 irc.reply 'My limit of outstanding web queries has been reached. Try again in a moment.'
24 line = "http://#{line}" unless line =~ /^http:\/\//
25 if !(uri = URI.parse(line)) or !uri.kind_of?(URI::HTTP)
26 irc.reply 'Error parsing URL. For now, only HTTP URLs are accepted.'
31 Net::HTTP.start(uri.host, uri.port) do |http|
34 res = http.get(path.empty? ? '/' : path) do |s|
36 if buffer =~ /<title>(.+?)<\/title>/
37 irc.reply $1.decode_entities
41 irc.reply 'No title found in document.'
44 irc.reply 'Error connecting to host.'
53 def cmd_google(irc, line)
56 if !line or line.empty?
57 irc.reply 'USAGE: google <search string>'
60 if @requests >= @max_requests
61 irc.reply 'My limit of outstanding web queries has been reached. Try again in a moment.'
69 Net::HTTP.start('www.google.com') do |http|
70 search = line.gsub(/[^a-zA-Z0-9_\.\-]/) { |s| sprintf('%%%02x', s[0]) }
71 re = http.get("/search?ie=utf8&oe=utf8&q=#{search}",
72 { 'User-Agent' => 'CyBrowser' })
74 if re.body =~ /<a href="([^"]+)" class=l>(.+?)<\/a>/
75 link = $1.decode_entities
76 desc = $2.gsub('<b>', "\x02").gsub('</b>', "\x0f").decode_entities
77 irc.reply "#{link} (#{desc})"
78 elsif re.body =~ /did not match any documents/
79 irc.reply 'Nothing found.'
81 irc.reply "Error parsing Google output."
84 irc.reply "Google returned an error: #{re.code} #{re.message}"
93 help :google, 'Searches the web with Google, returning the first result.'
94 alias_method :cmd_lucky, :cmd_google
95 help :lucky, "Alias for 'google'. Type 'google?' for more information."