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 REG = Regexp.new('<div class="content">\s*<p>(.*?)</p>\s+</div>', Regexp::MULTILINE)
14 REGPOST = Regexp.new('title>(.*?) \| Group Hug')
17 @confessions = Array.new
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.")
24 def post_confession(m, params)
25 c = params[:confession]
28 m.reply _("Confession must be at least 10 words. You need %{m} more.") % {:m => diff}
31 uri = "http://beta.grouphug.us/confess"
32 form_id = "form_id=confession_node_form"
36 msg = [form_id,body,changed,op].join("&")
38 response = bot.httputil.post(uri, msg)
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}
44 m.reply _("I couldn't share your confession.")
49 def confess(m, params)
50 opts = { :cache => false }
53 # Fetch a specific question - separate from cache
55 path = "confessions/#{params[:num]}"
57 data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
60 confession = res[0][0].ircify_html
61 confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num]
63 else # Cache random confessions
64 if @confessions.empty?
65 data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
68 @confessions << quote[0].ircify_html
71 confession = @confessions.pop
75 m.reply "failed to connect to grouphug.us"
81 plugin = GrouphugPlugin.new
83 plugin.default_auth('create', false)
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'