2013-08-31 14:50:59 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2014-06-06 21:07:30 +00:00
|
|
|
import sys, getpass, urllib.request, urllib.error
|
2013-08-31 14:50:59 +00:00
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
def mgmt(cmd, data=None):
|
|
|
|
req = urllib.request.Request('http://localhost:10222' + cmd, urllib.parse.urlencode(data).encode("utf8") if data else None)
|
|
|
|
try:
|
|
|
|
response = urllib.request.urlopen(req)
|
|
|
|
except urllib.error.HTTPError as e:
|
|
|
|
print(e.read().decode('utf8'))
|
|
|
|
sys.exit(1)
|
|
|
|
return response.read().decode('utf8')
|
2013-08-31 14:50:59 +00:00
|
|
|
|
2014-06-06 21:07:30 +00:00
|
|
|
def read_password():
|
|
|
|
first = getpass.getpass('password: ')
|
|
|
|
second = getpass.getpass(' (again): ')
|
|
|
|
while first != second:
|
|
|
|
print('Passwords not the same. Try again.')
|
|
|
|
first = getpass.getpass('password: ')
|
|
|
|
second = getpass.getpass(' (again): ')
|
|
|
|
return first
|
|
|
|
|
2013-08-31 14:50:59 +00:00
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("Usage: ")
|
2014-03-16 20:29:47 +00:00
|
|
|
print(" tools/mail.py user (lists users)")
|
|
|
|
print(" tools/mail.py user add user@domain.com [password]")
|
|
|
|
print(" tools/mail.py user password user@domain.com [password]")
|
|
|
|
print(" tools/mail.py user remove user@domain.com")
|
|
|
|
print(" tools/mail.py alias (lists aliases)")
|
|
|
|
print(" tools/mail.py alias add incoming.name@domain.com sent.to@other.domain.com")
|
|
|
|
print(" tools/mail.py alias remove incoming.name@domain.com")
|
2013-08-31 14:50:59 +00:00
|
|
|
print()
|
|
|
|
print("Removing a mail user does not delete their mail folders on disk. It only prevents IMAP/SMTP login.")
|
|
|
|
print()
|
|
|
|
|
|
|
|
elif sys.argv[1] == "user" and len(sys.argv) == 2:
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/users"))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] in ("add", "password"):
|
|
|
|
if len(sys.argv) < 5:
|
|
|
|
if len(sys.argv) < 4:
|
|
|
|
email = input("email: ")
|
|
|
|
else:
|
|
|
|
email = sys.argv[3]
|
2014-06-06 21:07:30 +00:00
|
|
|
pw = read_password()
|
2013-08-31 14:50:59 +00:00
|
|
|
else:
|
|
|
|
email, pw = sys.argv[3:5]
|
|
|
|
|
|
|
|
if sys.argv[2] == "add":
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/users/add", { "email": email, "password": pw }))
|
2013-08-31 14:50:59 +00:00
|
|
|
elif sys.argv[2] == "password":
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/users/password", { "email": email, "password": pw }))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] == "remove" and len(sys.argv) == 4:
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/users/remove", { "email": sys.argv[3] }))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "alias" and len(sys.argv) == 2:
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/aliases"))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "alias" and sys.argv[2] == "add" and len(sys.argv) == 5:
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/aliases/add", { "source": sys.argv[3], "destination": sys.argv[4] }))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "alias" and sys.argv[2] == "remove" and len(sys.argv) == 4:
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/aliases/remove", { "source": sys.argv[3] }))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
print("Invalid command-line arguments.")
|
|
|
|
|