]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/oxford.rb
fix: in-memory registry persist correctly
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / oxford.rb
1 # encoding: UTF-8
2 #-- vim:sw=2:et
3 #++
4 #
5 # :title: Oxford Dictionary lookup plugin for rbot
6 #
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
9 # License:: GPL v2
10 #
11
12 class OxfordPlugin < Plugin
13   Config.register Config::IntegerValue.new('oxford.hits',
14     :default => 3,
15     :desc => "Number of hits to return from a dictionary lookup")
16   Config.register Config::IntegerValue.new('oxford.first_par',
17     :default => 0,
18     :desc => "When set to n > 0, the bot will return the first paragraph from the first n dictionary hits")
19
20   def initialize
21     super
22     @oxurl = "http://www.oxforddictionaries.com/definition/english/%s"
23   end
24
25   def help(plugin, topic="")
26     'oxford <word>: check for <word> on the oxford english dictionary.'
27   end
28
29   def oxford(m, params)
30     justcheck = params[:justcheck]
31
32     word = params[:word].join
33     [word, word + "_1"].each { |check|
34       url = @oxurl % CGI.escape(check)
35       if params[:british]
36         url << "?view=uk"
37       end
38       h = @bot.httputil.get(url, :max_redir => 5)
39       if h
40         defs = h.split("<span class=\"definition\">")
41         defs = defs[1..-1].map {|d| d.split("</span>")[0]}
42         if defs.size == 0
43           return false if justcheck
44           m.reply "#{word} not found"
45           return false
46         end
47         m.reply("#{word}: #{url}") unless justcheck
48         defn = defs[0]
49         m.reply("#{Bold}%s#{Bold}: %s" % [word, defn.ircify_html(:nbsp => :space)], :overlong => :truncate)
50         return true
51       end
52     }
53   end
54
55   def is_british?(word)
56     return oxford(nil, :word => word, :justcheck => true, :british => true)
57   end
58 end
59
60 plugin = OxfordPlugin.new
61 plugin.map 'oxford *word', :action => 'oxford', :threaded => true
62