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.")
50 def confess(m, params)
51 opts = { :cache => false }
54 # Fetch a specific question - separate from cache
56 path = "confessions/#{params[:num]}"
58 data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
59 start = data.index(START)
60 res = data[start, data.length - start].scan(REG)
61 confession = res.first[0].ircify_html
62 confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num]
64 else # Cache random confessions
65 if @confessions.empty?
66 data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
67 start = data.index(START)
68 res = data[start, data.length - start].scan(REG)
70 @confessions << quote[0].ircify_html
73 confession = @confessions.pop
78 m.reply "failed to connect to grouphug.us"
84 plugin = GrouphugPlugin.new
86 plugin.default_auth('create', false)
88 plugin.map "grouphug [:num]",
89 :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
90 plugin.map "confess [:num]",
91 :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
92 plugin.map "confess *confession", :thread => true, :action => :post_confession, :auth_path => 'create'