desarrollosconsejeria/adtos-aula/1.7-0/serveradtospro/clservidor.pas @ 23a384b9
23a384b9 | Elisa | unit clservidor;
|
|
{$mode objfpc}{$H+}
|
|||
interface
|
|||
uses
|
|||
unix,Classes, SysUtils, StrUtils, IniFiles,process;
|
|||
type
|
|||
{ Tservidor }
|
|||
Tservidor = class
|
|||
private
|
|||
fInterface:string;
|
|||
fIP:String;
|
|||
fPuerto:integer;
|
|||
fIPAdministrador:String;
|
|||
fUsuarioEquipoProfesor:String;
|
|||
fHomeUsuario:string;
|
|||
fDirRecibidos:string;
|
|||
procedure Ejecutar(comando: string);
|
|||
procedure LeerSetupIni(archivo:string);
|
|||
function ObtenerMiIp():string;
|
|||
Function NumOrdLet(num:integer):string;
|
|||
procedure OrdenarDesactivarInternet();
|
|||
public
|
|||
constructor create (archivoini:string='/etc/adtosaula.conf');
|
|||
property IP:String read fIP;
|
|||
property Puerto:integer read fPuerto;
|
|||
property IPAdministrador:String read fIPAdministrador write fIPAdministrador;
|
|||
property UsuarioEquipoProfesor:String read fUsuarioEquipoProfesor write fUsuarioEquipoProfesor;
|
|||
property DirRecibidos:String read fDirRecibidos write fDirRecibidos;
|
|||
property HomeUsuario:String read fHomeUsuario write fHomeUsuario;
|
|||
function ObtenerNombreUsuario():string;
|
|||
function ObtenerHomeUsuario(usuario:string):string;
|
|||
function HallarDato(Cmd: string): string;
|
|||
end;
|
|||
implementation
|
|||
uses globales;
|
|||
Function Tservidor.NumOrdLet(num:integer):string;
|
|||
var
|
|||
letraord:string;
|
|||
begin
|
|||
num:=succ(num);
|
|||
letraord:=ifthen(num<10,'0'+inttostr(num),inttostr(num));
|
|||
result:=letraord;
|
|||
end;
|
|||
procedure Tservidor.Ejecutar (comando:string);
|
|||
begin;
|
|||
unix.fpSystem(comando);
|
|||
end;
|
|||
function Tservidor.HallarDato(Cmd:string):String;
|
|||
var
|
|||
file1: TextFile;
|
|||
s,t: ansistring;
|
|||
n: longint;
|
|||
j:integer;
|
|||
begin
|
|||
j:=0;
|
|||
n:=popen(file1, Cmd, 'r');
|
|||
if n=-1 then
|
|||
begin
|
|||
result:='';
|
|||
Exit;
|
|||
end;
|
|||
t:='';
|
|||
while not eof(file1) do begin
|
|||
Readln(File1,s);
|
|||
t:=t+s+Chr(10);
|
|||
inc(j);
|
|||
end;
|
|||
pclose(file1);
|
|||
result := trim(t);
|
|||
end;
|
|||
constructor Tservidor.create(archivoini:string='/etc/adtosaula.conf');
|
|||
begin
|
|||
inherited create;
|
|||
fInterface:='eth0';
|
|||
fIPAdministrador:='';
|
|||
fUsuarioEquipoProfesor:=ObtenerNombreUsuario();
|
|||
fHomeUsuario:=ObtenerHomeUsuario(fUsuarioEquipoProfesor);
|
|||
fPuerto:=PUERTO_USUARIO;
|
|||
if FileExists(archivoini) then
|
|||
LeerSetupIni(archivoini);
|
|||
fIP:=ObtenerMiIP();
|
|||
end;
|
|||
procedure Tservidor.OrdenarDesactivarInternet();
|
|||
begin
|
|||
end;
|
|||
function Tservidor.ObtenerHomeUsuario(usuario:string):string;
|
|||
const
|
|||
ORD_HOME:string='echo ~%s';
|
|||
var
|
|||
homeusu:string;
|
|||
orden:string;
|
|||
begin
|
|||
orden:=Format(ORD_HOME,[usuario]);
|
|||
homeusu:=HallarDato(orden);
|
|||
fDirRecibidos:=homeusu+'/recibidos_administrador';
|
|||
result:=trim(homeusu);
|
|||
end;
|
|||
function Tservidor.ObtenerNombreUsuario():string;
|
|||
var
|
|||
nombreusu:string;
|
|||
begin
|
|||
nombreusu:=GetEnvironmentVariable('USER');
|
|||
if nombreusu='' then nombreusu:=' ';
|
|||
fUsuarioEquipoProfesor:=nombreusu;
|
|||
result:=nombreusu;
|
|||
end;
|
|||
procedure Tservidor.LeerSetupIni(archivo:string);
|
|||
var
|
|||
FicheroIni:TMemIniFile;
|
|||
begin
|
|||
try
|
|||
FicheroIni:=TMemIniFile.create(archivo);
|
|||
try
|
|||
with FicheroIni do
|
|||
begin
|
|||
fInterface:=ReadString('Red','Interface','eth0');
|
|||
fIPAdministrador:=ReadString('Administrador','IP','');
|
|||
end;
|
|||
finally
|
|||
FicheroIni.free;
|
|||
end;
|
|||
except
|
|||
writeln('Error al cargar datos');
|
|||
end;
|
|||
end;
|
|||
function Tservidor.ObtenerMiIp():string;
|
|||
var
|
|||
mip:string;
|
|||
begin
|
|||
mip:=trim(HallarDato('/sbin/ifconfig ' +fInterface+ ' | grep "inet" | head -n 1| tr -s " " | sed "s/^[ \t]*//" | cut -f2 -d":" | cut -f1 -d" "'));
|
|||
result:=mip;
|
|||
end;
|
|||
end.
|
|||