diff --git a/client/conversation.py b/client/conversation.py index ae1a514..50f7ed8 100644 --- a/client/conversation.py +++ b/client/conversation.py @@ -13,7 +13,7 @@ def __init__(self, persona, mic, profile): self.mic = mic self.profile = profile self.brain = Brain(mic, profile) - self.notifier = Notifier(profile) + self.notifier = Notifier(profile, self.brain) self.wxbot = None def is_proper_time(self): diff --git a/client/notifier.py b/client/notifier.py index 16589eb..d1b1fbd 100644 --- a/client/notifier.py +++ b/client/notifier.py @@ -17,11 +17,12 @@ def __init__(self, gather, timestamp): def run(self): self.timestamp = self.gather(self.timestamp) - def __init__(self, profile): + def __init__(self, profile, brain): self._logger = logging.getLogger(__name__) self.q = Queue.Queue() self.profile = profile self.notifiers = [] + self.brain = brain if 'email' in profile and \ ('enable' not in profile['email'] or profile['email']['enable']): @@ -52,6 +53,10 @@ def styleEmail(e): return subject.replace('[echo]', '') else: return "" + elif Email.isControlEmail(e, self.profile): + self.brain.query([subject.replace('[control]', '') + .strip()], None) + return "" sender = Email.getSender(e) return "您有来自 %s 的新邮件 %s" % (sender, subject) for e in emails: diff --git a/client/plugins/Email.py b/client/plugins/Email.py index 195a9ea..9e4730c 100644 --- a/client/plugins/Email.py +++ b/client/plugins/Email.py @@ -40,6 +40,14 @@ def getSender(msg): return sender +def isSelfEmail(msg, profile): + """ Whether the email is sent by the user """ + fromstr = msg["From"] + addr = (fromstr[fromstr.find('<')+1:fromstr.find('>')]).strip('\"') + address = profile['email']['address'].strip() + return addr == address + + def getSubject(msg, profile): """ Returns the title of an email @@ -57,7 +65,7 @@ def getSubject(msg, profile): return '' if 'read_email_title' in profile: to_read = profile['read_email_title'] - if '[echo]' in sub: + if '[echo]' in sub or '[control]' in sub: return sub if to_read: return '邮件标题为 %s' % sub @@ -83,6 +91,14 @@ def isEchoEmail(msg, profile): return False +def isControlEmail(msg, profile): + """ Whether an email is a control email""" + subject = getSubject(msg, profile) + if '[control]' in subject and isSelfEmail(msg, profile): + return True + return False + + def getDate(email): return parser.parse(email.get('date')) @@ -142,6 +158,7 @@ def fetchUnreadEmails(profile, since=None, markRead=False, limit=None): if isEchoEmail(msg, profile): conn.store(num, '+FLAGS', '\Seen') + conn.close() conn.logout()