#!/usr/bin/python3 import requests import ovh import pycurl import json import argparse def writeRedirectionsToFile(client,domain): # Grab bill list mailbox = client.get('/email/domain/' + domain + '/redirection') file = open(redir_file,'wt') for id in mailbox: redirection = client.get('/email/domain/' + domain + '/redirection/'+str(id)) file.write(redirection['from']+','+redirection['to']+','+redirection['id']+'\n') file.close() def checkRedirExists(client,domain,fr,to): mailbox = client.get('/email/domain/' + domain + '/redirection') for id in mailbox: redirection = client.get('/email/domain/' + domain +'/redirection/'+str(id)) if (redirection['from'] == fr and redirection['to'] == to): print("Redirection existante") return id elif (redirection['from'] == fr and redirection ['to'] != to): print("Redirection existante vers l'adresse " + redirection['to']) return id else: return False 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 def delRedirection(client,domain,fr,to): id_redir = checkRedirExists(client,domain,fr,to) if not id_redir: return False else: result = client.delete('/email/domain/'+ domain +'/redirection/' + str(id_redir)) 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 = './data/redirections.txt' try: input = raw_input except NameError: pass client = ovh.Client() if (args.method == "write"): writeRedirectionsToFile(client,args.domain) elif (args.method == "add"): if not (checkRedirExists(client,args.domain,args.fr,args.to)): addRedirection(client,args.domain,args.fr,args.to) elif (args.method == "del"): status = delRedirection(client,args.domain,args.fr,args.to)