2015-06-04 20:18:44 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: cp1252 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
eMail-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages via eMail/SMTP
|
|
|
|
|
|
|
|
|
|
@author: Jens Herrmann
|
|
|
|
|
|
|
|
|
|
@requires: eMail-Configuration has to be set in the config.ini
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging # Global logger
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
import smtplib #for the SMTP client
|
|
|
|
|
from email.mime.text import MIMEText # Import the email modules we'll need
|
2015-06-04 20:26:46 +02:00
|
|
|
from email.utils import formatdate # need for confirm to RFC2822 standard
|
|
|
|
|
from email.utils import make_msgid # need for confirm to RFC2822 standard
|
2015-06-04 20:18:44 +02:00
|
|
|
|
|
|
|
|
from includes import globals # Global variables
|
2015-07-03 23:43:22 +02:00
|
|
|
from includes.helper import timeHandler # helper function
|
2015-06-04 20:18:44 +02:00
|
|
|
|
2015-06-29 12:19:44 +02:00
|
|
|
##
|
|
|
|
|
#
|
2015-06-29 23:24:48 +02:00
|
|
|
# onLoad (init) function of plugin
|
|
|
|
|
# will be called one time by the pluginLoader on start
|
2015-06-29 12:19:44 +02:00
|
|
|
#
|
|
|
|
|
def onLoad():
|
|
|
|
|
"""
|
|
|
|
|
While loading the plugins by pluginLoader.loadPlugins()
|
2015-06-29 23:24:48 +02:00
|
|
|
this onLoad() routine is called one time for initialize the plugin
|
2015-06-29 12:19:44 +02:00
|
|
|
|
|
|
|
|
@requires: nothing
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-29 12:19:44 +02:00
|
|
|
@return: nothing
|
|
|
|
|
"""
|
2015-06-29 23:24:48 +02:00
|
|
|
# nothing to do for this plugin
|
|
|
|
|
return
|
2015-06-04 20:18:44 +02:00
|
|
|
|
|
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# do send mail
|
|
|
|
|
#
|
|
|
|
|
def doSendmail(server, subject, mailtext):
|
|
|
|
|
"""
|
|
|
|
|
This local function send the eMail
|
|
|
|
|
|
|
|
|
|
@type server: SMTP
|
|
|
|
|
@param server: An SMTP-Object that represents an open connection to an eMail-Server
|
|
|
|
|
@type subject: string
|
|
|
|
|
@param subject: Subject for the eMail
|
|
|
|
|
@type mailtext: string
|
|
|
|
|
@param mailtext: Mailtext for the eMail
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-04 20:18:44 +02:00
|
|
|
@return: nothing
|
|
|
|
|
@exception: Exception if smtp.sendmail failed
|
|
|
|
|
"""
|
2015-07-02 09:02:49 +02:00
|
|
|
try:
|
2015-06-04 20:18:44 +02:00
|
|
|
msg = MIMEText(mailtext)
|
|
|
|
|
msg['From'] = globals.config.get("eMail", "from")
|
|
|
|
|
msg['To'] = globals.config.get("eMail", "to")
|
|
|
|
|
msg['Subject'] = subject
|
|
|
|
|
msg['Date'] = formatdate()
|
|
|
|
|
msg['Message-Id'] = make_msgid()
|
|
|
|
|
msg['Priority'] = globals.config.get("eMail", "priority")
|
2015-07-03 23:43:22 +02:00
|
|
|
server.sendmail(globals.config.get("eMail", "from"), globals.config.get("eMail", "to").split(), msg.as_string())
|
2015-06-04 20:18:44 +02:00
|
|
|
except:
|
2015-06-23 22:27:48 +02:00
|
|
|
logging.error("send eMail failed")
|
|
|
|
|
logging.debug("send eMail failed", exc_info=True)
|
|
|
|
|
raise
|
2015-06-04 20:18:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# Main function of eMail-plugin
|
|
|
|
|
# will be called by the alarmHandler
|
|
|
|
|
#
|
|
|
|
|
def run(typ,freq,data):
|
|
|
|
|
"""
|
|
|
|
|
This function is the implementation of the eMail-Plugin.
|
|
|
|
|
It will send the data via eMail (SMTP)
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-04 20:18:44 +02:00
|
|
|
The configuration for the eMail-Connection is set in the config.ini.
|
|
|
|
|
If an user is set, the HTTP-Request is authenticatet.
|
|
|
|
|
|
|
|
|
|
@type typ: string (FMS|ZVEI|POC)
|
|
|
|
|
@param typ: Typ of the dataset for sending via eMail
|
|
|
|
|
@type data: map of data (structure see interface.txt)
|
|
|
|
|
@param data: Contains the parameter for dispatch to eMail.
|
|
|
|
|
@type freq: string
|
|
|
|
|
@keyword freq: frequency of the SDR Stick
|
|
|
|
|
|
|
|
|
|
@requires: eMail-Configuration has to be set in the config.ini
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-04 20:18:44 +02:00
|
|
|
@return: nothing
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
#
|
|
|
|
|
# ConfigParser
|
|
|
|
|
#
|
|
|
|
|
logging.debug("reading config file")
|
|
|
|
|
try:
|
|
|
|
|
for key,val in globals.config.items("eMail"):
|
|
|
|
|
logging.debug(" - %s = %s", key, val)
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-04 20:18:44 +02:00
|
|
|
except:
|
2015-06-23 22:27:48 +02:00
|
|
|
logging.error("cannot read config file")
|
|
|
|
|
logging.debug("cannot read config file", exc_info=True)
|
2015-06-29 12:25:21 +02:00
|
|
|
else: # Without config, plugin couldn't work
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
#
|
|
|
|
|
# connect to SMTP-Server
|
|
|
|
|
#
|
|
|
|
|
server = smtplib.SMTP(globals.config.get("eMail", "smtp_server"), globals.config.get("eMail", "smtp_port"))
|
|
|
|
|
# debug-level to shell (0=no debug|1)
|
|
|
|
|
server.set_debuglevel(0)
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-29 12:25:21 +02:00
|
|
|
# if tls is enabled, starttls
|
|
|
|
|
if globals.config.get("eMail", "tls"):
|
|
|
|
|
server.starttls()
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-29 12:25:21 +02:00
|
|
|
# if user is given, login
|
|
|
|
|
if globals.config.get("eMail", "user"):
|
|
|
|
|
server.login(globals.config.get("eMail", "user"), globals.config.get("eMail", "password"))
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-29 12:25:21 +02:00
|
|
|
except:
|
|
|
|
|
logging.error("cannot connect to eMail")
|
|
|
|
|
logging.debug("cannot connect to eMail", exc_info=True)
|
|
|
|
|
# Without connection, plugin couldn't work
|
|
|
|
|
return
|
2015-06-04 20:18:44 +02:00
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
2015-06-29 12:25:21 +02:00
|
|
|
if typ == "FMS":
|
|
|
|
|
logging.debug("Start FMS to eMail")
|
|
|
|
|
try:
|
|
|
|
|
# read subject-structure from config.ini
|
|
|
|
|
subject = globals.config.get("eMail", "fms_subject")
|
|
|
|
|
subject = subject.replace("%FMS%", data["fms"]).replace("%STATUS%", data["status"]) #replace Wildcards
|
|
|
|
|
subject = subject.replace("%DIR%", data["direction"]).replace("%DIRT%", data["directionText"]) #replace Wildcards
|
|
|
|
|
subject = subject.replace("%TSI%", data["tsi"]) #replace Wildcards
|
|
|
|
|
subject = subject.replace("%DESCR%", data["description"]) # replace Wildcards
|
2015-07-03 23:43:22 +02:00
|
|
|
subject = subject.replace("%TIME%", timeHandler.curtime("H:M:S")).replace("%DATE%", timeHandler.curtime("Y-m-d")) # replace Wildcards
|
2015-06-29 12:25:21 +02:00
|
|
|
# read mailtext-structure from config.ini
|
|
|
|
|
mailtext = globals.config.get("eMail", "fms_message")
|
|
|
|
|
mailtext = mailtext.replace("%FMS%", data["fms"]).replace("%STATUS%", data["status"]) #replace Wildcards
|
|
|
|
|
mailtext = mailtext.replace("%DIR%", data["direction"]).replace("%DIRT%", data["directionText"]) #replace Wildcards
|
|
|
|
|
mailtext = mailtext.replace("%TSI%", data["tsi"]) #replace Wildcards
|
|
|
|
|
mailtext = mailtext.replace("%DESCR%", data["description"]) # replace Wildcards
|
2015-07-03 23:43:22 +02:00
|
|
|
mailtext = mailtext.replace("%TIME%", timeHandler.curtime("H:M:S")).replace("%DATE%", timeHandler.curtime("Y-m-d")) # replace Wildcards
|
2015-06-29 12:25:21 +02:00
|
|
|
# send eMail
|
|
|
|
|
doSendmail(server, subject, mailtext)
|
|
|
|
|
except:
|
|
|
|
|
logging.error("%s to eMail failed", typ)
|
|
|
|
|
logging.debug("%s to eMail failed", typ, exc_info=True)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
elif typ == "ZVEI":
|
|
|
|
|
logging.debug("Start ZVEI to eMail")
|
|
|
|
|
try:
|
|
|
|
|
# read subject-structure from config.ini
|
|
|
|
|
subject = globals.config.get("eMail", "zvei_subject")
|
|
|
|
|
subject = subject.replace("%ZVEI%", data["zvei"]) #replace Wildcards
|
|
|
|
|
subject = subject.replace("%DESCR%", data["description"]) # replace Wildcards
|
2015-07-03 23:43:22 +02:00
|
|
|
subject = subject.replace("%TIME%", timeHandler.curtime("H:M:S")).replace("%DATE%", timeHandler.curtime("Y-m-d")) # replace Wildcards
|
2015-06-29 12:25:21 +02:00
|
|
|
# read mailtext-structure from config.ini
|
|
|
|
|
mailtext = globals.config.get("eMail", "zvei_message")
|
|
|
|
|
mailtext = mailtext.replace("%ZVEI%", data["zvei"]) #replace Wildcards
|
|
|
|
|
mailtext = mailtext.replace("%DESCR%", data["description"]) # replace Wildcards
|
2015-07-03 23:43:22 +02:00
|
|
|
mailtext = mailtext.replace("%TIME%", timeHandler.curtime("H:M:S")).replace("%DATE%", timeHandler.curtime("Y-m-d")) # replace Wildcards
|
2015-06-29 12:25:21 +02:00
|
|
|
# send eMail
|
|
|
|
|
doSendmail(server, subject, mailtext)
|
|
|
|
|
except:
|
|
|
|
|
logging.error("%s to eMail failed", typ)
|
|
|
|
|
logging.debug("%s to eMail failed", typ, exc_info=True)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
elif typ == "POC":
|
|
|
|
|
logging.debug("Start POC to eMail")
|
|
|
|
|
try:
|
|
|
|
|
# read subject-structure from config.ini
|
|
|
|
|
subject = globals.config.get("eMail", "poc_subject")
|
|
|
|
|
subject = subject.replace("%RIC%", data["ric"]) #replace Wildcards
|
|
|
|
|
subject = subject.replace("%FUNC%", data["function"]).replace("%FUNCCHAR%", data["functionChar"]) #replace Wildcards
|
|
|
|
|
subject = subject.replace("%MSG%", data["msg"]).replace("%BITRATE%", str(data["bitrate"])) #replace Wildcards
|
|
|
|
|
subject = subject.replace("%DESCR%", data["description"]) # replace Wildcards
|
2015-07-03 23:43:22 +02:00
|
|
|
subject = subject.replace("%TIME%", timeHandler.curtime("H:M:S")).replace("%DATE%", timeHandler.curtime("Y-m-d")) # replace Wildcards
|
2015-06-29 12:25:21 +02:00
|
|
|
# read mailtext-structure from config.ini
|
|
|
|
|
mailtext = globals.config.get("eMail", "poc_message")
|
|
|
|
|
mailtext = mailtext.replace("%RIC%", data["ric"]) #replace Wildcards
|
|
|
|
|
mailtext = mailtext.replace("%FUNC%", data["function"]).replace("%FUNCCHAR%", data["functionChar"]) #replace Wildcards
|
|
|
|
|
mailtext = mailtext.replace("%MSG%", data["msg"]).replace("%BITRATE%", str(data["bitrate"])) #replace Wildcards
|
|
|
|
|
mailtext = mailtext.replace("%DESCR%", data["description"]) # replace Wildcards
|
2015-07-03 23:43:22 +02:00
|
|
|
mailtext = mailtext.replace("%TIME%", timeHandler.curtime("H:M:S")).replace("%DATE%", timeHandler.curtime("Y-m-d")) # replace Wildcards
|
2015-06-29 12:25:21 +02:00
|
|
|
# send eMail
|
|
|
|
|
doSendmail(server, subject, mailtext)
|
|
|
|
|
except:
|
|
|
|
|
logging.error("%s to eMail failed", typ)
|
|
|
|
|
logging.debug("%s to eMail failed", typ, exc_info=True)
|
|
|
|
|
return
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-29 12:25:21 +02:00
|
|
|
else:
|
2015-07-02 09:02:49 +02:00
|
|
|
logging.warning("Invalid Typ: %s", typ)
|
2015-06-29 12:25:21 +02:00
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
logging.debug("close eMail-Connection")
|
2015-07-02 09:02:49 +02:00
|
|
|
try:
|
2015-06-29 12:25:21 +02:00
|
|
|
server.quit()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-04 20:18:44 +02:00
|
|
|
except:
|
2015-06-23 22:27:48 +02:00
|
|
|
# something very mysterious
|
|
|
|
|
logging.error("unknown error")
|
2015-07-02 09:02:49 +02:00
|
|
|
logging.debug("unknown error", exc_info=True)
|