From e8d45fc4779b4a2703f727c3495691d4e86cf57a Mon Sep 17 00:00:00 2001 From: rune Date: Tue, 3 Oct 2023 10:15:50 +0200 Subject: [PATCH] Code cleanup, changed version check and updated search function --- malias.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/malias.py b/malias.py index bc36cab..22c04d5 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.3' +app_version = '0.3.1' def get_latest_release(): @@ -41,13 +41,12 @@ def get_latest_release(): return remoteData['tag_name'] -latest_release = get_latest_release() - def release_check(): + latest_release = get_latest_release() if app_version != latest_release: print('[b]New version available @ [i]https://iurl.no/malias[/b][/i]') else: - print ('') + print ('You have the the latest version. Version: %s' %(app_version)) def connect_database(): @@ -135,7 +134,6 @@ def set_mailserver(server): logging.info(now + ' - Info : mailcow server updated') print('Your mail server has been updated.') conn.commit() - release_check() def apikey(key): @@ -145,7 +143,7 @@ def apikey(key): logging.info(now + ' - Info : API key updated') print('Your API key has been updated.') conn.commit() - release_check() + def get_mail_domains(info): apikey = get_api() @@ -169,7 +167,7 @@ def get_mail_domains(info): 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))) - release_check() + else: return(remoteData) @@ -197,7 +195,7 @@ def create(alias,to_address): conn.commit() logging.info(now + ' - Info : alias %s created for %s on the mailcow instance %s ' %(alias,to_address,server)) print('\n[b]Info[/b] : alias %s created for %s on the mailcow instance %s \n' %(alias,to_address,server)) - release_check() + def delete_alias(alias): @@ -228,7 +226,6 @@ def delete_alias(alias): else: print('\n[b]Error[/b] : The alias %s not found') - release_check() @@ -256,7 +253,6 @@ def copy_data(): else: print('\n[b]Info[/b] : aliases alreday imported from the mailcow instance %s to local DB\n\n[i]Updating with any missing aliases![/i]' %(mail_server)) update_data() - release_check() def update_data(): @@ -341,7 +337,7 @@ def list_alias(): print(the_alias + '\tgoes to\t\t' + the_goto) i=i+1 print('\n\nTotal number of aliases %s on instance [b]%s[/b] and %s on [b]local DB[/b].' %(str(i),mail_server,str(l))) - release_check() + def alias_id(alias): apikey = get_api() @@ -391,6 +387,8 @@ def check_local_db(alias_id): def search(alias): apikey = get_api() mail_server = get_settings('mail_server') + alias_server = number_of_aliases_on_server() + alias_db = number_of_aliases_in_db() cursor = conn.cursor() cursor.execute('SELECT data_copy FROM settings where id = 1') result = cursor.fetchone()[0] @@ -405,8 +403,8 @@ def search(alias): the_alias = remoteData[i][1].ljust(20,' ') print(the_alias + '\tgoes to\t\t' + remoteData[i][2]) i=i+1 - print('\n\nData from local DB\n Run [i]malias -c to get any aliases on the server but [b]not[/b] in the local DB![/i]') - release_check() + print('\n\nData from local DB') + else: req = urllib.request.Request('https://'+mail_server+'/api/v1/get/alias/all') req.add_header('Content-Type', 'application/json') @@ -422,7 +420,9 @@ def search(alias): print(remoteData[i]['address'] + '\tgoes to\t\t' + remoteData[i]['goto']) i=i+1 print('\n\nData from server') - release_check() + if alias_server != alias_db: + print('\n\nThere are %s aliases on the server and %s aliases in local DB' %(str(alias_server),str(alias_db))) + print('Run [i]malias -c[/i] to update local DB') def get_mailcow_version(): @@ -434,11 +434,7 @@ def get_mailcow_version(): current = urllib.request.urlopen(req) remote = current.read().decode('utf-8') remoteData = json.loads(remote) - #repo = Repo("https://github.com/mailcow/mailcow-dockerized") - # info = repo.git.ls_remote() - # tags = repo.tags remote_heads = git.cmd.Git().ls_remote('https://github.com/mailcow/mailcow-dockerized', tags=True) - # tags = remote_heads.rstrip('refs/tags/') tags = remote_heads.splitlines() for x in tags: string = x.rsplit('/',2) @@ -454,7 +450,7 @@ def get_mailcow_version(): def show_current_info(): API = get_api() mail_server = get_settings('mail_server') - + latest_release = get_latest_release() if API == 'DUMMY_KEY': API = 'Missing API Key!' @@ -516,7 +512,10 @@ parser.add_argument('-d', '--delete', help='Delete alias.\n\n', nargs=1, metavar=('alias@domain.com'), required=False, action="append") -parser.add_argument('-i', '--info', help='Show current version and config info\n\n', +parser.add_argument('-i', '--info', help='Show current config and appliacation info\n\n', + required=False, action='store_true') + +parser.add_argument('-v', '--version', help='Show current version\n\n', required=False, action='store_true') parser.add_argument('-c', '--copy', help='Copy alias data from mailcow server to local DB.\n\n', @@ -543,6 +542,8 @@ elif args['delete']: delete_alias(args['delete'][0][0]) elif args['info']: show_current_info() +elif args['version']: + release_check() elif args['copy']: copy_data() elif args['list']: