Exemple #1
0
    def _message(self, req):
        output = Output()

        for bot in bots.all():
            code = bot['code']
            try:
                env = Env(code, bot['name'], bot['user'])
                env.onMessage(req['name'], req['message'])
                bots.update_lastsaid(bot['name'], env.output.lastsaid)
                output.combine(env.output)
            except Exception as e:
                res = self._removebot(bot, e)
                output.pms.update(res['pms'])
                continue
        return output.serialize()
Exemple #2
0
 def __init__(self, code, botname, botowner):
     self._botname = botname
     self._botowner = botowner
     self._code = code
     self._data = None
     self._modified = False
     self.output = Output()
Exemple #3
0
    def _removebot(self, req, e=None):
        name = req['name']
        bot = bots.get(name)
        user = bot['user']
        code = bot['code']

        output = Output()

        # remove the bot
        if e:
            m = 'ERROR in bot `{0}`: {1}\n{2}'.format(name,
                                                      str(e),
                                                      traceback.format_exc())
            output.pms.setdefault(user, []).append(m)

        m = ('Bot `{0}` was killed due to errors.\n' +
             'Here lies its code:\n{1}').format(name, code)
        output.pms[user].append(m)

        bots.remove(name)

        return output.serialize()
Exemple #4
0
class Env(DSL):
    def __init__(self, code, botname, botowner):
        self._botname = botname
        self._botowner = botowner
        self._code = code
        self._data = None
        self._modified = False
        self.output = Output()

    def compileCode(self):
        rundsl(self._code, dslcode, self._botname, self._botowner)

    def loadData(self):
        data = db.botdata.find_one({'botname': self._botname})
        self._data = data['data'] if data else {}

    def saveData(self):
        if not self._modified:
            return # don't do a db query if you don't have to

        db.botdata.update({'botname': self._botname},
                          {
                              'botname': self._botname,
                              'data': self._data,
                          },
                          upsert=True, safe=True)

    def onMessage(self, name, message):
        self.loadData()
        data = json.dumps(self._data)
        args = json.dumps([name, message])
        curlcount = 0
        print '=----------------------======'
        curldata = {}
        while curlcount < 4:
            try:
                curls = json.dumps(curldata)
                resp = rundsl(self._code,
                              dslcode,
                              self._botname,
                              self._botowner,
                              data,
                              'onMessage',
                              args,
                              curls)
                break
            except UserWarning as e:
                msg = str(e)
                if msg.startswith('CURLEXCEPTION: '):
                    url = msg[len('CURLEXCEPTION: '):]

                    # curl the url and put the result into curldata
                    curldata[url] = requests.get(url).text

                    curlcount += 1
                    if curlcount == 4:
                        raise RuntimeError('You can only call curl 3 times!')
                else:
                    raise
        print '=----------------------======'

        ret = json.loads(resp)
        data = ret['data']
        output = ret['output']
        if self._data != data:
            self._data = data
            self._modified = True
        self.output.parse(output)
        self.saveData()