rp

simple email tools
git clone https://git.parazyd.org/rp
Log | Files | Refs | README | LICENSE

commit e80b9d4c13eb2332d1a9fb555e73508fa3607b11
parent f3fa45fa0c8fe75b9c193edb4efb304276c7e9af
Author: parazyd <parazyd@dyne.org>
Date:   Thu, 20 Dec 2018 12:57:33 -0500

Refactor and lint rpsync.

Diffstat:
Mbin/rpsync | 71+++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 39 insertions(+), 32 deletions(-)

diff --git a/bin/rpsync b/bin/rpsync @@ -37,6 +37,38 @@ def parsecfg(): return cfgmap +def imapconnect(host, port): + """ Abstraction layer for an IMAP connection """ + if port == 'tcp' or port == 'imap' or port == 143: + return IMAP4(host=host, port=port) + elif port == 'tcps' or port == 'imaps' or port == 993: + return IMAP4_SSL(host=host, port=port) + + print('Invalid host and port: %s:%s' % (host, port)) + return None + + +def fetchmail(imapctx, mailbox, dryrun=False): + """ Fetch all emails from a given mailbox """ + data = imapctx.select(mailbox) + data = imapctx.fetch('1:*', '(FLAGS)') + print('* New mail in %s (%d)' % (mailbox.decode('utf-8'), len(data[1]))) + + if not dryrun: + for i in data[1]: + email = imapctx.fetch(i.split()[0], 'body[]') + emailtopipe = email[1][0][1] + mbx = mailbox.replace(b'.', b'/') + + proc = Popen(['/usr/libexec/dovecot/deliver', '-m', mbx], + stdin=PIPE) + proc.stdin.write(emailtopipe) + proc.communicate() + proc.stdin.close() + imapctx.store(i.split()[0], '+FLAGS', '\\Deleted') + imapctx.expunge() + + def main(): """ Main routine """ if len(argv) > 1 and argv[1] != '-n': @@ -44,26 +76,18 @@ def main(): dryrun = False if '-n' in argv: + print("* Dry run enabled") dryrun = True config = parsecfg() - ntype = config['rnet'].split('!')[0] rhost = config['rnet'].split('!')[1] rport = config['rnet'].split('!')[2] - if ntype == 'tcp' or rport == 'imap': - rport = 143 - elif ntype == 'tcps' or rport == 'imaps': - rport = 993 - - if rport == 993: - imap = IMAP4_SSL(host=rhost, port=rport) - else: - imap = IMAP4(host=rhost, port=rport) - print("* Connecting to %s:%s" % (rhost, rport)) - if dryrun: - print("* Dry run enabled") + imap = imapconnect(rhost, rport) + if not imap: + exit(1) + data = imap.login(config['ruser'], config['rpass']) data = imap.list() @@ -73,30 +97,13 @@ def main(): hasmail = [] for i in boxes: - data = imap.status(i, '(MESSAGES)') - status = data[1][0].split(b' ', 1) + status = imap.status(i, '(MESSAGES)')[1][0].split(b' ', 1) if status[1] != b'(MESSAGES 0)': hasmail.append(i) if hasmail: for i in hasmail: - print('* New mail in', i.decode('utf-8')) - if not dryrun: - data = imap.select(i) - data = imap.fetch('1:*', '(FLAGS)') - - for j in data[1]: - email = imap.fetch(j.split()[0], 'body[]') - emailtopipe = email[1][0][1] - mbx = i.replace(b'.', b'/') - - proc = Popen(['/usr/libexec/dovecot/deliver', '-m', mbx], - stdin=PIPE) - proc.stdin.write(emailtopipe) - proc.communicate() - proc.stdin.close() - imap.store(j.split()[0], '+FLAGS', '\\Deleted') - imap.expunge() + fetchmail(imap, i, dryrun=dryrun) else: print('* No new mail')