From 3fa8d5d312b0ae69a3f99ea621f93e63ad1f1560 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Tue, 28 Aug 2012 11:29:08 +1200 Subject: [PATCH] Added early untested version of new python exec code - this probably will not work. --- plugins/factoids.py | 24 ++++++------------------ plugins/util/exec.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 plugins/util/exec.py diff --git a/plugins/factoids.py b/plugins/factoids.py index 91ff591..9121a3a 100755 --- a/plugins/factoids.py +++ b/plugins/factoids.py @@ -1,6 +1,7 @@ # Written by Scaevolus 2010 from util import hook, http from util.text import multiword_replace +from util.eval import execute_eval import string import sqlite3 import re @@ -17,23 +18,6 @@ shortcodes = { '[/i]': '\x16'} -def python(data, args, input): - # this is all shit, replacement code is in the works - variables = "input='%s'; nick='%s'; chan='%s'; bot_nick='%s';" % (args, - input.nick, input.chan, input.conn.nick) - statement = variables + data - data = data[4:].strip() - req = http.get("http://eval.appspot.com/eval", statement=statement).splitlines() - if len(req) == 0: - return "Failed to recieve response from remote Python API.." - req[0] = re_lineends.split(req[0])[0] - if not req[0] == 'Traceback (most recent call last):': - result = req[0].decode('utf8', 'ignore') - else: - result = req[-1].decode('utf8', 'ignore') - return result - - def db_init(db): db.execute("create table if not exists mem(word, data, nick," " primary key(word))") @@ -151,7 +135,11 @@ def factoid(inp, say=None, db=None, bot=None, me=None, conn=None, input=None): if data: # if the factoid starts with , its a dynamic one if data.startswith(""): - result = python(data[4:].strip(), arguments, input) + data = data[4:].strip() + variables = "input='%s'; nick='%s'; chan='%s'; bot_nick='%s';" % (args, + input.nick, input.chan, input.conn.nick) + result = execute_eval(variables + data) + else: result = data diff --git a/plugins/util/exec.py b/plugins/util/exec.py new file mode 100644 index 0000000..55a4ef2 --- /dev/null +++ b/plugins/util/exec.py @@ -0,0 +1,28 @@ +import http +import json, urllib2, sys + + +def haste(data): + URL = "http://paste.dmptr.com" + request = urllib2.Request(URL + "/documents", data) + response = urllib2.urlopen(request) + return("%s/%s" % (URL, json.loads(response.read())['key'])) + + +def execute_eval(code, paste_multiline=True): + while True: + output = http.get("http://eval.appspot.com/eval", statement=code).rstrip('\n') + if output: + break + else: + pass + + if "Traceback (most recent call last):" in output: + status = "Python error: " + else: + status = "Code executed sucessfully: " + + if "\n" in output and paste_multiline: + return status + haste(output) + else: + return output \ No newline at end of file