2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-08 07:24:21 +00:00

75_MSG: tidy up code

git-svn-id: https://svn.fhem.de/fhem/trunk@8534 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
gandy92 2015-05-06 13:35:50 +00:00
parent af7b2f8958
commit 504a85cce8

View File

@ -1,6 +1,17 @@
##############################################
####################################################
# $Id$
##############################################
####################################################
#
# Created 2012 by rbente
#
####################################################
#
# History:
#
# 2015-05-06: tidy up code for restructuring
# 2015-05-05: remove dependency on Switch
#
#
package main;
use strict;
@ -11,12 +22,8 @@ my %sets = (
"send" => "MSG",
"write" => "MSG",
);
###########################################################
## Initialize Function
##
############################################################
sub
MSG_Initialize($)
sub MSG_Initialize($)
{
my ($hash) = @_;
@ -25,12 +32,7 @@ MSG_Initialize($)
$hash->{AttrList} = "loglevel:0,1,2,3,4,5,6";
}
###########################################################
## Set Function
##
############################################################
sub
MSG_Set($@)
sub MSG_Set($@)
{
my ($hash, @a) = @_;
return "Unknown argument $a[1], choose one of -> " . join(" ", sort keys %sets)
@ -41,15 +43,14 @@ MSG_Set($@)
return "no set value specified" if (int(@a) < 1);
return "Unknown argument ?" if ($a[0] eq "?");
########## Check, if we have send or wait as parameter
# Check, if we have send or wait as parameter
if (($a[0] eq "send") || ($a[0] eq "write"))
{
########## Check if the device is defined that we like to use as frontend
# Check if the device is defined that we like to use as frontend
return "Please define $a[1] first" if (!defined($defs{ $a[1] }));
########## switch based on the device type
########## valid device types are:
########## MSGFile = write to a file
########## MSGMail = send an email
if (!defined($defs{ $a[1] }{TYPE}))
{
return "TYPE for $defs{$a[1]} not defined";
@ -63,29 +64,35 @@ MSG_Set($@)
elsif ($defs{ $a[1] }{TYPE} eq "MSGFile")
{
return "No filename specified, use attr <name> filename <filename> $a[1] $defs{$a[1]}{TYPE}" if (!AttrVal($a[1],"filename",""));
return "No filename specified, use attr <name> filename <filename> $a[1] $defs{$a[1]}{TYPE}"
if (!AttrVal($a[1], "filename", ""));
########## open the file, new = delete file before create it
########## append = add lines to the existing file contents
# open the file, new = delete file before create it
# append = add lines to the existing file contents
if (AttrVal($a[1], "filemode", "") eq "new")
{
open(FHEMMSGFILE, ">" . AttrVal($a[1],"filename","")) || return "Can not open the file: $!";
open(FHEMMSGFILE, ">" . AttrVal($a[1], "filename", ""))
|| return "Can not open the file: $!";
}
else
{
open(FHEMMSGFILE, ">>" . AttrVal($a[1],"filename","")) || return "Can not open the file: $!";
open(FHEMMSGFILE, ">>" . AttrVal($a[1], "filename", ""))
|| return "Can not open the file: $!";
}
########## loop thru the stored lines and write them to the file
########## number of lines are in the Readings / msgcount
# loop thru the stored lines and write them to the file
# number of lines are in the Readings / msgcount
my $i;
if (ReadingsVal($a[1], "msgcount", 0) > 0)
{
for ($i = 0 ; $i < ReadingsVal($a[1], "msgcount", 0) ; $i++)
{
print FHEMMSGFILE $data{$a[1]}{$i} || return "Can not write to the file: $!";;
print FHEMMSGFILE $data{ $a[1] }{$i}
|| return "Can not write to the file: $!";
}
}
########## close the file and send a message to the log
# close the file and send a message to the log
close(FHEMMSGFILE);
Log 1, "<MSG> write to File: " . AttrVal($a[1], "filename", "");
} # END MSGFile
@ -102,28 +109,33 @@ MSG_Set($@)
elsif ($defs{ $a[1] }{TYPE} eq "MSGMail")
{
########## check all the needed data
# check all required data
my $from = AttrVal($a[1], "from", "");
return "No <from> address specified, use attr $a[1] from <mail-address>" if (!$from);
return "No <from> address specified, use attr $a[1] from <mail-address>"
if (!$from);
my $to = AttrVal($a[1], "to", "");
return "No <to> address specified, use attr $a[1] to <mail-address>" if (!$to);
return "No <to> address specified, use attr $a[1] to <mail-address>"
if (!$to);
my $subject = AttrVal($a[1], "subject", "");
return "No <subject> specified, use attr $a[1] subject <text>" if (!$subject);
return "No <subject> specified, use attr $a[1] subject <text>"
if (!$subject);
my $authfile = AttrVal($a[1], "authfile", "");
return "No <authfile> specified, use attr $a[1] authfile <filename>" if (!$authfile);
return "No <authfile> specified, use attr $a[1] authfile <filename>"
if (!$authfile);
my $smtphost = AttrVal($a[1], "smtphost", "");
return "No <smtphost> name specified, use attr $a[1] sntphost <hostname>" if (!$smtphost);
return "No <smtphost> name specified, use attr $a[1] sntphost <hostname>"
if (!$smtphost);
my $smtpport = AttrVal($a[1], "smtpport", "465"); # 465 is the default port
my $cc = AttrVal($a[1], "cc", ""); # Carbon Copy
open(FHEMAUTHFILE, "<" . $authfile) || return "Can not open authfile $authfile: $!";;
open(FHEMAUTHFILE, "<" . $authfile)
|| return "Can not open authfile $authfile: $!";
my @auth = <FHEMAUTHFILE>;
close(FHEMAUTHFILE);
chomp(@auth);
# Log 1, "MSG User = <" . @auth[0] . "> Passwort = <" . @auth[1] . ">";
########## compose message
# compose message
my $i;
my $mess = "";
for ($i = 0 ; $i < ReadingsVal($a[1], "msgcount", 0) ; $i++)
@ -139,11 +151,13 @@ MSG_Set($@)
Data => $mess
);
########## login to the SMTP Host using SSL and send the message
# login to the SMTP Host using SSL and send the message
my $smtp;
my $smtperrmsg = "SMTP Error: ";
$smtp = Net::SMTP::SSL->new($smtphost, Port=>$smtpport) or return $smtperrmsg . " Can't connect to host $smtphost";
$smtp->auth($auth[0], $auth[1]) or return $smtperrmsg . " Can't authenticate: " . $smtp->message();
$smtp = Net::SMTP::SSL->new($smtphost, Port => $smtpport)
or return $smtperrmsg . " Can't connect to host $smtphost";
$smtp->auth($auth[0], $auth[1])
or return $smtperrmsg . " Can't authenticate: " . $smtp->message();
$smtp->mail($from) or return $smtperrmsg . $smtp->message();
$smtp->to($to) or return $smtperrmsg . $smtp->message();
if ($cc ne '')
@ -152,7 +166,8 @@ MSG_Set($@)
$smtp->cc($cc) or return $smtperrmsg . $smtp->message();
}
$smtp->data() or return $smtperrmsg . $smtp->message();
$smtp->datasend($mailmsg->as_string) or return $smtperrmsg .$smtp->message();
$smtp->datasend($mailmsg->as_string)
or return $smtperrmsg . $smtp->message();
$smtp->dataend() or return $smtperrmsg . $smtp->message();
$smtp->quit() or return $smtperrmsg . $smtp->message();
@ -166,7 +181,8 @@ MSG_Set($@)
} ###> END if(($a[0] eq "send") || ($a[0] eq "write"))
my $v = join(" ", @a);
Log GetLogLevel($name, 2), "MSG set $name $v";
########## update stats
# update stats
$hash->{CHANGED}[0] = $v;
$hash->{STATE} = $v;
$hash->{READINGS}{state}{TIME} = TimeNow();
@ -174,13 +190,7 @@ MSG_Set($@)
return undef;
}
###########################################################
## Define Function
##
############################################################
sub
MSG_Define($$)
sub MSG_Define($$)
{
my ($hash, $def) = @_;
my @a = split("[ \t][ \t]*", $def);
@ -193,7 +203,6 @@ MSG_Define($$)
return undef;
}
1;
=pod