Beispiel #1
0
    def command(self):
        session, config, idx = self.session, self.session.config, self._idx()
        mode = 'download'
        name_fmt = None

        args = list(self.args)
        if args[0] in ('inline', 'inline-preview', 'preview', 'download'):
            mode = args.pop(0)

        if len(args) > 0 and args[-1].startswith('>'):
            forbid = security.forbid_command(self,
                                             security.CC_ACCESS_FILESYSTEM)
            if forbid:
                return self._error(forbid)
            name_fmt = args.pop(-1)[1:]

        if (args[0].startswith('#') or
                args[0].startswith('part:') or
                args[0].startswith('ext:')):
            cid = args.pop(0)
        else:
            cid = args.pop(-1)

        emails = [Email(idx, i) for i in self._choose_messages(args)]
        results = []
        for e in emails:
            if cid[0] == '*':
                tree = e.get_message_tree(want=['attachments'])
                cids = [('#{0!s}'.format(a['count'])) for a in tree['attachments']
                        if a['filename'].lower().endswith(cid[1:].lower())]
            else:
                cids = [cid]

            for c in cids:
                fn, info = e.extract_attachment(session, c,
                                                name_fmt=name_fmt, mode=mode)
                if info:
                    info['idx'] = e.msg_idx_pos
                    if fn:
                        info['created_file'] = fn
                    results.append(info)
        return results
Beispiel #2
0
 def command(self):
     event_id = (self.data.get('event_id', [None])[0]
                 or (self.args and self.args[0])
                 or (self.session.last_event_id))
     if not event_id:
         return self._error(_('Need an event ID!'))
     event = self.session.config.event_log.get(event_id)
     if event:
         try:
             sc = event.source_class
             forbid = security.forbid_command(self, sc.COMMAND_SECURITY)
             if forbid:
                 return self._error(forbid)
             else:
                 return sc.Undo(self, event)
         except (NameError, AttributeError):
             self._ignore_exception()
             return self._error(_('Event %s is not undoable') % event_id)
     else:
         return self._error(_('Event %s not found') % event_id)
Beispiel #3
0
    def run(self, *args, **kwargs):
        if self.COMMAND_SECURITY is not None:
            forbidden = security.forbid_command(self)
            if forbidden:
                return self._error(forbidden)

        with MultiContext(self.WITH_CONTEXT):
            if self.IS_USER_ACTIVITY:
                try:
                    self.record_user_activity()
                    mailpile.util.LIVE_USER_ACTIVITIES += 1
                    rv = self._run(*args, **kwargs)
                    self._maybe_trigger_cache_refresh()
                    return rv
                finally:
                    mailpile.util.LIVE_USER_ACTIVITIES -= 1
            else:
                rv = self._run(*args, **kwargs)
                self._maybe_trigger_cache_refresh()
                return rv
Beispiel #4
0
    def command(self):
        session, config, idx = self.session, self.session.config, self._idx()
        mode = "download"
        name_fmt = None

        args = list(self.args)
        if args[0] in ("inline", "inline-preview", "preview", "download"):
            mode = args.pop(0)

        if len(args) > 0 and args[-1].startswith(">"):
            forbid = security.forbid_command(self, security.CC_ACCESS_FILESYSTEM)
            if forbid:
                return self._error(forbid)
            name_fmt = args.pop(-1)[1:]

        if args[0].startswith("#") or args[0].startswith("part:") or args[0].startswith("ext:"):
            cid = args.pop(0)
        else:
            cid = args.pop(-1)

        emails = [Email(idx, i) for i in self._choose_messages(args)]
        results = []
        for e in emails:
            if cid[0] == "*":
                tree = e.get_message_tree(want=["attachments"])
                cids = [
                    ("#%s" % a["count"]) for a in tree["attachments"] if a["filename"].lower().endswith(cid[1:].lower())
                ]
            else:
                cids = [cid]

            for c in cids:
                fn, info = e.extract_attachment(session, c, name_fmt=name_fmt, mode=mode)
                if info:
                    info["idx"] = e.msg_idx_pos
                    if fn:
                        info["created_file"] = fn
                    results.append(info)
        return results
Beispiel #5
0
    def command(self, emails=None):
        session, idx = self.session, self._idx()
        args = list(self.args)

        files = []
        filedata = {}
        if 'file-data' in self.data:
            count = 0
            for fd in self.data['file-data']:
                fn = (hasattr(fd, 'filename')
                      and fd.filename or 'attach-%d.dat' % count)
                filedata[fn] = fd
                files.append(fn)
                count += 1
        else:
            if args:
                fb = security.forbid_command(self,
                                             security.CC_ACCESS_FILESYSTEM)
                if fb:
                    return self._error(fb)
            while os.path.exists(args[-1]):
                files.append(args.pop(-1))

        if not files:
            return self._error(_('No files found'))

        if not emails:
            args.extend(['=%s' % mid for mid in self.data.get('mid', [])])
            emails = [self._actualize_ephemeral(i) for i in
                      self._choose_messages(args, allow_ephemeral=True)]
        if not emails:
            return self._error(_('No messages selected'))

        updated = []
        errors = []
        def err(msg):
            errors.append(msg)
            session.ui.error(msg)
        for email in emails:
            subject = email.get_msg_info(MailIndex.MSG_SUBJECT)
            try:
                email.add_attachments(session, files, filedata=filedata)
                updated.append(email)
            except KeyboardInterrupt:
                raise
            except NotEditableError:
                err(_('Read-only message: %s') % subject)
            except:
                err(_('Error attaching to %s') % subject)
                self._ignore_exception()

        if errors:
            self.message = _('Attached %s to %d messages, failed %d'
                             ) % (', '.join(files), len(updated), len(errors))
        else:
            self.message = _('Attached %s to %d messages'
                             ) % (', '.join(files), len(updated))

        session.ui.notify(self.message)
        return self._return_search_results(self.message, updated,
                                           expand=updated, error=errors)
Beispiel #6
0
    def command(self, emails=None):
        session, idx = self.session, self._idx()
        args = list(self.args)

        files = []
        filedata = {}
        if 'file-data' in self.data:
            count = 0
            for fd in self.data['file-data']:
                fn = (hasattr(fd, 'filename') and fd.filename
                      or 'attach-%d.dat' % count)
                filedata[fn] = fd
                files.append(fn)
                count += 1
        else:
            if args:
                fb = security.forbid_command(self,
                                             security.CC_ACCESS_FILESYSTEM)
                if fb:
                    return self._error(fb)
            while os.path.exists(args[-1]):
                files.append(args.pop(-1))

        if not files:
            return self._error(_('No files found'))

        if not emails:
            args.extend(['=%s' % mid for mid in self.data.get('mid', [])])
            emails = [
                self._actualize_ephemeral(i)
                for i in self._choose_messages(args, allow_ephemeral=True)
            ]
        if not emails:
            return self._error(_('No messages selected'))

        updated = []
        errors = []

        def err(msg):
            errors.append(msg)
            session.ui.error(msg)

        for email in emails:
            subject = email.get_msg_info(MailIndex.MSG_SUBJECT)
            try:
                email.add_attachments(session, files, filedata=filedata)
                updated.append(email)
            except KeyboardInterrupt:
                raise
            except NotEditableError:
                err(_('Read-only message: %s') % subject)
            except:
                err(_('Error attaching to %s') % subject)
                self._ignore_exception()

        if errors:
            self.message = _('Attached %s to %d messages, failed %d') % (
                ', '.join(files), len(updated), len(errors))
        else:
            self.message = _('Attached %s to %d messages') % (', '.join(files),
                                                              len(updated))

        session.ui.notify(self.message)
        return self._return_search_results(self.message,
                                           updated,
                                           expand=updated,
                                           error=errors)