Implement XEP-012 (Last Activity) for Account JIDs

darcs-hash:20080529165258-86b55-19507eed98d1d6214dedaacc620da54bff1acf37.gz
This commit is contained in:
David Rousselie
2008-05-29 18:52:58 +02:00
parent 697e47e2dc
commit b6c899846a
5 changed files with 92 additions and 9 deletions

View File

@@ -54,6 +54,7 @@ class MailFeeder(Feeder):
(if waiting for password). (if waiting for password).
""" """
if _account.password is None: if _account.password is None:
_account.first_check = True
if not _account.waiting_password_reply: if not _account.waiting_password_reply:
account_manager = self.component.account_manager account_manager = self.component.account_manager
self.component.send_stanzas(\ self.component.send_stanzas(\
@@ -82,9 +83,9 @@ class MailFeeder(Feeder):
"""Check for new emails for given MailAccount and return a list of """Check for new emails for given MailAccount and return a list of
those emails or a summary. those emails or a summary.
""" """
self.__logger.debug("MailFeeder.feed")
result = [] result = []
if _account.first_check: if _account.first_check:
_account.first_check = False
_account.lastcheck = int(time.time()) _account.lastcheck = int(time.time())
if _account.live_email_only: if _account.live_email_only:
continue_checking = self.initialize_live_email(_account) continue_checking = self.initialize_live_email(_account)

View File

@@ -1,34 +1,51 @@
# -*- coding: utf-8 -*-
## ##
## presence.py ## presence.py
## Login : David Rousselie <dax@happycoders.org> ## Login : David Rousselie <dax@happycoders.org>
## Started on Wed Jun 27 22:05:08 2007 David Rousselie ## Started on Wed Jun 27 22:05:08 2007 David Rousselie
## $Id$ ## $Id$
## ##
## Copyright (C) 2007 David Rousselie ## Copyright (C) 2007 David Rousselie
## This program is free software; you can redistribute it and/or modify ## 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 ## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or ## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version. ## (at your option) any later version.
## ##
## This program is distributed in the hope that it will be useful, ## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of ## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details. ## GNU General Public License for more details.
## ##
## You should have received a copy of the GNU General Public License ## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software ## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ##
import re import re
import time
from jcl.jabber.presence import DefaultPresenceHandler, \ from jcl.jabber.presence import DefaultPresenceHandler, \
DefaultSubscribeHandler, DefaultUnsubscribeHandler DefaultSubscribeHandler, DefaultUnsubscribeHandler
from jcl.model.account import LegacyJID from jcl.model.account import LegacyJID
import jcl.jabber as jabber
from jcl.jabber import Handler from jcl.jabber import Handler
from jmc.jabber import MailHandler 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): class MailPresenceHandler(DefaultPresenceHandler):
"""Define filter for legacy JIDs presence handling""" """Define filter for legacy JIDs presence handling"""

View File

@@ -215,6 +215,30 @@ class MailComponent_TestCase(JCLTestCase):
########################################################################### ###########################################################################
# 'feed' test methods # '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): def test_feed_live_email_init_no_password(self):
account11 = MockIMAPAccount(user=User(jid="test1@test.com"), account11 = MockIMAPAccount(user=User(jid="test1@test.com"),
name="account11", name="account11",

View File

@@ -1,30 +1,72 @@
# -*- coding: utf-8 -*-
## ##
## presence.py ## presence.py
## Login : <dax@happycoders.org> ## Login : <dax@happycoders.org>
## Started on Thu Dec 6 08:19:59 2007 David Rousselie ## Started on Thu Dec 6 08:19:59 2007 David Rousselie
## $Id$ ## $Id$
## ##
## Copyright (C) 2007 David Rousselie ## Copyright (C) 2007 David Rousselie
## This program is free software; you can redistribute it and/or modify ## 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 ## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or ## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version. ## (at your option) any later version.
## ##
## This program is distributed in the hope that it will be useful, ## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of ## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details. ## GNU General Public License for more details.
## ##
## You should have received a copy of the GNU General Public License ## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software ## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ##
import unittest 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(): def suite():
test_suite = unittest.TestSuite() test_suite = unittest.TestSuite()
#test_suite.addTest(unittest.makeSuite(_TestCase, 'test')) test_suite.addTest(unittest.makeSuite(MailAccountIQLastHandler_TestCase, 'test'))
return test_suite return test_suite
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -248,7 +248,6 @@ class MailAccount(PresenceAccount):
def set_status(self, status): def set_status(self, status):
"""Set current Jabber status""" """Set current Jabber status"""
if status != account.OFFLINE and self._status == account.OFFLINE: if status != account.OFFLINE and self._status == account.OFFLINE:
PresenceAccount.set_status(self, status) PresenceAccount.set_status(self, status)
self.first_check = True self.first_check = True