diff --git a/malias.py b/malias.py index 4dcf6ae..805aac8 100644 --- a/malias.py +++ b/malias.py @@ -50,7 +50,7 @@ def connect_database(): id INTEGER NOT NULL PRIMARY KEY, first_run INTEGER, server TEXT, - data_copy INTEGER" + data_copy INTEGER )''') c.execute('''CREATE TABLE IF NOT EXISTS aliases (id integer NOT NULL PRIMARY KEY, @@ -62,7 +62,8 @@ def connect_database(): first_run(conn) return conn - + + def first_run(conn): now = datetime.now().strftime("%m-%d-%Y %H:%M") cursor = conn.cursor() @@ -70,7 +71,7 @@ def first_run(conn): count = cursor.fetchone()[0] if count == 0: logging.error(now + ' - First run!') - cursor.execute('INSERT INTO settings values(?,?,?)', (1, 1, 'dummy.server')) + cursor.execute('INSERT INTO settings values(?,?,?,?)', (1, 1, 'dummy.server',0)) cursor.execute('INSERT INTO apikey values(?,?)', (1, 'DUMMY_KEY')) conn.commit() return None @@ -83,15 +84,18 @@ def get_settings(kind): data = cursor.fetchall() first_run_status = data[0][1] mail_server = data[0][2] + copy_status = data[0][3] if kind == 'mail_server': if mail_server == 'dummy.server': - print('Error: No MailCow server active. Please add one with [b]malias -m [i]your.server[/i][/b]') + print('Error: No mailcow server active. Please add one with [b]malias -m [i]your.server[/i][/b]') exit(0) else: return mail_server if kind == 'first_run_status': return first_run_status - + if kind == 'copy_status': + return copy_status + def get_api(): @@ -109,7 +113,7 @@ def set_mailserver(server): now = datetime.now().strftime("%m-%d-%Y %H:%M") cursor = conn.cursor() cursor.execute('UPDATE settings SET server = ? WHERE id = 1',(server,)) - logging.info(now + ' - Info : MailCow server updated') + logging.info(now + ' - Info : mailcow server updated') print('Your mail server has been updated.') conn.commit() @@ -129,8 +133,8 @@ def create(alias,to_address): server = get_settings('mail_server') apikey = get_api() if checklist(alias) == True: - logging.error(now + ' - Error : alias %s exists on the MailCow instance %s ' %(alias,server)) - print('\n[b]Error[/b] : alias %s exists on the MailCow instance %s \n' %(alias,server)) + logging.error(now + ' - Error : alias %s exists on the mailcow instance %s ' %(alias,server)) + print('\n[b]Error[/b] : alias %s exists on the mailcow instance %s \n' %(alias,server)) exit(0) else: data = {'address': alias,'goto': to_address,'active': "1"} @@ -139,24 +143,73 @@ def create(alias,to_address): 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)) + 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() + 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)) -def delete(alias): - print('Delete : ' + alias) - +def delete_alias(alias): + server = get_settings('mail_server') + apikey = get_api() + if checklist(alias) == True: + the_alias_id = alias_id(alias) + data = {'id': the_alias_id} + headers = {'X-API-Key': apikey, "Content-Type": "application/json"} + response = requests.post('https://'+server+'/api/v1/delete/alias', + data=json.dumps(data), headers=headers) + response_data = response.json() + print(response_data) + exit(0) + if response_data[0]['type'] == 'success': + if check_local_db(the_alias_id) == 1: + now = datetime.now().strftime("%m-%d-%Y %H:%M") + cursor = conn.cursor() + cursor.execute('DELETE from aliases where id = ?',(the_alias_id,)) + logging.info(now + ' - Info : alias %s deleted from the mailcow instance %s and Local DB' %(alias,server)) + conn.commit() + print('\n[b]Info[/b] : alias %s deleted from the mailcow instance %s and local DB' %(alias,server)) + else: + logging.info(now + ' - Info : alias %s deleted from the mailcow instance %s.' %(alias,server)) + print('\n[b]Info[/b] : alias %s deleted from the mailcow instance %s.' %(alias,server)) + else: + logging.error(now + ' - Error : alias %s NOT deleted from the mailcow instance %s. Error : %s' %(alias,server,response_data)) + conn.commit() + print('\n[b]Error[/b] : alias %s NOT deleted from the mailcow instance %s. Error : %s' %(alias,server,response_data)) + else: + print('\n[b]Error[/b] : The alias %s not found') def copy_data(): - print('copy') - + apikey = get_api() + mail_server = get_settings('mail_server') + if get_settings('copy_status') == 0: + now = datetime.now().strftime("%m-%d-%Y %H:%M") + cursor = conn.cursor() + 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 data in remoteData: + cursor.execute('INSERT INTO aliases values(?,?,?,?)', (remoteData[i]['id'], remoteData[i]['address'],remoteData[i]['goto'],now)) + i=i+1 + + cursor.execute('UPDATE settings SET data_copy = ? WHERE id = 1',(1,)) + conn.commit() + logging.info(now + ' - Info : aliases imported from the mailcow instance %s to local DB' %(mail_server)) + print('\n[b]Info[/b] : aliases imported from the mailcow instance %s to local DB\n' %(mail_server)) + else: + print('\n[b]Info[/b] : aliases alreday imported from the mailcow instance %s to local DB\n' %(mail_server)) + def checklist(alias): apikey = get_api() @@ -185,8 +238,11 @@ def list_alias(): remote = current.read().decode('utf-8') remoteData = json.loads(remote) i = 0 + print('\n[b]malias[/b] - All aliases on %s' %(mail_server)) + print('===================================================') for search in remoteData: - print(remoteData[i]['address']) + the_alias = remoteData[i]['address'].ljust(20,' ') + print(the_alias + '\tgoes to\t\t' + remoteData[i]['goto']) i=i+1 @@ -215,13 +271,35 @@ def number_of_aliases_in_db(): return count + +def number_of_aliases_on_server(): + 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) + return len(remoteData) + + +def check_local_db(alias_id): + cursor = conn.cursor() + cursor.execute('SELECT count(*) FROM aliases where id = ?',(alias_id,)) + count = cursor.fetchone()[0] + return count + + + + def search(alias): results = checklist(alias) mail_server = get_settings('mail_server') if results == True: - print('\n\nThe mail address %s exists. Using the MailCow instance : %s\n\n'%(alias,mail_server)) + print('\n\nThe mail address %s exists. Using the mailcow instance : %s\n\n'%(alias,mail_server)) else: - print('\n\nThe mail address %s [b]does not[/b] exists. Using the MailCow instance : %s\n\n'%(alias,mail_server)) + print('\n\nThe mail address %s [b]does not[/b] exists. Using the mailcow instance : %s\n\n'%(alias,mail_server)) @@ -233,15 +311,15 @@ def show_current_info(): API = 'Missing API Key!' if mail_server == 'dummy.server': - mail_server = 'Missing address to MailCow instance!' + mail_server = 'Missing address to mailcow instance!' aliases_server = number_of_aliases_on_server() alias_db = number_of_aliases_in_db() - print('\n[b]malias[/b] - Manage aliases on MailCow Instance.') + print('\n[b]malias[/b] - Manage aliases on mailcow Instance.') print('===================================================') print('API key : [b]%s[/b]' % (API)) - print('MailCow Instance : [b]%s[/b]' % (mail_server)) + print('mailcow Instance : [b]%s[/b]' % (mail_server)) print('Logfile : [b]%s[/b]' % (logfile)) print('Aliases on server : [b]%s[/b]' % (aliases_server)) print('Aliases in DB : [b]%s[/b]' % (alias_db)) @@ -254,9 +332,9 @@ conn = connect_database() parser = argparse.ArgumentParser(prog='malias', - description='Application description', + description='This is a simple application to help you create and delete aliases on a mailcow instance.\nIf you find any issues or would like to submit a PR - please head over to https://gitlab.pm/rune/malias. \n\nI hope this makes your mailcow life a bit easier!', formatter_class=RawTextHelpFormatter, - epilog='Making MailCow easier...') + epilog='Making mailcow easier...') parser.add_argument('-k', '--api', help='Add/Change API key.\n\n', nargs=1, metavar=('APIkey'), required=False, action="append") @@ -264,19 +342,27 @@ parser.add_argument('-k', '--api', help='Add/Change API key.\n\n', parser.add_argument('-s', '--search', help='Search for alias.\n\n', nargs=1, metavar=('alias@domain.com'), required=False, action="append") -parser.add_argument('-m', '--server', help='Add/Uppdate MailCow instance.\n\n', - nargs=1, metavar=('alias@domain.com'), required=False, action="append") +parser.add_argument('-m', '--server', help='Add/Uppdate mailcow instance.\n\n', + nargs=1, metavar=('mailcow-server.tld'), required=False, action="append") parser.add_argument('-a', '--add', help='Add new alias.\n\n', nargs=2, metavar=('alias@domain.com', 'to@domain.com'), required=False, action="append") +parser.add_argument('-d', '--delete', help='Delete alias.\n\n', + nargs=1, metavar=('alias@domain.com'), required=False, action="append") + + parser.add_argument('-v', '--version', help='Show current version and config info\n\n', required=False, action='store_true') -parser.add_argument('-c', '--copy', help='Copy alias data from MailCow server to local DB.\n\n', +parser.add_argument('-c', '--copy', help='Copy alias data from mailcow server to local DB.\n\n', required=False, action='store_true') +parser.add_argument('-l', '--list', help='List all aliases on the Mailcow instance.\n\n', + required=False, action='store_true') + + args = vars(parser.parse_args()) if args['api']: @@ -287,14 +373,15 @@ elif args['server']: set_mailserver(args['server'][0][0]) elif args['add']: create(args['add'][0][0],args['add'][0][1]) +elif args['delete']: + delete_alias(args['delete'][0][0]) elif args['version']: show_current_info() -elif args['version']: +elif args['copy']: copy_data() - +elif args['list']: + list_alias() + 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') \ No newline at end of file + 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') \ No newline at end of file