From 5499f6e3e0f97438c59861bf2679f912dbefe57b Mon Sep 17 00:00:00 2001 From: JHCD Date: Thu, 4 Jun 2015 20:18:44 +0200 Subject: [PATCH] add eMail-plugin for fms, zvei and pocsag plugin dispatch messages via eMail/SMTP supports: tls, user/password, wildcards in subject and message-text --- config/config.template.ini | 44 +++++++++ plugins/eMail/eMail.py | 184 +++++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 plugins/eMail/eMail.py diff --git a/config/config.template.ini b/config/config.template.ini index 950f91c..4919ab3 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -113,6 +113,50 @@ zvei_url = poc_url = +[eMail] +# SMTP-Server +smtp_server = localhost +# Port of SMTP-Server (default: +smtp_port = +# use tls for connection (0|1) +tls = 0 +# Use this, when SMTP-Server has restricted access +user = +password = + +# Parameters for Alarm-Msg: +# "to" could be more than one address, comma separated +from = local@localhost +to = user@irgendwo, user2@woanders + +# Priority of the eMail: +# normal|urgent|non-urgent +priority = urgent + +# %FMS% = FMS Code +# %STATUS% = FMS Status +# %DIR% = Direction of the telegram (0/1) +# %DIRT% = Direction of the telegram (Text-String) +# %TSI% = Tactical Short Information (I-IV) +# %TIME% = Date/Time (by script) +fms_subject = FMS: %FMS% +fms_message = %TIME%: %FMS% - Status: %STATUS% - Direction: %DIRT% - TSI: %TSI% + +# %ZVEI% = ZVEI 5-tone Code +# %TIME% = Date/Time (by script) +zvei_subject = Alarm: %ZVEI% +zvei_message = %TIME%: %ZVEI% + +# %RIC% = Pocsag RIC +# %FUNC% = Pocsac function/Subric (1-4) +# %FUNCD% = Pocsac function/Subric (a-d) +# %MSG% = Message of the Pocsag telegram +# %BITRATE% = Bitrate of the Pocsag telegram +# %TIME% = Date/Time (by script) +poc_subject = Alarm: %RIC%%FUNCD% +poc_message = %TIME%: %MSG% + + [BosMon] #Server as IP of DNS-Name (without http://) #actually no ssl supported diff --git a/plugins/eMail/eMail.py b/plugins/eMail/eMail.py new file mode 100644 index 0000000..f346bd1 --- /dev/null +++ b/plugins/eMail/eMail.py @@ -0,0 +1,184 @@ +#!/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 +from email.utils import formatdate # need for conform to RFC2822 standard +from email.utils import make_msgid # need for conform to RFC2822 standard + +from includes import globals # Global variables + +## +# +# Private helper function for a printable Timestamp +# +def curtime(): + return time.strftime("%Y-%m-%d %H:%M:%S") + +## +# +# 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 + + @return: nothing + @exception: Exception if smtp.sendmail failed + """ + try: + 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") + server.sendmail(globals.config.get("eMail", "from"), globals.config.get("eMail", "to"), msg.as_string()) + except: + logging.exception("send eMail failed") + + +## +# +# 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) + + 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 + + @return: nothing + @exception: Exception if ConfigParser failed + @exception: Exception if connect to SMTP-Server failed + @exception: Exception if + """ + try: + # + # ConfigParser + # + logging.debug("reading config file") + try: + for key,val in globals.config.items("eMail"): + logging.debug(" - %s = %s", key, val) + + except: + logging.exception("cannot read config file") + + 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(1) + + # if tls is enabled, starttls + if globals.config.get("eMail", "tls"): + server.starttls() + + # if user is given, login + if globals.config.get("eMail", "user"): + server.login(globals.config.get("eMail", "user"), globals.config.get("eMail", "password")) + + except: + logging.exception("cannot connect to eMail") + + else: + + 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("%TIME%", curtime()) # replace Wildcards + # 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("%TIME%", curtime()) # replace Wildcards + # send eMail + doSendmail(server, subject, mailtext) + except: + logging.exception("FMS to eMail failed") + + 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("%TIME%", curtime()) # replace Wildcards + # read mailtext-structure from config.ini + mailtext = globals.config.get("eMail", "zvei_message") + mailtext = mailtext.replace("%ZVEI%", data["zvei"]) #replace Wildcards + mailtext = mailtext.replace("%TIME%", curtime()) # replace Wildcards + # send eMail + doSendmail(server, subject, mailtext) + except: + logging.exception("ZVEI to eMail failed") + + elif typ == "POC": + logging.debug("Start POC to eMail") + try: + # replace 1-4 with a-d for use as %FUNCD% + data["functionD"] = data["function"].replace("1", "a").replace("2", "b").replace("3", "c").replace("4", "d") + # read subject-structure from config.ini + subject = globals.config.get("eMail", "poc_subject") + subject = subject.replace("%RIC%", data["ric"]).replace("%FUNC%", data["function"]).replace("%FUNCD%", data["functionD"]) #replace Wildcards + subject = subject.replace("%MSG%", data["msg"]).replace("%BITRATE%", str(data["bitrate"])) #replace Wildcards + subject = subject.replace("%TIME%", curtime()) # replace Wildcards + # read mailtext-structure from config.ini + mailtext = globals.config.get("eMail", "poc_message") + mailtext = mailtext.replace("%RIC%", data["ric"]).replace("%FUNC%", data["function"]).replace("%FUNCD%", data["functionD"]) #replace Wildcards + mailtext = mailtext.replace("%MSG%", data["msg"]).replace("%BITRATE%", str(data["bitrate"])) #replace Wildcards + mailtext = mailtext.replace("%TIME%", curtime()) # replace Wildcards + # send eMail + doSendmail(server, subject, mailtext) + except: + logging.exception("POC to eMail failed") + + else: + logging.warning("Invalid Typ: %s", typ) + + finally: + logging.debug("close eMail-Connection") + server.quit() + + except: + logging.exception("") \ No newline at end of file