Exemple #1
0
    def apply(self, ui):
        if self.envelope == None:
            self.envelope = Envelope()
        if self.template is not None:
            # get location of tempsdir, containing msg templates
            tempdir = settings.config.get("general", "template_dir")
            tempdir = os.path.expanduser(tempdir)
            if not tempdir:
                xdgdir = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
                tempdir = os.path.join(xdgdir, "alot", "templates")

            path = os.path.expanduser(self.template)
            if not os.path.dirname(path):  # use tempsdir
                if not os.path.isdir(tempdir):
                    ui.notify("no templates directory: %s" % tempdir, priority="error")
                    return
                path = os.path.join(tempdir, path)

            if not os.path.isfile(path):
                ui.notify("could not find template: %s" % path, priority="error")
                return
            try:
                self.envelope.parse_template(open(path).read())
            except Exception, e:
                ui.notify(str(e), priority="error")
                return
Exemple #2
0
    def apply(self, ui):
        if self.envelope == None:
            self.envelope = Envelope()
        if self.template is not None:
            #get location of tempsdir, containing msg templates
            tempdir = settings.config.get('general', 'template_dir')
            tempdir = os.path.expanduser(tempdir)
            if not tempdir:
                xdgdir = os.environ.get('XDG_CONFIG_HOME',
                                        os.path.expanduser('~/.config'))
                tempdir = os.path.join(xdgdir, 'alot', 'templates')

            path = os.path.expanduser(self.template)
            if not os.path.dirname(path):  # use tempsdir
                if not os.path.isdir(tempdir):
                    ui.notify('no templates directory: %s' % tempdir,
                              priority='error')
                    return
                path = os.path.join(tempdir, path)

            if not os.path.isfile(path):
                ui.notify('could not find template: %s' % path,
                          priority='error')
                return
            try:
                self.envelope.parse_template(open(path).read())
            except Exception, e:
                ui.notify(str(e), priority='error')
                return
Exemple #3
0
    def apply(self, ui):
        if not self.message:
            self.message = ui.current_buffer.get_selected_message()
        mail = self.message.get_email()

        envelope = Envelope()
        if self.inline:  # inline mode
            # set body text
            name, address = self.message.get_author()
            timestamp = self.message.get_date()
            qf = settings.config.get_hook("forward_prefix")
            if qf:
                quote = qf(
                    name,
                    address,
                    timestamp,
                    ui=ui,
                    dbm=ui.dbman,
                    aman=ui.accountman,
                    log=ui.logger,
                    config=settings.config,
                )
            else:
                quote = "Forwarded message from %s (%s):\n" % (name, timestamp)
            mailcontent = quote
            for line in self.message.accumulate_body().splitlines():
                mailcontent += ">" + line + "\n"

            envelope.body = mailcontent

        else:  # attach original mode
            # attach original msg
            mail.set_default_type("message/rfc822")
            mail["Content-Disposition"] = "attachment"
            envelope.attachments.append(mail)

        # copy subject
        subject = decode_header(mail.get("Subject", ""))
        subject = "Fwd: " + subject
        envelope.add("Subject", subject)

        # set From
        # we look for own addresses in the To,Cc,Ccc headers in that order
        # and use the first match as new From header if there is one.
        my_addresses = ui.accountman.get_addresses()
        matched_address = ""
        in_to = [a for a in my_addresses if a in mail.get("To", "")]
        if in_to:
            matched_address = in_to[0]
        else:
            cc = mail.get("Cc", "") + mail.get("Bcc", "")
            in_cc = [a for a in my_addresses if a in cc]
            if in_cc:
                matched_address = in_cc[0]
        if matched_address:
            account = ui.accountman.get_account_by_address(matched_address)
            fromstring = "%s <%s>" % (account.realname, account.address)
            envelope.add("From", fromstring)
        ui.apply_command(ComposeCommand(envelope=envelope))
Exemple #4
0
    def apply(self, ui):
        if not self.message:
            self.message = ui.current_buffer.get_selected_message()
        mail = self.message.get_email()
        # set body text
        name, address = self.message.get_author()
        timestamp = self.message.get_date()
        mailcontent = self.message.accumulate_body()
        envelope = Envelope(bodytext=mailcontent)

        # copy selected headers
        to_copy = ['Subject', 'From', 'To', 'Cc', 'Bcc', 'In-Reply-To',
                   'References']
        for key in to_copy:
            value = decode_header(mail.get(key, ''))
            if value:
                envelope.add(key, value)

        # copy attachments
        for b in self.message.get_attachments():
            envelope.attach(b)

        ui.apply_command(ComposeCommand(envelope=envelope))
Exemple #5
0
    def apply(self, ui):
        if not self.message:
            self.message = ui.current_buffer.get_selected_message()
        mail = self.message.get_email()

        envelope = Envelope()
        if self.inline:  # inline mode
            # set body text
            name, address = self.message.get_author()
            timestamp = self.message.get_date()
            qf = settings.config.get_hook('forward_prefix')
            if qf:
                quote = qf(name, address, timestamp,
                             ui=ui, dbm=ui.dbman, aman=ui.accountman,
                             config=settings.config)
            else:
                quote = 'Forwarded message from %s (%s):\n' % (name, timestamp)
            mailcontent = quote
            for line in self.message.accumulate_body().splitlines():
                mailcontent += '>' + line + '\n'

            envelope.body = mailcontent

        else:  # attach original mode
            # attach original msg
            mail.set_default_type('message/rfc822')
            mail['Content-Disposition'] = 'attachment'
            envelope.attachments.append(mail)

        # copy subject
        subject = decode_header(mail.get('Subject', ''))
        subject = 'Fwd: ' + subject
        envelope.add('Subject', subject)

        # set From
        # we look for own addresses in the To,Cc,Ccc headers in that order
        # and use the first match as new From header if there is one.
        my_addresses = ui.accountman.get_addresses()
        matched_address = ''
        in_to = [a for a in my_addresses if a in mail.get('To', '')]
        if in_to:
            matched_address = in_to[0]
        else:
            cc = mail.get('Cc', '') + mail.get('Bcc', '')
            in_cc = [a for a in my_addresses if a in cc]
            if in_cc:
                matched_address = in_cc[0]
        if matched_address:
            account = ui.accountman.get_account_by_address(matched_address)
            fromstring = '%s <%s>' % (account.realname, account.address)
            envelope.add('From', fromstring)
        ui.apply_command(ComposeCommand(envelope=envelope))
Exemple #6
0
    def apply(self, ui):
        if not self.message:
            self.message = ui.current_buffer.get_selected_message()
        mail = self.message.get_email()
        # set body text
        name, address = self.message.get_author()
        timestamp = self.message.get_date()
        qf = settings.config.get_hook('reply_prefix')
        if qf:
            quotestring = qf(name, address, timestamp,
                             ui=ui, dbm=ui.dbman, aman=ui.accountman,
                             config=settings.config)
        else:
            quotestring = 'Quoting %s (%s)\n' % (name, timestamp)
        mailcontent = quotestring
        for line in self.message.accumulate_body().splitlines():
            mailcontent += '>' + line + '\n'

        envelope = Envelope(bodytext=mailcontent)

        # copy subject
        subject = decode_header(mail.get('Subject', ''))
        if not subject.startswith('Re:'):
            subject = 'Re: ' + subject
        envelope.add('Subject', subject)

        # set From
        my_addresses = ui.accountman.get_addresses()
        matched_address = ''
        in_to = [a for a in my_addresses if a in mail.get('To', '')]
        if in_to:
            matched_address = in_to[0]
        else:
            cc = mail.get('Cc', '') + mail.get('Bcc', '')
            in_cc = [a for a in my_addresses if a in cc]
            if in_cc:
                matched_address = in_cc[0]
        if matched_address:
            account = ui.accountman.get_account_by_address(matched_address)
            fromstring = '%s <%s>' % (account.realname, account.address)
            envelope.add('From', fromstring)

        # set To
        if self.groupreply:
            cleared = self.clear_my_address(my_addresses, mail.get('To', ''))
            if cleared:
                logging.info(mail['From'] + ', ' + cleared)
                to = mail['From'] + ', ' + cleared
                envelope.add('To', decode_header(to))

            else:
                envelope.add('To', decode_header(mail['From']))
            # copy cc and bcc for group-replies
            if 'Cc' in mail:
                cc = self.clear_my_address(my_addresses, mail['Cc'])
                envelope.add('Cc', decode_header(cc))
            if 'Bcc' in mail:
                bcc = self.clear_my_address(my_addresses, mail['Bcc'])
                envelope.add('Bcc', decode_header(bcc))
        else:
            envelope.add('To', decode_header(mail['From']))

        # set In-Reply-To header
        envelope.add('In-Reply-To', '<%s>' % self.message.get_message_id())

        # set References header
        old_references = mail.get('References', '')
        if old_references:
            old_references = old_references.split()
            references = old_references[-8:]
            if len(old_references) > 8:
                references = old_references[:1] + references
            references.append('<%s>' % self.message.get_message_id())
            envelope.add('References', ' '.join(references))
        else:
            envelope.add('References', '<%s>' % self.message.get_message_id())

        ui.apply_command(ComposeCommand(envelope=envelope))
Exemple #7
0
class ComposeCommand(Command):
    """compose a new email"""

    def __init__(
        self,
        envelope=None,
        headers={},
        template=None,
        sender=u"",
        subject=u"",
        to=[],
        cc=[],
        bcc=[],
        attach=None,
        **kwargs
    ):
        """
        :param envelope: use existing envelope
        :type envelope: :class:`~alot.message.Envelope`
        :param headers: forced header values
        :type header: doct (str->str)
        :param template: name of template to parse into the envelope after
                         creation. This should be the name of a file in your
                         template_dir
        :type template: str
        :param sender: From-header value
        :type sender: str
        :param subject: Subject-header value
        :type subject: str
        :param to: To-header value
        :type to: str
        :param cc: Cc-header value
        :type cc: str
        :param bcc: Bcc-header value
        :type bcc: str
        :param attach: Path to files to be attached (globable)
        :type attach: str
        """

        Command.__init__(self, **kwargs)

        self.envelope = envelope
        self.template = template
        self.headers = headers
        self.sender = sender
        self.subject = subject
        self.to = to
        self.cc = cc
        self.bcc = bcc
        self.attach = attach

    @inlineCallbacks
    def apply(self, ui):
        if self.envelope == None:
            self.envelope = Envelope()
        if self.template is not None:
            # get location of tempsdir, containing msg templates
            tempdir = settings.config.get("general", "template_dir")
            tempdir = os.path.expanduser(tempdir)
            if not tempdir:
                xdgdir = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
                tempdir = os.path.join(xdgdir, "alot", "templates")

            path = os.path.expanduser(self.template)
            if not os.path.dirname(path):  # use tempsdir
                if not os.path.isdir(tempdir):
                    ui.notify("no templates directory: %s" % tempdir, priority="error")
                    return
                path = os.path.join(tempdir, path)

            if not os.path.isfile(path):
                ui.notify("could not find template: %s" % path, priority="error")
                return
            try:
                self.envelope.parse_template(open(path).read())
            except Exception, e:
                ui.notify(str(e), priority="error")
                return

        # set forced headers
        for key, value in self.headers.items():
            self.envelope.add(key, value)

        # set forced headers for separate parameters
        if self.sender:
            self.envelope.add("From", self.sender)
        if self.subject:
            self.envelope.add("Subject", self.subject)
        if self.to:
            self.envelope.add("To", ",".join(self.to))
        if self.cc:
            self.envelope.add("Cc", ",".join(self.cc))
        if self.bcc:
            self.envelope.add("Bcc", ",".join(self.bcc))

        # get missing From header
        if not "From" in self.envelope.headers:
            accounts = ui.accountman.get_accounts()
            if len(accounts) == 1:
                a = accounts[0]
                fromstring = "%s <%s>" % (a.realname, a.address)
                self.envelope.add("From", fromstring)
            else:
                cmpl = AccountCompleter(ui.accountman)
                fromaddress = yield ui.prompt(prefix="From>", completer=cmpl, tab=1)
                if fromaddress is None:
                    ui.notify("canceled")
                    return
                a = ui.accountman.get_account_by_address(fromaddress)
                if a is not None:
                    fromstring = "%s <%s>" % (a.realname, a.address)
                    self.envelope.add("From", fromstring)
                else:
                    self.envelope.add("From", fromaddress)

        # get missing To header
        if "To" not in self.envelope.headers:
            sender = self.envelope.get("From")
            name, addr = email.Utils.parseaddr(sender)
            a = ui.accountman.get_account_by_address(addr)

            allbooks = not settings.config.getboolean("general", "complete_matching_abook_only")
            ui.logger.debug(allbooks)
            abooks = ui.accountman.get_addressbooks(order=[a], append_remaining=allbooks)
            ui.logger.debug(abooks)
            to = yield ui.prompt(prefix="To>", completer=ContactsCompleter(abooks))
            if to == None:
                ui.notify("canceled")
                return
            self.envelope.add("To", to)

        if settings.config.getboolean("general", "ask_subject") and not "Subject" in self.envelope.headers:
            subject = yield ui.prompt(prefix="Subject>")
            ui.logger.debug('SUBJECT: "%s"' % subject)
            if subject == None:
                ui.notify("canceled")
                return
            self.envelope.add("Subject", subject)

        if self.attach:
            for a in self.attach:
                self.envelope.attach(a)

        cmd = commands.envelope.EditCommand(envelope=self.envelope)
        ui.apply_command(cmd)
Exemple #8
0
class ComposeCommand(Command):
    """compose a new email"""
    def __init__(self, envelope=None, headers={}, template=None,
                 sender=u'', subject=u'', to=[], cc=[], bcc=[], attach=None,
                 **kwargs):
        """
        :param envelope: use existing envelope
        :type envelope: :class:`~alot.message.Envelope`
        :param headers: forced header values
        :type header: doct (str->str)
        :param template: name of template to parse into the envelope after
                         creation. This should be the name of a file in your
                         template_dir
        :type template: str
        :param sender: From-header value
        :type sender: str
        :param subject: Subject-header value
        :type subject: str
        :param to: To-header value
        :type to: str
        :param cc: Cc-header value
        :type cc: str
        :param bcc: Bcc-header value
        :type bcc: str
        :param attach: Path to files to be attached (globable)
        :type attach: str
        """

        Command.__init__(self, **kwargs)

        self.envelope = envelope
        self.template = template
        self.headers = headers
        self.sender = sender
        self.subject = subject
        self.to = to
        self.cc = cc
        self.bcc = bcc
        self.attach = attach

    @inlineCallbacks
    def apply(self, ui):
        if self.envelope == None:
            self.envelope = Envelope()
        if self.template is not None:
            #get location of tempsdir, containing msg templates
            tempdir = settings.config.get('general', 'template_dir')
            tempdir = os.path.expanduser(tempdir)
            if not tempdir:
                xdgdir = os.environ.get('XDG_CONFIG_HOME',
                                        os.path.expanduser('~/.config'))
                tempdir = os.path.join(xdgdir, 'alot', 'templates')

            path = os.path.expanduser(self.template)
            if not os.path.dirname(path):  # use tempsdir
                if not os.path.isdir(tempdir):
                    ui.notify('no templates directory: %s' % tempdir,
                              priority='error')
                    return
                path = os.path.join(tempdir, path)

            if not os.path.isfile(path):
                ui.notify('could not find template: %s' % path,
                          priority='error')
                return
            try:
                self.envelope.parse_template(open(path).read())
            except Exception, e:
                ui.notify(str(e), priority='error')
                return

        # set forced headers
        for key, value in self.headers.items():
            self.envelope.add(key, value)

        # set forced headers for separate parameters
        if self.sender:
            self.envelope.add('From', self.sender)
        if self.subject:
            self.envelope.add('Subject', self.subject)
        if self.to:
            self.envelope.add('To', ','.join(self.to))
        if self.cc:
            self.envelope.add('Cc', ','.join(self.cc))
        if self.bcc:
            self.envelope.add('Bcc', ','.join(self.bcc))

        # get missing From header
        if not 'From' in self.envelope.headers:
            accounts = ui.accountman.get_accounts()
            if len(accounts) == 1:
                a = accounts[0]
                fromstring = "%s <%s>" % (a.realname, a.address)
                self.envelope.add('From', fromstring)
            else:
                cmpl = AccountCompleter(ui.accountman)
                fromaddress = yield ui.prompt(prefix='From>', completer=cmpl,
                                              tab=1)
                if fromaddress is None:
                    ui.notify('canceled')
                    return
                a = ui.accountman.get_account_by_address(fromaddress)
                if a is not None:
                    fromstring = "%s <%s>" % (a.realname, a.address)
                    self.envelope.add('From', fromstring)
                else:
                    self.envelope.add('From', fromaddress)

        # add signature
        name, addr = email.Utils.parseaddr(self.envelope['From'])
        account = ui.accountman.get_account_by_address(addr)
        if account is not None:
            if account.signature:
                logging.debug('has signature')
                sig = os.path.expanduser(account.signature)
                if os.path.isfile(sig):
                    logging.debug('is file')
                    if account.signature_as_attachment:
                        name = account.signature_filename or None
                        self.envelope.attach(sig, filename=name)
                        logging.debug('attached')
                    else:
                        sigcontent = open(sig).read()
                        enc = helper.guess_encoding(sigcontent)
                        mimetype = helper.guess_mimetype(sigcontent)
                        if mimetype.startswith('text'):
                            sigcontent = helper.string_decode(sigcontent, enc)
                            self.envelope.body += '\n' + sigcontent
                else:
                    ui.notify('could not locate signature: %s' % sig,
                              priority='error')
                    if (yield ui.choice('send without signature',
                                        select='yes', cancel='no')) == 'no':
                        return

        # get missing To header
        if 'To' not in self.envelope.headers:
            sender = self.envelope.get('From')
            name, addr = email.Utils.parseaddr(sender)
            account = ui.accountman.get_account_by_address(addr)

            allbooks = not settings.config.getboolean('general',
                                'complete_matching_abook_only')
            logging.debug(allbooks)
            if account is not None:
                abooks = ui.accountman.get_addressbooks(order=[account],
                                                        append_remaining=allbooks)
                logging.debug(abooks)
                completer = ContactsCompleter(abooks)
            else:
                completer = None
            to = yield ui.prompt(prefix='To>',
                                 completer=completer)
            if to == None:
                ui.notify('canceled')
                return
            self.envelope.add('To', to)

        if settings.config.getboolean('general', 'ask_subject') and \
           not 'Subject' in self.envelope.headers:
            subject = yield ui.prompt(prefix='Subject>')
            logging.debug('SUBJECT: "%s"' % subject)
            if subject == None:
                ui.notify('canceled')
                return
            self.envelope.add('Subject', subject)

        if self.attach:
            for a in self.attach:
                self.envelope.attach(a)

        cmd = commands.envelope.EditCommand(envelope=self.envelope)
        ui.apply_command(cmd)
Exemple #9
0
    def apply(self, ui):
        if not self.message:
            self.message = ui.current_buffer.get_selected_message()
        mail = self.message.get_email()
        # set body text
        name, address = self.message.get_author()
        timestamp = self.message.get_date()
        qf = settings.config.get_hook("reply_prefix")
        if qf:
            quotestring = qf(
                name, address, timestamp, ui=ui, dbm=ui.dbman, aman=ui.accountman, log=ui.logger, config=settings.config
            )
        else:
            quotestring = "Quoting %s (%s)\n" % (name, timestamp)
        mailcontent = quotestring
        for line in self.message.accumulate_body().splitlines():
            mailcontent += ">" + line + "\n"

        envelope = Envelope(bodytext=mailcontent)

        # copy subject
        subject = decode_header(mail.get("Subject", ""))
        if not subject.startswith("Re:"):
            subject = "Re: " + subject
        envelope.add("Subject", subject)

        # set From
        my_addresses = ui.accountman.get_addresses()
        matched_address = ""
        in_to = [a for a in my_addresses if a in mail.get("To", "")]
        if in_to:
            matched_address = in_to[0]
        else:
            cc = mail.get("Cc", "") + mail.get("Bcc", "")
            in_cc = [a for a in my_addresses if a in cc]
            if in_cc:
                matched_address = in_cc[0]
        if matched_address:
            account = ui.accountman.get_account_by_address(matched_address)
            fromstring = "%s <%s>" % (account.realname, account.address)
            envelope.add("From", fromstring)

        # set To
        if self.groupreply:
            cleared = self.clear_my_address(my_addresses, mail.get("To", ""))
            if cleared:
                logging.info(mail["From"] + ", " + cleared)
                to = mail["From"] + ", " + cleared
                envelope.add("To", decode_header(to))

            else:
                envelope.add("To", decode_header(mail["From"]))
            # copy cc and bcc for group-replies
            if "Cc" in mail:
                cc = self.clear_my_address(my_addresses, mail["Cc"])
                envelope.add("Cc", decode_header(cc))
            if "Bcc" in mail:
                bcc = self.clear_my_address(my_addresses, mail["Bcc"])
                envelope.add("Bcc", decode_header(bcc))
        else:
            envelope.add("To", decode_header(mail["From"]))

        # set In-Reply-To header
        envelope.add("In-Reply-To", "<%s>" % self.message.get_message_id())

        # set References header
        old_references = mail.get("References", "")
        if old_references:
            old_references = old_references.split()
            references = old_references[-8:]
            if len(old_references) > 8:
                references = old_references[:1] + references
            references.append("<%s>" % self.message.get_message_id())
            envelope.add("References", " ".join(references))
        else:
            envelope.add("References", "<%s>" % self.message.get_message_id())

        ui.apply_command(ComposeCommand(envelope=envelope))