rp

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

commit ac731932cad9538da973a2c2d5db9e5cb2af51b4
parent e66e1fa67038b6e7ee683853c918ac297b0d2cf1
Author: parazyd <parazyd@dyne.org>
Date:   Fri, 22 Feb 2019 19:21:06 +0100

Refactor rpsync to allow simplifying rpwatch.

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

diff --git a/bin/rpsync b/bin/rpsync @@ -39,34 +39,32 @@ def parsecfg(): def imapconnect(host, port): """ Abstraction layer for an IMAP connection """ - if port == 'imap' or port == 143: + if port in ('imap', 143): return IMAP4(host=host, port=port) - elif port == 'imaps' or port == 993: + if port in ('imaps', 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): +def fetchmail(imapctx, mailbox): """ 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'/') + 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() + 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(): @@ -76,14 +74,12 @@ def main(): dryrun = False if '-n' in argv: - print('* Dry run enabled') dryrun = True config = parsecfg() rhost = config['rnet'].split('!')[1] rport = config['rnet'].split('!')[2] - print('* Connecting to %s:%s' % (rhost, rport)) imap = imapconnect(rhost, rport) if not imap: exit(1) @@ -99,11 +95,14 @@ def main(): for i in boxes: status = imap.status(i, '(MESSAGES)')[1][0].split(b' ', 1) if status[1] != b'(MESSAGES 0)': + nmails = status[1].decode('utf-8').split(' ', 1)[1] + print('* New mail in %s (%s' % (i.decode('utf-8'), nmails)) hasmail.append(i) if hasmail: - for i in hasmail: - fetchmail(imap, i, dryrun=dryrun) + if not dryrun: + for i in hasmail: + fetchmail(imap, i) else: print('* No new mail')