]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/grouphug.rb
hangman plugin: rudimentary stats tracking along with some other enhancements
[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   REGPOST = Regexp.new('title>(.*?) \| Group Hug')
15   def initialize
16     super
17     @confessions = Array.new
18   end
19
20   def help( plugin, topic="" )
21     return _("Grouphug plugin. Confess! Usage: 'confess' for random confession, 'confess <number>' for specific one, 'confess <confession>' to share your own confession. Confessions must be at least 10 words.")
22   end
23
24   def post_confession(m, params)
25     c = params[:confession]
26     if c.length < 10
27       diff = 10 - c.length
28       m.reply _("Confession must be at least 10 words. You need %{m} more.") % {:m => diff}
29       return
30     end
31     uri = "http://beta.grouphug.us/confess"
32     form_id = "form_id=confession_node_form"
33     op = "op=Submit"
34     changed = "changed="
35     body = "body=#{c}"
36     msg = [form_id,body,changed,op].join("&")
37
38     response = bot.httputil.post(uri, msg)
39     debug response.body
40     if response.class == Net::HTTPOK
41       num = response.body.scan(REGPOST)
42       m.reply _("Confession posted: http://beta.grouphug.us/confessions/%{n}") % {:n => num}
43     else
44       m.reply _("I couldn't share your confession.")
45     end
46   end
47
48
49   def confess(m, params)
50     opts = { :cache => false }
51     path = "random"
52     begin
53       # Fetch a specific question - separate from cache
54       if params[:num]
55         path = "confessions/#{params[:num]}"
56         opts.delete(:cache)
57         data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
58
59         res = data.scan(REG)
60         confession = res[2][0].ircify_html # use res[2] to skip the new sidebar "Group Hug is run by one person..." and other text.
61         confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num]
62         m.reply confession
63       else # Cache random confessions
64         if @confessions.empty?
65           data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
66           res = data.scan(REG)
67           res.each do |quote|
68             @confessions << quote[0].ircify_html
69           end
70         end
71         confession = @confessions.pop
72         m.reply confession
73       end
74     rescue
75       m.reply "failed to connect to grouphug.us"
76     end
77   end
78 end
79
80
81 plugin = GrouphugPlugin.new
82
83 plugin.default_auth('create', false)
84
85 plugin.map "grouphug [:num]",
86   :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
87 plugin.map "confess [:num]",
88   :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
89 plugin.map "confess *confession", :thread => true, :action => :post_confession, :auth_path => 'create'
90