2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-02-08 05:15:10 +00:00

50_SSChatBot: contrib 1.0.0

git-svn-id: https://svn.fhem.de/fhem/trunk@20654 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
nasseeder1 2019-12-03 21:31:10 +00:00
parent 5d73706160
commit 796200e887

View File

@ -41,6 +41,7 @@ use MIME::Base64;
use Time::HiRes; use Time::HiRes;
use HttpUtils; use HttpUtils;
use Encode; use Encode;
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
eval "use FHEM::Meta;1" or my $modMetaAbsent = 1; eval "use FHEM::Meta;1" or my $modMetaAbsent = 1;
eval "use Net::Domain qw(hostname hostfqdn hostdomain domainname);1" or my $SSChatBotNDom = "Net::Domain"; eval "use Net::Domain qw(hostname hostfqdn hostdomain domainname);1" or my $SSChatBotNDom = "Net::Domain";
@ -91,6 +92,7 @@ sub SSChatBot_Initialize($) {
"allowedUserForSet:--wait#for#userlist-- ". "allowedUserForSet:--wait#for#userlist-- ".
"allowedUserForGet:--wait#for#userlist-- ". "allowedUserForGet:--wait#for#userlist-- ".
"allowedUserForCode:--wait#for#userlist-- ". "allowedUserForCode:--wait#for#userlist-- ".
"ownCommand ".
"showTokenInLog:1,0 ". "showTokenInLog:1,0 ".
"httptimeout ". "httptimeout ".
$readingFnAttributes; $readingFnAttributes;
@ -1047,7 +1049,7 @@ sub SSChatBot_chatop_parse ($) {
my $list = $modules{$hash->{TYPE}}{AttrList}; my $list = $modules{$hash->{TYPE}}{AttrList};
my @deva = split(" ", $list); my @deva = split(" ", $list);
foreach (@deva) { foreach (@deva) {
push @newa, $_ if($_ !~ /defaultPeer:|allowedUserFor(Set|Get|Code):|/); push @newa, $_ if($_ !~ /defaultPeer:|allowedUserFor(Set|Get|Code):/);
} }
push @newa, ($uids?"defaultPeer:multiple-strict,$uids ":"defaultPeer:--no#userlist#selectable--"); push @newa, ($uids?"defaultPeer:multiple-strict,$uids ":"defaultPeer:--no#userlist#selectable--");
push @newa, ($uids?"allowedUserForSet:multiple-strict,$uids ":"allowedUserForSet:--no#userlist#selectable--"); push @newa, ($uids?"allowedUserForSet:multiple-strict,$uids ":"allowedUserForSet:--no#userlist#selectable--");
@ -1381,8 +1383,10 @@ sub SSChatBot_formText ($) {
%replacements = ( %replacements = (
'"' => "´", # doppelte Hochkomma sind im Text nicht erlaubt '"' => "´", # doppelte Hochkomma sind im Text nicht erlaubt
" H" => " h", # Bug im Chat wenn vor großem H ein Zeichen + Leerzeichen vorangeht " H" => " h", # Bug im Chat wenn vor großem H ein Zeichen + Leerzeichen vorangeht
"#" => "", # Hashtags sind im Text nicht erlaubt "#" => "%23", # Hashtags sind im Text nicht erlaubt und wird encodiert
"&" => "", # & ist im Text nicht erlaubt "&" => "%26", # & ist im Text nicht erlaubt und wird encodiert
"%" => "%25", # % ist nicht erlaubt und wird encodiert
"+" => "%2B",
); );
$txt =~ s/\n/ESC_newline_ESC/g; $txt =~ s/\n/ESC_newline_ESC/g;
@ -1496,7 +1500,7 @@ sub SSChatBot_CGI() {
my ($request) = @_; my ($request) = @_;
my ($hash,$name,$link,$args); my ($hash,$name,$link,$args);
my ($text,$timestamp,$channelid,$channelname,$userid,$username,$postid,$triggerword) = ("","","","","","","",""); my ($text,$timestamp,$channelid,$channelname,$userid,$username,$postid,$triggerword) = ("","","","","","","","");
my ($command,$cr,$au) = ("","",""); my ($command,$cr,$au,$arg) = ("","","","");
my @aul; my @aul;
my $state = "active"; my $state = "active";
@ -1582,16 +1586,21 @@ sub SSChatBot_CGI() {
$text = urlDecode($h->{text}); $text = urlDecode($h->{text});
Log3($name, 4, "$name - text received: ".$text); Log3($name, 4, "$name - text received: ".$text);
if($text =~ /^\/([Ss]et.*?|[Gg]et.*?|[Cc]ode.*?)\s+(.*)$/) { # Befehle in FHEM ausführen my $uc = AttrVal($name,"ownCommand", ""); # User Commands zusammenstellen
$command = "$1 ".$2; if ($uc) {
($uc,$arg) = split(" ", $uc, 2);
}
if($text =~ /^\/([Ss]et.*?|[Gg]et.*?|[Cc]ode.*?)\s+(.*)$/) { # vordefinierte Befehle in FHEM ausführen
my $p1 = $1; my $p1 = $1;
my $p2 = $2; my $p2 = $2;
if($p1 =~ /set.*/i) { if($p1 =~ /set.*/i) {
$command = "set ".$p2;
$au = AttrVal($name,"allowedUserForSet", "all"); $au = AttrVal($name,"allowedUserForSet", "all");
@aul = split(",",$au); @aul = split(",",$au);
if($au eq "all" || $username ~~ @aul) { if($au eq "all" || $username ~~ @aul) {
Log3($name, 4, "$name - Synology Chat user \"$username\" execute FHEM command: set ".$p2); Log3($name, 4, "$name - Synology Chat user \"$username\" execute FHEM command: ".$command);
$cr = CommandSet(undef, $p2); # set-Befehl in FHEM ausführen $cr = CommandSet(undef, $p2); # set-Befehl in FHEM ausführen
} else { } else {
$cr = "User \"$username\" is not allowed execute \"$command\" command"; $cr = "User \"$username\" is not allowed execute \"$command\" command";
@ -1599,11 +1608,12 @@ sub SSChatBot_CGI() {
Log3($name, 2, "$name - WARNING - Chat user \"$username\" is not authorized for \"$command\" command. Execution denied !"); Log3($name, 2, "$name - WARNING - Chat user \"$username\" is not authorized for \"$command\" command. Execution denied !");
} }
} elsif ($p1 =~ /get.*/i) { } elsif ($p1 =~ /get.*/i) {
$command = "get ".$p2;
$au = AttrVal($name,"allowedUserForGet", "all"); $au = AttrVal($name,"allowedUserForGet", "all");
@aul = split(",",$au); @aul = split(",",$au);
if($au eq "all" || $username ~~ @aul) { if($au eq "all" || $username ~~ @aul) {
Log3($name, 4, "$name - Synology Chat user \"$username\" execute FHEM command: get ".$p2); Log3($name, 4, "$name - Synology Chat user \"$username\" execute FHEM command: ".$command);
$cr = CommandGet(undef, $p2); # get-Befehl in FHEM ausführen $cr = CommandGet(undef, $p2); # get-Befehl in FHEM ausführen
} else { } else {
$cr = "User \"$username\" is not allowed execute \"$command\" command"; $cr = "User \"$username\" is not allowed execute \"$command\" command";
@ -1612,6 +1622,7 @@ sub SSChatBot_CGI() {
} }
} elsif ($p1 =~ /code.*/i) { } elsif ($p1 =~ /code.*/i) {
$command = $p2;
$au = AttrVal($name,"allowedUserForCode", "all"); $au = AttrVal($name,"allowedUserForCode", "all");
@aul = split(",",$au); @aul = split(",",$au);
if($au eq "all" || $username ~~ @aul) { if($au eq "all" || $username ~~ @aul) {
@ -1622,7 +1633,7 @@ sub SSChatBot_CGI() {
$p2 = ''; $p2 = '';
} }
Log3($name, 4, "$name - Synology Chat user \"$username\" execute FHEM command: ".$p2); Log3($name, 4, "$name - Synology Chat user \"$username\" execute FHEM command: ".$p2);
$cr = AnalyzeCommand(undef, $p2) if($p2); # Perl Code in FHEM ausführen $cr = AnalyzePerlCommand(undef, $p2) if($p2); # Perl Code in FHEM ausführen
} else { } else {
$cr = "User \"$username\" is not allowed execute \"$command\" command"; $cr = "User \"$username\" is not allowed execute \"$command\" command";
$state = "command execution denied"; $state = "command execution denied";
@ -1640,6 +1651,29 @@ sub SSChatBot_CGI() {
RemoveInternalTimer($hash, "SSChatBot_getapisites"); RemoveInternalTimer($hash, "SSChatBot_getapisites");
InternalTimer(gettimeofday()+1, "SSChatBot_getapisites", "$name", 0); InternalTimer(gettimeofday()+1, "SSChatBot_getapisites", "$name", 0);
} }
if($uc && $uc =~ /^$text(\s+)?$/) { # User eigene Befehle, z.B.: /Wetter
$au = AttrVal($name,"allowedUserForOwn", "all");
@aul = split(",",$au);
if($au eq "all" || $username ~~ @aul) {
Log3($name, 4, "$name - Synology Chat user \"$username\" execute FHEM command: ".$arg);
$cr = AnalyzeCommandChain(undef, $arg); # FHEM Befehlsketten ausführen
} else {
$cr = "User \"$username\" is not allowed execute \"$arg\" command";
$state = "command execution denied";
Log3($name, 2, "$name - WARNING - Chat user \"$username\" is not authorized for \"$arg\" command. Execution denied !");
}
$cr = $cr ne ""?$cr:"command '$arg' executed";
Log3($name, 4, "$name - FHEM command return: ".$cr);
$cr = SSChatBot_formText($cr);
SSChatBot_addQueue($name, "sendItem", "chatbot", $userid, $cr, "", "", "");
RemoveInternalTimer($hash, "SSChatBot_getapisites");
InternalTimer(gettimeofday()+1, "SSChatBot_getapisites", "$name", 0);
}
} }
if ($h->{trigger_word}) { if ($h->{trigger_word}) {