Permalink
Browse files

Initial commit.

Signed-off-by: Jordon Bedwell <jordon@envygeeks.com>
  • Loading branch information...
envygeeks committed Feb 13, 2012
0 parents commit 1d84987021b4f8ad0aa244116edf13aeae1d00de
Showing with 159 additions and 0 deletions.
  1. +124 −0 pry_bot
  2. +4 −0 readme.md
  3. +31 −0 usr.local.bin.pry_bot
124 pry_bot
@@ -0,0 +1,124 @@
+#!/usr/bin/ruby1.9.3
+
+require 'json/ext'
+require 'cinch'
+require 'fileutils'
+require 'faraday'
+require 'logger'
+require 'stringio'
+require 'shellwords'
+
+log = Logger.new('/var/log/pry/bot.log')
+
+# Changing soon since we plan to use EGRedis-Cluster to cache URI's of
+# docs so we don't spam Github with a bunch of redundant informations.
+
+class Gistme
+ def self.create_gist(file, desc, content)
+ conn = Faraday.new(:url => 'https://api.github.com') do |builder|
+ builder.use Faraday::Adapter::EMSynchrony
+ end
+
+ gist = conn.post do |req|
+ req.body = {
+ files: {
+ file => {
+ content: content
+ }
+ },
+ public: true,
+ description: desc
+ }.to_json
+
+ req.url '/gists'
+ req.headers['Content-Type'] = 'application/json'
+ end
+
+ gist = JSON.parse(gist.body)
+ if gist['html_url']
+ return gist['html_url']
+ end
+
+ gist['message']
+ end
+end
+
+Thread.abort_on_exception = true
+Cinch::Bot.new do
+ configure do |configuration|
+ configuration.channels = ['#pry']
+ configuration.nick = 'pry[bot]'
+ configuration.user = 'bot'
+ configuration.name = 'Server 40522'
+ configuration.server = 'irc.freenode.org'
+ end
+
+ on :message, /\Ashow-(doc|method)\s(.+)/ do |message, command, meth|
+ begin
+ Timeout.timeout 5 do
@dominikh

dominikh Feb 13, 2012

how can this ever time out? The thread in the next line will be created almost instantly, and it could run forever, Timeout wouldn't care, or know, it is done pretty much the instant the thread got created.

@envygeeks

envygeeks Feb 13, 2012

Contributor

@dominikh That problem is already fixed it just hasn't been pushed yet.

+ Thread.new(message, command, meth) do |message, command, meth|
@dominikh

dominikh Feb 13, 2012

Why create a thread here, anyway? Cinch already stuffs each execution of a handler into its own thread.

+ begin
+ out = StringIO.new
+ IO.popen('pry --no-color --no-history --simple-prompt','r+') do |io|
+ io.puts "show-#{command} #{meth}"
+ io.close_write
+ out.puts io.read
+ end
+
+ if out !~ /\A>>\s*Error/
+ out = out.string.split(/\n/)[2..-1]
+ file = out[0].split[1].split(/\//).last
+ desc = out[0].split(/:/)[1].split(/\//).last
+
+ if command == 'method'
+ comment = '#'
+ if file.split('.').last == 'c' || file.split('.').last == 'h'
+ comment = '//'
+ end
+
+ out = out.map.with_index do |value, iteration|
+ if iteration <= 4 && !value.empty? && value =~ /\A[a-zA-Z\s{1}]+:\s.*/
+ value = "#{comment} #{value}"
+ end
+
+ value
+ end
+ else
+ file = file.gsub /\.[a-z\-_0-9]+\Z/, '.rdoc'
+ out = out.map.with_index do |value, iteration|
+ if iteration <= 4 && !value.empty? && value =~ /\A[a-zA-Z\s{1}]+:\s.*/
+ value = " #{value}"
+ end
+ end
+ end
+
+ final_output = Gistme.create_gist(file, desc, out[1..-1].join("\n"))
+ if final_output =~ /\Ahttp/
+ message.reply final_output
+
+ if meth =~ /\APry/i && command == 'doc'
+ message.reply 'If this doc is ugly please submit a ticket for a format fix.'
+ end
+ else
+ raise
+ end
+ else
+ # Not Found, more than likely....
+ message.reply 'Unknown document.'
+ end
+ rescue => error
+ log.fatal error
+ message.reply 'Game over: FATALITY!!'
+ end
+ end
+ end
+ rescue Timeout::Error
+ log.fatal 'MASTER EGMONIT PLEASE KILL ME FOR I HAVE MADE A CRITICAL MISTAKE.'
+ message.reply "CRITICAL ERROR: I'm obligated by master EGMONIT to ask only for death."
+ end
+ end
+
+ on :message, /\A(?:eval|!!)\s(.+)/ do |message, code|
+ message.reply 'This feature is not supported yet'
+ end
+end.start
@@ -0,0 +1,4 @@
+Pry-Bot is the bot that sits in #pry and gives you the same docs you would have access
+to in Pry itself if you installed pry-doc and did show-doc and show-method. It will
+soon also be able to safely evaluate Ruby code. It also includes the apparmor profiles
+to keep it from going crazy if something goes wrong.
@@ -0,0 +1,31 @@
+# Last Modified: Mon Feb 13 09:01:14 2012
+#include <tunables/global>
+
+/usr/local/bin/pry_bot {
+ #include <abstractions/base>
+ #include <abstractions/ruby>
+
+ deny capability dac_override,
+ capability dac_read_search,
+ deny /etc/inputrc r,
+ deny network inet6 dgram,
+
+ network inet dgram,
+ network inet stream,
+
+ /etc/gai.conf r,
+ /etc/host.conf r,
+ /etc/hosts r,
+ /etc/nsswitch.conf r,
+ /etc/resolv.conf r,
+ /home/pry/.pry_history r,
+ /usr/bin/ruby1.9.1 ix,
+ /usr/lib/ruby/1.9.1/x86_64-linux/** mr,
+ /usr/local/bin/pry rix,
+ /usr/local/bin/pry_bot r,
+ /var/lib/gems/1.9.1/gems/** mr,
+ /var/lib/gems/1.9.1/specifications/ r,
+ /var/lib/gems/1.9.1/specifications/* r,
+ /var/log/pry r,
+ /var/log/pry/bot.log rw,
+}

0 comments on commit 1d84987

Please sign in to comment.