From eaa39c91e9511083a8db00fddcfa894dd201fed6 Mon Sep 17 00:00:00 2001 From: rudolfkoenig <> Date: Thu, 8 Aug 2013 13:26:43 +0000 Subject: [PATCH] encoding command added (by justme1968) git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@3615 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- FHEM/98_telnet.pm | 32 +++++++++++++++++++++++++++++++- fhem.pl | 12 ++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/FHEM/98_telnet.pm b/FHEM/98_telnet.pm index e3c9b4a42..a2d31d498 100644 --- a/FHEM/98_telnet.pm +++ b/FHEM/98_telnet.pm @@ -21,9 +21,32 @@ telnet_Initialize($) $hash->{AttrFn} = "telnet_Attr"; $hash->{NotifyFn}= "telnet_SecurityCheck"; $hash->{AttrList} = "loglevel:0,1,2,3,4,5,6 globalpassword password ". - "allowfrom SSL connectTimeout connectInterval"; + "allowfrom SSL connectTimeout connectInterval ". + "encoding:utf8,latin1"; $hash->{ActivateInformFn} = "telnet_ActivateInform"; + my %lhash = ( Fn=>"CommandTelnetEncoding", + ClientFilter => "telnet", + Hlp=>"[utf8|latin1],query and set the character encoding for the current telnet session" ); + $cmds{encoding} = \%lhash; +} +sub +CommandTelnetEncoding($$) +{ + my ($hash, $param) = @_; + + my $ret = ""; + + if( !$param ) { + $ret = "current encoding is $hash->{encoding}"; + } elsif( $param eq "utf8" || $param eq "latin1" ) { + $hash->{encoding} = $param; + $ret = "encoding changed to $param"; + } else { + $ret = "unknown encoding >>$param<<"; + } + + return $ret; } ##################################### @@ -160,6 +183,7 @@ telnet_Read($) if($hash->{SERVERSOCKET}) { # Accept and create a child my $chash = TcpServer_Accept($hash, "telnet"); return if(!$chash); + $chash->{encoding} = AttrVal($name, "encoding", "utf8"); syswrite($chash->{CD}, sprintf("%c%c%cPassword: ", 255, 251, 1)) # WILL ECHO if(telnet_pw($name, $chash->{NAME})); return; @@ -251,6 +275,7 @@ telnet_Read($) $ret .= ($hash->{prevlines} ? "> " : "fhem> ") if($gotCmd && $hash->{prompt} && !$hash->{rcvdQuit}); if($ret) { + $ret = utf8ToLatin1($ret) if( $hash->{encoding} eq "latin1" ); $ret =~ s/\n/\r\n/g if($pw); # only for DOS telnet for(;;) { my $l = syswrite($hash->{CD}, $ret); @@ -429,6 +454,11 @@ telnet_ActivateInform($) try to connect again after this many seconds. Default is 60.
+ +
  • encoding
    + Sets the encoding for the data send to the client. Possible values are latin1 and utf8. Default is utf8. +

  • + diff --git a/fhem.pl b/fhem.pl index 9103630e5..58c0a02c4 100755 --- a/fhem.pl +++ b/fhem.pl @@ -241,6 +241,7 @@ $readingFnAttributes = "event-on-change-reading event-on-update-reading ". "include" => { Fn=>"CommandInclude", Hlp=>",read the commands from " }, "inform" => { Fn=>"CommandInform", + ClientFilter => "telnet", Hlp=>"{on|timer|raw|off},echo all events to this client" }, "iowrite" => { Fn=>"CommandIOWrite", Hlp=>" ,write raw data with iodev" }, @@ -249,8 +250,10 @@ $readingFnAttributes = "event-on-change-reading event-on-update-reading ". "modify" => { Fn=>"CommandModify", Hlp=>"device ,modify the definition (e.g. at, notify)" }, "quit" => { Fn=>"CommandQuit", + ClientFilter => "telnet", Hlp=>",end the client session" }, "exit" => { Fn=>"CommandQuit", + ClientFilter => "telnet", Hlp=>",end the client session" }, "reload" => { Fn=>"CommandReload", Hlp=>",reload the given module (e.g. 99_PRIV)" }, @@ -772,9 +775,13 @@ AnalyzeCommand($$) map { $fn = $_ if(lc($fn) eq lc($_)); } keys %modules; $fn = LoadModule($fn); $fn = lc($fn) if(defined($cmds{lc($fn)})); - return "Unknown command $fn, try help" if(!defined($cmds{$fn})); + return "Unknown command $fn, try help." if(!defined($cmds{$fn})); } + if($cl && $cmds{$fn}{ClientFilter} && + $cl->{TYPE} !~ m/$cmds{$fn}{ClientFilter}/) { + return "This command ($fn) is not valid for this input channel."; + } $param = "" if(!defined($param)); no strict "refs"; @@ -866,8 +873,9 @@ CommandHelp($$) for my $cmd (sort keys %cmds) { next if(!$cmds{$cmd}{Hlp}); + next if($cl && $cmds{$cmd}{ClientFilter} && + $cl->{TYPE} !~ m/$cmds{$cmd}{ClientFilter}/); my @a = split(",", $cmds{$cmd}{Hlp}, 2); - $str .= sprintf("%-9s %-25s %s\n", $cmd, $a[0], $a[1]); } return $str;