Ejemplo n.º 1
0
    def _run_cmd(self, stanza, body):
        """ 执行命令 """
        cmd = body[1:]
        c, args = self._parse_args(cmd)
        email = get_email(stanza.from_jid)
        cmds = [v.get('name') for v in self._get_cmd()]
        cmds.append('_ping')
        cmds.append('_tq')
        if c not in cmds:
            self._message_bus.send_all_msg(stanza, body)
            return
        try:
            self._logger.info('%s run cmd %s', email, c)
            m =getattr(self, c)(stanza, *args)
        except Exception as e:
            self._logger.warning(e.message)
            nick = Logics.get_one(stanza.from_jid).nick
            errorinfo = traceback.format_exc()
            body = u'{0} run command {1} happend an error:\
                    {2}'.format(nick, c, errorinfo)
            self._message_bus.send_to_admin(stanza, body)
            self._message_bus.send_back_msg(stanza, c + ' 命令异常,已通知管理员')
            return

        return m
Ejemplo n.º 2
0
 def ls(self, stanza, *args):
     """列出成员"""
     frm = stanza.from_jid
     femail = get_email(frm)
     members = Logics.get_members()
     onlines = []
     offlines = []
     for m in members:
         if m.email == USER:
             continue
         status = m.status
         isonline = bool([status.status for status in m.status
                      if status.status])
         status_text = " ".join(status.statustext for status in m.status
                                 if status.statustext)
         if m.email == femail:
             onlines.append("** {0}".format(m.nick))
         elif m.email != femail and isonline:
             onlines.append("* {0} {1}".format(m.nick, status_text))
         else:
             offlines.append("  {0}".format(m.nick))
     onlines = sorted(onlines, key = lambda k:k.decode('utf-8')[1],
                      reverse=True)
     offlines = sorted(offlines, key = lambda k:k.decode('utf-8')[1],
                       reverse=True)
     body = []
     body.insert(0, 'Pythoner Club 所有成员(** 表示你自己, * 表示在线):')
     body.extend(onlines)
     body.extend(offlines)
     online_num = len(onlines)
     total = online_num + len(offlines)
     body.append('共列出 {0} 位成员 {1} 位在线'.format(total, online_num))
     self._send_cmd_result(stanza, '\n'.join(body))
Ejemplo n.º 3
0
    def shell(self, stanza, *args):
        """ 一个Python shell, 也可通过 >>> <statement>来执行"""
        if len(args) < 1: return self.help(stanza, 'shell')
        code = ' '.join(args)
        email = get_email(stanza.from_jid)
        if code.strip() in ["cls", "clear"]:
            url = "http://pythonec.appspot.com/drop"
            params = [("session", email),]
        else:
            url = "http://pythonec.appspot.com/shell"
            #url = "http://localhost:8880/shell"
            params =  dict(session = email, statement=code.encode("utf-8"))


        def read_shell(resp):
            result = resp.read()
            nick = Logics.get_one(stanza.from_jid).nick
            if not result:
                nick = "{0}:[OK]".format(nick)
            else:
                nick = "{0}:[OUT]".format(nick)

            if len(result) > 200:
                callback = partial(self._message_bus.send_sys_msg, stanza)
                self._paste(stanza, "python", result, nick, callback)
            else:
                self._message_bus.send_sys_msg(stanza, u"{0} {1}"
                                               .format(nick, result))

        self._http_stream.get(url, params, readback = read_shell)
Ejemplo n.º 4
0
    def shell(self, stanza, *args):
        """ 一个Python shell, 也可通过 >>> <statement>来执行"""
        if len(args) < 1: return self.help(stanza, 'shell')
        code = ' '.join(args)
        email = get_email(stanza.from_jid)
        if code.strip() in ["cls", "clear"]:
            url = "http://pythonec.appspot.com/drop"
            params = [
                ("session", email),
            ]
        else:
            url = "http://pythonec.appspot.com/shell"
            #url = "http://localhost:8880/shell"
            params = dict(session=email, statement=code.encode("utf-8"))

        def read_shell(resp):
            result = resp.read()
            nick = Logics.get_one(stanza.from_jid).nick
            if not result:
                nick = "{0}:[OK]".format(nick)
            else:
                nick = "{0}:[OUT]".format(nick)

            if len(result) > 200:
                callback = partial(self._message_bus.send_sys_msg, stanza)
                self._paste(stanza, "python", result, nick, callback)
            else:
                self._message_bus.send_sys_msg(stanza,
                                               u"{0} {1}".format(nick, result))

        self._http_stream.get(url, params, readback=read_shell)
Ejemplo n.º 5
0
    def _run_cmd(self, stanza, body):
        """ 执行命令 """
        cmd = body[1:]
        c, args = self._parse_args(cmd)
        email = get_email(stanza.from_jid)
        cmds = [v.get('name') for v in self._get_cmd()]
        cmds.append('_ping')
        cmds.append('_tq')
        if c not in cmds:
            self._message_bus.send_all_msg(stanza, body)
            return
        try:
            self._logger.info('%s run cmd %s', email, c)
            m = getattr(self, c)(stanza, *args)
        except Exception as e:
            self._logger.warning(e.message)
            nick = Logics.get_one(stanza.from_jid).nick
            errorinfo = traceback.format_exc()
            body = u'{0} run command {1} happend an error:\
                    {2}'.format(nick, c, errorinfo)
            self._message_bus.send_to_admin(stanza, body)
            self._message_bus.send_back_msg(stanza, c + ' 命令异常,已通知管理员')
            return

        return m
Ejemplo n.º 6
0
 def ls(self, stanza, *args):
     """列出成员"""
     frm = stanza.from_jid
     femail = get_email(frm)
     members = Logics.get_members()
     onlines = []
     offlines = []
     for m in members:
         if m.email == USER:
             continue
         status = m.status
         isonline = bool(
             [status.status for status in m.status if status.status])
         status_text = " ".join(status.statustext for status in m.status
                                if status.statustext)
         if m.email == femail:
             onlines.append("** {0}".format(m.nick))
         elif m.email != femail and isonline:
             onlines.append("* {0} {1}".format(m.nick, status_text))
         else:
             offlines.append("  {0}".format(m.nick))
     onlines = sorted(onlines,
                      key=lambda k: k.decode('utf-8')[1],
                      reverse=True)
     offlines = sorted(offlines,
                       key=lambda k: k.decode('utf-8')[1],
                       reverse=True)
     body = []
     body.insert(0, 'Pythoner Club 所有成员(** 表示你自己, * 表示在线):')
     body.extend(onlines)
     body.extend(offlines)
     online_num = len(onlines)
     total = online_num + len(offlines)
     body.append('共列出 {0} 位成员 {1} 位在线'.format(total, online_num))
     self._send_cmd_result(stanza, '\n'.join(body))
Ejemplo n.º 7
0
 def get_one(cls, jid, **kwargs):
     """ 获取一个成员
     Arguments:
         `jid`   -   成员jid
     """
     email = get_email(jid)
     return cls.wrap_member(cls.db[const.MEMBER].find_one({"email": email}),
                            **kwargs)
Ejemplo n.º 8
0
 def get_members(remove = None):
     """ 获取所有成员
     Arguments:
         `remove`    -   排除成员
     """
     remove_email = get_email(remove)
     if remove:
         return session.query(Member).filter(Member.email != remove_email).all()
     return session.query(Member).all()
Ejemplo n.º 9
0
    def add(cls, jid, nick=None, show=None):
        """ 添加一个成员
        Arguments:
            `jid`   -   成员jid
            `nick`  -   昵称
            `show`  -   stanze.show
        """
        if cls.get_one(jid):
            return
        if not nick:
            nick = get_email(jid).split("@")[0]
        doc = {"email": get_email(jid), "nick": nick, "isonline": True,
               "join_date": now()}
        mid = cls.db[const.MEMBER].insert(doc)
        cls.db[const.STATUS].insert({"mid": mid, "statustext": show,
                                     "resource": jid.resource,
                                     "status": const.ONLINE})

        return cls.get_one(jid)
Ejemplo n.º 10
0
 def get_members(remove=None):
     """ 获取所有成员
     Arguments:
         `remove`    -   排除成员
     """
     remove_email = get_email(remove)
     if remove:
         return session.query(Member).filter(
             Member.email != remove_email).all()
     return session.query(Member).all()
Ejemplo n.º 11
0
 def get_members(cls, remove=None, **kwargs):
     """ 获取所有成员
     Arguments:
         `remove`    -   排除成员
     """
     remove_email = get_email(remove)
     if remove:
         ms = cls.db[const.MEMBER].find({"email": {"$ne": remove_email}})
         return cls.wrap_member(list(ms), **kwargs)
     ms = cls.db[const.MEMBER].find()
     return cls.wrap_member(list(ms), **kwargs)
Ejemplo n.º 12
0
 def get_one(cls, jid):
     """ 获取一个成员
     Arguments:
         `jid`   -   成员jid
     """
     email = get_email(jid)
     try:
         m = session.query(Member).filter(Member.email == email).one()
     except NoResultFound:
         m = None
     return m
Ejemplo n.º 13
0
 def get_one(cls, jid):
     """ 获取一个成员
     Arguments:
         `jid`   -   成员jid
     """
     email = get_email(jid)
     try:
         m = session.query(Member).filter(Member.email == email).one()
     except NoResultFound:
         m = None
     return m
Ejemplo n.º 14
0
 def send_command(self, stanza, body):
     """ 处理命令
         为防止阻塞使用线程池处理命令
     """
     email = get_email(stanza.from_jid)
     self.logger.info("{0} run command {1}".format(stanza.from_jid, body))
     if email in ADMINS:
         target = self.admin_cmd_handler._run_cmd
     else:
         target = self.cmd_handler._run_cmd
     target(stanza, body)
Ejemplo n.º 15
0
 def send_command(self, stanza,  body):
     """ 处理命令
         为防止阻塞使用线程池处理命令
     """
     email = get_email(stanza.from_jid)
     self.logger.info("{0} run command {1}".format(stanza.from_jid, body))
     if email in ADMINS:
         target = self.admin_cmd_handler._run_cmd
     else:
         target = self.cmd_handler._run_cmd
     target(stanza, body)
Ejemplo n.º 16
0
    def drop(cls, jid):
        """ 删除一个成员
        Arguments:
            `jid`   -   成员jid
        """
        m = cls.get_one(jid)
        try:
            cls.db[const.MEMBER].remove({"email": get_email(jid)})
            cls.db[const.STATUS].remove({"mid": m._id})
            cls.db[const.INFO].remove({"mid": m._id})
        except:
            traceback.print_exc()

        return
Ejemplo n.º 17
0
 def __init__(self, jid, nick = None):
     self.nick = nick if nick else jid.local
     self.email = get_email(jid)
     self.join_date = now()
Ejemplo n.º 18
0
 def __init__(self, to_member, content):
     self.to_member = get_email(to_member)
     self.content = content
     self.pubdate = now()
Ejemplo n.º 19
0
 def set_today_rp(cls, jid, rp):
     cls.set_info(jid, const.INFO_RP, rp)
     cls.set_info(jid, const.INFO_RP_DATE, time.time())
     cls.db[const.MEMBER].update({"email": get_email(jid)},
                                 {"$push": {"rps": {"value": rp, "date": now()}}})