Changeset 134

Show
Ignore:
Timestamp:
03/06/08 08:34:19 (10 months ago)
Author:
David Rousselie <dax@happycoders.org>
Message:

get-email ad-hoc command basic behavior

Files:

Legend:

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

    r130 r134  
    2525 
    2626from pyxmpp.jabber.dataforms import Form 
     27 
    2728import jcl.model.account as account 
     29import jcl.jabber.command as command 
     30from jcl.jabber.command import JCLCommandManager 
    2831 
    2932from jmc.model.account import MailAccount 
    30 from jcl.jabber.command import JCLCommandManager 
    31 import jcl.jabber.command as command 
     33from jmc.jabber.feeder import MailSender 
    3234 
    3335class MailCommandManager(JCLCommandManager): 
     
    4749        #self.commands["jmc#retrieve-attachment"] = (False, command.account_node_re) 
    4850        self.commands["jmc#force-check"] = (False, re.compile(".*")) 
     51        self.commands["jmc#get-email"] = (False, command.account_node_re) 
    4952 
    5053    # Delayed to JMC 0.3.1 
     
    8588        self.add_actions(command_node, [command.ACTION_COMPLETE]) 
    8689        session_context["user_jids"] = [unicode(info_query.get_from().bare())] 
    87         return (self.add_form_select_accounts(session_context, command_node, 
    88                                               lang_class, "TODO:TITLE", 
    89                                               "TODO:DESC", format_as_xml=True, 
    90                                               show_user_jid=False), []) 
     90        return (self.add_form_select_accounts(\ 
     91                session_context, command_node, lang_class, 
     92                lang_class.command_force_check, 
     93                lang_class.command_force_check_1_description, 
     94                format_as_xml=True, show_user_jid=False), []) 
    9195 
    9296    def execute_force_check_1(self, info_query, session_context, 
     
    118122 
    119123    execute_force_check_2 = execute_force_check_1 
     124 
     125    def execute_get_email_1(self, info_query, session_context, 
     126                            command_node, lang_class): 
     127        self.__logger.debug("Executing command 'get-email' step 1") 
     128        self.add_actions(command_node, [command.ACTION_COMPLETE]) 
     129        bare_from_jid = info_query.get_from().bare() 
     130        account_name = info_query.get_to().node 
     131        _account = account.get_account(bare_from_jid, account_name) 
     132        if _account is not None: 
     133            result_form = Form(\ 
     134                xmlnode_or_type="form", 
     135                title=lang_class.command_get_email, 
     136                instructions=lang_class.command_get_email_1_description) 
     137            email_list = _account.get_mail_list_summary() 
     138            field = result_form.add_field(name="emails", 
     139                                          field_type="list-multi", 
     140                                          label=lang_class.field_email_subject) 
     141            for (email_index, email_subject) in email_list: 
     142                field.add_option(label=email_subject, values=[email_index]) 
     143            result_form.add_field(name="fetch_more", 
     144                                  field_type="boolean", 
     145                                  label=lang_class.field_select_more_emails) 
     146            result_form.as_xml(command_node) 
     147            return (result_form, []) 
     148        else: 
     149            # TODO Error 
     150            return (None, []) 
     151 
     152    def execute_get_email_2(self, info_query, session_context, 
     153                            command_node, lang_class): 
     154        self.__logger.debug("Executing command 'get-email' step 2") 
     155        result = [] 
     156        mail_sender = MailSender(self.component) 
     157        bare_from_jid = info_query.get_from().bare() 
     158        account_name = info_query.get_to().node 
     159        _account = account.get_account(bare_from_jid, account_name) 
     160        if _account is not None: 
     161            for email_index in session_context["emails"]: 
     162                (email_body, email_from) = _account.get_mail(email_index) 
     163                result.append(\ 
     164                    mail_sender.create_full_email_message(\ 
     165                        email_from, 
     166                        lang_class.mail_subject % (email_from), 
     167                        email_body, 
     168                        _account)) 
     169            result_form = Form(\ 
     170                xmlnode_or_type="form", 
     171                title=lang_class.command_get_email, 
     172                instructions=lang_class.command_get_email_2_description \ 
     173                    % (len(session_context["emails"]))) 
     174            result_form.as_xml(command_node) 
     175        command_node.setProp("status", command.STATUS_COMPLETED) 
     176        return (None, result) 
  • src/jmc/jabber/component.py

    r132 r134  
    2222## 
    2323 
    24 import logging 
    25  
    26 from pyxmpp.jid import JID 
    27  
    2824from sqlobject.sqlbuilder import AND 
    2925 
     
    3127import jcl.model as model 
    3228from jcl.model import account 
    33 from jcl.model.account import Account, User, PresenceAccount 
     29from jcl.model.account import Account, User 
    3430from jcl.jabber.disco import  \ 
    3531     AccountTypeDiscoGetInfoHandler, AccountDiscoGetInfoHandler 
    36 from jcl.jabber.feeder import FeederComponent, Feeder, MessageSender, \ 
    37     HeadlineSender, FeederHandler 
     32from jcl.jabber.feeder import FeederComponent 
    3833from jcl.jabber.command import CommandRootDiscoGetInfoHandler 
    3934from jcl.jabber.component import AccountManager 
     35from jmc.model.account import IMAPAccount, POP3Account, \ 
     36    SMTPAccount 
    4037 
    4138from jmc.jabber.disco import MailRootDiscoGetInfoHandler, \ 
     
    4643from jmc.jabber.presence import MailSubscribeHandler, \ 
    4744     MailUnsubscribeHandler, MailPresenceHandler 
    48 from jmc.model.account import MailAccount, IMAPAccount, POP3Account, \ 
    49     SMTPAccount 
    5045from jmc.lang import Lang 
    5146from jmc.jabber.command import MailCommandManager 
     47from jmc.jabber.feeder import MailFeederHandler, MailFeeder, MailSender 
    5248 
    5349class MailAccountManager(AccountManager): 
     
    157153            lang_class = self.lang.get_default_lang_class() 
    158154        self.handler.handle(None, lang_class, accounts) 
    159  
    160 class MailFeeder(Feeder): 
    161     """Email check""" 
    162  
    163     def __init__(self, component): 
    164         """MailFeeder constructor""" 
    165         Feeder.__init__(self, component) 
    166         self.__logger = logging.getLogger("jmc.jabber.component.MailFeeder") 
    167  
    168     def initialize_live_email(self, _account): 
    169         """For live email checking account, mark emails received while 
    170         offline as read. 
    171         Return a boolean to continue mail checking or not 
    172         (if waiting for password). 
    173         """ 
    174         if _account.password is None: 
    175             if not _account.waiting_password_reply: 
    176                 account_manager = self.component.account_manager 
    177                 self.component.send_stanzas(\ 
    178                     account_manager.ask_password(_account, 
    179                                                  _account.default_lang_class)) 
    180             return False 
    181         try: 
    182             _account.connect() 
    183             _account.mark_all_as_read() 
    184             _account.disconnect() 
    185             _account.first_check = False 
    186             _account.error = None 
    187             return True 
    188         except Exception, e: 
    189             if _account.connected: 
    190                 try: 
    191                     _account.disconnect() 
    192                 except: 
    193                     # We have done everything we could 
    194                     _account.connected = False 
    195             self.component.send_error(_account, e) 
    196             return False 
    197  
    198     def feed(self, _account): 
    199         """Check for new emails for given MailAccount and return a list of 
    200         those emails or a summary. 
    201         """ 
    202         self.__logger.debug("MailFeeder.feed") 
    203         result = [] 
    204         if _account.first_check and _account.live_email_only: 
    205             continue_checking = self.initialize_live_email(_account) 
    206             if not continue_checking: 
    207                 return result 
    208         _account.lastcheck += 1 
    209         if _account.lastcheck == _account.interval: 
    210             _account.lastcheck = 0 
    211             action = _account.action 
    212             if action != PresenceAccount.DO_NOTHING: 
    213                 try: 
    214                     if _account.password is None: 
    215                         account_manager = self.component.account_manager 
    216                         self.component.send_stanzas(\ 
    217                             account_manager.ask_password(_account, 
    218                                                          _account.default_lang_class)) 
    219                         return result 
    220                     self.__logger.debug("Checking " + _account.name) 
    221                     self.__logger.debug("\t" + _account.login \ 
    222                                             + "@" + _account.host) 
    223                     _account.connect() 
    224                     mail_list = _account.get_new_mail_list() 
    225                     default_lang_class = _account.default_lang_class 
    226                     if action == MailAccount.RETRIEVE: 
    227                         # TODO : use generator (yield) 
    228                         mail_index = _account.get_next_mail_index(mail_list) 
    229                         while mail_index is not None: 
    230                             (body, email_from) = _account.get_mail(mail_index) 
    231                             result.append((email_from, 
    232                                            default_lang_class.new_mail_subject\ 
    233                                                % (email_from), 
    234                                            body)) 
    235                             mail_index = _account.get_next_mail_index(mail_list) 
    236                     elif action == MailAccount.DIGEST: 
    237                         body = "" 
    238                         new_mail_count = 0 
    239                         mail_index = _account.get_next_mail_index(mail_list) 
    240                         while mail_index is not None: 
    241                             (tmp_body, from_email) = \ 
    242                                        _account.get_mail_summary(mail_index) 
    243                             body += tmp_body 
    244                             body += "\n----------------------------------\n" 
    245                             mail_index = _account.get_next_mail_index(mail_list) 
    246                             new_mail_count += 1 
    247                         if body != "": 
    248                             result.append((None, 
    249                                            default_lang_class.new_digest_subject\ 
    250                                                % (new_mail_count), 
    251                                            body)) 
    252                     else: 
    253                         raise Exception("Unkown action: " + str(action) \ 
    254                                             + "\nPlease reconfigure account.") 
    255                     _account.disconnect() 
    256                     _account.error = None 
    257                     self.__logger.debug("\nCHECK_MAIL ends " + _account.jid) 
    258                 except Exception, e: 
    259                     if _account.connected: 
    260                         try: 
    261                             _account.disconnect() 
    262                         except: 
    263                             # We have done everything we could 
    264                             _account.connected = False 
    265                     self.component.send_error(_account, e) 
    266         return result 
    267  
    268 class MailSender(HeadlineSender): 
    269     """Send emails messages to jabber users""" 
    270  
    271     def send(self, to_account, data): 
    272         """Call MessageSender send method""" 
    273         MessageSender.send(self, to_account, data) 
    274          
    275     def create_message(self, to_account, data): 
    276         """Send given emails (in data) as Jabber messages""" 
    277         email_from, subject, body = data 
    278         if to_account.action == MailAccount.RETRIEVE: 
    279             message = MessageSender.create_message(self, to_account, 
    280                                                    (subject, body)) 
    281             msg_node = message.get_node() 
    282             addresses_node = msg_node.newChild(None, "addresses", None) 
    283             address_ns = addresses_node.newNs("http://jabber.org/protocol/address", None) 
    284             addresses_node.setNs(address_ns) 
    285             replyto_address_node = addresses_node.newChild(address_ns, "address", None) 
    286             replyto_address_node.setProp("type", "replyto") 
    287             replyto_jid = email_from.replace('@', '%', 1) + "@" \ 
    288                           + unicode(JID(to_account.jid).domain) 
    289             replyto_address_node.setProp("jid", replyto_jid) 
    290         elif to_account.action == MailAccount.DIGEST: 
    291             message = HeadlineSender.create_message(self, to_account, 
    292                                                     (subject, body)) 
    293         else: 
    294             message = None 
    295         return message 
    296  
    297 class MailFeederHandler(FeederHandler): 
    298     def filter(self, stanza, lang_class): 
    299         """Return only email account type to check mail from 
    300         """ 
    301         accounts = account.get_all_accounts(account_class=MailAccount) 
    302         return accounts 
  • src/jmc/jabber/tests/command.py

    <
    r130 r134  
    2525from ConfigParser import ConfigParser 
    2626import os 
     27import sys 
     28import logging 
    2729 
    2830from pyxmpp.iq import Iq 
    29 from pyxmpp.jabber.dataforms import Form 
     31from pyxmpp.jabber.dataforms import Field 
    3032 
    3133import jcl.tests 
    32 from jcl.jabber.tests.command import JCLCommandManager_TestCase 
     34from jcl.tests import JCLTestCase 
    3335from jcl.jabber.feeder import Feeder 
    34 from jcl.model.account import User 
     36from jcl.model.account import User, Account, PresenceAccount 
     37from jcl.jabber.tests.command import JCLCommandManagerTestCase 
    3538import jcl.jabber.command as command 
    3639 
     
    3841    MailAccount 
    3942from jmc.jabber.component import MailComponent 
    40  
     43from jmc.lang import Lang 
    4144from jmc.jabber.tests.component import MockIMAPAccount 
    42  
    43 class MailCommandManager_TestCase(JCLCommandManager_TestCase): 
    44     def setUp(self): 
    45         JCLCommandManager_TestCase.setUp(self, tables=[POP3Account, IMAPAccount, 
    46                                                        SMTPAccount, MailAccount, 
    47                                                        MockIMAPAccount]) 
     45from jmc.jabber.command import MailCommandManager 
     46 
     47class MailCommandManagerTestCase(JCLCommandManagerTestCase): 
     48    def setUp(self, tables=[]): 
     49        tables += [POP3Account, IMAPAccount, SMTPAccount, MailAccount, 
     50                   MockIMAPAccount, User, Account, PresenceAccount] 
     51        JCLTestCase.setUp(self, tables=tables) 
    4852        self.config_file = tempfile.mktemp(".conf", "jmctest", jcl.tests.DB_DIR) 
    4953        self.config = ConfigParser() 
     
    5660                                  self.config_file) 
    5761        self.comp.set_admins(["admin@test.com"]) 
    58         self.command_manager = command.command_manager 
    59  
    60     def tearDown(self): 
    61         JCLCommandManager_TestCase.tearDown(self) 
    62         if os.path.exists(self.config_file): 
    63             os.unlink(self.config_file) 
    64  
    65 #     def test_execute_retrieve_attachment(self): 
    66 #         self.comp.account_manager.account_classes = (POP3Account, IMAPAccount, 
    67 #                                                      SMTPAccount, MockIMAPAccount) 
    68 #         account1 = MockIMAPAccount(user=User(jid="test1@test.com"), 
    69 #                                    name="account1", 
    70 #                                    jid="account1@" + unicode(self.comp.jid)) 
    71 #         info_query = Iq(stanza_type="set", 
    72 #                         from_jid="test1@test.com", 
    73 #                         to_jid="account1@" + unicode(self.comp.jid)) 
    74 #         command_node = info_query.set_new_content(command.COMMAND_NS, "command") 
    75 #         command_node.setProp("node", "jmc#retrieve-attachment") 
    76 #         result = self.command_manager.apply_command_action(info_query, 
    77 #                                                            "jmc#retrieve-attachment", 
    78 #                                                            "execute") 
    79 #         self.assertNotEquals(result, None) 
    80 #         self.assertEquals(len(result), 1) 
    81 #         print str(result[0].xmlnode) 
    82 #         xml_command = result[0].xpath_eval("c:command", 
    83 #                                            {"c": "http://jabber.org/protocol/commands"})[0] 
    84 #         self.assertEquals(xml_command.prop("status"), "executing") 
    85 #         self.assertNotEquals(xml_command.prop("sessionid"), None) 
    86 #         self._check_actions(result[0], ["next"]) 
    87 #         print str(result[0].xmlnode) 
    88 #         x_data = result[0].xpath_eval("c:command/data:x", 
    89 #                                       {"c": "http://jabber.org/protocol/commands", 
    90 #                                        "data": "jabber:x:data"}) 
    91 #         self.assertEquals(len(x_data), 1) 
    92 #         self.assertEquals(x_data[0].prop("type"), "form") 
    93 #         options = result[0].xpath_eval("c:command/data:x/data:field[1]/data:option", 
    94 #                                        {"c": "http://jabber.org/protocol/commands", 
    95 #                                         "data": "jabber:x:data"}) 
    96 #         self.assertEquals(len(options), 3) 
    97 #         self.assertEquals(options[0].prop("label"), "Next") 
    98 #         self.assertEquals(options[0].children.name, "value") 
    99 #         self.assertEquals(options[0].children.content, "-1") 
    100 #         self.assertEquals(options[1].prop("label"), "mail 1") 
    101 #         self.assertEquals(options[1].children.name, "value") 
    102 #         self.assertEquals(options[1].children.content, "1") 
    103 #         self.assertEquals(options[2].prop("label"), "mail 2") 
    104 #         self.assertEquals(options[2].children.name, "value") 
    105 #         self.assertEquals(options[2].children.content, "2") 
    106  
    107 #         # Delayed to JMC 0.3.1 
    108 #         return 
    109 #         # Second step: TODO 
    110 #         info_query = Iq(stanza_type="set", 
    111 #                         from_jid="admin@test.com", 
    112 #                         to_jid=self.comp.jid) 
    113 #         command_node = info_query.set_new_content(command.COMMAND_NS, "command") 
    114 #         command_node.setProp("node", "http://jabber.org/protocol/admin#add-user") 
    115 #         session_id = xml_command.prop("sessionid") 
    116 #         command_node.setProp("sessionid", session_id) 
    117 #         command_node.setProp("action", "next") 
    118 #         submit_form = Form(xmlnode_or_type="submit") 
    119 #         submit_form.add_field(field_type="list-single", 
    120 #                               name="account_type", 
    121 #                               value="Example") 
    122 #         submit_form.add_field(field_type="jid-single", 
    123 #                               name="user_jid", 
    124 #                               value="user2@test.com") 
    125 #         submit_form.as_xml(command_node) 
    126 #         result = self.command_manager.apply_command_action(info_query, 
    127 #                                                            "http://jabber.org/protocol/admin#add-user", 
    128 #                                                            "next") 
    129 #         self.assertNotEquals(result, None) 
    130 #         self.assertEquals(len(result), 1) 
    131 #         xml_command = result[0].xpath_eval("c:command", 
    132 #                                            {"c": "http://jabber.org/protocol/commands"})[0] 
    133 #         self.assertEquals(xml_command.prop("status"), "executing") 
    134 #         self.assertEquals(xml_command.prop("sessionid"), session_id) 
    135 #         self._check_actions(result[0], ["prev", "complete"], 1) 
    136 #         x_data = result[0].xpath_eval("c:command/data:x", 
    137 #                                       {"c": "http://jabber.org/protocol/commands", 
    138 #                                        "data": "jabber:x:data"}) 
    139 #         self.assertEquals(len(x_data), 1) 
    140 #         self.assertEquals(x_data[0].prop("type"), "form") 
    141 #         fields = result[0].xpath_eval("c:command/data:x/data:field", 
    142 #                                       {"c": "http://jabber.org/protocol/commands", 
    143 #                                        "data": "jabber:x:data"}) 
    144 #         self.assertEquals(len(fields), 6) 
    145 #         context_session = self.command_manager.sessions[session_id][1] 
    146 #         self.assertEquals(context_session["account_type"], ["Example"]) 
    147 #         self.assertEquals(context_session["user_jid"], ["user2@test.com"]) 
    148  
    149 #         # Third step 
    150 #         info_query = Iq(stanza_type="set", 
    151 #                         from_jid="admin@test.com", 
    152 #                         to_jid=self.comp.jid) 
    153 #         command_node = info_query.set_new_content(command.COMMAND_NS, "command") 
    154 #         command_node.setProp("node", "http://jabber.org/protocol/admin#add-user") 
    155 #         command_node.setProp("sessionid", session_id) 
    156 #         command_node.setProp("action", "complete") 
    157 #         submit_form = Form(xmlnode_or_type="submit") 
    158 #         submit_form.add_field(field_type="text-single", 
    159 #                               name="name", 
    160 #                               value="account1") 
    161 #         submit_form.add_field(field_type="text-single", 
    162 #                               name="login", 
    163 #                               value="login1") 
    164 #         submit_form.add_field(field_type="text-private", 
    165 #                               name="password", 
    166 #                               value="pass1") 
    167 #         submit_form.add_field(field_type="boolean", 
    168 #                               name="store_password", 
    169 #                               value="1") 
    170 #         submit_form.add_field(field_type="list-single", 
    171 #                               name="test_enum", 
    172 #                               value="choice2") 
    173 #         submit_form.add_field(field_type="text-single", 
    174 #                               name="test_int", 
    175 #                               value="42") 
    176 #         submit_form.as_xml(command_node) 
    177  
    178 #         result = self.command_manager.apply_command_action(info_query, 
    179 #                                                            "http://jabber.org/protocol/admin#add-user", 
    180 #                                                            "execute") 
    181 #         xml_command = result[0].xpath_eval("c:command", 
    182 #                                            {"c": "http://jabber.org/protocol/commands"})[0] 
    183 #         self.assertEquals(xml_command.prop("status"), "completed") 
    184 #         self.assertEquals(xml_command.prop("sessionid"), session_id) 
    185 #         self._check_actions(result[0]) 
    186  
    187 #         self.assertEquals(context_session["name"], ["account1"]) 
    188 #         self.assertEquals(context_session["login"], ["login1"]) 
    189 #         self.assertEquals(context_session["password"], ["pass1"]) 
    190 #         self.assertEquals(context_session["store_password"], ["1"]) 
    191 #         self.assertEquals(context_session["test_enum"], ["choice2"]) 
    192 #         self.assertEquals(context_session["test_int"], ["42"]) 
    193  
    194 #         model.db_connect() 
    195 #         _account = account.get_account("user2@test.com", 
    196 #                                        "account1") 
    197 #         self.assertNotEquals(_account, None) 
    198 #         self.assertEquals(_account.user.jid, "user2@test.com") 
    199 #         self.assertEquals(_account.name, "account1") 
    200 #         self.assertEquals(_account.jid, "account1@" + unicode(self.comp.jid)) 
    201 #         model.db_disconnect() 
    202  
    203 #         stanza_sent = result 
    204 #         self.assertEquals(len(stanza_sent), 4) 
    205 #         iq_result = stanza_sent[0] 
    206 #         self.assertTrue(isinstance(iq_result, Iq)) 
    207 #         self.assertEquals(iq_result.get_node().prop("type"), "result") 
    208 #         self.assertEquals(iq_result.get_from(), self.comp.jid) 
    209 #         self.assertEquals(iq_result.get_to(), "admin@test.com") 
    210 #         presence_component = stanza_sent[1] 
    211 #         self.assertTrue(isinstance(presence_component, Presence)) 
    212 #         self.assertEquals(presence_component.get_from(), self.comp.jid) 
    213 #         self.assertEquals(presence_component.get_to(), "user2@test.com") 
    214 #         self.assertEquals(presence_component.get_node().prop("type"), 
    215 #                           "subscribe") 
    216 #         message = stanza_sent[2] 
    217 #         self.assertTrue(isinstance(message, Message)) 
    218 #         self.assertEquals(message.get_from(), self.comp.jid) 
    219 #         self.assertEquals(message.get_to(), "user2@test.com") 
    220 #         self.assertEquals(message.get_subject(), 
    221 #                           _account.get_new_message_subject(Lang.en)) 
    222 #         self.assertEquals(message.get_body(), 
    223 #                           _account.get_new_message_body(Lang.en)) 
    224 #         presence_account = stanza_sent[3] 
    225 #         self.assertTrue(isinstance(presence_account, Presence)) 
    226 #         self.assertEquals(presence_account.get_from(), "account1@" + unicode(self.comp.jid)) 
    227 #         self.assertEquals(presence_account.get_to(), "user2@test.com") 
    228 #         self.assertEquals(presence_account.get_node().prop("type"), 
    229 #                           "subscribe") 
    230  
    231     def test_execute_force_check(self): 
     62        self.command_manager = MailCommandManager(self.comp, 
     63                                                  self.comp.account_manager) 
    23264        self.comp.account_manager.account_classes = (POP3Account, IMAPAccount, 
    23365                                                     SMTPAccount, MockIMAPAccount) 
     66        self.user1 = User(jid="test1@test.com") 
     67        self.account11 = MockIMAPAccount(user=self.user1, 
     68                                         name="account11", 
     69                                         jid="account11@" + unicode(self.comp.jid)) 
     70        self.account12 = MockIMAPAccount(user=self.user1, 
     71                                         name="account12", 
     72                                         jid="account12@" + unicode(self.comp.jid)) 
     73        self.user2 = User(jid="test2@test.com") 
     74        self.account21 = MockIMAPAccount(user=self.user2, 
     75                                         name="account21", 
     76                                         jid="account21@" + unicode(self.comp.jid)) 
     77        self.account22 = MockIMAPAccount(user=self.user2, 
     78                                         name="account11", 
     79                                         jid="account11@" + unicode(self.comp.jid)) 
     80        self.user3 = User(jid="test3@test.com") 
     81        self.account31 = MockIMAPAccount(user=self.user3, 
     82                                         name="account31", 
     83                                         jid="account31@" + unicode(self.comp.jid)) 
     84        self.account32 = MockIMAPAccount(user=self.user3, 
     85                                         name="account32", 
     86                                         jid="account32@" + unicode(self.comp.jid)) 
     87        self.info_query = Iq(stanza_type="set", 
     88                             from_jid="admin@test.com", 
     89                             to_jid=self.comp.jid) 
     90        self.command_node = self.info_query.set_new_content(command.COMMAND_NS, 
     91                                                            "command") 
    23492        class MockFeederHandler(Feeder): 
    23593            def __init__(self, component): 
     
    243101 
    244102        self.comp.handler.feeder = MockFeederHandler(self.comp) 
    245         user1 = User(jid="test1@test.com") 
    246         user2 = User(jid="test2@test.com") 
    247         account11 = MockIMAPAccount(user=user1, 
    248                                     name="account11", 
    249                                     jid="account11@" + unicode(self.comp.jid)) 
    250         account12 = MockIMAPAccount(user=user1, 
    251                                     name="account12", 
    252                                     jid="account12@" + unicode(self.comp.jid)) 
    253         account21 = MockIMAPAccount(user=user2, 
    254                                     name="account21", 
    255                                     jid="account21@" + unicode(self.comp.jid)) 
    256         account22 = MockIMAPAccount(user=user2, 
    257                                     name="account11", 
    258                                     jid="account11@" + unicode(self.comp.jid)) 
    259         info_query = Iq(stanza_type="set", 
    260                         from_jid="test1@test.com", 
    261                         to_jid="account11@" + unicode(self.comp.jid)) 
    262         command_node = info_query.set_new_content(command.COMMAND_NS, "command") 
    263         command_node.setProp("node", "jmc#force-check") 
    264         result = self.command_manager.apply_command_action(info_query, 
    265                                                            "jmc#force-check", 
    266                                                            "execute") 
    267         self.assertNotEquals(result, None) 
    268         self.assertEquals(len(result), 1) 
    269         xml_command = result[0].xpath_eval("c:command", 
    270                                            {"c": "http://jabber.org/protocol/commands"})[0] 
    271         self.assertEquals(xml_command.prop("status"), "completed") 
    272         self._check_actions(result[0]) 
     103 
     104    def tearDown(self): 
     105        JCLTestCase.tearDown(self) 
     106        if os.path.exists(self.config_file): 
     107            os.unlink(self.config_file) 
     108 
     109class MailCommandManagerForceCheckCommand_TestCase(MailCommandManagerTestCase): 
     110    """ 
     111    Test 'force-check' ad-hoc command 
     112    """ 
     113 
     114    def setUp(self, tables=[]): 
     115        """ 
     116        Prepare data 
     117        """ 
     118        MailCommandManagerTestCase.setUp(self, tables) 
     119        self.command_node.setProp("node", "jmc#force-check") 
     120 
     121    def test_execute_force_check(self): 
     122        self.info_query.set_from("test1@test.com") 
     123        self.info_query.set_to("account11@" + unicode(self.comp.jid)) 
     124        result = self.command_manager.apply_command_action(\ 
     125            self.info_query, 
     126            "jmc#force-check", 
     127            "execute") 
     128        result_iq = result[0].xmlnode 
     129        result_iq.setNs(None) 
     130        self.assertTrue(jcl.tests.is_xml_equal(\ 
     131                u"<iq from='account11@" + unicode(self.comp.jid) 
     132                + "' to='test1@test.com' type='result'>" 
     133                + "<command xmlns='http://jabber.org/protocol/commands' " 
     134                + "status='completed'>" 
     135                + "</command></iq>", 
     136                result_iq, True, test_sibling=False)) 
    273137        feeder = self.comp.handler.feeder 
    274138        self.assertEquals(len(feeder.checked_accounts), 1) 
    275         self.assertEquals(feeder.checked_accounts[0], account11) 
     139        self.assertEquals(feeder.checked_accounts[0], self.account11) 
    276140 
    277141    def test_execute_force_check_root_node(self): 
    278         self.comp.account_manager.account_classes = (POP3Account, IMAPAccount, 
    279                                                      SMTPAccount, MockIMAPAccount) 
    280         class MockFeederHandler(Feeder): 
    281             def __init__(self, component): 
    282                 Feeder.__init__(self, component) 
    283                 self.checked_accounts = [] 
    284  
    285             def feed(self, _account): 
    286                 self.checked_accounts.append(_account) 
    287                 assert(_account.lastcheck == (_account.interval - 1)) 
    288                 return [] 
    289  
    290         self.comp.handler.feeder = MockFeederHandler(self.comp) 
    291         user1 = User(jid="test1@test.com") 
    292         user2 = User(jid="test2@test.com") 
    293         account11 = MockIMAPAccount(user=user1, 
    294                                     name="account11", 
    295                                     jid="account11@" + unicode(self.comp.jid)) 
    296         account12 = MockIMAPAccount(user=user1, 
    297                                     name="account12", 
    298                                     jid="account12@" + unicode(self.comp.jid)) 
    299         account21 = MockIMAPAccount(user=user2, 
    300                                     name="account21", 
    301                                     jid="account21@" + unicode(self.comp.jid)) 
    302         account22 = MockIMAPAccount(user=user2, 
    303                                     name="account11", 
    304                                     jid="account11@" + unicode(self.comp.jid)) 
    305         info_query = Iq(stanza_type="set", 
    306                         from_jid="test1@test.com", 
    307                         to_jid=self.comp.jid) 
    308         command_node = info_query.set_new_content(command.COMMAND_NS, "command") 
    309         command_node.setProp("node", "jmc#force-check") 
    310         result = self.command_manager.apply_command_action(info_query, 
    311                                                            "jmc#force-check", 
    312                                                            "execute") 
    313         self.assertNotEquals(result, None) 
    314         self.assertEquals(len(result), 1) 
    315         xml_command = result[0].xpath_eval("c:command", 
    316                                            {"c": "http://jabber.org/protocol/commands"})[0] 
    317         self.assertEquals(xml_command.prop("status"), "executing") 
    318         self.assertNotEquals(xml_command.prop("sessionid"), None) 
    319         self._check_actions(result[0], ["complete"]) 
    320         session_id = xml_command.prop("sessionid") 
     142        self.info_query.set_from("test1@test.com") 
     143        result = self.command_manager.apply_command_action(\ 
     144            self.info_query, 
     145            "jmc#force-check", 
     146            "execute") 
     147        result_iq = result[0].xmlnode 
     148        result_iq.setNs(None) 
     149        self.assertTrue(jcl.tests.is_xml_equal(\ 
     150                u"<iq from='jmc.test.com' to='test1@test.com' type='result'>" 
     151                + "<command xmlns='http://jabber.org/protocol/commands'" 
     152                + "status='executing'>" 
     153                + "<actions execute='complete'><complete/></actions>" 
     154                + "<x xmlns='jabber:x:data' type='form'>" 
     155                + "<title>" + Lang.en.command_force_check + "</title>" 
     156                + "<instructions>" + Lang.en.command_force_check_1_description 
     157                + "</instructions>" 
     158                + "<field var='account_names' type='list-multi' label='" 
     159                + Lang.en.field_accounts + "'>" 
     160                + "<option label=\"account11 (IMAP)\">" 
     161                + "<value>account11/test1@test.com</value></option>" 
     162                + "<option label=\"account12 (IMAP)\">" 
     163                + "<value>account12/test1@test.com</value></option>" 
     164                + "<option label=\"account11 (MockIMAP)\">" 
     165                + "<value>account11/test1@test.com</value></option>" 
     166                + "<option label=\"account12 (MockIMAP)\">" 
     167                + "<value>account12/test1@test.com</value></option>" 
     168                + "</field></x></command></iq>", 
     169                result_iq, True)) 
     170        session_id = result_iq.children.prop("sessionid") 
     171        self.assertNotEquals(session_id, None) 
    321172        context_session = self.command_manager.sessions[session_id][1] 
    322173        self.assertEquals(context_session["user_jids"], 
     
    324175 
    325176        # Second step 
    326         info_query = Iq(stanza_type="set", 
    327                         from_jid="admin@test.com", 
    328                         to_jid=self.comp.jid) 
    329         command_node = info_query.set_new_content(command.COMMAND_NS, "command") 
    330         command_node.setProp("node", "jmc#force-check") 
    331         command_node.setProp("sessionid", session_id) 
    332         command_node.setProp("action", "complete") 
    333         submit_form = Form(xmlnode_or_type="submit") 
    334         submit_form.add_field(field_type="list-multi", 
    335                               name="account_names", 
    336                               values=["account11/test1@test.com", 
    337                                       "account12/test1@test.com"]) 
    338         submit_form.as_xml(command_node) 
    339         result = self.command_manager.apply_command_action(info_query, 
    340                                                            "jmc#force-check", 
    341                                                            "execute") 
    342         xml_command = result[0].xpath_eval("c:command", 
    343                                            {"c": "http://jabber.org/protocol/commands"})[0] 
    344         self.assertEquals(xml_command.prop("status"), "completed") 
    345         self.assertEquals(xml_command.prop("sessionid"), session_id) 
    346         self._check_actions(result[0]) 
     177        info_query = self.prepare_submit(\ 
     178            node="jmc#force-check", 
     179            session_id=session_id, 
     180            from_jid="test1@test.com", 
     181            to_jid=unicode(self.comp.jid), 
     182            fields=[Field(field_type="list-multi", 
     183                          name="account_names", 
     184                          values=["account11/test1@test.com", 
     185                                  "account12/test1@test.com"])], 
     186            action="complete") 
     187        result = self.command_manager.apply_command_action(\ 
     188            info_query, 
     189            "jmc#force-check", 
     190            "execute") 
     191        result_iq = result[0].xmlnode 
     192        result_iq.setNs(None) 
     193        self.assertTrue(jcl.tests.is_xml_equal(\ 
     194                u"<iq from='" + unicode(self.comp.jid) 
     195                + "' to='test1@test.com' type='result'>" 
     196                + "<command xmlns='http://jabber.org/protocol/commands' " 
     197                + "status='completed'>" 
     198                + "</command></iq>", 
     199                result_iq, True, test_sibling=False)) 
    347200        self.assertEquals(context_session["account_names"], 
    348201                          ["account11/test1@test.com", 
     
    350203        feeder = self.comp.handler.feeder 
    351204        self.assertEquals(len(feeder.checked_accounts), 2) 
    352         self.assertEquals(feeder.checked_accounts[0], account11) 
    353         self.assertEquals(feeder.checked_accounts[1], account12) 
     205        self.assertEquals(feeder.checked_accounts[0], self.account11) 
     206        self.assertEquals(feeder.checked_accounts[1], self.account12) 
     207 
     208class MailCommandManagerGetEmailCommand_TestCase(MailCommandManagerTestCase): 
     209    """ 
     210    Test 'get-email' ad-hoc command 
     211    """ 
     212 
     213    def setUp(self, tables=[]): 
     214        """ 
     215        Prepare data 
     216        """ 
     217        MailCommandManagerTestCase.setUp(self, tables) 
     218        self.command_node.setProp("node", "jmc#get-email") 
     219        def get_email(email_index): 
     220            """ 
     221            Mock method for IMAPAccount.get_email 
     222            """ 
     223            return ("mail body " + str(email_index), 
     224                    "from" + str(email_index) + "@test.com") 
     225        self.account11.__dict__["get_mail"] = get_email 
     226 
     227    def check_step_1 (self, result): 
     228        """ 
     229        Check first step result of get-email ad-hoc command 
     230        """ 
     231        result_iq = result[0].xmlnode 
     232        result_iq.setNs(None) 
     233        self.assertTrue(jcl.tests.is_xml_equal(\ 
     234                u"<iq from='account11@" + unicode(self.comp.jid)  
     235                + "' to='test1@test.com' type='result'>" 
     236                + "<command xmlns='http://jabber.org/protocol/commands'" 
     237                + "status='executing'>" 
     238                + "<actions execute='complete'><complete/></actions>" 
     239                + "<x xmlns='jabber:x:data' type='form'>" 
     240                + "<title>" + Lang.en.command_get_email + "</title>" 
     241                + "<instructions>" + Lang.en.command_get_email_1_description 
     242                + "</instructions>" 
     243                + "<field var='emails' type='list-multi' label='" 
     244                + Lang.en.field_email_subject + "'>" 
     245                + "<option label=\"mail 1\">" 
     246                + "<value>1</value></option>" 
     247                + "<option label=\"mail 2\">" 
     248                + "<value>2</value></option>" 
     249                + "</field><field var='fetch_more' type='boolean' label='" 
     250                + Lang.en.field_select_more_emails + "'>" 
     251                + "</field></x></command></iq>", 
     252                result_iq, True)) 
     253        session_id = result_iq.children.prop("sessionid") 
     254        self.assertNotEquals(session_id, None) 
     255        return session_id 
     256 
     257    def test_execute_get_email(self): 
     258        """ 
     259        Test single email retrieval 
     260        """ 
     261        self.info_query.set_from("test1@test.com") 
     262        self.info_query.set_to("account11@" + unicode(self.comp.jid)) 
     263        result = self.command_manager.apply_command_action(\ 
     264            self.info_query, 
     265            "jmc#get-email", 
     266            "execute") 
     267        session_id = self.check_step_1(result) 
     268 
     269        # Second step 
     270        info_query = self.prepare_submit(\ 
     271            node="jmc#get-email", 
     272            session_id=session_id, 
     273            from_jid="test1@test.com", 
     274            to_jid="account11@jmc.test.com", 
     275            fields=[Field(field_type="list-multi", 
     276                          name="emails", 
     277                          values=["1"]), 
     278                    Field(field_type="boolean", 
     279                          name="fetch_more", 
     280                          value=False)], 
     281            action="complete") 
     282        result = self.command_manager.apply_command_action(\