]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/grouphug.rb
search: wolfram fix
[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     @bot.register_filter(:grouphug, :htmlinfo) { |s| grouphug_filter(s) }
20   end
21
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.")
24   end
25
26   def post_confession(m, params)
27     c = params[:confession]
28     if c.length < 10
29       diff = 10 - c.length
30       m.reply _("Confession must be at least 10 words. You need %{m} more.") % {:m => diff}
31       return
32     end
33     uri = "http://beta.grouphug.us/confess"
34     form_id = "form_id=confession_node_form"
35     op = "op=Submit"
36     changed = "changed="
37     body = "body=#{c}"
38     msg = [form_id,body,changed,op].join("&")
39
40     response = bot.httputil.post(uri, msg)
41     debug response.body
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}
45     else
46       m.reply _("I couldn't share your confession.")
47     end
48   end
49
50   def get_confessions(html)
51     return [] unless html
52     start = html.index(START)
53     res = html[start, html.length - start].scan(REG)
54     return [] unless res
55     return res.map { |quote|
56       quote[0].ircify_html
57     }
58   end
59
60   def confess(m, params)
61     opts = { :cache => false }
62     path = "random"
63     begin
64       # Fetch a specific question - separate from cache
65       if params[:num]
66         path = "confessions/#{params[:num]}"
67         opts.delete(:cache)
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!"
72           warn confessions
73         end
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)
80         end
81         @confessions << "no confessions found!" if @confessions.empty?
82         m.reply @confessions.pop
83       end
84     rescue Exception => e
85       error e
86       m.reply "failed to connect to grouphug.us"
87     end
88   end
89
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})
93     return unless loc
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
99     return {
100           :title => title,
101           :content => confessions.first,
102           :grouphug_confessions => confessions
103     }
104   end
105
106 end
107
108
109 plugin = GrouphugPlugin.new
110
111 plugin.default_auth('create', false)
112
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'
118