Esempio n. 1
0
    def Login(self, qq=None, user=None):
        self.conf = QConf(qq, user)
        session, contactdb, self.conf = QLogin(conf=self.conf)

        # main thread
        self.SendTo = session.SendTo
        self.groupKick = session.GroupKick
        self.groupSetAdmin = session.GroupSetAdmin
        self.groupShut = session.GroupShut
        self.groupSetCard = session.GroupSetCard

        # main thread
        self.List = contactdb.List
        self.Update = contactdb.Update
        self.StrOfList = contactdb.StrOfList
        self.ObjOfList = contactdb.ObjOfList
        self.findSender = contactdb.FindSender
        self.firstFetch = contactdb.FirstFetch
        self.Delete = contactdb.db.Delete
        self.Modify = contactdb.db.Modify

        # child thread 1
        self.poll = session.Copy().Poll

        # child thread 2
        self.termForver = QTermServer(self.conf.termServerPort).Run
Esempio n. 2
0
def QLogin(qq=None, user=None):
    conf = QConf(qq, user)
    conf.Display()

    if conf.qq:
        INFO('开始自动登录...')
        picklePath = conf.PicklePath()
        session = QSession()
        contactdb = QContactDB(session)
        try:
            contactdb.Restore(picklePath)
        except Exception as e:
            WARN('自动登录失败,原因:%s', e)
        else:
            INFO('成功从文件 "%s" 中恢复登录信息' % picklePath)

            try:
                contactdb.session.TestLogin()
            except RequestError:
                WARN('自动登录失败,原因:上次保存的登录信息已过期')
            except Exception as e:
                WARN('自动登录失败,原因:%s', e)
                DEBUG('', exc_info=True)
            else:
                return contactdb.session.Copy(), contactdb, conf

    INFO('开始手动登录...')
    session = QSession()
    session.Login(conf)
    contactdb = QContactDB(session, conf.PicklePath())
    contactdb.Dump()
    return session.Copy(), contactdb, conf
Esempio n. 3
0
def runBot(argv):
    # argv -1, sub program call
    if sys.argv[-1] == '--subprocessCall':
        # Remove the last item
        sys.argv.pop()
        try:
            # Bot login, run
            # QQBot is defined below.
            bot = QQBot._bot
            bot.Login(argv)
            bot.Run()
        finally:
            # Finally, always run, no matter what
            # If bot has conf, bot conf storeQQ
            if hasattr(bot, 'conf'):
                bot.conf.StoreQQ()
    else:
        # Not sub program called
        conf = QConf()

        # QQ run in background
        if conf.daemon:
            conf.Daemonize()

        if sys.argv[0].endswith('py') or sys.argv[0].endswith('pyc'):
            args = [sys.executable] + sys.argv
        else:
            args = sys.argv

        args = args + ['--mailAuthCode', conf.mailAuthCode]
        args = args + ['--qq', conf.qq]
        args = args + ['--subprocessCall']

        while True:
            p = subprocess.Popen(args)
            code = p.wait()
            if code == 0:
                INFO('QQBot 正常停止')
                sys.exit(code)
            elif code == RESTART:
                args[-2] = conf.LoadQQ()
                INFO('5 秒后重新启动 QQBot (自动登陆,qq=%s)', args[-2])
                time.sleep(5)
            elif code == FRESH_RESTART:
                args[-2] = ''
                INFO('5 秒后重新启动 QQBot (手工登陆)')
                time.sleep(5)
            else:
                CRITICAL('QQBOT 异常停止(code=%s)', code)
                if conf.restartOnOffline:
                    args[-2] = conf.LoadQQ()
                    INFO('15秒后重新启动 QQBot (自动登陆,qq=%s)', args[-2])
                    time.sleep(15)
                else:
                    sys.exit(code)
Esempio n. 4
0
    def init(self, argv):
        for name, slots in self.slotsTable.items():
            setattr(self, name, self.wrap(slots))

        self.conf = QConf(argv)
        self.conf.Display()

        for pluginName in self.conf.plugins:
            self.Plug(pluginName)

        self.onInit()
Esempio n. 5
0
    def __init__(self, qq=None, user=None, conf=None, ai=None):
        MessageFactory.__init__(self)
        self.conf = conf if conf else QConf(qq, user)
        ai = ai if ai else BasicAI()
        termServer = QTermServer(self.conf.termServerPort)

        self.On('qqmessage', ai.OnQQMessage)  # main thread
        self.On('polltimeout', ai.OnPollTimeout)  # main thread
        self.On('termmessage', ai.OnTermMessage)  # main thread
        self.On('pollcomplete', QQBot.onPollComplete)  # main thread

        self.AddGenerator(self.pollForever)  # child thread 1
        self.AddGenerator(termServer.Run)  # child thread 2
Esempio n. 6
0
def runBot(botCls, qq, user):
    if sys.argv[-1] == '--subprocessCall':
        isSubprocessCall = True
        sys.argv.pop()
    else:
        isSubprocessCall = False

    if isSubprocessCall:
        bot = botCls()
        try:
            bot.Login(qq, user)
            bot.Run()
        finally:
            if hasattr(bot, 'conf'):
                bot.conf.StoreQQ()
    else:
        conf = QConf(qq, user)

        if sys.argv[0].endswith('py') or sys.argv[0].endswith('pyc'):
            args = [sys.executable] + sys.argv
        else:
            args = sys.argv

        args = args + ['--mailAuthCode', conf.mailAuthCode]
        args = args + ['--qq', conf.qq]
        args = args + ['--subprocessCall']

        while True:
            p = subprocess.Popen(args)
            pid = p.pid
            code = p.wait()
            qq = conf.LoadQQ(pid)
            if code == 0:
                INFO('QQBot 正常停止')
                sys.exit(code)
            elif code == RESTART:
                args[-2] = qq
                INFO('5 秒后重新启动 QQBot (自动登陆,qq=%s)', args[-2])
                time.sleep(5)
            elif code == FRESH_RESTART:
                args[-2] = ''
                INFO('5 秒后重新启动 QQBot (手工登陆)')
                time.sleep(5)
            else:
                CRITICAL('QQBOT 异常停止(code=%s)', code)
                if conf.restartOnOffline:
                    args[-2] = qq
                    INFO('15秒后重新启动 QQBot (自动登陆,qq=%s)', args[-2])
                    time.sleep(15)
                else:
                    sys.exit(code)
Esempio n. 7
0
def QLogin(qq=None, user=None, conf=None):
    if not conf:
        conf = QConf(qq, user)

    conf.Display()

    if conf.qq:
        INFO('开始自动登录...')
        picklePath = conf.PicklePath()
        session = QSession()
        try:
            with open(picklePath, 'rb') as f:
                session.__dict__ = pickle.load(f)
            session.dbname = conf.absPath(session.dbbasename)
        except Exception as e:
            WARN('自动登录失败,原因:%s', e)
        else:
            INFO('成功从文件 "%s" 中恢复登录信息' % SYSTEMSTR2STR(picklePath))

            try:
                session.TestLogin()
            except RequestError:
                WARN('自动登录失败,原因:上次保存的登录信息已过期')
            except Exception as e:
                WARN('自动登录失败,原因:%s', e)
                DEBUG('', exc_info=True)
            else:
                return session, QContactDB(session), conf

            if os.path.exists(session.dbname):
                try:
                    os.remove(session.dbname)
                except OSError:
                    pass
                except:
                    WARN('', exc_info=True)

    INFO('开始手动登录...')
    session = QSession()
    session.Login(conf)
    picklePath = conf.PicklePath()
    try:
        with open(picklePath, 'wb') as f:
            pickle.dump((session.__dict__), f)
    except Exception as e:
        WARN('保存登录信息及联系人失败:%s %s', (e, SYSTEMSTR2STR(picklePath)))
    else:
        INFO('登录信息已保存至: %s' % SYSTEMSTR2STR(picklePath))

    return session, QContactDB(session), conf
Esempio n. 8
0
def QLogin(qq=None, user=None):
    conf = QConf(qq, user)
    conf.Display()

    if conf.qq:
        INFO('开始自动登录...')
        picklePath = conf.PicklePath()
        session = QSession()
        try:
            with open(picklePath, 'rb') as f:
                session.__dict__ = pickle.load(f)
        except Exception as e:
            WARN('自动登录失败,原因:%s', e)
        else:
            INFO('成功从文件 "%s" 中恢复登录信息' % picklePath)

            try:
                session.TestLogin()
            except RequestError:
                WARN('自动登录失败,原因:上次保存的登录信息已过期')
            except Exception as e:
                WARN('自动登录失败,原因:%s', e)
                DEBUG('', exc_info=True)
            else:
                return session, QContactDB(session), conf

            try:
                os.remove(session.dbname)
            except:
                pass

    INFO('开始手动登录...')
    session = QSession()
    session.Login(conf)
    picklePath = conf.PicklePath()
    try:
        with open(picklePath, 'wb') as f:
            pickle.dump((session.__dict__), f)
    except Exception as e:
        WARN('保存登录信息及联系人失败:%s %s', (e, picklePath))
    else:
        INFO('登录信息已保存至文件:file://%s' % picklePath)

    return session, QContactDB(session), conf
Esempio n. 9
0
def runBot(botCls, qq, user):
    if sys.argv[-1] == '--subprocessCall':
        isSubprocessCall = True
        sys.argv.pop()
    else:
        isSubprocessCall = False

    if isSubprocessCall:
        bot = botCls()
        bot.Login(qq, user)
        bot.Run()
    else:
        conf = QConf(qq, user)

        if sys.argv[0].endswith('py') or sys.argv[0].endswith('pyc'):
            args = [sys.executable] + sys.argv
        else:
            args = sys.argv

        args = args + ['--mailAuthCode', conf.mailAuthCode]
        args = args + ['--qq', conf.qq]
        args = args + ['--subprocessCall']

        while True:
            code = subprocess.call(args)
            if code == 0:
                INFO('QQBot 正常停止')
                sys.exit(code)
            elif code == RESTART:
                args[-2] = conf.LoadQQ()
                INFO('5 秒后重新启动 QQBot (自动登陆)')
                time.sleep(5)
            elif code == FRESH_RESTART:
                args[-2] = ''
                INFO('5 秒后重新启动 QQBot (手工登陆)')
                time.sleep(5)
            else:
                CRITICAL('QQBOT 异常停止(code=%s)', code)
                if conf.restartOnOffline:
                    args[-2] = conf.LoadQQ()
                    INFO('30秒后重新启动 QQBot (自动登陆)')
                    time.sleep(30)
                else:
                    sys.exit(code)
Esempio n. 10
0
    INFO('正在获取 %s ...', rname)
    try:
        if ttype == 'buddy':
            table = fetchBuddyTable(self)
        elif ttype == 'group':
            table = fetchGroupTable(self)
        elif ttype == 'discuss':
            table = fetchDiscussTable(self)
        elif ttype == 'group-member':
            table = fetchGroupMemberTable(self, tinfo)
        else:
            table = fetchDiscussMemberTable(self, tinfo)
    except RequestError:
        table = None
    except:
        ERROR('', exc_info=True)
        table = None

    if table is None:
        ERROR('获取 %s 失败', rname)

    return table


if __name__ == '__main__':
    from qqbot.qconf import QConf
    from qqbot.basicqsession import BasicQSession

    self = BasicQSession()
    self.Login(QConf())
Esempio n. 11
0
                    pass
                except:
                    WARN('', exc_info=True)

    INFO('开始手动登录...')
    session = QSession()
    session.Login(conf)
    picklePath = conf.PicklePath()
    try:
        with open(picklePath, 'wb') as f:
            pickle.dump((session.__dict__), f)
    except Exception as e:
        WARN('保存登录信息及联系人失败:%s %s', (e, SYSTEMSTR2STR(picklePath)))
    else:
        INFO('登录信息已保存至:%s' % SYSTEMSTR2STR(picklePath))

    return session, QContactDB(session)


class QSession(BasicQSession, GroupManagerSession):
    pass


if __name__ == '__main__':
    from qqbot.qconf import QConf
    conf = QConf(['-q', '1261422618'])
    conf.Display()
    session, contactdb = QLogin(conf)
    self = session
    c = contactdb.List('buddy', 'Eva')[0]
Esempio n. 12
0
            email_id = id_list[i]
        except IndexError:
            return None
        data = conn.fetch(email_id, '(BODY.PEEK[HEADER.FIELDS (SUBJECT)])')[1]
        if not PY3:
            msg = message_from_string(data[0][1])
            s, encoding = decode_header(msg['Subject'])[0]
            subject = s.decode(encoding or 'utf-8').encode('utf-8')
        else:
            msg = message_from_bytes(data[0][1])
            s, encoding = decode_header(msg['Subject'])[0]
            subject = s if type(s) is str else s.decode(encoding or 'utf-8')
        return subject

if __name__ == '__main__':
    from qqbot.qconf import QConf

    conf = QConf(['-u', 'xxx'])
    conf.Display()

    ma = MailAgent(conf.mailAccount, conf.mailAuthCode)

    with ma.SMTP() as s:
        s.send(conf.mailAccount, 'hello', 'faf房间多啦')
    print('send ok')

    with ma.IMAP() as i:
        subject = i.getSubject(-1)
        print('latest email: ' + str(subject))
    print('recv ok')
Esempio n. 13
0
        if ttype == 'buddy':
            table = fetchBuddyTable(self)
        elif ttype == 'group':
            table = fetchGroupTable(self)
        elif ttype == 'discuss':
            table = fetchDiscussTable(self)
        elif ttype == 'group-member':
            table = fetchGroupMemberTable(self, tinfo)
        else:
            table = fetchDiscussMemberTable(self, tinfo)
    except RequestError:
        table = None
    except:
        ERROR('', exc_info=True)
        table = None

    if table is None:
        ERROR('获取 %s 失败', rname)

    return table


if __name__ == '__main__':
    from qqbot.qconf import QConf
    from qqbot.basicqsession import BasicQSession

    self = BasicQSession()
    conf = QConf(['-q', '158297369'])
    conf.Display()
    self.Login(conf)