#!/bin/sh
#------------------------------------------------------------------------------
# /usr/local/htdocs/mailmanager/mailmanager.cgi - mail manager script
#
# Copyright (c) 2004-2017  Marcus Herleb, marcus(at)herleb(dot)de
# Copyright (c) 2018-2018  The Eisfair Team, team(at)eisfair(dot)org
#
# Creation:     2004-03-01 fm
# Last Update:  $Id$
#
#   **** Thanks to Juergen Edner ****
#   **** Thanks to Harald Cramer ****
#   **** Thanks to Dennis Neuhaeuser ****
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version..
#------------------------------------------------------------------------------

myname=`basename $0`
tag=`date +%d.%m.%Y`
uhr=`date +%T`
RTIME=20

exim_spoolpath=/var/spool/exim
exim_pidfile=$exim_spoolpath/exim-daemon.pid
exim_ssmtp_pidfile=$exim_spoolpath/exim-ssmtp-daemon.pid
lockfile=/etc/nologin

no_fetch="unchecked"
fetch_status="unchecked"
no_exim="unchecked"
exim_status="unchecked"
no_ssmtp="unchecked"
ssmtp_status="unchecked"
no_pop3="unchecked"
pop3_status="unchecked"
no_imap4="unchecked"
imap4_status="unchecked"

# HTML-Header
echo "Content-Type: text/html; charset=iso-8859-1"
echo "Cache-control: no-store" # HTTP/1.1 (or no-cache?)
echo "Pragma: no-cache"        # HTTP/1.0
echo
echo "<html><head>"
echo "<title>EIS Mail Manager</title>"
echo '<LINK REL=STYLESHEET TYPE="text/css" HREF="../main.css">'
echo "<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"$RTIME;URL=$myname\">"
echo '<script type="text/javascript">
<!--
setTimeout("mailaction.close()",10000);
//-->
</script>'
echo "</head>"
echo
echo '<body bgcolor="white">'

#--------------------------------------------------------
# check XINET
killall -0 xinetd 2>/dev/null  # -0 is only a check

if [ $? -eq 0 ]
then
    no_xinetd=""            ## running
else
    no_xinetd="true"        ## not running
fi

#--------------------------------------------------------
# Check fetchmail
killall -0 fetchmail 2>/dev/null  # -0 is only a check

if [ $? -eq 0 ]
then
    no_fetch=""             ## running
else
    no_fetch="true"         ## not running
fi

#--------------------------------------------------------
# check SMTP / exim
if [ -e $exim_pidfile ]
then
    no_exim=""              ## running
else
    no_exim="true"          ## not running
fi

#--------------------------------------------------------
# check SSMTP
if [ -e $exim_ssmtp_pidfile ]
then
    no_ssmtp=""             ## running
else
    no_ssmtp="true"         ## not running
fi

#--------------------------------------------------------
# check POP3
if [ -z "$no_xinetd" ]
then
    grep 'disable[ \t]*=[ \t]no' /etc/xinetd.d/pop3 >/dev/null 2>&1

    if [ $? -eq 0 ]
    then
        if [ ! -f $lockfile ]
        then
            no_pop3=""      ## running
        fi
    fi
else
    no_pop3="true"          ## not running
fi

#--------------------------------------------------------
# check IMAP4
if [ -z "$no_xinetd" ]
then
    grep 'disable[ \t]*=[ \t]no' /etc/xinetd.d/imap >/dev/null 2>&1

    if [ $? -eq 0 ]
    then
        if [ ! -f $lockfile ]
        then
            no_imap4=""     ## running
        fi
    fi
else
    no_imap4="true"         ## not running
fi

#--------------------------------------------------------
if [ -z "$no_fetch" ]
then
    fetch_status="aktiv"
    startstop_M="Stop Mailserver"
    action_M=mailaction.cgi?Stop\
    f_color="#00FF00"    # green
else
    fetch_status="inaktiv"
    startstop_M="Start Mailserver"
    action_M=mailaction.cgi?Start\
    f_color="#FF0000"    # red
fi

if [ -z "$no_exim" ]
then
    smtp_status="aktiv"
    s_color="#00FF00"
else
    smtp_status="inaktiv"
    s_color="#FF0000"
fi

if [ -z "$no_ssmtp" ]
then
    ssmtp_status="aktiv"
    ss_color="#00FF00"
else
    ssmtp_status="inaktiv"
    ss_color="#FF0000"
fi

if [ -z "$no_pop3" ]
then
    pop3_status="aktiv"
    p_color="#00FF00"
else
    pop3_status="inaktiv"
    p_color="#FF0000"
fi

if [ -z "$no_imap4" ]
then
    imap_status="aktiv"
    i_color="#00FF00"
else
    imap_status="inaktiv"
    i_color="#FF0000"
fi

#--------------------------------------------------------
if [ -f $lockfile ]
then
    toggle_T="POP3/IMAP freigeben"
else
    toggle_T="POP3/IMAP sperren"
fi

action_T=mailaction.cgi?Toggle

if [ -z "${no_xinetd}" ]
then
    xinetd_status="aktiv"
    x_color="#00FF00"
    toggle_X="Stop XINETD"
    action_X="mailaction.cgi?XINETD_Stop"
else
    xinetd_status="inaktiv"
    x_color="#FF0000"
    toggle_X="Start XINETD"
    action_X="mailaction.cgi?XINETD_Start"
fi

#--------------------------------------------------------
echo '<table border="0">
            <tr>
                <td><font size="4">EIS Mail Manager 1.0.3 <font size=1> &copy; 2004, 2005, 2006 <a href=mailto:info@herleb.de>Marcus Herleb</a></font></font></td>
            </tr>

      </table>'

. /usr/local/htdocs/mailmanager/mailinfo.cgi

echo "<table border=\"0\" cellspacing=\"10\" height=\"262\" width=\"726\">
          <tr>
              <td bgcolor=\"#ffffff\" valign=\"top\" align=\"left\" width=\"396\">
                  <table border=\"0\" width=\"310\">
                      <tr>
                          <td colspan=\"2\" bgcolor=\"#cfcfcf\" height=\"20\"><b>&nbsp;Mail Status</b></td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" width=\"50%\">&nbsp;Fetchmail</td>
                          <td bgcolor=\"$f_color\" align=\"center\">$fetch_status</td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" width=\"50%\">&nbsp;SMTP Server</td>
                          <td bgcolor=\"$s_color\" align=\"center\">$smtp_status</td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" width=\"50%\">&nbsp;SSMTP Server</td>
                          <td bgcolor=\"$ss_color\" align=\"center\">$ssmtp_status</td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" width=\"50%\">&nbsp;POP3 daemon *)</td>
                          <td bgcolor=\"$p_color\" align=\"center\">$pop3_status</td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" width=\"50%\">&nbsp;IMAP4 daemon *)</td>
                          <td bgcolor=\"$i_color\" align=\"center\">$imap_status</td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" width=\"50%\">&nbsp;XINETD daemon *)</td>
                          <td bgcolor=\"$x_color\" align=\"center\">$xinetd_status</td>
                      </tr>
                      <tr>
                          <td colspan=\"2\" bgcolor=\"#cfcfcf\" height=\"18\">&nbsp;</td>
                      </tr>
                      <tr><td colspan=\"2\">
                          <span style=\"font-size:90%;\">*) POP3 und IMAP4 laufen nur, wenn auch XINETD l&auml;uft.</span>
                      </td></tr>
                      <tr>
                          <td colspan=\"2\" bgcolor=\"#cfcfcf\" height=\"20\"><b>&nbsp;Administration</b></td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" align=\"left\" width=\"50%\">
                              <input type=\"button\" style=\"width: 155px;\" value=\"Start Mail-Queue-Run\"
                                  onClick=mailaction=window.open(\"mailaction.cgi?Run\",\"mailaction\",\"width=220,height=80,left=0,top=0,resizable=yes\")></td>
                          <td bgcolor=\"#efefef\" align=\"right\" width=\"50%\">
                              <input type=\"button\" style=\"width: 155px;\" value=\"${startstop_M}\"
                                  onClick=mailaction=window.open(\"${action_M}\",\"mailaction\",\"width=220,height=260,left=0,top=0,resizable=yes\")></td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" align=\"left\" width=\"50%\">"

if [ -z "$no_fetch" ]
then
    echo "                    <input type=\"button\" style=\"width: 155px;\" value=\"Alle Mails abrufen\"
                                  onClick=mailaction=window.open(\"mailaction.cgi?Fetch\",\"mailaction\",\"width=400,height=80,left=0,top=0,resizable=yes\")>"
fi
echo  "                   </td>
                          <td bgcolor=\"#efefef\" align=\"right\" width=\"50%\">"

if [ -z "${no_xinetd}" ]
then
    echo "                    <input type=\"button\" style=\"width: 155px;\" value=\"${toggle_T}\"
                                  onClick=mailaction=window.open(\"${action_T}\",\"mailaction\",\"width=400,height=260,left=0,top=0,resizable=yes\")>"
fi

echo  "                   </td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" align=\"left\" width=\"50%\">
                              <input type=\"button\" style=\"width: 155px;\" value=\"TLS Zertifikate\"
                                  onClick=tls=window.open(\"mailaction.cgi?Check\",\"mailaction\",\"width=400,height=400,left=0,top=0,scrollbars=1,resizable=yes\")></td>
                          <td bgcolor=\"#efefef\" align=\"right\" width=\"50%\">
                              <input type=\"button\" style=\"width: 155px;\" value=\"${toggle_X}\"
                                  onClick=mailaction=window.open(\"${action_X}\",\"mailaction\",\"width=300,height=260,left=0,top=0,resizable=yes,\")></td>
                      </tr>
                      <tr>
                          <td colspan=\"2\" bgcolor=\"#cfcfcf\" height=\"18\">&nbsp;</td>
                      </tr>
                  </table>
              </td>
              <td width=\"415\" bgcolor=\"#ffffff\" valign=\"top\" align=\"left\">
                  <table border=\"0\">
                      <tr>
                          <td colspan=\"2\" bgcolor=\"#cfcfcf\" height=\"20\"><b>&nbsp;Mail Statistik</b></td>
                      </tr>
                      <tr>
                          <td bgcolor=\"#efefef\" nowrap=\"nowrap\">
                               <span>&nbsp;EXIM Statistik anzeigen&nbsp;</span></td>
                          <td bgcolor=\"#efefef\" align=\"center\">
                               <input type=\"button\" value=\"Zeigen\"
                                  onClick=statisitk=window.open(\"mail_view_stats.cgi?EXIM\",\"view\",\"width=700,height=600,left=0,top=0,scrollbars=1,resizable=yes\")></td>
                      </tr>"

if [ -f /usr/bin/perl ]
then
    echo "            <tr>
                          <td bgcolor=\"#efefef\" nowrap=\"nowrap\">
                              <span>&nbsp;EXIM&nbsp;Mail Log anzeigen&nbsp;</span></td>
                          <td bgcolor=\"#efefef\" align=\"center\">
                              <input type=\"button\" value=\"Zeigen\"
                                  onClick=statistik=window.open(\"mail_mainlog.cgi\",\"view\",\"width=1024,height=600,left=0,top=0,scrollbars=1,resizable=yes\")></td>
                      </tr>"
fi

echo "                <tr>
                            <td bgcolor=\"#efefef\" nowrap=\"nowrap\">
                               <span>&nbsp;Fetchmail Log anzeigen&nbsp;</span></td>
                            <td bgcolor=\"#efefef\" align=\"center\">
                               <input type=\"button\" value=\"Zeigen\"
                                  onClick=statistik=window.open(\"mail_view_stats.cgi?Fetchmail\",\"view\",\"width=900,height=600,left=0,top=0,scrollbars=1,resizable=yes\")></td>
                      </tr>"

exim_log_file=rejectlog

if [ -s $exim_spool_dir/log/$exim_log_file ]
then
    echo "            <tr>
                            <td bgcolor=\"#efefef\" nowrap=\"nowrap\">
                               <span style=\"color: red;\">&nbsp;Zur&uuml;ckgewiesene Mail&nbsp;</span></td>
                            <td bgcolor=\"#efefef\" align=\"center\">
                               <input type=\"button\" value=\"Zeigen\"
                                  onClick=statistik=window.open(\"mail_view_stats.cgi?Reject\",\"view\",\"width=1200,height=600,left=0,top=0,scrollbars=1,resizable=yes\")></td>
                      </tr>"
fi

exim_log_file=paniclog
if [ -s $exim_spool_dir/log/$exim_log_file ]
then
    echo "            <tr>
                            <td bgcolor=\"#efefef\" nowrap=\"nowrap\">
                              <span style=\"color: red;\">&nbsp;Panik Log&nbsp;</span></td>
                            <td bgcolor=\"#efefef\">
                              <input type=\"button\" value=\"Zeigen\"
                                 onClick=statistik=window.open(\"mail_view_stats.cgi?Panic\",\"view\",\"width=900,height=600,left=0,top=0,scrollbars=1,resizable=yes\")></td>
                      </tr>"
fi

echo "                <tr>
                          <td colspan=\"2\" bgcolor=\"#cfcfcf\" height=\"18\">&nbsp;</td>
                      </tr>
                  </table>
              </td>
          </tr>
      </table>
      <font size=2>Diese Seite erneuert sich alle '$RTIME' Sekunden. <br />
          Bis dahin werden angezeigte Zust&auml;nde nach &Auml;nderungen nicht korrekt angezeigt, es sei denn, es wird ein manueller 'Refresh' gemacht.</font>


    </body>
</html>"

exit 0