From b8f206ee322b66b9201c313f8d57224ca3e9afe5 Mon Sep 17 00:00:00 2001 From: Kirby Date: Thu, 12 Mar 2020 17:44:09 +0100 Subject: [PATCH] switching to argument based domain for mail script --- ovh-mail.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/ovh-mail.py b/ovh-mail.py index b900140..d825e66 100755 --- a/ovh-mail.py +++ b/ovh-mail.py @@ -6,19 +6,19 @@ import pycurl import json import argparse -def writeRedirectionsToFile(client): +def writeRedirectionsToFile(client,domain): # Grab bill list - mailbox = client.get('/email/domain/hyrule.ovh/redirection') + mailbox = client.get('/email/domain/' + domain + '/redirection') file = open(redir_file,'wt') for id in mailbox: - redirection = client.get('/email/domain/hyrule.ovh/redirection/'+str(id)) + redirection = client.get('/email/domain/' + domain + '/redirection/'+str(id)) file.write(redirection['from']+','+redirection['to']+','+redirection['id']+'\n') file.close() -def checkRedirExists(client,fr,to): - mailbox = client.get('/email/domain/hyrule.ovh/redirection') +def checkRedirExists(client,domain, fr,to): + mailbox = client.get('/email/domain/' + domain + '/redirection') for id in mailbox: - redirection = client.get('/email/domain/hyrule.ovh/redirection/'+str(id)) + redirection = client.get('/email/domain/' + domain +'/redirection/'+str(id)) if (redirection['from'] == fr and redirection['to'] == to): print("Redirection existante") return id @@ -28,18 +28,19 @@ def checkRedirExists(client,fr,to): else: return False -def addRedirection(client,fr,to): - result = client.post('/email/domain/hyrule.ovh/redirection',_from=fr,localCopy=False,to=to) +def addRedirection(client,domain,fr,to): + result = client.post('/email/domain/'+ domain +'/redirection',_from=fr,localCopy=False,to=to) print(json.dumps(result,indent=4)) return 0 parser = argparse.ArgumentParser(description='Process arguments') parser.add_argument('method', metavar='method', type=str, help='Function to use') +parser.add_argument('domain', metavar='domain', type=str, help='domain') parser.add_argument('--fr', metavar='fr', type=str, help='from mail address') parser.add_argument('--to', metavar='to', type=str, help='to mail address') args = parser.parse_args() -redir_file = '/var/scripts/ovh_api/data/redirections.txt' +redir_file = './data/redirections.txt' try: input = raw_input @@ -47,7 +48,7 @@ except NameError: pass client = ovh.Client() if (args.method == "write"): - writeRedirectionsToFile(client) + writeRedirectionsToFile(client,args.domain) elif (args.method == "add"): - if not (checkRedirExists(client,args.fr,args.to)): - addRedirection(client,args.fr,args.to) + if not (checkRedirExists(client,args.domain,args.fr,args.to)): + addRedirection(client,args.domain,args.fr,args.to)