Example #1
0
def QTerm():
    try:
        # python qterm.py -s
        # python qterm.py [PORT] [COMMAND]
        if len(sys.argv) == 2 and sys.argv[1] == '-s':
            QTermServer(DEFPORT).Test()
        else:
            if len(sys.argv) >= 2 and sys.argv[1].isdigit():
                port = int(sys.argv[1])
                command = ' '.join(sys.argv[2:]).strip()
            else:
                port = DEFPORT
                command = ' '.join(sys.argv[1:]).strip()
    
            if command:
                if not PY3:
                    command = command.decode(sys.getfilesystemencoding())
                command = command.encode('utf8')
                resp = BYTES2STR(query(port, command))
                if not resp:
                    PRINT('无法连接 QQBot-term 服务器')
                elif not resp.strip():
                    PRINT('QQBot 命令格式错误')
                else:
                    PRINT(resp.strip())

    except KeyboardInterrupt:
        pass
Example #2
0
 def Daemonize(self):
     if daemonable:
         logfile = self.absPath('daemon-%s.log' % self.qq)
         PRINT('将以 daemon 模式运行, log 文件: %s' % logfile)
         daemonize(logfile)
     else:
         PRINT('错误:无法以 daemon 模式运行')
         sys.exit(1)
Example #3
0
    def readConfFile(self):
        conf = ast.literal_eval(sampleConfStr)['默认配置']

        confPath = self.ConfPath()

        if os.path.exists(confPath):
            try:
                with open(confPath, 'rb') as f:
                    cusConf = ast.literal_eval(BYTES2STR(f.read()))

                if type(cusConf) is not dict:
                    raise ConfError('文件内容必须是一个dict')

                elif self.user is None:
                    pass

                elif self.user not in cusConf:
                    raise ConfError('用户 %s 不存在' % self.user)

                elif type(cusConf[self.user]) is not dict:
                    raise ConfError('用户 %s 的配置必须是一个 dict' % self.user)

                else:
                    for k, v in list(cusConf[self.user].items()):
                        if k not in conf:
                            raise ConfError('不存在的配置选项 %s.%s ' % (self.user, k))

                        elif type(v) is not type(conf[k]):
                            raise ConfError(
                                '%s.%s 必须是一个 %s' %
                                (self.user, k, type(conf[k]).__name__))

                        else:
                            conf[k] = v

            except (IOError, SyntaxError, ValueError, ConfError) as e:
                PRINT('配置文件 %s 错误: %s\n' % (confPath, e), end='')
                sys.exit(1)

        else:
            try:
                with open(confPath, 'wb') as f:
                    f.write(STR2BYTES(sampleConfStr))
            except IOError:
                pass

            if self.user is not None:
                PRINT('用户 %s 不存在\n' % self.user, end='')
                sys.exit(1)

        for k, v in list(conf.items()):
            if getattr(self, k, None) is None:
                setattr(self, k, v)

        if self.mailAccount and not self.mailAuthCode:
            msg = '请输入 %s 的 IMAP/SMTP 服务授权码: ' % self.mailAccount
            self.mailAuthCode = RAWINPUT(msg)
Example #4
0
    def readCmdLine(self):
        parser = argparse.ArgumentParser(add_help=False)
        parser.add_argument('-h', '--help', action='store_true')
        parser.add_argument('-u', '--user')
        parser.add_argument('-q', '--qq')
        parser.add_argument('-p', '--termServerPort', type=int)
        parser.add_argument('-ip', '--httpServerIP')
        parser.add_argument('-hp', '--httpServerPort', type=int)
        parser.add_argument('-m', '--mailAccount')
        parser.add_argument('-mc', '--mailAuthCode')
        parser.add_argument('-d', '--debug', action='store_true', default=None)
        parser.add_argument('-nd', '--nodebug', action='store_true')
        parser.add_argument('-r',
                            '--restartOnOffline',
                            action='store_true',
                            default=None)
        parser.add_argument('-nr', '--norestart', action='store_true')
        parser.add_argument('-fi', '--fetchInterval', type=int)
        parser.add_argument('-saf',
                            '--startAfterFetch',
                            action='store_true',
                            default=None)
        parser.add_argument('-mt', '--monitorTables')

        try:
            opts = parser.parse_args()
        except:
            PRINT(usage)
            sys.exit(1)

        if opts.help:
            PRINT(usage)
            sys.exit(0)

        if opts.nodebug:
            opts.debug = False

        if opts.norestart:
            opts.restartOnOffline = False

        delattr(opts, 'nodebug')
        delattr(opts, 'norestart')

        if opts.monitorTables:
            s = opts.monitorTables
            if not PY3:
                s = s.decode(sys.getfilesystemencoding()).encode('utf8')
            opts.monitorTables = s.split(',')

        for k, v in list(opts.__dict__.items()):
            if getattr(self, k, None) is None:
                setattr(self, k, v)
def QTerm():
    # python qterm.py [PORT] [COMMAND]
    if len(sys.argv) >= 2 and sys.argv[1].isdigit():
        port = sys.argv[1]
        command = ' '.join(sys.argv[2:]).strip()
    else:
        port = DEFPORT
        command = ' '.join(sys.argv[1:]).strip()

    if command:
        resp = BYTES2STR(Query(HOST, port, SYSTEMSTR2BYTES(command)))
        if not resp:
            PRINT('无法连接 QQBot-Term 服务器')
        elif not resp.strip():
            PRINT('QQBot 命令格式错误')
        else:
            PRINT(resp.strip())
Example #6
0
    def readConfFile(self):
        confPath = self.ConfPath()
        strConfPath = SYSTEMSTR2STR(confPath)
        conf = rootConf.copy()

        if os.path.exists(confPath):
            try:
                with open(confPath, 'rb') as f:
                    cusConf = ast.literal_eval(BYTES2STR(f.read()))

                if type(cusConf) is not dict:
                    raise ConfError('文件内容必须是一个 dict')

                if type(cusConf.get('默认配置', {})) is not dict:
                    raise ConfError('默认配置必须是一个 dict')

                if self.user is not None:
                    if self.user not in cusConf:
                        raise ConfError('用户 %s 不存在' % self.user)
                    elif type(cusConf[self.user]) is not dict:
                        raise ConfError('用户 %s 的配置必须是一个 dict' % self.user)
                    else:
                        names = ['默认配置', self.user]
                else:
                    names = ['默认配置']

                for name in names:
                    for k, v in list(cusConf.get(name, {}).items()):
                        if k in deprecatedConfKeys:
                            PRINT('被废弃的配置选项 %s ,将忽略此选项' % k)
                        elif k not in conf:
                            raise ConfError('不存在的配置选项 %s.%s ' % (name, k))
                        elif type(v) is not type(conf[k]):
                            t = type(conf[k]).__name__
                            raise ConfError('%s.%s 必须是一个 %s' % (name, k, t))
                        else:
                            conf[k] = v

            except (IOError, SyntaxError, ValueError, ConfError) as e:
                PRINT('配置文件 %s 错误: %s\n' % (strConfPath, e), end='')
                sys.exit(1)

        else:
            PRINT('未找到配置文件“%s”,将使用默认配置' % strConfPath)
            try:
                with open(confPath, 'wb') as f:
                    f.write(STR2BYTES(sampleConfStr))
            except IOError:
                pass
            else:
                PRINT('已创建一个默认配置文件“%s”' % strConfPath)

            if self.user is not None:
                PRINT('用户 %s 不存在\n' % self.user, end='')
                sys.exit(1)

        for k, v in list(conf.items()):
            if getattr(self, k, None) is None:
                setattr(self, k, v)

        if self.pluginPath and not os.path.isdir(STR2SYSTEMSTR(
                self.pluginPath)):
            PRINT('配置文件 %s 错误: 插件目录 “%s” 不存在\n' % \
                  (strConfPath, self.pluginPath), end='')
            sys.exit(1)

        if self.mailAccount and not self.mailAuthCode:
            msg = '请输入 %s 的 IMAP/SMTP 服务授权码: ' % self.mailAccount
            self.mailAuthCode = RAWINPUT(msg)

        if self.cmdQrcode:
            try:
                import PIL
                import wcwidth
            except ImportError:
                PRINT('您已选择以文本模式显示二维码,请先安装 pillow, wcwidth 库')
                sys.exit(1)
Example #7
0
    def readCmdLine(self, argv):
        if argv is None:
            argv = sys.argv[1:]

        parser = argparse.ArgumentParser(add_help=False)

        parser.add_argument('-h', '--help', action='store_true')

        parser.add_argument('-u', '--user')

        parser.add_argument('-q', '--qq')

        parser.add_argument('-b', '--bench')

        parser.add_argument('-p', '--termServerPort', type=int)

        parser.add_argument('-ip', '--httpServerIP')

        parser.add_argument('-hp', '--httpServerPort', type=int)

        parser.add_argument('-m', '--mailAccount')

        parser.add_argument('-mc', '--mailAuthCode')

        parser.add_argument('-cq',
                            '--cmdQrcode',
                            action='store_true',
                            default=None)

        parser.add_argument('-d', '--debug', action='store_true', default=None)

        parser.add_argument('-nd', '--nodebug', action='store_true')

        parser.add_argument('-r',
                            '--restartOnOffline',
                            action='store_true',
                            default=None)

        parser.add_argument('-nr', '--norestart', action='store_true')

        parser.add_argument('-dm',
                            '--daemon',
                            action='store_true',
                            default=None)

        parser.add_argument('-ndm', '--nodaemon', action='store_true')

        parser.add_argument('-saf',
                            '--startAfterFetch',
                            action='store_true',
                            default=None)

        parser.add_argument('-pp', '--pluginPath')

        parser.add_argument('-pl', '--plugins')

        try:
            opts = parser.parse_args(argv)
        except:
            PRINT(usage)
            sys.exit(1)

        if opts.help:
            PRINT(usage)
            sys.exit(0)

        if opts.nodebug:
            opts.debug = False

        if opts.norestart:
            opts.restartOnOffline = False

        if opts.nodaemon:
            opts.daemon = False

        delattr(opts, 'nodebug')
        delattr(opts, 'norestart')
        delattr(opts, 'nodaemon')

        if not opts.bench:
            opts.bench = os.path.join(os.path.expanduser('~'), '.qqbot-tmp')

        opts.bench = os.path.abspath(opts.bench)
        opts.benchstr = SYSTEMSTR2STR(opts.bench)

        if not os.path.exists(opts.bench):
            try:
                os.mkdir(opts.bench)
            except Exception as e:
                PRINT('无法创建工作目录 %s , %s' % (opts.benchstr, e))
                sys.exit(1)
        elif not os.path.isdir(opts.bench):
            PRINT('无法创建工作目录 %s ' % opts.benchstr)
            sys.exit(1)

        if opts.plugins:
            opts.plugins = SYSTEMSTR2STR(opts.plugins).split(',')

        if opts.pluginPath:
            opts.pluginPath = SYSTEMSTR2STR(opts.pluginPath)

        for k, v in list(opts.__dict__.items()):
            if getattr(self, k, None) is None:
                setattr(self, k, v)
Example #8
0
    def readCmdLine(self):
        parser = argparse.ArgumentParser(add_help=False)

        parser.add_argument('-h', '--help', action='store_true')

        parser.add_argument('-u', '--user')

        parser.add_argument('-q', '--qq')

        parser.add_argument('-p', '--termServerPort', type=int)

        parser.add_argument('-ip', '--httpServerIP')

        parser.add_argument('-hp', '--httpServerPort', type=int)

        parser.add_argument('-m', '--mailAccount')

        parser.add_argument('-mc', '--mailAuthCode')

        parser.add_argument('-cq',
                            '--cmdQrcode',
                            action='store_true',
                            default=None)

        parser.add_argument('-d', '--debug', action='store_true', default=None)

        parser.add_argument('-nd', '--nodebug', action='store_true')

        parser.add_argument('-r',
                            '--restartOnOffline',
                            action='store_true',
                            default=None)

        parser.add_argument('-nr', '--norestart', action='store_true')

        parser.add_argument('-saf',
                            '--startAfterFetch',
                            action='store_true',
                            default=None)

        parser.add_argument('-pp', '--pluginPath')

        parser.add_argument('-pl', '--plugins')

        try:
            opts = parser.parse_args()
        except:
            PRINT(usage)
            sys.exit(1)

        if opts.help:
            PRINT(usage)
            sys.exit(0)

        if opts.nodebug:
            opts.debug = False

        if opts.norestart:
            opts.restartOnOffline = False

        delattr(opts, 'nodebug')
        delattr(opts, 'norestart')

        if opts.plugins:
            opts.plugins = SYSTEMSTR2STR(opts.plugins).split(',')

        if opts.pluginPath:
            opts.pluginPath = SYSTEMSTR2STR(opts.pluginPath)

        for k, v in list(opts.__dict__.items()):
            if getattr(self, k, None) is None:
                setattr(self, k, v)