Changeset 95

Show
Ignore:
Timestamp:
06/19/07 17:05:57 (2 years ago)
Author:
David Rousselie <dax@happycoders.org>
Message:

Add Extended Stanza Addressing support

Add 'replyto' address in messages sent by JMC

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/jmc/jabber/component.py

    r94 r95  
    2929 
    3030from pyxmpp.message import Message 
     31from pyxmpp.jid import JID 
    3132 
    3233from jcl.model.account import Account, PresenceAccount, LegacyJID 
     
    157158                        while mail_index is not None: 
    158159                            (body, email_from) = _account.get_mail(mail_index) 
    159                             result.append((default_lang_class.new_mail_subject\ 
     160                            result.append((email_from, 
     161                                           default_lang_class.new_mail_subject\ 
    160162                                               % (email_from), 
    161163                                           body)) 
     
    173175                            new_mail_count += 1 
    174176                        if body != "": 
    175                             result.append((default_lang_class.new_digest_subject\ 
     177                            result.append((None, 
     178                                           default_lang_class.new_digest_subject\ 
    176179                                               % (new_mail_count), 
    177180                                           body)) 
     
    192195        return result 
    193196 
    194 class MailSender(MessageSender, HeadlineSender): 
     197class MailSender(HeadlineSender): 
    195198    """Send emails messages to jabber users""" 
    196199 
    197     def send(self, to_account, subject, body): 
     200    def send(self, to_account, data): 
     201        """Call MessageSender send method""" 
     202        MessageSender.send(self, to_account, data) 
     203         
     204    def create_message(self, to_account, data): 
    198205        """Send given emails (in data) as Jabber messages""" 
     206        email_from, subject, body = data 
    199207        if to_account.action == MailAccount.RETRIEVE: 
    200             MessageSender.send(self, to_account, subject, body) 
     208            message = MessageSender.create_message(self, to_account, 
     209                                                   (subject, body)) 
     210            msg_node = message.get_node() 
     211            addresses_node = msg_node.newChild(None, "addresses", None) 
     212            address_ns = addresses_node.newNs("http://jabber.org/protocol/address", None) 
     213            addresses_node.setNs(address_ns) 
     214            replyto_address_node = addresses_node.newChild(address_ns, "address", None) 
     215            replyto_address_node.setProp("type", "replyto") 
     216            replyto_jid = email_from.replace('@', '%', 1) + "@" \ 
     217                          + unicode(JID(to_account.jid).domain) 
     218            replyto_address_node.setProp("jid", replyto_jid) 
    201219        elif to_account.action == MailAccount.DIGEST: 
    202             HeadlineSender.send(self, to_account, subject, body) 
     220            message = HeadlineSender.create_message(self, to_account, 
     221                                                    subject, body) 
     222        else: 
     223            message = None 
     224        return message 
    203225 
    204226class MailHandler(Handler): 
  • src/jmc/jabber/tests/component.py

    r94 r95  
    4343    RootSendMailMessageHandler, MailHandler, MailSubscribeHandler, \ 
    4444    MailUnsubscribeHandler, NoAccountError, MailFeederHandler, \ 
    45     MailPresenceHandler, MailAccountManager 
     45    MailPresenceHandler, MailAccountManager, MailSender 
    4646from jmc.lang import Lang 
    4747 
     
    146146    def __init__(self): 
    147147        self.default_from = "user1@test.com" 
    148        
     148 
    149149    def create_email(self, from_email, to_email, subject, body): 
    150150        return (from_email, to_email, subject, body) 
    151               
     151 
    152152    def send_email(self, email): 
    153153        self.email = email 
     
    340340    def test_feed_retrieve_mail(self): 
    341341        def mock_get_mail(index): 
    342             return [("body1", "from1@test.com"), \ 
     342            return [("body1", "from1@test.com"), 
    343343                    ("body2", "from2@test.com")][index] 
    344344        account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) 
    345         account11 = MockIMAPAccount(user_jid = "test1@test.com", \ 
    346                                     name = "account11", \ 
    347                                     jid = "account11@jmc.test.com") 
     345        account11 = MockIMAPAccount(user_jid="test1@test.com", 
     346                                    name="account11", 
     347                                    jid="account11@jmc.test.com") 
    348348        account11._action = MailAccount.RETRIEVE 
    349349        account11.status = account.ONLINE 
     
    361361        self.assertEquals(len(self.comp.stream.sent), 0) 
    362362        self.assertEquals(len(result), 2) 
    363         self.assertEquals(result[0][0], \ 
     363        self.assertEquals(result[0][0], 
     364                          "from1@test.com") 
     365        self.assertEquals(result[0][1], 
    364366                          account11.default_lang_class.new_mail_subject \ 
    365367                          % ("from1@test.com")) 
    366         self.assertEquals(result[0][1], "body1") 
    367         self.assertEquals(result[1][0], \ 
     368        self.assertEquals(result[0][2], "body1") 
     369        self.assertEquals(result[1][0], 
     370                          "from2@test.com") 
     371        self.assertEquals(result[1][1], \ 
    368372                          account11.default_lang_class.new_mail_subject \ 
    369373                          % ("from2@test.com")) 
    370         self.assertEquals(result[1][1], "body2") 
     374        self.assertEquals(result[1][2], "body2") 
    371375        del account.hub.threadConnection 
    372376 
     
    415419        self.assertEquals(len(self.comp.stream.sent), 0) 
    416420        self.assertEquals(len(result), 1) 
    417         self.assertEquals(result[0][0], \ 
     421        self.assertEquals(result[0][1], \ 
    418422                          account11.default_lang_class.new_digest_subject \ 
    419423                          % (2)) 
    420         self.assertEquals(result[0][1], \ 
     424        self.assertEquals(result[0][2], \ 
    421425                          "body1\n----------------------------------\nbody2\n----------------------------------\n") 
    422426        del account.hub.threadConnection 
     
    677681        self.assertEquals(result[0].get_body(), 
    678682                          Lang.en.send_mail_error_no_to_header_body) 
     683 
     684class MailSender_TestCase(unittest.TestCase): 
     685    def setUp(self): 
     686        if os.path.exists(DB_PATH): 
     687            os.unlink(DB_PATH) 
     688        account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) 
     689        Account.createTable(ifNotExists=True) 
     690        PresenceAccount.createTable(ifNotExists=True) 
     691        MailAccount.createTable(ifNotExists=True) 
     692        IMAPAccount.createTable(ifNotExists=True) 
     693        POP3Account.createTable(ifNotExists=True) 
     694        del account.hub.threadConnection 
     695 
     696    def tearDown(self): 
     697        account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) 
     698        POP3Account.dropTable(ifExists=True) 
     699        IMAPAccount.dropTable(ifExists=True) 
     700        MailAccount.dropTable(ifExists=True) 
     701        PresenceAccount.dropTable(ifExists=True) 
     702        Account.dropTable(ifExists=True) 
     703        del TheURIOpener.cachedURIs['sqlite://' + DB_URL] 
     704        account.hub.threadConnection.close() 
     705        del account.hub.threadConnection 
     706        if os.path.exists(DB_PATH): 
     707            os.unlink(DB_PATH) 
     708 
     709    def test_create_message(self): 
     710        mail_sender = MailSender() 
     711        account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) 
     712        account11 = IMAPAccount(user_jid="test1@test.com", 
     713                                name="account11", 
     714                                jid="account11@jmc.test.com") 
     715        account11.online_action = MailAccount.RETRIEVE 
     716        account11.status = account.ONLINE 
     717        message = mail_sender.create_message(account11, ("from@test.com", 
     718                                                         "subject", 
     719                                                         "message body")) 
     720        self.assertEquals(message.get_to(), account11.user_jid) 
     721        del account.hub.threadConnection 
     722        self.assertEquals(message.get_subject(), "subject") 
     723        self.assertEquals(message.get_body(), "message body") 
     724        addresses = message.xpath_eval("add:addresses/add:address", 
     725                                       {"add": "http://jabber.org/protocol/address"}) 
     726        self.assertEquals(len(addresses), 1) 
     727        self.assertEquals(addresses[0].prop("type"), 
     728                          "replyto") 
     729        self.assertEquals(addresses[0].prop("jid"), 
     730                          "from%test.com@jmc.test.com") 
    679731 
    680732class MailHandler_TestCase(unittest.TestCase): 
     
    928980    suite.addTest(unittest.makeSuite(MailAccountManager_TestCase, 'test')) 
    929981    suite.addTest(unittest.makeSuite(RootSendMailMessageHandler_TestCase, 'test')) 
     982    suite.addTest(unittest.makeSuite(MailSender_TestCase, 'test')) 
    930983    suite.addTest(unittest.makeSuite(MailHandler_TestCase, 'test')) 
    931984    suite.addTest(unittest.makeSuite(MailUnsubscribeHandler_TestCase, 'test'))