+class CheckMail:
+ implements(imap4.IMailboxListener)
+
+ def __init__(self):
+ print('[CheckMail] __init__')
+ createFactory(self, config.plugins.emailimap.username.value, config.plugins.emailimap.server.value, config.plugins.emailimap.port.value)
+ self._timer = eTimer()
+ # self._timer.timeout.get().append(self._checkMail)
+ self._timer.callback.append(self._checkMail)
+ self._timer.start(config.plugins.emailimap.checkPeriod.value)
+ self._unseenList = None
+ self._proto = None
+
+ def exit(self):
+ if self._proto:
+ self._proto.logout()
+ self._proto = None
+ self._timer.stop()
+
+ def _checkMail(self):
+ print('[CheckMail] _checkMail ')
+ if self._proto:
+ self._proto.search(imap4.Query(unseen=1)).addCallback(self._cbNotify).addErrback(self._ebNotify, "search")
+
+ def _cbNotify(self, newUnseenList):
+ def haveSeenBefore(messageNo): return messageNo not in self._unseenList
+
+ print("[CheckMail] _cbNotify newUnseenList: %s" %repr(newUnseenList))
+ if self._unseenList is None:
+ Notifications.AddNotification(MessageBox, str(len(newUnseenList)) + ' ' + _("unread messages in mailbox"), type=MessageBox.TYPE_INFO, timeout=config.plugins.emailimap.timeout.value)
+ else:
+ newMessages = filter(haveSeenBefore, newUnseenList)
+ if newMessages:
+ print("[CheckMail] _cbNotify newMessages: %s" %repr(newMessages))
+ newMessageSet = imap4.MessageSet()
+ for messageNo in newMessages:
+ newMessageSet.add(messageNo)
+ self._proto.fetchHeaders(newMessageSet).addCallback(self._onHeaderList).addErrback(self._ebNotify, "fetchHeaders")
+ self._unseenList = newUnseenList
+
+ def _onHeaderList(self, headers):
+ # print("[CheckMail] _onHeaderList headers: %s" %repr(headers))
+ message = _("New mail arrived:\n\n")
+ for h in headers:
+ m = MessageHeader(h, headers[h]['RFC822.HEADER'])
+ message += m.getSenderString() + '\n' + m.getSubject() + '\n\n'
+ Notifications.AddNotification(MessageBox, message, type=MessageBox.TYPE_INFO, timeout=config.plugins.emailimap.timeout.value)
+
+ def _ebNotify(self, result, where):
+ print("[CheckMail] _ebNotify result: %s" %(result.getErrorMessage()))
+ Notifications.AddNotification(MessageBox, _("In") + " " + where + ": " +result.getErrorMessage() + '\n' + _("mail check process stopped"), type=MessageBox.TYPE_ERROR, timeout=config.plugins.emailimap.timeout.value)
+ self.exit()
+
+ def _cbOk(self, result):
+ print("[CheckMail] _cbOk result: %s" %repr(result))
+
+ def onConnect(self, proto=None):
+ print('[CheckMail] onConnect ')
+ if not proto:
+ proto = self._proto
+ else:
+ self._proto = proto
+ proto.getCapabilities().addCallback(self._cbCapabilities).addErrback(self._ebNotify)
+
+ def onConnectFailed(self, reason):
+ print('[CheckMail] onConnectFailed: ' + reason.getErrorMessage())
+ self._ebNotify(reason, "onConnectFailed")
+
+ def _cbCapabilities(self,reason):
+ print "#"*30
+ print "# If you have problems to log into your imap-server, please send me the output of the following line"
+ print "# cbCapabilities",reason
+ print "#"*30
+ self._doLogin()
+
+ def _doLogin(self):
+ useTLS = False #True
+ if useTLS:
+ #d = self._proto.startTLS().addCallback(self._proto.authenticate, config.plugins.emailimap.password.value)
+ d = self._proto.startTLS().addCallback(self._proto.authenticate) # don't know, why authenticate wants no param...
+ else:
+ d = self._proto.authenticate(config.plugins.emailimap.password.value)
+ d.addCallback(self._onAuthentication).addErrback(self._onAuthenticationFailed)
+
+ def _onAuthentication(self, result):
+ print("[CheckMail] onAuthentication: logged in")
+ self._proto.examine('inbox').addCallback(self._cbOk).addErrback(self._ebNotify, "examine")
+ self._checkMail()
+
+ def _onAuthenticationFailed(self, failure):
+ # If it failed because no SASL mechanisms match
+ print("[CheckMail] onAuthenticationFailed: " + failure.getErrorMessage() + ' ' + str(self._proto))
+ try:
+ failure.trap(imap4.NoSupportedAuthentication)
+ self._doLoginInsecure()
+ except Exception,e:
+ print e,e.message
+
+ def _doLoginInsecure(self):
+ print("[CheckMail] doLoginInsecure")
+ self._proto.login(config.plugins.emailimap.username.value, config.plugins.emailimap.password.value
+ ).addCallback(self._onAuthentication).addErrback(self._ebNotify, "login")
+
+mailChecker = None
+def autostart(reason, **kwargs):
+ # ouch, this is a hack
+ #===========================================================================
+ # if kwargs.has_key("session"):
+ # global my_global_session
+ # my_global_session = kwargs["session"]
+ # return
+ #===========================================================================
+ print("[EmailClient] - Autostart")
+ global mailChecker
+ if config.plugins.emailimap.checkForNewMails.value and not mailChecker:
+ mailChecker = CheckMail()
+
+