This commit is contained in:
rune 2023-04-12 11:57:35 +02:00
parent 3351ca9b98
commit 5bd4b9b781

View File

@ -15,6 +15,8 @@ from datetime import datetime
from string import ascii_letters, digits from string import ascii_letters, digits
from rich import print from rich import print
from argparse import RawTextHelpFormatter from argparse import RawTextHelpFormatter
#from urllib import Request, urlopen
from urllib.request import urlopen
# Info pages for dev # Info pages for dev
# https://mailcow.docs.apiary.io/#reference/aliases/get-aliases/get-aliases # https://mailcow.docs.apiary.io/#reference/aliases/get-aliases/get-aliases
@ -130,25 +132,21 @@ def create(alias,to_address):
print('\n[b]Error[/b] : alias %s exists on the MailCow instance %s \n' %(alias,server)) print('\n[b]Error[/b] : alias %s exists on the MailCow instance %s \n' %(alias,server))
exit(0) exit(0)
else: else:
values = """ data = {'address': alias,'goto': to_address,'active': "1"}
{ headers = {'X-API-Key': apikey, "Content-Type": "application/json"}
"address": alias, response = requests.post('https://'+server+'/api/v1/add/alias',
"goto": to_address, data=json.dumps(data), headers=headers)
"active": "1" mail_id = alias_id(alias)
} if mail_id == None:
""" logging.error(now + ' - Error : alias %s not created on the MailCow instance %s ' %(alias,server))
print('\n[b]Error[/b] : alias %s exists on the MailCow instance %s \n' %(alias,server))
headers = { else:
'Content-Type': 'application/json', cursor = conn.cursor()
'X-API-Key': apikey cursor.execute('INSERT INTO aliases values(?,?,?,?)', (mail_id, alias,to_address,now))
} conn.commit()
request = Request('https://rune.pm/api/v1/add/alias', data=values, headers=headers)
response_body = urlopen(request).read()
print (response_body)
def delete(alias): def delete(alias):
print('Delete : ' + alias) print('Delete : ' + alias)
@ -164,13 +162,13 @@ def checklist(alias):
remoteData = json.loads(remote) remoteData = json.loads(remote)
i = 0 i = 0
for search in remoteData: for search in remoteData:
if remoteData[i]['address'] == alias: if alias in remoteData[i]['address']:
return True return True
i=i+1 i=i+1
return None return None
def number_of_aliases_on_server(): def list_alias():
apikey = get_api() apikey = get_api()
mail_server = get_settings('mail_server') mail_server = get_settings('mail_server')
req = urllib.request.Request('https://'+mail_server+'/api/v1/get/alias/all') req = urllib.request.Request('https://'+mail_server+'/api/v1/get/alias/all')
@ -180,10 +178,28 @@ def number_of_aliases_on_server():
remote = current.read().decode('utf-8') remote = current.read().decode('utf-8')
remoteData = json.loads(remote) remoteData = json.loads(remote)
i = 0 i = 0
for count in remoteData: for search in remoteData:
print(remoteData[i]['address'])
i=i+1 i=i+1
return i
def alias_id(alias):
apikey = get_api()
mail_server = get_settings('mail_server')
req = urllib.request.Request('https://'+mail_server+'/api/v1/get/alias/all')
req.add_header('Content-Type', 'application/json')
req.add_header('X-API-Key', apikey)
current = urllib.request.urlopen(req)
remote = current.read().decode('utf-8')
remoteData = json.loads(remote)
i = 0
for search in remoteData:
if remoteData[i]['address'] == alias:
return remoteData[i]['id']
i=i+1
return None
def number_of_aliases_in_db(): def number_of_aliases_in_db():
cursor = conn.cursor() cursor = conn.cursor()
@ -268,4 +284,5 @@ elif args['version']:
else: else:
# get_settings(first_run_status) # get_settings(first_run_status)
get_api() get_api()
list_alias()
print('\n\nError use [b]malias -h[/b] to see the help screen!\n\n\n') print('\n\nError use [b]malias -h[/b] to see the help screen!\n\n\n')