def command(self): session, config, idx = self.session, self.session.config, self._idx() args = list(self.args) flags = [] while args and args[0][:1] == '-': flags.append(args.pop(0)) msg_idxs = list(self._choose_messages(args)) if not msg_idxs: return self._error('No messages selected') wrote = [] for msg_idx in msg_idxs: e = Email(idx, msg_idx) ts = long(e.get_msg_info(field=idx.MSG_DATE), 36) dt = datetime.datetime.fromtimestamp(ts) subject = e.get_msg_info(field=idx.MSG_SUBJECT) fn = ('%4.4d-%2.2d-%2.2d.%s.%s.html' % (dt.year, dt.month, dt.day, CleanText(subject, banned=CleanText.NONDNS, replace='_' ).clean.replace('____', '_')[:50], e.msg_mid()) ).encode('ascii', 'ignore') session.ui.mark(_('Printing e-mail to %s') % fn) smv = SingleMessageView(session, arg=['=%s' % e.msg_mid()]) html = smv.run().as_html() if '-sign' in flags: key = config.prefs.gpg_recipient html = '<printed ts=%d -->\n%s\n<!-- \n' % (time.time(), html) rc, signed = self._gnupg().sign(html.encode('utf-8'), fromkey=key, clearsign=True) if rc != 0: return self._error('Failed to sign printout') html = '<!--\n%s\n-->\n' % signed.decode('utf-8') with open(fn, 'wb') as fd: fd.write(html.encode('utf-8')) wrote.append({'mid': e.msg_mid(), 'filename': fn}) return self._success(_('Printed to %d files') % len(wrote), wrote)
def command(self): session, config, idx = self.session, self.session.config, self._idx() args = list(self.args) flags = [] while args and args[0][:1] == '-': flags.append(args.pop(0)) msg_idxs = list(self._choose_messages(args)) if not msg_idxs: return self._error('No messages selected') wrote = [] for msg_idx in msg_idxs: e = Email(idx, msg_idx) ts = long(e.get_msg_info(field=idx.MSG_DATE), 36) dt = datetime.datetime.fromtimestamp(ts) subject = e.get_msg_info(field=idx.MSG_SUBJECT) fn = ('%4.4d-%2.2d-%2.2d.%s.%s.html' % (dt.year, dt.month, dt.day, CleanText(subject, banned=CleanText.NONDNS, replace='_').clean.replace('____', '_')[:50], e.msg_mid())).encode('ascii', 'ignore') session.ui.mark(_('Printing e-mail to %s') % fn) smv = SingleMessageView(session, arg=['=%s' % e.msg_mid()]) html = smv.run().as_html() if '-sign' in flags: key = config.prefs.gpg_recipient html = '<printed ts=%d -->\n%s\n<!-- \n' % (time.time(), html) rc, signed = self._gnupg().sign(html.encode('utf-8'), fromkey=key, clearsign=True) if rc != 0: return self._error('Failed to sign printout') html = '<!--\n%s\n-->\n' % signed.decode('utf-8') with open(fn, 'wb') as fd: fd.write(html.encode('utf-8')) wrote.append({'mid': e.msg_mid(), 'filename': fn}) return self._success(_('Printed to %d files') % len(wrote), wrote)
def command(self, save=True): session, config, idx = self.session, self.session.config, self._idx() mbox_type = config.prefs.export_format args = list(self.args) if args and ':' in args[-1]: mbox_type, path = args.pop(-1).split(':', 1) else: path = self.export_path(mbox_type) flat = notags = False while args and args[0][:1] == '-': option = args.pop(0).replace('-', '') if option == 'flat': flat = True elif option == 'notags': notags = True if os.path.exists(path): return self._error('Already exists: %s' % path) msg_idxs = list(self._choose_messages(args)) if not msg_idxs: session.ui.warning('No messages selected') return False # Exporting messages without their threads barely makes any # sense. if not flat: for i in reversed(range(0, len(msg_idxs))): mi = msg_idxs[i] msg_idxs[i:i+1] = [int(m[idx.MSG_MID], 36) for m in idx.get_conversation(msg_idx=mi)] # Let's always export in the same order. Stability is nice. msg_idxs.sort() try: mbox = self.create_mailbox(mbox_type, path) except (IOError, OSError): mbox = None if mbox is None: if not os.path.exists(os.path.dirname(path)): reason = _('Parent directory does not exist.') else: reason = _('Is the disk full? Are permissions lacking?') return self._error(_('Failed to create mailbox: %s') % reason) exported = {} failed = [] while msg_idxs: msg_idx = msg_idxs.pop(0) if msg_idx not in exported: e = Email(idx, msg_idx) session.ui.mark(_('Exporting message =%s ...') % e.msg_mid()) fd = e.get_file() if fd is None: failed.append(e.msg_mid()) session.ui.warning(_('Message =%s is unreadable! Skipping.' ) % e.msg_mid()) continue try: data = fd.read() if not notags: tags = [tag.slug for tag in (self.session.config.get_tag(t) or t for t in e.get_msg_info(idx.MSG_TAGS).split(',') if t) if hasattr(tag, 'slug')] lf = '\r\n' if ('\r\n' in data[:200]) else '\n' header, body = data.split(lf+lf, 1) data = str(lf.join([ header, 'X-Mailpile-Tags: ' + '; '.join(sorted(tags) ).encode('utf-8'), '', body ])) mbox.add(data.replace('\r\n', '\n')) exported[msg_idx] = 1 finally: fd.close() mbox.flush() result = { 'exported': len(exported), 'created': path } if failed: result['failed'] = failed return self._success( _('Exported %d messages to %s') % (len(exported), path), result)