]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/google.rb
Wed Jul 20 23:30:01 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / rbot / plugins / google.rb
1 require 'net/http'
2 require 'uri/common'
3
4 Net::HTTP.version_1_2
5
6 class GooglePlugin < Plugin
7   def help(plugin, topic="")
8     "search <string> => search google for <string>"
9   end
10   def privmsg(m)
11     unless(m.params && m.params.length > 0)
12       m.reply "incorrect usage: " + help(m.plugin)
13       return
14     end
15     searchfor = URI.escape m.params
16
17     query = "/search?q=#{searchfor}&btnI=I%27m%20feeling%20lucky"
18     result = "not found!"
19
20     proxy_host = nil
21     proxy_port = nil
22
23     if(ENV['http_proxy'])
24       if(ENV['http_proxy'] =~ /^http:\/\/(.+):(\d+)$/)
25         proxy_host = $1
26         proxy_port = $2
27       end
28     end
29
30     http = @bot.httputil.get_proxy(URI.parse("http://www.google.com"))
31
32     begin
33       http.start {|http|
34         resp = http.get(query)
35         if resp.code == "302"
36           result = resp['location']
37         end
38       }
39     rescue => e
40       p e
41       if e.response && e.response['location']
42         result = e.response['location']
43       else
44         result = "error!"
45       end
46     end
47     m.reply "#{m.params}: #{result}"
48   end
49 end
50 plugin = GooglePlugin.new
51 plugin.register("search")