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 rich import print
from argparse import RawTextHelpFormatter
#from urllib import Request, urlopen
from urllib.request import urlopen
# Info pages for dev
# 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))
exit(0)
else:
values = """
{
"address": alias,
"goto": to_address,
"active": "1"
}
"""
headers = {
'Content-Type': 'application/json',
'X-API-Key': apikey
}
request = Request('https://rune.pm/api/v1/add/alias', data=values, headers=headers)
response_body = urlopen(request).read()
print (response_body)
data = {'address': alias,'goto': to_address,'active': "1"}
headers = {'X-API-Key': apikey, "Content-Type": "application/json"}
response = requests.post('https://'+server+'/api/v1/add/alias',
data=json.dumps(data), headers=headers)
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))
else:
cursor = conn.cursor()
cursor.execute('INSERT INTO aliases values(?,?,?,?)', (mail_id, alias,to_address,now))
conn.commit()
def delete(alias):
print('Delete : ' + alias)
@ -164,13 +162,13 @@ def checklist(alias):
remoteData = json.loads(remote)
i = 0
for search in remoteData:
if remoteData[i]['address'] == alias:
if alias in remoteData[i]['address']:
return True
i=i+1
return None
def number_of_aliases_on_server():
def list_alias():
apikey = get_api()
mail_server = get_settings('mail_server')
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')
remoteData = json.loads(remote)
i = 0
for count in remoteData:
for search in remoteData:
print(remoteData[i]['address'])
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():
cursor = conn.cursor()
@ -268,4 +284,5 @@ elif args['version']:
else:
# get_settings(first_run_status)
get_api()
list_alias()
print('\n\nError use [b]malias -h[/b] to see the help screen!\n\n\n')