commit 3687d0c3c958ddc58b34c40e7ccd7e31ec4365a3
parent a557183d5be5b614d3d4260f2402db748b4c2eb8
Author: parazyd <parazyd@dyne.org>
Date: Sat, 3 Mar 2018 21:12:57 +0100
Add python implementation of rpsync.
Diffstat:
M | bin/rpsync | | | 184 | +++++++++++++++++++++++++++++++++++++++++++------------------------------------ |
1 file changed, 100 insertions(+), 84 deletions(-)
diff --git a/bin/rpsync b/bin/rpsync
@@ -1,84 +1,100 @@
-#!/bin/sh
-
-export PATH="/usr/local/bin:/usr/bin:/bin"
-profile="${RPPROFILE:-$HOME/.rp/default}"
-
-usage() {
- printf " * usage: $(basename $0) [-n] [-d]\n"
- printf "\t-n: dry run\n"
- printf "\t-d: delete remote emails\n"
- exit 1
-}
-
-
-for i in $@; do
- case "$i" in
- -n)
- dryrun="--dry"
- ;;
- -d)
- delete="--delete1"
- ;;
- *)
- usage
- ;;
- esac
-done
-
-rnet="$(rpheaders rnet < ${profile}/config)"
-case "$rnet" in
- tcps*) tls1="--tls1"; rport=993;;
-esac
-[ -n "$tls1" ] || rport=143
-
-lnet="$(rpheaders lnet < ${profile}/config)"
-case "$lnet" in
- tcps*) tls2="--tls2"; rport=993;;
-esac
-[ -n "$tls2" ] || lport=143
-
-ruser="$(rpheaders ruser < "$profile/config")"
-rpass="$(rpheaders rpass < "$profile/config")"
-luser="$(rpheaders luser < "$profile/config")"
-lpass="$(rpheaders lpass < "$profile/config")"
-
-echo "$rpass" > "$profile/tmp/rimap"
-echo "$lpass" > "$profile/tmp/limap"
-
-rhost="$(echo "$rnet" | cut -d'!' -f2)"
-lhost="$(echo "$lnet" | cut -d'!' -f2)"
-
-nc -w 1 "$rhost" "$rport" || {
- echo " * $rhost unreachable"
- exit 1
-}
-
-[ -n "$delete" ] && echo " * remote deletion enabled"
-[ -n "$dryrun" ] && echo " * dry run enabled"
-
-imapsync \
- --tmpdir "$profile/tmp" \
- --nolog \
- --usecache \
- --host1 "$rhost" $tls1 \
- --user1 "$ruser" \
- --passfile1 "$profile/tmp/rimap" \
- --host2 "$lhost" $tls2 \
- --user2 "$luser" \
- --passfile2 "$profile/tmp/limap" \
- --automap ${dryrun} ${delete} \
- --no-modulesversion
-
-_e=$?
-
-rm -f "$profile/tmp/rimap" "$profile/tmp/limap"
-
-case "$_e" in
- 0)
- echo " * synced successfully"
- ;;
- *)
- echo " * error while syncing"
- exit $_e
- ;;
-esac
+#!/usr/bin/env python3
+# Copy me if you can
+# by parazyd
+
+from os import getenv
+from os.path import join
+from sys import argv
+from imaplib import IMAP4, IMAP4_SSL
+from subprocess import Popen, PIPE
+from time import time
+
+
+PROFILE = getenv('RPPROFILE')
+if not PROFILE:
+ PROFILE = join(getenv('HOME'), '.rp/default')
+
+
+def usage():
+ print(" * usage: %s [-n]" % argv[0])
+ print("\t-n: dry run")
+ exit(1)
+
+
+def parsecfg():
+ cfgmap = {}
+ rawtxt = open(join(PROFILE, 'config')).read()
+ contents = rawtxt.split('\n')[:-1]
+ for i in contents:
+ key = i.split(': ', 1)[0]
+ val = i.split(': ', 1)[1]
+ cfgmap[key] = val
+ return cfgmap
+
+
+def main():
+ if len(argv) > 1 and argv[1] != '-n':
+ usage()
+
+ dryrun = False
+ if '-n' in argv: 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)
+
+ data = imap.login(config['ruser'], config['rpass'])
+ data = imap.list()
+
+ boxes = []
+ for mbox in data[1]:
+ boxes.append(mbox.split()[2])
+
+ hasmail = []
+ for i in boxes:
+ data = imap.status(i, '(MESSAGES)')
+ status = data[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]
+ mb = i.replace(b'.', b'/')
+
+ proc = Popen(['/usr/libexec/dovecot/deliver', '-m', mb],
+ stdin=PIPE)
+ proc.stdin.write(emailtopipe)
+ proc.communicate()
+ proc.stdin.close()
+ imap.store(j.split()[0], '+FLAGS', '\\Deleted')
+ imap.expunge()
+ else:
+ print(' * no new mail')
+
+ imap.logout()
+
+
+if __name__ == '__main__':
+ T1 = time()
+ main()
+ T2 = time()
+ print(" * total time: %d seconds" % int(T2 - T1))