예제 #1
0
class QrcodeManager(object):
    def __init__(self, conf):
        qrcodeId = uuid.uuid4().hex
        self.qrcodePath = conf.QrcodePath(qrcodeId)

        if conf.mailAccount:
            self.mailAgent = MailAgent(conf.mailAccount,
                                       conf.mailAuthCode,
                                       name='QQBot管理员')

            html = ('<p>您的 QQBot 正在登录,请尽快用手机 QQ 扫描下面的二维码。'
                    '若二维码已过期,请将本邮件设为已读邮件,之后 QQBot 会在'
                    '1~2分钟内将最新的二维码发送到本邮箱。</p>'
                    '<p>{{png}}</p>')

            html += '<p>conf.user=%r, conf.qq=%r</p>' % (conf.user, conf.qq)

            self.qrcodeMail = {
                'to_addr': conf.mailAccount,
                'html': html,
                'subject': ('%s[%s]' % ('QQBot二维码', qrcodeId)),
                'to_name': '我'
            }

            self.qrcode = LockedValue(None)

        else:
            self.mailAgent = None

        # self.cmdQrcode = conf.cmdQrcode

    def Show(self, qrcode):
        with open(self.qrcodePath, 'wb') as f:
            f.write(qrcode)

        from qqbot import _bot
        if hasattr(_bot, 'onQrcode'):
            _bot.onQrcode(self.qrcodePath, qrcode)

        if self.mailAgent:
            if self.qrcode.getVal() is None:
                self.qrcode.setVal(qrcode)
                # first show, start a thread to send emails
                StartDaemonThread(self.sendEmail)
            else:
                self.qrcode.setVal(qrcode)

    def sendEmail(self):
        lastSubject = ''
        while True:
            if lastSubject != self.qrcodeMail['subject']:
                qrcode = self.qrcode.getVal()
                if qrcode is None:
                    break
                qrcode = '' if self.qrcodeServer else qrcode
                try:
                    with self.mailAgent.SMTP() as smtp:
                        smtp.send(png_content=qrcode, **self.qrcodeMail)
                except Exception as e:
                    WARN('无法将二维码发送至邮箱%s %s',
                         self.mailAgent.account,
                         e,
                         exc_info=True)
                else:
                    INFO('已将二维码发送至邮箱%s', self.mailAgent.account)
                    if self.qrcodeServer:
                        break
                    else:
                        lastSubject = self.qrcodeMail['subject']
            else:
                time.sleep(65)
                qrcode = self.qrcode.getVal()
                if qrcode is None:
                    break
                try:
                    DEBUG('开始查询邮箱 %s 中的最近的邮件', self.mailAgent.account)
                    with self.mailAgent.IMAP() as imap:
                        lastSubject = imap.getSubject(-1)
                except Exception as e:
                    WARN('查询邮箱 %s 中的邮件失败 %s', self.mailAgent.account, e)
                else:
                    DEBUG('最近的邮件: %s', lastSubject)

    def Destroy(self):
        if self.mailAgent:
            self.qrcode.setVal(None)

        try:
            os.remove(self.qrcodePath)
        except OSError:
            pass
예제 #2
0
class QrcodeManager:
    def __init__(self, conf):
        qrcodeId = uuid.uuid4().hex
        self.qrcodePath = conf.QrcodePath(qrcodeId)
        if conf.httpServerName:
            self.qrcodeServer = QrcodeServer(conf.httpServerName,
                                             conf.httpServerPort,
                                             conf.QrcodePath)
            self.qrcodeURL = self.qrcodeServer.QrcodeURL(qrcodeId)
        else:
            self.qrcodeServer = None

        if conf.mailAccount:
            self.mailAgent = MailAgent(conf.mailAccount,
                                       conf.mailAuthCode,
                                       name='QQBot管理员')
            if self.qrcodeServer:
                html = ('<p>您的 QQBot 正在登录,请尽快用手机 QQ 扫描下面的二维码。'
                        '若二维码已过期,请重新打开本邮件。若您看不到二维码图片,请确保'
                        '图片地址 <a href="{0}">{0}</a> 可以通过公网访问。</p>'
                        '<p><img src="{0}"></p>').format(self.qrcodeURL)
            else:
                html = ('<p>您的 QQBot 正在登录,请尽快用手机 QQ 扫描下面的二维码。'
                        '若二维码已过期,请将本邮件删除,删除后 QQBot 会在几分钟后将'
                        '最新的二维码发送到本邮箱。</p>'
                        '<p>{{png}}</p>')
            self.qrcodeMail = {
                'to_addr': conf.mailAccount,
                'html': html,
                'subject': ('%s[%s]' % ('QQBot二维码', qrcodeId)),
                'png_content': '',
                'to_name': '我'
            }
            self.hasSent = False
        else:
            self.mailAgent = None

    def Show(self, qrcode):
        with open(self.qrcodePath, 'wb') as f:
            f.write(qrcode)

        if self.qrcodeServer is None and self.mailAgent is None:
            try:
                showImage(self.qrcodePath)
            except Exception as e:
                WARN('弹出二维码失败(%s),请手动打开 file://%s', e, self.qrcodePath)

        if self.qrcodeServer:
            INFO('请使用浏览器访问二维码,图片地址: %s', self.qrcodeURL)

        if self.mailAgent:
            if not self.hasSent:
                needSend = True
                self.hasSent = True
            elif self.qrcodeServer:
                needSend = False
            else:
                try:
                    with self.mailAgent.IMAP() as imap:
                        last_subject = imap.getSubject(-1)
                except Exception as e:
                    WARN('查询 %s 中的邮件失败(%s)', self.mailAgent.account, e)
                    needSend = True
                else:
                    needSend = (last_subject != self.qrcodeMail['subject'])
                    if not needSend:
                        INFO('最近发送的二维码邮件尚未被删除,因此不再重复发送')

            if needSend:
                if not self.qrcodeServer:
                    self.qrcodeMail['png_content'] = qrcode
                try:
                    with self.mailAgent.SMTP() as smtp:
                        smtp.send(**self.qrcodeMail)
                except Exception as e:
                    WARN('无法将二维码发送至邮箱%s(%s)', self.mailAgent.account, e)
                else:
                    INFO('已将二维码发送至邮箱%s', self.mailAgent.account)

    def DelPng(self):
        self.hasSent = False
        try:
            os.remove(self.qrcodePath)
        except OSError:
            pass

    def Destroy(self):
        self.qrcodeServer and self.qrcodeServer.Join()
예제 #3
0
class QrcodeManager:
    def __init__(self, conf):
        qrcodeId = uuid.uuid4().hex
        self.qrcodePath = conf.QrcodePath(qrcodeId)
        if conf.httpServerIP:
            self.qrcodeServer = QrcodeServer(conf.httpServerIP,
                                             conf.httpServerPort,
                                             os.path.dirname(self.qrcodePath))
            self.qrcodeURL = self.qrcodeServer.qrcodeURL
        else:
            self.qrcodeServer = None

        if conf.mailAccount:
            self.mailAgent = MailAgent(conf.mailAccount,
                                       conf.mailAuthCode,
                                       name='QQBot管理员')

            if self.qrcodeServer:
                html = ('<p>您的 QQBot 正在登录,请尽快用手机 QQ 扫描下面的二维码。'
                        '若二维码已过期,请重新打开本邮件。若您看不到二维码图片,请确保'
                        '图片地址 <a href="{0}">{0}</a> 可以通过公网访问。</p>'
                        '<p><img src="{0}"></p>').format(self.qrcodeURL)
            else:
                html = ('<p>您的 QQBot 正在登录,请尽快用手机 QQ 扫描下面的二维码。'
                        '若二维码已过期,请将本邮件删除,删除后 QQBot 会在1~3分钟内'
                        '将最新的二维码发送到本邮箱。</p>'
                        '<p>{{png}}</p>')

            self.qrcodeMail = {
                'to_addr': conf.mailAccount,
                'html': html,
                'subject': ('%s[%s]' % ('QQBot二维码', qrcodeId)),
                'to_name': '我'
            }

            self.qrcode = LockedValue(None)

        else:
            self.mailAgent = None

    def Show(self, qrcode):
        with open(self.qrcodePath, 'wb') as f:
            f.write(qrcode)

        try:
            showImage(self.qrcodePath)
        except Exception as e:
            WARN('无法弹出二维码图片 file://%s 。%s', self.qrcodePath, e)

        if self.qrcodeServer:
            INFO('请使用浏览器访问二维码,图片地址: %s', self.qrcodeURL)

        if self.mailAgent:
            if self.qrcode.getVal() is None:
                self.qrcode.setVal(qrcode)
                StartThread(self.sendEmail, daemon=True)
            else:
                self.qrcode.setVal(qrcode)

    def sendEmail(self):
        lastSubject = ''
        while True:
            qrcode = self.qrcode.getVal()

            if qrcode is None:
                break

            if lastSubject != self.qrcodeMail['subject']:
                qrcode = '' if self.qrcodeServer else qrcode
                try:
                    with self.mailAgent.SMTP() as smtp:
                        smtp.send(png_content=qrcode, **self.qrcodeMail)
                except Exception as e:
                    WARN('无法将二维码发送至邮箱%s %s', self.mailAgent.account, e)
                    time.sleep(10)
                else:
                    INFO('已将二维码发送至邮箱%s', self.mailAgent.account)
                    if self.qrcodeServer:
                        break
                    else:
                        lastSubject = self.qrcodeMail['subject']
            else:
                time.sleep(20)
                try:
                    INFO('开始查询邮箱 %s 中的最近的邮件', self.mailAgent.account)
                    with self.mailAgent.IMAP() as imap:
                        lastSubject = imap.getSubject(-1)
                except Exception as e:
                    WARN('查询邮箱 %s 中的邮件失败 %s', self.mailAgent.account, e)
                else:
                    INFO('最近的邮件: %s', lastSubject)

    def Destroy(self):
        if self.mailAgent:
            self.qrcode.setVal(None)

        try:
            os.remove(self.qrcodePath)
        except OSError:
            pass
예제 #4
0
class QrcodeManager:
    def __init__(self, conf):
        qrcodeId = uuid.uuid4().hex
        if conf.mailAccount:
            self.mailAgent = MailAgent(conf.mailAccount,
                                       conf.mailAuthCode,
                                       name='QQBot管理员')
            self.qrcodeMail = {
                'to_addr':
                conf.mailAccount,
                'html': ('<p>您的 QQBot 正在登录,请尽快用手机 QQ 扫描下面的二维码。'
                         '若您用手机 QQ 客户端打开此邮件,请直接长按二维码后扫码。若二'
                         '维码已过期,请将本邮件设为已读或删除,之后 QQBot 会将最新的'
                         '二维码发送到本邮箱。</p>'
                         '<p>{{png}}</p>'),
                'subject': ('%s[%s]' % ('QQBot二维码', qrcodeId)),
                'to_name':
                '我'
            }
            self.hasSent = False
        else:
            self.mailAgent = None
            self.qrcodePath = conf.QrcodePath(qrcodeId)

    def Show(self, qrcode):
        if self.mailAgent is None:
            with open(self.qrcodePath, 'wb') as f:
                f.write(qrcode)
            try:
                showImage(self.qrcodePath)
            except Exception as e:
                WARN('弹出二维码失败 %s,请手动打开 file://%s', e, self.qrcodePath)
        else:
            if not self.hasSent:
                needSend = True
                self.hasSent = True
            else:
                try:
                    with self.mailAgent.IMAP() as imap:
                        last_subject = imap.getUnSeenSubject(-1)[0]
                except Exception as e:
                    WARN('查询 %s 中的邮件失败 %s', self.mailAgent.account, e)
                    needSend = True
                else:
                    needSend = (last_subject != self.qrcodeMail['subject'])

            if needSend:
                try:
                    with self.mailAgent.SMTP() as smtp:
                        smtp.send(png_content=qrcode, **self.qrcodeMail)
                except Exception as e:
                    WARN('无法将二维码发送至邮箱%s(%s)', self.mailAgent.account, e)
                else:
                    INFO('已将二维码发送至邮箱%s', self.mailAgent.account)

    def Clear(self):
        self.hasSent = False
        if not self.mailAgent:
            try:
                os.remove(self.qrcodePath)
            except OSError:
                pass
        else:
            t = threading.Thread(target=self.delMail)
            t.daemon = True
            t.start()

    def delMail(self):
        try:
            time.sleep(1)
            with self.mailAgent.IMAP() as imap:
                imap.delMail(subject=self.qrcodeMail['subject'])
        except Exception as e:
            WARN('删除二维码邮件失败 %s', e)