Logo Search packages:      
Sourcecode: ibid version File versions  Download package


# Copyright (c) 2009-2010, Michael Gorven, Stefano Rivera, Jonathan Hitchcock
# Released under terms of the MIT/X/Expat Licence. See COPYING for details.

from __future__ import division
from random import random, randint

import logging
from os import kill
from signal import SIGTERM
from subprocess import Popen, PIPE
from time import time, sleep

from ibid.compat import all, factorial
from ibid.config import Option, FloatOption
from ibid.plugins import Processor, match
from ibid.utils import file_in_path, unicode_output

    from ast import NodeTransformer, Pow, Name, Load, Call, copy_location, parse

except ImportError:
    from compiler import ast, pycodegen, parse, misc, walk
    class NodeTransformer(object):
    # ExpressionCodeGenerator doesn't inherit __futures__ from calling module:
    class FD_ExpressionCodeGenerator(pycodegen.ExpressionCodeGenerator):
        futures = ('division',)

help = {}
log = logging.getLogger('calc')

help['bc'] = u'Calculate mathematical expressions using bc'
class BC(Processor):
    u"""bc <expression>"""

    feature = 'bc'

    bc = Option('bc', 'Path to bc executable', 'bc')
    bc_timeout = FloatOption('bc_timeout', 'Maximum BC execution time (sec)', 2.0)

    def setup(self):
        if not file_in_path(self.bc):
            raise Exception("Cannot locate bc executable")

    def calculate(self, event, expression):
        bc = Popen([self.bc, '-l'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
        start_time = time()
        bc.stdin.write(expression.encode('utf-8') + '\n')

        while bc.poll() is None and time() - start_time < self.bc_timeout:

        if bc.poll() is None:
            kill(bc.pid, SIGTERM)
            event.addresponse(u'Sorry, that took too long. I stopped waiting')

        output = bc.stdout.read()
        error = bc.stderr.read()

        code = bc.wait()

        if code == 0:
            if output:
                output = unicode_output(output.strip())
                output = output.replace('\\\n', '')
                error = unicode_output(error.strip())
                error = error.split(":", 1)[1].strip()
                error = error[0].lower() + error[1:].split('\n')[0]
                event.addresponse(u"I'm sorry, I couldn't deal with the %s", error)
            event.addresponse(u"Error running bc")
            error = unicode_output(error.strip())
            raise Exception("BC Error: %s" % error)

help['calc'] = u'Returns the anwser to mathematical expressions. Uses Python syntax and semantics (i.e. radians)'
class LimitException(Exception):

class AccessException(Exception):

def limited_pow(*args):
    "We don't want users to DOS the bot. Pow is the most dangerous function. Limit it"

    # Large modulo-powers are ok, but otherwise we don't want enormous operands
    if (all(isinstance(arg, (int, long)) for arg in args)
            and not (len(args) == 3 and args[2] < 1e100)):
            answer = pow(float(args[0]), float(args[1]))
            if answer > 1e+300:
                raise LimitException

        except OverflowError, e:
            raise LimitException(e)

    return pow(*args)

def limited_factorial(x):
    if x >= 500:
        raise LimitException
    return factorial(x)

# ast method
class PowSubstitutionTransformer(NodeTransformer):
    def visit_BinOp(self, node):
        if isinstance(node.op, Pow):
            fnode = Name('pow', Load())
            copy_location(fnode, node)
            cnode = Call(fnode, [node.left, node.right], [], None, None)
            copy_location(cnode, node)
            return cnode
        return node
    def visit_Attribute(self, node):
        raise AccessException

# compiler method
class PowSubstitutionWalker(object):
    def visitPower(self, node, *args):
        walk(node.left, self)
        walk(node.right, self)
        cnode = ast.CallFunc(ast.Name('pow'), [node.left, node.right], None, None)
        node.left = cnode
        # Little hack: instead of trying to turn node into a CallFunc, we just do pow(left, right)**1
        node.right = ast.Const(1)
    def visitGetattr(self, node, *args):
        raise AccessException

class Calc(Processor):
    u"""[calc] <expression>"""
    feature = 'calc'

    priority = 500

    extras = ('abs', 'round', 'min', 'max')
    banned = ('for', 'yield', 'lambda', '__', 'is')

    # Create a safe dict to pass to eval() as locals
    safe = {}
    exec('from math import *', safe)
    del safe['__builtins__']
    for function in extras:
        safe[function] = eval(function)
    safe['pow'] = limited_pow
    safe['factorial'] = limited_factorial

    def calculate(self, event, expression):
        for term in self.banned:
            if term in expression:

            # We need to remove all power operators and replace with our limited pow
            # ast is the new method (Python >=2.6) compiler is the old
            ast = parse(expression, mode='eval')
            if transform_method == 'ast':
                ast = PowSubstitutionTransformer().visit(ast)
                code = compile(ast, '<string>', 'eval')
                misc.set_filename('<string>', ast)
                walk(ast, PowSubstitutionWalker())
                code = FD_ExpressionCodeGenerator(ast).getCode()

            result = eval(code, {'__builtins__': None}, self.safe)

        except ZeroDivisionError, e:
            event.addresponse(u"I can't divide by zero.")
        except ArithmeticError, e:
            event.addresponse(u"I can't do that: %s", unicode(e))
        except ValueError, e:
            if unicode(e) == u"math domain error":
                event.addresponse(u"I can't do that: %s", unicode(e))
        except LimitException, e:
            event.addresponse(u"I'm afraid I'm not allowed to play with big numbers")
        except AccessException, e:
            event.addresponse(u"You're not allowed to do that")
        except Exception, e:

        if isinstance(result, (int, long, float, complex)):

help['random'] = u'Generates random numbers.'
class Random(Processor):
    u"""random [ <max> | <min> <max> ]"""
    feature = 'random'

    def random(self, event, begin, end):
        if not begin and not end:
            event.addresponse(u'I always liked %f', random())
            begin = int(begin)
            end = end and int(end) or 0
            event.addresponse(u'I always liked %i', randint(min(begin,end), max(begin,end)))

# vi: set et sta sw=4 ts=4:

Generated by  Doxygen 1.6.0   Back to index