]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/grouphug.rb
fa35803ce19b296c29a9004343304b9e9fd68a78
[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   START = '<div id="main"'
14   REG  = Regexp.new('<div class="content">\s*<p>(.*?)</p>\s+</div>', Regexp::MULTILINE)
15   REGPOST = Regexp.new('title>(.*?) \| Group Hug')
16   def initialize
17     super
18     @confessions = Array.new
19   end
20
21   def help( plugin, topic="" )
22     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.")
23   end
24
25   def post_confession(m, params)
26     c = params[:confession]
27     if c.length < 10
28       diff = 10 - c.length
29       m.reply _("Confession must be at least 10 words. You need %{m} more.") % {:m => diff}
30       return
31     end
32     uri = "http://beta.grouphug.us/confess"
33     form_id = "form_id=confession_node_form"
34     op = "op=Submit"
35     changed = "changed="
36     body = "body=#{c}"
37     msg = [form_id,body,changed,op].join("&")
38
39     response = bot.httputil.post(uri, msg)
40     debug response.body
41     if response.class == Net::HTTPOK
42       num = response.body.scan(REGPOST)
43       m.reply _("Confession posted: http://beta.grouphug.us/confessions/%{n}") % {:n => num}
44     else
45       m.reply _("I couldn't share your confession.")
46     end
47   end
48
49   def get_confessions(html)
50     return [] unless html
51     start = html.index(START)
52     res = html[start, html.length - start].scan(REG)
53     return [] unless res
54     return res.map { |quote|
55       quote[0].ircify_html
56     }
57   end
58
59   def confess(m, params)
60     opts = { :cache => false }
61     path = "random"
62     begin
63       # Fetch a specific question - separate from cache
64       if params[:num]
65         path = "confessions/#{params[:num]}"
66         opts.delete(:cache)
67         data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
68         confessions = get_confessions(data)
69         if confessions.length > 1
70           warn "more than one confession found!"
71           warn confessions
72         end
73         confessions << "no confession ##{params[:num]} found" if confessions.empty?
74         m.reply confessions.first
75       else # Cache random confessions
76         if @confessions.empty?
77           data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
78           @confessions.replace get_confessions(data)
79         end
80         @confessions << "no confessions found!" if @confessions.empty?
81         m.reply @confessions.pop
82       end
83     rescue Exception => e
84       error e
85       m.reply "failed to connect to grouphug.us"
86     end
87   end
88 end
89
90
91 plugin = GrouphugPlugin.new
92
93 plugin.default_auth('create', false)
94
95 plugin.map "grouphug [:num]",
96   :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
97 plugin.map "confess [:num]",
98   :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
99 plugin.map "confess *confession", :thread => true, :action => :post_confession, :auth_path => 'create'
100