Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/imail/index.py

223 lines
6.2 KiB

3 years ago
# coding:utf-8
import sys
import io
import os
import time
import re
import socket
import json
from datetime import datetime
web_dir = os.getcwd() + "/web"
if os.path.exists(web_dir):
sys.path.append(web_dir)
os.chdir(web_dir)
import core.mw as mw
3 years ago
app_debug = False
if mw.isAppleSystem():
app_debug = True
class App:
__setupPath = '/www/server/imail'
3 years ago
__SR = ''
3 years ago
def __init__(self):
self.__setupPath = self.getServerDir()
3 years ago
self.__SR = '''#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
export USER=%s
export HOME=%s && ''' % ( self.getRunUser(), self.getHomeDir())
3 years ago
def getArgs(self):
args = sys.argv[3:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
3 years ago
def __release_port(self, port):
from collections import namedtuple
try:
import firewall_api
3 years ago
firewall_api.firewall_api().addAcceptPortArgs(port, 'IMail-Server', 'port')
3 years ago
return port
except Exception as e:
return "Release failed {}".format(e)
def openPort(self):
for i in ["25", "110", "143", "465", "995", "993", "587"]:
self.__release_port(i)
return True
3 years ago
def getPluginName(self):
3 years ago
return 'imail'
3 years ago
def getPluginDir(self):
return mw.getPluginDir() + '/' + self.getPluginName()
def getServerDir(self):
return mw.getServerDir() + '/' + self.getPluginName()
3 years ago
def getInitdConfTpl(self):
path = self.getPluginDir() + "/init.d/imail.tpl"
return path
def getHomeDir(self):
if mw.isAppleSystem():
user = mw.execShell(
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
return '/Users/' + user
else:
return '/root'
def getRunUser(self):
if mw.isAppleSystem():
user = mw.execShell(
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
return user
else:
return 'root'
3 years ago
def status(self):
data = mw.execShell(
"ps -ef|grep " + self.getPluginName() + " |grep -v grep | grep -v python | awk '{print $2}'")
if data[0] == '':
return 'stop'
return 'start'
3 years ago
def contentReplace(self, content):
service_path = mw.getServerDir()
6 months ago
content = content.replace('{$ROOT_PATH}', mw.getFatherDir())
3 years ago
content = content.replace('{$SERVER_PATH}', service_path)
content = content.replace('{$RUN_USER}', self.getRunUser())
content = content.replace('{$HOME_DIR}', self.getHomeDir())
return content
3 years ago
def initDreplace(self):
file_tpl = self.getInitdConfTpl()
service_path = mw.getServerDir()
initD_path = self.getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
3 years ago
self.openPort()
3 years ago
file_bin = initD_path + '/' + self.getPluginName()
if not os.path.exists(file_bin):
content = mw.readFile(file_tpl)
3 years ago
content = self.contentReplace(content)
3 years ago
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
# systemd
3 years ago
systemDir = mw.systemdCfgDir()
systemService = systemDir + '/imail.service'
3 years ago
systemServiceTpl = self.getPluginDir() + '/init.d/imail.service.tpl'
3 years ago
if os.path.exists(systemDir) and not os.path.exists(systemService):
service_path = mw.getServerDir()
se_content = mw.readFile(systemServiceTpl)
se_content = se_content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(systemService, se_content)
mw.execShell('systemctl daemon-reload')
3 years ago
log_path = self.getServerDir() + '/logs'
3 years ago
if not os.path.exists(log_path):
os.mkdir(log_path)
3 years ago
return file_bin
def imOp(self, method):
file = self.initDreplace()
if not mw.isAppleSystem():
3 years ago
cmd = 'systemctl {} {}'.format(method, self.getPluginName())
data = mw.execShell(cmd)
3 years ago
if data[1] == '':
return 'ok'
return 'fail'
3 years ago
data = mw.execShell(self.__SR + file + ' ' + method)
3 years ago
if data[1] == '':
return 'ok'
return data[0]
def start(self):
return self.imOp('start')
def stop(self):
return self.imOp('stop')
def restart(self):
return self.imOp('restart')
def reload(self):
return self.imOp('reload')
3 years ago
def initd_status(self):
if mw.isAppleSystem():
return "Apple Computer does not support"
cmd = 'systemctl status imail | grep loaded | grep "enabled;"'
data = mw.execShell(cmd)
if data[0] == '':
return 'fail'
return 'ok'
def initd_install(self):
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl enable imail')
return 'ok'
def initd_uinstall(self):
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl disable imail')
return 'ok'
def conf(self):
3 years ago
conf_path = self.getServerDir() + '/custom/conf/app.conf'
if not os.path.exists(conf_path):
return mw.returnJson(False, "请先安装初始化!<br/>默认地址:http://" + mw.getLocalIp() + ":1080")
3 years ago
return self.getServerDir() + '/custom/conf/app.conf'
def run_log(self):
3 years ago
ilog = self.getServerDir() + '/logs/imail.log'
if not os.path.exists(ilog):
return mw.returnJson(False, "请先安装初始化!<br/>默认地址:http://" + mw.getLocalIp() + ":1080")
3 years ago
return self.getServerDir() + '/logs/imail.log'
3 years ago
if __name__ == "__main__":
func = sys.argv[1]
classApp = App()
try:
data = eval("classApp." + func + "()")
print(data)
except Exception as e:
print('error:' + str(e))