def execute_force_check_1(self, info_query, session_context, command_node, lang_class): self.__logger.debug("Executing command 'force-check' step 1") bare_from_jid = info_query.get_from().bare() account_name = info_query.get_to().node accounts = [] if account_name is None: if session_context.has_key("account_names"): for account_name in session_context["account_names"]: name, user_jid = self.get_name_and_jid(account_name) _account = account.get_account(user_jid, name) _account.lastcheck = int(time.time()) \ - (_account.interval * self.component.time_unit) accounts.append(_account) else: return self.execute_force_check_root_node(info_query, session_context, command_node, lang_class) else: _account = account.get_account(bare_from_jid, account_name) if _account is not None: _account.lastcheck = int(time.time()) \ - (_account.interval * self.component.time_unit) accounts.append(_account) command_node.setProp("status", command.STATUS_COMPLETED) self.component.check_email_accounts(accounts, lang_class) return (None, [])
def execute_retrieve_attachment_1(self, info_query, session_context, command_node, lang_class): self.__logger.debug("Executing command 'retrieve-attachment' step 1") self.add_actions(command_node, [command.ACTION_NEXT]) bare_from_jid = info_query.get_from().bare() account_name = info_query.get_to().node _account = account.get_account(bare_from_jid, account_name, MailAccount) if _account is not None: result_form = Form(xmlnode_or_type="form", title="TITLE", instructions="INS") field = result_form.add_field(name="attachments", field_type="list-multi", label="select attachments") field.add_option(label="Next", value="-1") for (mail_id, mail_title) in _account.get_mail_with_attachment_list(): field.add_option(label=mail_title, value=mail_id) result_form.as_xml(command_node) return (result_form, []) else: # ERROR return (None, [])
def execute_get_email_last(self, info_query, session_context, command_node, lang_class): self.__logger.debug("Executing command 'get-email' last step") result = [] mail_sender = MailSender(self.component) bare_from_jid = info_query.get_from().bare() account_name = info_query.get_to().node _account = account.get_account(bare_from_jid, account_name) if _account is not None: _account.connect() for email_index in session_context["emails"]: (email_body, email_from) = _account.get_mail(email_index) result.append(\ mail_sender.create_full_email_message(\ email_from, lang_class.mail_subject % (email_from), email_body, _account)) _account.disconnect() result_form = Form(\ xmlnode_or_type="form", title=lang_class.command_get_email, instructions=lang_class.command_get_email_2_description \ % (len(session_context["emails"]))) result_form.as_xml(command_node) command_node.setProp("status", command.STATUS_COMPLETED) return (None, result)
def get_account_filter(self, stanza, lang_class, node=None): """Filter stanzas sent to account jid, only if account exists""" name = stanza.get_to().node if name is not None: return account.get_account(unicode(stanza.get_from().bare()), name) else: return None
def test_get_account(self): user1 = User(jid="*****@*****.**") ExampleAccount(user=user1, name="account11", jid="*****@*****.**") ExampleAccount(user=user1, name="account12", jid="*****@*****.**") ExampleAccount(user=User(jid="*****@*****.**"), name="account11", jid="*****@*****.**") _account = account.get_account("*****@*****.**", "account11") self.assertEquals(_account.user.jid, "*****@*****.**") self.assertEquals(_account.name, "account11")
def filter(self, stanza, lang_class): """ Return the uniq account associated with a name and user JID. DB connection might already be opened. """ _account = account.get_account(unicode(stanza.get_from().bare()), stanza.get_to().node) if ( hasattr(_account, "password") and hasattr(_account, "waiting_password_reply") and (getattr(_account, "waiting_password_reply") == True) and self.password_regexp.search(stanza.get_subject()) is not None ): return _account else: return None
def execute_get_email(self, info_query, session_context, command_node, lang_class): self.__logger.debug("Executing command 'get-email' step 1") if "fetch_more" in session_context \ and session_context["fetch_more"][-1] == "0": return self.execute_get_email_last(info_query, session_context, command_node, lang_class) else: self.add_actions(command_node, [command.ACTION_COMPLETE]) bare_from_jid = info_query.get_from().bare() account_name = info_query.get_to().node _account = account.get_account(bare_from_jid, account_name) if _account is not None: result_form = Form(\ xmlnode_or_type="form", title=lang_class.command_get_email, instructions=lang_class.command_get_email_1_description) if not "start_index" in session_context: session_context["start_index"] = 1 self.__logger.debug("Checking email list summary from index " + str(session_context["start_index"]) + " to " + str(session_context["start_index"] + 10)) _account.connect() email_list = _account.get_mail_list_summary(\ start_index=session_context["start_index"], end_index=session_context["start_index"] + 9) _account.disconnect() session_context["start_index"] += 10 field = result_form.add_field(name="emails", field_type="list-multi", label=lang_class.field_email_subject) for (email_index, email_subject) in email_list: field.add_option(label=email_subject, value=email_index) if len(email_list) == 10: result_form.add_field(name="fetch_more", field_type="boolean", label=lang_class.field_select_more_emails) else: session_context["fetch_more"] = ["0"] result_form.as_xml(command_node) return (result_form, []) else: raise CommandError("item-not-found")
def account_get_register(self, info_query, name, from_jid, account_type, lang_class): """ Handle get_register on an IMAP account. Return a preinitialized form. account_type contains 'account_type + imap_dir' """ splitted_node = account_type.split("/") splitted_node_len = len(splitted_node) if splitted_node_len == 1 or splitted_node[0] != "IMAP": return AccountManager.account_get_register(self, info_query, name, from_jid, account_type, lang_class) else: info_query = info_query.make_result_response() model.db_connect() imap_dir = "/".join(splitted_node[1:]) bare_from_jid = from_jid.bare() _account = account.get_account_filter( AND( AND(User.q.jid == unicode(bare_from_jid), Account.q.userID == User.q.id), IMAPAccount.q.mailbox == imap_dir, ), account_class=IMAPAccount, ) query = info_query.new_query("jabber:iq:register") if _account is not None: # update account result = self.generate_registration_form_init(lang_class, _account) else: _account = account.get_account(bare_from_jid, name, IMAPAccount) if _account is not None: # create new account based on current one result = self.generate_registration_form_init(lang_class, _account) result["name"].value = None result["name"].type = "text-single" result["mailbox"].value = imap_dir.replace("/", _account.delimiter) else: # create new account from scratch result = self.generate_registration_form(lang_class, IMAPAccount, bare_from_jid) result["mailbox"].value = imap_dir result.as_xml(query) return [info_query]