]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/google.rb
initial import of rbot
[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 = Net::HTTP.new("www.google.com", 80, proxy_host, proxy_port)
31
32     http.start {|http|
33       begin
34         resp , = http.get(query)
35         if resp.code == "302"
36           result = resp['location']
37         end
38       rescue => e
39         p e
40         if e.response && e.response['location']
41           result = e.response['location']
42         else
43           result = "error!"
44         end
45       end
46     }
47     m.reply "#{m.params}: #{result}"
48   end
49 end
50 plugin = GooglePlugin.new
51 plugin.register("search")