From b6c899846a3afec612aac8f5024766bca4b0b6fd Mon Sep 17 00:00:00 2001 From: David Rousselie Date: Thu, 29 May 2008 18:52:58 +0200 Subject: Implement XEP-012 (Last Activity) for Account JIDs darcs-hash:20080529165258-86b55-19507eed98d1d6214dedaacc620da54bff1acf37.gz --- src/jmc/jabber/feeder.py | 3 ++- src/jmc/jabber/presence.py | 23 +++++++++++++++--- src/jmc/jabber/tests/component.py | 24 +++++++++++++++++++ src/jmc/jabber/tests/presence.py | 50 +++++++++++++++++++++++++++++++++++---- src/jmc/model/account.py | 1 - 5 files changed, 92 insertions(+), 9 deletions(-) diff --git a/src/jmc/jabber/feeder.py b/src/jmc/jabber/feeder.py index 8177171..9b2377d 100644 --- a/src/jmc/jabber/feeder.py +++ b/src/jmc/jabber/feeder.py @@ -54,6 +54,7 @@ class MailFeeder(Feeder): (if waiting for password). """ if _account.password is None: + _account.first_check = True if not _account.waiting_password_reply: account_manager = self.component.account_manager self.component.send_stanzas(\ @@ -82,9 +83,9 @@ class MailFeeder(Feeder): """Check for new emails for given MailAccount and return a list of those emails or a summary. """ - self.__logger.debug("MailFeeder.feed") result = [] if _account.first_check: + _account.first_check = False _account.lastcheck = int(time.time()) if _account.live_email_only: continue_checking = self.initialize_live_email(_account) diff --git a/src/jmc/jabber/presence.py b/src/jmc/jabber/presence.py index 6c0cd32..f0d2905 100644 --- a/src/jmc/jabber/presence.py +++ b/src/jmc/jabber/presence.py @@ -1,34 +1,51 @@ +# -*- coding: utf-8 -*- ## ## presence.py ## Login : David Rousselie ## Started on Wed Jun 27 22:05:08 2007 David Rousselie ## $Id$ -## +## ## Copyright (C) 2007 David Rousselie ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## import re +import time from jcl.jabber.presence import DefaultPresenceHandler, \ DefaultSubscribeHandler, DefaultUnsubscribeHandler from jcl.model.account import LegacyJID +import jcl.jabber as jabber from jcl.jabber import Handler from jmc.jabber import MailHandler +class MailAccountIQLastHandler(DefaultPresenceHandler): + """Handle jabber:iq:last request for account JID""" + + filter = jabber.get_account_filter + + def handle(self, stanza, lang_class, data): + """Return same presence as receive one""" + _account = data + result = stanza.make_result_response() + query = result.new_query("jabber:iq:last") + query.setProp("seconds", + unicode(int(time.time()) - _account.lastcheck)) + return [result] + class MailPresenceHandler(DefaultPresenceHandler): """Define filter for legacy JIDs presence handling""" diff --git a/src/jmc/jabber/tests/component.py b/src/jmc/jabber/tests/component.py index a88f63b..051a863 100644 --- a/src/jmc/jabber/tests/component.py +++ b/src/jmc/jabber/tests/component.py @@ -215,6 +215,30 @@ class MailComponent_TestCase(JCLTestCase): ########################################################################### # 'feed' test methods ########################################################################### + def test_feed_first_check(self): + account11 = MockIMAPAccount(user=User(jid="test1@test.com"), + name="account11", + jid="account11@jmc.test.com") + account11.status = account.ONLINE + self.assertTrue(account11.first_check) + self.assertEquals(account11.error, None) + self.assertFalse(account11.waiting_password_reply) + account11.live_email_only = False + account11.lastcheck = 0 + account11.password = "" + result = self.comp.handler.feeder.feed(account11) + self.assertEquals(len(result), 0) + sent = self.comp.stream.sent + self.assertEquals(len(sent), 0) + self.assertFalse(account11.first_check) + self.assertFalse(account11.waiting_password_reply) + self.assertEquals(account11.error, None) + self.assertFalse(account11.connected) + self.assertFalse(account11.has_connected) + self.assertFalse(account11.marked_all_as_read) + self.assertTrue(self._account_has_been_checked(account11, + 0)) + def test_feed_live_email_init_no_password(self): account11 = MockIMAPAccount(user=User(jid="test1@test.com"), name="account11", diff --git a/src/jmc/jabber/tests/presence.py b/src/jmc/jabber/tests/presence.py index c1846eb..e5c9775 100644 --- a/src/jmc/jabber/tests/presence.py +++ b/src/jmc/jabber/tests/presence.py @@ -1,30 +1,72 @@ +# -*- coding: utf-8 -*- ## ## presence.py ## Login : ## Started on Thu Dec 6 08:19:59 2007 David Rousselie ## $Id$ -## +## ## Copyright (C) 2007 David Rousselie ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## import unittest +import time + +from pyxmpp.iq import Iq + +from jcl.model.account import User, LegacyJID, Account +from jcl.tests import JCLTestCase + +from jmc.jabber.component import MailComponent +from jmc.jabber.presence import MailAccountIQLastHandler + +class MailAccountIQLastHandler_TestCase(JCLTestCase): + def setUp(self): + JCLTestCase.setUp(self, tables=[User, LegacyJID, Account]) + self.comp = MailComponent("jmc.test.com", + "password", + "localhost", + "5347", + None, None) + self.handler = MailAccountIQLastHandler(self.comp) + + def test_handle(self): + user1 = User(jid="user1@test.com") + account11 = Account(user=user1, + name="account11", + jid="account11@jcl.test.com") + account12 = Account(user=user1, + name="account12", + jid="account12@jcl.test.com") + info_query = Iq(from_jid="user1@test.com", + to_jid="account11@jcl.test.com", + stanza_type="get") + account11.lastcheck = int(time.time()) + time.sleep(1) + result = self.handler.handle(info_query, None, account11) + self.assertEquals(len(result), 1) + self.assertEquals(result[0].get_to(), "user1@test.com") + self.assertEquals(result[0].get_from(), "account11@jcl.test.com") + self.assertEquals(result[0].get_type(), "result") + self.assertNotEquals(result[0].xmlnode.children, None) + self.assertEquals(result[0].xmlnode.children.name, "query") + self.assertEquals(int(result[0].xmlnode.children.prop("seconds")), 1) def suite(): test_suite = unittest.TestSuite() - #test_suite.addTest(unittest.makeSuite(_TestCase, 'test')) + test_suite.addTest(unittest.makeSuite(MailAccountIQLastHandler_TestCase, 'test')) return test_suite if __name__ == '__main__': diff --git a/src/jmc/model/account.py b/src/jmc/model/account.py index e5590f3..7ea153a 100644 --- a/src/jmc/model/account.py +++ b/src/jmc/model/account.py @@ -248,7 +248,6 @@ class MailAccount(PresenceAccount): def set_status(self, status): """Set current Jabber status""" - if status != account.OFFLINE and self._status == account.OFFLINE: PresenceAccount.set_status(self, status) self.first_check = True -- cgit v1.2.3