mailcnf_posttools.py 5.59 KB
#!/usr/bin/python3

# -*- coding: UTF-8 -*-# enable debugging
from mysql.connector import MySQLConnection, Error
from read_dbconfig import read_db_config
import tornado.ioloop
import tornado.web
import  tornado.escape
import  tornado
import os.path
import sys
from subprocess import Popen, PIPE
#from itertools import izip
#from baseclasses.htmltools import htmltools

from ncauth.ncauth_base import ncauth_base
class mailcnf_posttools(ncauth_base):
    
    def get(self):
         self.redirect("/")
    
    @tornado.web.authenticated
    def post(self): 
        nav=all
        subnav=all
        content=all
        msglvl=""  
        msg=False
        
        mainfnc="posttools"
        #module= self.get_argument("module", None, False)
        #mainfnc= self.get_argument("mainfnc", None, False)
        #if module== False:
        module="mailcnf"
        #if mainfnc== False:
        mainfnc="posttools"	 
        id= self.get_argument("id", None, False)
        mailname= self.get_argument("mailname", None, False)
        action= self.get_argument("action", None, False)
        fnc= self.get_argument("fnc", None, False)
        if action == 'postersuper1':
             msg = self.postsuperdel(id,mailname)
             msglvl="alert-info"  
        if action == 'postersupersend1':
             msg = self.postersupersend1(id,mailname)
             msglvl="alert-info"  
        if action == 'postersupershow':
             msg = '<h3>Inhalt der Mail</h3><pre>'+self.postersupershow(id,mailname)+'</pre><h3>Ende der Mail</h3>'
             msglvl="alert-info"  
        loader =tornado. template.Loader(os.path.join(os.path.dirname(__file__), "../templates"))
        navigation=loader.load("navigation.html").generate(nav=nav)
        subnavigation=loader.load("subnav_"+module+"_"+mainfnc+".html").generate(subnav=subnav)
        mailstuff=self.mailcheck()
        mailstufflist=self.mailchecktuple()
        number=10
        checklogin=self.checklogin(number)
        maincontent=loader.load("content_"+module+"_"+mainfnc+".html").generate(mailstuff=mailstuff,mailstufflist=mailstufflist,checklogin=checklogin)
        self.render('base.html', navigation=navigation, subnavigation=subnavigation, maincontent=maincontent, msg=msg, msglvl=msglvl)
    
    

    def mailcheck(self):
        old_dir = os.getcwd()
        os.chdir( os.path.join(os.path.dirname(__file__), "") )
        os.chdir( '..')
        p1 = str(Popen(['/usr/sbin/postqueue','-p'],stdout=PIPE) .communicate()[0])
        thatLine = p1.replace('\\n', '<br />')
        thatLine = thatLine.replace('b\'', '<br>')
        thatLine = thatLine.replace('\'', '')
        print(str(thatLine))
        return thatLine
        
    def mailchecktuple(self):
        old_dir = os.getcwd()
        os.chdir( os.path.join(os.path.dirname(__file__), "") )
        os.chdir( '..')
#        p1 = Popen(['/usr/sbin/postqueue','-p'],stdout=PIPE)
        p1 = str(Popen("/usr/sbin/postqueue -p | awk 'NR % 4 == 2' | awk   '{print $1 \"\\n\" $7 }' |egrep -v '^-' | egrep -v '^$' " ,  stdout=PIPE, shell=True).communicate()[0])
#        p3 = Popen(['egrep', '-v','"\'-\'"'], stdin=p2.stdout, stdout=PIPE)
#        p4 = Popen(['egrep', '-v','"^$"'], stdin=p3.stdout, stdout=PIPE)
#        p5 = Popen(['egrep', '-v','"^\("'], stdin=p4.stdout, stdout=PIPE).communicate()[0]
        thatLine = p1.replace('\\n', ';')
        thatLine = thatLine.replace('b\'', '')
        thatLine = thatLine.replace('\'', '')
        #print(str(thatLine))
		
		
        list = [item for item in thatLine.split(';') if item.strip()]
#        tuple = (item for item in thatLine.split(';') if item.strip())
        i = iter(list)
        dict = (zip(i, i))
        return dict

    def postsuperdel(self,id,mailname=''):
        #self.write("postsuper -d "+str(id)+" " )
        old_dir = os.getcwd()
        os.chdir( os.path.join(os.path.dirname(__file__), "") )
        os.chdir( '..')
        p1 = str(Popen("/usr/sbin/postsuper -d "+str(id) ,  stdout=PIPE, shell=True).communicate()[0])
        return "Nachricht "+str(id)+" von  "+str(mailname)+" gelöscht" 
	
    def postersupersend1(self,id,mailname=''):
        #self.write("postsuper -d "+str(id)+" " )
        old_dir = os.getcwd()
        os.chdir( os.path.join(os.path.dirname(__file__), "") )
        os.chdir( '..')
        p1 = str(Popen("/usr/sbin/postqueue -i "+str(id) ,  stdout=PIPE, shell=True).communicate()[0])
        return "Nachricht "+str(id)+" von  "+str(mailname)+" wird erneut versucht zu senden" 
	
    def postersupershow(self,id,mailname=''):
        #self.write("postsuper -d "+str(id)+" " )
        old_dir = os.getcwd()
        os.chdir( os.path.join(os.path.dirname(__file__), "") )
        os.chdir( '..')
        p1 = str(Popen("/usr/sbin/postcat -q "+str(id) ,  stdout=PIPE, shell=True).communicate()[0])
        thatLine = p1.replace('\\n', '<br />')
        thatLine = thatLine.replace('b\'', '<br>')
        thatLine = thatLine.replace('\'', '')
        print(str(thatLine))
        return thatLine
        #return "Nachricht "+str(id)+" von  "+str(mailname)+" wird erneut versucht zu senden" 
	
    
    
    
    def checklogin(self,number):
        #self.write("postsuper -d "+str(id)+" " )
        old_dir = os.getcwd()
        os.chdir( os.path.join(os.path.dirname(__file__), "") )
        os.chdir( '..')
        p1 = str(Popen("grep sasl_username /var/log/mail.log |awk {'print $NF'}|sort| uniq -c|sort -n| awk '$1 > "+str(number)+" { print $1 \" \" $2 }' " ,  stdout=PIPE, shell=True).communicate()[0])
        thatLine = p1.replace('\\n', '<br>')
        thatLine = thatLine.replace('b\'', '<br>')
        thatLine = thatLine.replace('\'', '')
        return thatLine