From 7f12bd374358c9b9af01487598d21f97d3c484b4 Mon Sep 17 00:00:00 2001 From: rune Date: Fri, 1 Sep 2023 11:22:54 +0200 Subject: [PATCH] Added function for listing mail domains --- malias.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/malias.py b/malias.py index ff0196d..87bc285 100755 --- a/malias.py +++ b/malias.py @@ -29,7 +29,7 @@ database = filepath.joinpath('malias.db') logfile = filepath.joinpath('malias.log') Path(filepath).mkdir(parents=True, exist_ok=True) logging.basicConfig(filename=logfile,level=logging.INFO,format='%(message)s') -app_version = '0.2.2' +app_version = '0.2.3' def connect_database(): @@ -127,6 +127,30 @@ def apikey(key): print('Your API key has been updated.') conn.commit() + +def get_mail_domains(): + apikey = get_api() + cursor = conn.cursor() + mail_server = get_settings('mail_server') + req = urllib.request.Request('https://'+mail_server+'/api/v1/get/domain/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) + total_aliases = 0 + i=0 + print('\n[b]malias[/b] - All email domains on %s' %(mail_server)) + print('==================================================================') + for domains in remoteData: + cursor.execute('SELECT count(*) FROM aliases where alias like ? or goto like ?', ('%'+remoteData[i]['domain_name']+'%','%'+remoteData[i]['domain_name']+'%',)) + count = cursor.fetchone()[0] + total_aliases += count + print('%s \t\twith %s aliases' %(remoteData[i]['domain_name'],count)) + i+=1 + print('\n\nThere is a total of %s domains with %s aliases.' %(str(i),str(total_aliases))) + + def create(alias,to_address): now = datetime.now().strftime("%m-%d-%Y %H:%M") @@ -431,6 +455,9 @@ parser.add_argument('-c', '--copy', help='Copy alias data from mailcow server to parser.add_argument('-l', '--list', help='List all aliases on the Mailcow instance.\n\n', required=False, action='store_true') +parser.add_argument('-o', '--domains', help='List all mail domains on the Mailcow instance.\n\n', + required=False, action='store_true') + args = vars(parser.parse_args()) @@ -450,6 +477,8 @@ elif args['copy']: copy_data() elif args['list']: list_alias() +elif args['domains']: + get_mail_domains() else: