Skip to content
Newer
Older
100644 64 lines (49 sloc) 1.4 KB
d1c842d @tsion Use bundler
authored Aug 3, 2012
1 require 'bundler/setup'
604b141 @tsion Initial commit. All appears to be well.
authored Feb 6, 2011
2 require 'cinch'
3 require 'patron'
4 require 'uri'
884b41c @tsion Using htmlentities gem rather than the built-in CGI.unescapeHTML, whi…
authored Feb 6, 2011
5 require 'htmlentities'
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 27, 2012
6 require 'configru'
7
8 Configru.load do
9 just 'config.yml'
10
11 defaults do
12 port 6667
13 end
14 end
604b141 @tsion Initial commit. All appears to be well.
authored Feb 7, 2011
15
16 bot = Cinch::Bot.new do
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 28, 2012
17 configure do |c|
18 c.server = Configru.server
19 c.port = Configru.port
20 c.nick = Configru.nick
21 c.channels = Configru.channels
22 end
23
24 helpers do
25 def get_title(body)
26 title = body[/<title>(.*?)<\/title>/m, 1]
97fb746 @tsion Fix HTMLEntities usage
authored Aug 22, 2012
27 HTMLEntities.new.decode(title).gsub("\n", " ").strip if title
604b141 @tsion Initial commit. All appears to be well.
authored Feb 7, 2011
28 end
524f173 @tsion Remove end-of-line whitespace.
authored Feb 21, 2012
29
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 28, 2012
30 def yubnub(query)
31 sess = Patron::Session.new
32 sess.timeout = 10
33 sess.base_url = "http://yubnub.org"
34 sess.headers['User-Agent'] = 'yurbnurb/1.0'
524f173 @tsion Remove end-of-line whitespace.
authored Feb 21, 2012
35
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 28, 2012
36 r = sess.get("/parser/parse?command=" + URI.escape(query))
524f173 @tsion Remove end-of-line whitespace.
authored Feb 21, 2012
37
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 28, 2012
38 return "HTTP error code #{r.status} while executing command (#{r.url})" if r.status >= 400
524f173 @tsion Remove end-of-line whitespace.
authored Feb 21, 2012
39
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 28, 2012
40 if r.body[0..1000].include?("<html") || r.body[0..1000].include?("<!doctype")
41 title = get_title(r.body)
42 return title ? "#{r.url} \"#{title}\"" : r.url
43 end
524f173 @tsion Remove end-of-line whitespace.
authored Feb 21, 2012
44
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 28, 2012
45 return r.url if r.body.chomp.include?("\n")
524f173 @tsion Remove end-of-line whitespace.
authored Feb 21, 2012
46
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 28, 2012
47 r.body.chomp
48 rescue Patron::Error => e
49 "A network error occurred: #{e.message}"
604b141 @tsion Initial commit. All appears to be well.
authored Feb 7, 2011
50 end
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 28, 2012
51 end
524f173 @tsion Remove end-of-line whitespace.
authored Feb 21, 2012
52
56b8c22 @tsion Use Configru gem for configuration.
authored Feb 28, 2012
53 on :message, /^#{Regexp.escape nick}\S*[:,]\s*(.+)$/ do |m, query|
54 m.reply("#{m.user.nick}: #{yubnub(query)}")
55 end
639e489 @tsion Add inline {{command ...}} syntax
authored Aug 30, 2012
56
57 on :message, /{{(.+?)}}/ do |m, query|
58 m.reply("#{yubnub(query)}")
59 end
604b141 @tsion Initial commit. All appears to be well.
authored Feb 7, 2011
60 end
61
62 bot.start
63
Something went wrong with that request. Please try again.