From 10af809c0c75f6b229f72356eca07d19a4f24480 Mon Sep 17 00:00:00 2001 From: Konstantin Ryabitsev Date: Mon, 21 Dec 2020 11:55:43 -0500 Subject: Check if -o is a maildir When -o is a valid maildir, then instead of saving an .mbox file add messages to the maildir instead. This should allow "b4 mbox" to quickly add threads to existing mail spools. Signed-off-by: Konstantin Ryabitsev --- b4/mbox.py | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/b4/mbox.py b/b4/mbox.py index 200cf1c..eeca363 100644 --- a/b4/mbox.py +++ b/b4/mbox.py @@ -537,23 +537,37 @@ def main(cmdargs): if cmdargs.subcmd == 'am': mbox_to_am(threadfile, cmdargs) os.unlink(threadfile) - else: - mbx = mailbox.mbox(threadfile) - logger.critical('%s messages in the thread', len(mbx)) - mbx.close() - if cmdargs.outdir == '-': - logger.info('---') - with open(threadfile, 'rb') as fh: - shutil.copyfileobj(fh, sys.stdout.buffer) - os.unlink(threadfile) - return + return - if cmdargs.wantname: - savefile = os.path.join(cmdargs.outdir, cmdargs.wantname) - else: - msgid = b4.get_msgid(cmdargs) - savefile = os.path.join(cmdargs.outdir, '%s.mbx' % msgid) + mbx = mailbox.mbox(threadfile) + logger.info('%s messages in the thread', len(mbx)) + if cmdargs.outdir == '-': + mbx.close() + logger.info('---') + with open(threadfile, 'rb') as fh: + shutil.copyfileobj(fh, sys.stdout.buffer) + os.unlink(threadfile) + return - shutil.copy(threadfile, savefile) - logger.info('Saved %s', savefile) + # Check if outdir is a maildir + if (os.path.isdir(os.path.join(cmdargs.outdir, 'new')) + and os.path.isdir(os.path.join(cmdargs.outdir, 'cur')) + and os.path.isdir(os.path.join(cmdargs.outdir, 'tmp'))): + mdr = mailbox.Maildir(cmdargs.outdir) + for msg in mbx: + mdr.add(msg) + logger.info('Added to maildir %s', cmdargs.outdir) + mbx.close() os.unlink(threadfile) + return + + if cmdargs.wantname: + savefile = os.path.join(cmdargs.outdir, cmdargs.wantname) + else: + msgid = b4.get_msgid(cmdargs) + savefile = os.path.join(cmdargs.outdir, '%s.mbx' % msgid) + + mbx.close() + shutil.copy(threadfile, savefile) + logger.info('Saved %s', savefile) + os.unlink(threadfile) -- cgit v1.2.3