Added function to list timed aliases. Cleand up the code some
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@ list_alias.py
|
|||||||
malias.zip
|
malias.zip
|
||||||
malias_local.py
|
malias_local.py
|
||||||
*.json
|
*.json
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
.python-version
|
@@ -17,7 +17,7 @@ malias -h
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
usage: malias [-h] [-c] [-s server APIKey] [-a alias@domain.com to@domain.com] [-f alias@domain.com] [-d alias@domain.com] [-t user@domain.com domain.com] [-l] [-o] [-e] [-v]
|
usage: malias [-h] [-c] [-s server APIKey] [-a alias@domain.com to@domain.com] [-f alias@domain.com] [-d alias@domain.com] [-t user@domain.com domain.com] [-w alias@domain.com] [-l] [-o] [-e] [-v]
|
||||||
|
|
||||||
Malias is an application for adding, creating, and deleting aliases on a Mailcow instance.
|
Malias is an application for adding, creating, and deleting aliases on a Mailcow instance.
|
||||||
|
|
||||||
@@ -45,6 +45,9 @@ options:
|
|||||||
The domain.com is which domain to use when creating the timed-alias.
|
The domain.com is which domain to use when creating the timed-alias.
|
||||||
One year validity
|
One year validity
|
||||||
|
|
||||||
|
-w alias@domain.com, --alias alias@domain.com
|
||||||
|
List timed (temprary) aliases connected toone account.
|
||||||
|
|
||||||
-l, --list List all aliases on the Mailcow instance.
|
-l, --list List all aliases on the Mailcow instance.
|
||||||
|
|
||||||
-o, --domains List all mail domains on the Mailcow instance.
|
-o, --domains List all mail domains on the Mailcow instance.
|
||||||
|
37
malias.py
37
malias.py
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3 -W ignore::DeprecationWarning
|
||||||
import sys
|
import sys
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -10,7 +10,7 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from rich import print
|
from rich import print
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
from string import ascii_letters, digits
|
from string import ascii_letters, digits
|
||||||
from argparse import RawTextHelpFormatter
|
from argparse import RawTextHelpFormatter
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
@@ -32,6 +32,11 @@ app_version = '2.0'
|
|||||||
db_version = '2.0.0'
|
db_version = '2.0.0'
|
||||||
footer = 'malias version %s'%(app_version)
|
footer = 'malias version %s'%(app_version)
|
||||||
|
|
||||||
|
|
||||||
|
def unix_to_datetime(unix_timestamp):
|
||||||
|
return datetime.fromtimestamp(unix_timestamp)
|
||||||
|
|
||||||
|
|
||||||
def get_latest_release():
|
def get_latest_release():
|
||||||
version = 'N/A'
|
version = 'N/A'
|
||||||
req = httpx.get('https://gitlab.pm/api/v1/repos/rune/malias/releases/latest',
|
req = httpx.get('https://gitlab.pm/api/v1/repos/rune/malias/releases/latest',
|
||||||
@@ -457,7 +462,7 @@ def create_timed(username,domain):
|
|||||||
print('[b][red]Error[/red][/b] : something went wrong. The server responded with [b]access denied[/b].')
|
print('[b][red]Error[/red][/b] : something went wrong. The server responded with [b]access denied[/b].')
|
||||||
exit(0)
|
exit(0)
|
||||||
alias = get_last_timed(username)
|
alias = get_last_timed(username)
|
||||||
validity = datetime.utcfromtimestamp(alias['validity']).strftime('%d-%m-%Y %H:%M')
|
validity = unix_to_datetime(alias['validity'])
|
||||||
print('The timed alias %s was created. The alias is valid until %s UTC\n' %(alias['address'], validity))
|
print('The timed alias %s was created. The alias is valid until %s UTC\n' %(alias['address'], validity))
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute('INSERT INTO timedaliases values(?,?,?,?)', (alias['validity'],alias['address'],username,validity))
|
cursor.execute('INSERT INTO timedaliases values(?,?,?,?)', (alias['validity'],alias['address'],username,validity))
|
||||||
@@ -514,6 +519,26 @@ def export_data():
|
|||||||
json.dump(data, outfile, ensure_ascii=False, indent=4)
|
json.dump(data, outfile, ensure_ascii=False, indent=4)
|
||||||
|
|
||||||
|
|
||||||
|
def list_timed_aliases(account):
|
||||||
|
connection = get_settings('connection')
|
||||||
|
req = httpx.get('https://'+connection['server']+'/api/v1/get/time_limited_aliases/'+account,
|
||||||
|
headers={"Content-Type": "application/json",
|
||||||
|
'X-API-Key': connection['key']
|
||||||
|
}
|
||||||
|
)
|
||||||
|
data = req.json()
|
||||||
|
i = 0
|
||||||
|
print('\n[b]malias[/b] - Timed aliases on %s for %s' %(connection['server'], account))
|
||||||
|
print('==========================================================================================================')
|
||||||
|
for data in data:
|
||||||
|
the_alias = data['address'].ljust(30,' ')
|
||||||
|
the_goto = data['goto'].ljust(20,' ')
|
||||||
|
the_validity = unix_to_datetime(data['validity'])
|
||||||
|
print(the_alias + '\tgoes to\t\t' + the_goto+'Valid to: '+str(the_validity))
|
||||||
|
i=i+1
|
||||||
|
#print('\n\nTotal number of timed aliases on instance [b]%s[/b] for account.' %(connection['server'],account))
|
||||||
|
print('\n'+footer)
|
||||||
|
|
||||||
|
|
||||||
# For Testing purposes
|
# For Testing purposes
|
||||||
|
|
||||||
@@ -554,6 +579,8 @@ parser.add_argument('-d', '--delete', help='Delete alias.\n\n',
|
|||||||
nargs=1, metavar=('alias@domain.com'), required=False, action="append")
|
nargs=1, metavar=('alias@domain.com'), required=False, action="append")
|
||||||
parser.add_argument('-t', '--timed', help='Add new time limited alias for user on domain. \nThe user@domain.com is where you want the alias to be delivered to.\nThe domain.com is which domain to use when creating the timed-alias.\nOne year validity\n\n',
|
parser.add_argument('-t', '--timed', help='Add new time limited alias for user on domain. \nThe user@domain.com is where you want the alias to be delivered to.\nThe domain.com is which domain to use when creating the timed-alias.\nOne year validity\n\n',
|
||||||
nargs=2, metavar=('user@domain.com', 'domain.com'), required=False, action="append")
|
nargs=2, metavar=('user@domain.com', 'domain.com'), required=False, action="append")
|
||||||
|
parser.add_argument('-w', '--alias', help='List timed (temprary) aliases connected toone account.\n\n',
|
||||||
|
nargs=1, metavar=('alias@domain.com'), required=False, action="append")
|
||||||
parser.add_argument('-l', '--list', help='List all aliases on the Mailcow instance.\n\n',
|
parser.add_argument('-l', '--list', help='List all aliases on the Mailcow instance.\n\n',
|
||||||
required=False, action='store_true')
|
required=False, action='store_true')
|
||||||
parser.add_argument('-o', '--domains', help='List all mail domains on the Mailcow instance.\n\n',
|
parser.add_argument('-o', '--domains', help='List all mail domains on the Mailcow instance.\n\n',
|
||||||
@@ -580,6 +607,8 @@ elif args['delete']:
|
|||||||
delete_alias(args['delete'][0][0])
|
delete_alias(args['delete'][0][0])
|
||||||
elif args['timed']:
|
elif args['timed']:
|
||||||
create_timed(args['timed'][0][0],args['timed'][0][1])
|
create_timed(args['timed'][0][0],args['timed'][0][1])
|
||||||
|
elif args['alias']:
|
||||||
|
list_timed_aliases(args['alias'][0][0])
|
||||||
elif args['list']:
|
elif args['list']:
|
||||||
list_alias()
|
list_alias()
|
||||||
elif args['domains']:
|
elif args['domains']:
|
||||||
@@ -588,4 +617,4 @@ elif args['export']:
|
|||||||
export_data()
|
export_data()
|
||||||
else:
|
else:
|
||||||
print('\n\nEh, sorry! I need something more to help you! If you write [b]malias -h[/b] I\'ll show a help screen to get you going!!!\n\n\n')
|
print('\n\nEh, sorry! I need something more to help you! If you write [b]malias -h[/b] I\'ll show a help screen to get you going!!!\n\n\n')
|
||||||
#export_data()
|
|
||||||
|
Reference in New Issue
Block a user