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
19 @bot.register_filter(:grouphug, :htmlinfo) { |s| grouphug_filter(s) }
22 def help( plugin, topic="" )
23 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.")
26 def post_confession(m, params)
27 c = params[:confession]
30 m.reply _("Confession must be at least 10 words. You need %{m} more.") % {:m => diff}
33 uri = "http://beta.grouphug.us/confess"
34 form_id = "form_id=confession_node_form"
38 msg = [form_id,body,changed,op].join("&")
40 response = bot.httputil.post(uri, msg)
42 if response.class == Net::HTTPOK
43 num = response.body.scan(REGPOST)
44 m.reply _("Confession posted: http://beta.grouphug.us/confessions/%{n}") % {:n => num}
46 m.reply _("I couldn't share your confession.")
50 def get_confessions(html)
52 start = html.index(START)
53 res = html[start, html.length - start].scan(REG)
55 return res.map { |quote|
60 def confess(m, params)
61 opts = { :cache => false }
64 # Fetch a specific question - separate from cache
66 path = "confessions/#{params[:num]}"
68 data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
69 confessions = get_confessions(data)
70 if confessions.length > 1
71 warn "more than one confession found!"
74 confessions << "no confession ##{params[:num]} found" if confessions.empty?
75 m.reply confessions.first
76 else # Cache random confessions
77 if @confessions.empty?
78 data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
79 @confessions.replace get_confessions(data)
81 @confessions << "no confessions found!" if @confessions.empty?
82 m.reply @confessions.pop
86 m.reply "failed to connect to grouphug.us"
90 def grouphug_filter(s)
91 # check if we like the location of the page
92 loc = Utils.check_location(s, %r{http://(?:.*\.)?grouphug\.us})
94 # check if there are any conefssions
95 confessions = get_confessions(s[:text])
96 return if confessions.empty?
97 title = s[:text].ircify_html_title
98 # return the first confession
101 :content => confessions.first,
102 :grouphug_confessions => confessions
109 plugin = GrouphugPlugin.new
111 plugin.default_auth('create', false)
113 plugin.map "grouphug [:num]",
114 :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
115 plugin.map "confess [:num]",
116 :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
117 plugin.map "confess *confession", :thread => true, :action => :post_confession, :auth_path => 'create'