4 # :title: Grouphug Plugin for rbot
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
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')
18 @confessions = Array.new
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.")
25 def post_confession(m, params)
26 c = params[:confession]
29 m.reply _("Confession must be at least 10 words. You need %{m} more.") % {:m => diff}
32 uri = "http://beta.grouphug.us/confess"
33 form_id = "form_id=confession_node_form"
37 msg = [form_id,body,changed,op].join("&")
39 response = bot.httputil.post(uri, msg)
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}
45 m.reply _("I couldn't share your confession.")
49 def get_confessions(html)
51 start = html.index(START)
52 res = html[start, html.length - start].scan(REG)
54 return res.map { |quote|
59 def confess(m, params)
60 opts = { :cache => false }
63 # Fetch a specific question - separate from cache
65 path = "confessions/#{params[:num]}"
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!"
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)
80 @confessions << "no confessions found!" if @confessions.empty?
81 m.reply @confessions.pop
85 m.reply "failed to connect to grouphug.us"
91 plugin = GrouphugPlugin.new
93 plugin.default_auth('create', false)
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'