Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
f653a20d35 | |||
7f12bd3743 |
71
malias.py
71
malias.py
@@ -29,7 +29,7 @@ database = filepath.joinpath('malias.db')
|
|||||||
logfile = filepath.joinpath('malias.log')
|
logfile = filepath.joinpath('malias.log')
|
||||||
Path(filepath).mkdir(parents=True, exist_ok=True)
|
Path(filepath).mkdir(parents=True, exist_ok=True)
|
||||||
logging.basicConfig(filename=logfile,level=logging.INFO,format='%(message)s')
|
logging.basicConfig(filename=logfile,level=logging.INFO,format='%(message)s')
|
||||||
app_version = '0.2.2'
|
app_version = '0.2.4'
|
||||||
|
|
||||||
|
|
||||||
def connect_database():
|
def connect_database():
|
||||||
@@ -127,6 +127,30 @@ def apikey(key):
|
|||||||
print('Your API key has been updated.')
|
print('Your API key has been updated.')
|
||||||
conn.commit()
|
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):
|
def create(alias,to_address):
|
||||||
now = datetime.now().strftime("%m-%d-%Y %H:%M")
|
now = datetime.now().strftime("%m-%d-%Y %H:%M")
|
||||||
@@ -206,8 +230,42 @@ def copy_data():
|
|||||||
logging.info(now + ' - Info : aliases imported from the mailcow instance %s to local DB' %(mail_server))
|
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))
|
print('\n[b]Info[/b] : aliases imported from the mailcow instance %s to local DB\n' %(mail_server))
|
||||||
else:
|
else:
|
||||||
print('\n[b]Info[/b] : aliases alreday imported from the mailcow instance %s to local DB\n' %(mail_server))
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
def update_data():
|
||||||
|
apikey = get_api()
|
||||||
|
mail_server = get_settings('mail_server')
|
||||||
|
if get_settings('copy_status') == 1:
|
||||||
|
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
|
||||||
|
count_alias = 0
|
||||||
|
cursor = conn.cursor()
|
||||||
|
for data in remoteData:
|
||||||
|
cursor.execute('SELECT count(*) FROM aliases where alias like ? and goto like ?', (remoteData[i]['address'],remoteData[i]['goto'],))
|
||||||
|
count = cursor.fetchone()[0]
|
||||||
|
if count >= 1 :
|
||||||
|
i+=1
|
||||||
|
else:
|
||||||
|
cursor.execute('INSERT INTO aliases values(?,?,?,?)', (remoteData[i]['id'], remoteData[i]['address'],remoteData[i]['goto'],now))
|
||||||
|
count_alias+=1
|
||||||
|
i+=1
|
||||||
|
conn.commit()
|
||||||
|
if count_alias > 0:
|
||||||
|
logging.info(now + ' - Info : Local DB updated with %s new aliases from %s ' %(str(count_alias),mail_server))
|
||||||
|
print('\n[b]Info[/b] : Local DB update with %s new aliases from %s \n' %(str(count_alias),mail_server))
|
||||||
|
else:
|
||||||
|
print('\n[b]Info[/b] : No missing aliases from local DB \n')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def checklist(alias):
|
def checklist(alias):
|
||||||
alias_exist = None
|
alias_exist = None
|
||||||
@@ -393,7 +451,7 @@ def show_current_info():
|
|||||||
print('Aliases on server : [b]%s[/b]' % (aliases_server))
|
print('Aliases on server : [b]%s[/b]' % (aliases_server))
|
||||||
print('Aliases in DB : [b]%s[/b]' % (alias_db))
|
print('Aliases in DB : [b]%s[/b]' % (alias_db))
|
||||||
print('')
|
print('')
|
||||||
print('App version : [b]%s[/b] (https://gitlab.pm/rune/malias)' % (app_version))
|
print('App version : [b]%s[/b] (https://iurl.no/malias)' % (app_version))
|
||||||
print('')
|
print('')
|
||||||
|
|
||||||
|
|
||||||
@@ -431,6 +489,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',
|
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',
|
||||||
|
required=False, action='store_true')
|
||||||
|
|
||||||
|
|
||||||
args = vars(parser.parse_args())
|
args = vars(parser.parse_args())
|
||||||
|
|
||||||
@@ -450,6 +511,8 @@ elif args['copy']:
|
|||||||
copy_data()
|
copy_data()
|
||||||
elif args['list']:
|
elif args['list']:
|
||||||
list_alias()
|
list_alias()
|
||||||
|
elif args['domains']:
|
||||||
|
get_mail_domains()
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user