]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/grouphug.rb
figlet plugin: font is now a config value
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / grouphug.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Grouphug Plugin for rbot
5 #
6 # Author:: Mark Kretschmann <markey@web.de>
7 # Author:: Casey Link <unnamedrambler@gmail.com>
8 # Copyright:: (C) 2005 Mark Kretschmann
9 # Copyright:: (C) 2008 Casey Link
10 # License:: GPL v2
11
12 class GrouphugPlugin < Plugin
13   REG  = Regexp.new('<div class="content">\s*<p>(.*?)</p>\s+</div>', Regexp::MULTILINE)
14   def initialize
15     super
16     @confessions = Array.new
17   end
18
19   def help( plugin, topic="" )
20     return "Grouphug plugin. Confess! Usage: 'confess' for random confession, 'confess <number>' for specific one."
21   end
22
23   def confess(m, params)
24     opts = { :cache => false }
25     path = "random"
26     begin
27       # Fetch a specific question - separate from cache
28       if params[:num]
29         path = "confessions/#{params[:num]}"
30         opts.delete(:cache)
31         data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
32
33         res = data.scan(REG)
34         confession = res[0][0].ircify_html
35         confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num]
36         m.reply confession
37       else # Cache random confessions
38         if @confessions.empty?
39           data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
40           res = data.scan(REG)
41           res.each do |quote|
42             @confessions << quote[0].ircify_html
43           end
44         end
45         confession = @confessions.pop
46         m.reply confession
47       end
48     rescue
49       m.reply "failed to connect to grouphug.us"
50     end
51   end
52 end
53
54
55 plugin = GrouphugPlugin.new
56
57 plugin.map "grouphug [:num]",
58   :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
59 plugin.map "confess [:num]",
60   :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
61