Proyecto

General

Perfil

Descargar (42,5 KB) Estadísticas
| Rama: | Revisión:
a2ba555f Elisa
unit demonio ;

{$mode objfpc}{$H+}

interface

uses
clcomandosroot,clservidor,globales,Classes , SysUtils , FileUtil , LResources ,
IdCmdTCPServer, IdCommandHandlers,IdContext, HTTPDefs, IdSync;

type

{ TDataModule1 }

TDataModule1 = class (TDataModule )
IdCmdTCPOrdenes: TIdCmdTCPServer;

procedure activarSonidoCommand(ASender: TIdCommand);
procedure apagarSalvaCommand (ASender : TIdCommand );
procedure aplicacionCommand (ASender : TIdCommand );
procedure archivoCommand (ASender : TIdCommand );
procedure carpetaCommand (ASender : TIdCommand );
procedure cerrarAlumnoCommand(ASender: TIdCommand);
procedure desactivarTRCommand (ASender : TIdCommand );
procedure desactivarTRNoCommand (ASender : TIdCommand );
procedure desbloquearMonitorOscuroCommand(ASender: TIdCommand);
procedure devolverArchivosCommand (ASender : TIdCommand );
procedure hallarIPCommand (ASender : TIdCommand );
procedure hallarUsuarioCommand (ASender : TIdCommand );
procedure IdCmdTCPOrdenesConnect(AContext: TIdContext);
procedure iniciarCuestionCommand (ASender : TIdCommand );
procedure iniciarExamenCommand (ASender : TIdCommand );
procedure limpiarCommand (ASender : TIdCommand );
procedure mensajeCommand (ASender : TIdCommand );
procedure mensajeNoCommand (ASender : TIdCommand );
procedure mostrarCommand (ASender : TIdCommand );
procedure mostrarNoCommand (ASender : TIdCommand );
procedure navegarCommand (ASender : TIdCommand );
procedure presentadorCommand (ASender : TIdCommand );
procedure ratonCommand (ASender : TIdCommand );
procedure ratonNoCommand (ASender : TIdCommand );
procedure recogerArchivosCommand (ASender : TIdCommand );
procedure Timer1Timer(Sender: TObject);
procedure verCommand (ASender : TIdCommand );
procedure verNoCommand (ASender : TIdCommand );
procedure videoCommand (ASender : TIdCommand );
procedure videoNoCommand (ASender : TIdCommand );
private
procedure CerrarServidor();
procedure DesativaSalvaCanon ;
procedure iniciarClienteVNC();
procedure IniciarDemonio(mip:string);
procedure IniciarDemonio1(mip:string);
{ private declarations }
public
{ public declarations }
end ;

var
DataModule1 : TDataModule1 ;
miservidor:Tservidor;
CONST
ORD_NUMERO_INCORRECTA:string='Orden o número parámetros incorrectos';


implementation

{ TDataModule1 }
procedure TDataModule1.IniciarDemonio1(mip:string);
var
aCommand:TIdCommandHandler;
begin
IdCmdTCPOrdenes:=TIdCmdTCPServer.Create(nil);
with IdCmdTCPOrdenes do
begin
Bindings.Clear;
Bindings.Add;
Bindings.items[0].IP:=mip;
Bindings.Items[0].Port:=PUERTO_USUARIO;
aCommand:=CommandHandlers.Add;
aCommand.Command := 'navegar';
aCommand.OnCommand := @navegarCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='202';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'mensaje';
aCommand.OnCommand := @mensajeCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:='$';
aCommand.ParamDelimiter:='$';
aCommand.NormalReply.Code:='204';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'mensajeNo';
aCommand.OnCommand := @mensajeNoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='205';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'aplicacion';
aCommand.OnCommand := @aplicacionCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='206';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'video';
aCommand.OnCommand := @videoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='208';
aCommand.Enabled:=true;


aCommand:=CommandHandlers.Add;
aCommand.Command := 'videoNo';
aCommand.OnCommand := @videoNoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='209';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'ver';
aCommand.OnCommand := @verCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='210';
aCommand.Enabled:=true;


aCommand:=CommandHandlers.Add;
aCommand.Command := 'verNo';
aCommand.OnCommand := @verNoCommand;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='211';
aCommand.Enabled:=true;
aCommand.Disconnect:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'mostrar';
aCommand.OnCommand := @mostrarCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='212';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'mostrarNo';
aCommand.OnCommand := @mostrarNoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='213';
aCommand.Enabled:=true;


aCommand:=CommandHandlers.Add;
aCommand.Command := 'raton';
aCommand.OnCommand := @ratonCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='214';
aCommand.Enabled:=true;


aCommand:=CommandHandlers.Add;
aCommand.Command := 'ratonNo';
aCommand.OnCommand := @ratonNoCommand;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='215';
aCommand.Enabled:=true;
aCommand.Disconnect:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'presentador';
aCommand.OnCommand := @presentadorCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='217';
aCommand.Enabled:=true;



aCommand:=CommandHandlers.Add;
aCommand.Command := 'desactivarTR';
aCommand.OnCommand := @desactivarTRCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='219';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'desactivarTRNo';
aCommand.OnCommand := @desactivarTRNoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='220';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'limpiar';
aCommand.OnCommand := @limpiarCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='225';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'apagarSalva';
aCommand.OnCommand := @apagarSalvaCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='226';
aCommand.Enabled:=true;



aCommand:=CommandHandlers.Add;
aCommand.Command := 'iniciarCuestion';
aCommand.OnCommand := @iniciarCuestionCommand;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='250';
aCommand.Enabled:=true;
aCommand.Disconnect:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'iniciarExamen';
aCommand.OnCommand := @iniciarExamenCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='251';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'carpeta';
aCommand.OnCommand := @carpetaCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='253';
aCommand.Enabled:=true;


aCommand:=CommandHandlers.Add;
aCommand.Command := 'archivo';
aCommand.OnCommand := @archivoCommand;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='252';
aCommand.Enabled:=true;
aCommand.Disconnect:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'recogerArchivos';
aCommand.OnCommand := @recogerArchivosCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='254';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'devolverArchivos';
aCommand.OnCommand := @devolverArchivosCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='255';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'hallarUsuario';
aCommand.OnCommand := @hallarUsuarioCommand;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='256';
aCommand.Enabled:=true;
aCommand.Disconnect:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'cerrarAlumno';
aCommand.OnCommand := @cerrarAlumnoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='257';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'activarSonido';
aCommand.OnCommand := @activarSonidoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='258';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'desbloquearMonitorOscuro';
aCommand.OnCommand := @desbloquearMonitorOscuroCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='402';
aCommand.Enabled:=true;

OnConnect:=@IdCmdTCPOrdenesConnect;
DefaultPort:=PUERTO_USUARIO;
MaxConnections:=0;
ListenQueue:=15;
TerminateWaitTime:=5000;
active:=true;

end;
end;
procedure TDataModule1.IniciarDemonio(mip:string);
var
orden, usuarioA:String;
begin
usuarioA:= GetEnvironmentVariable('USER');
orden:= 'cierraServidorAlumno ' + usuarioA;
// writeln(orden);
try
IniciarDemonio1(mip);
except
try
with ThrComandosRoot.create('localhost',36999,400,orden) do Resume;
sleep(3000);
IniciarDemonio1(mip);
except
IdCmdTCPOrdenes.active:=false;
writeln('Serveralumnolinex ya está iniciado');
Halt;
end;
end;

end;

procedure TDataModule1.CerrarServidor();
var
lista:Tlist;
i:integer;
begin
lista:=IdCmdTCPOrdenes.Contexts.LockList;
try
for i:=(lista.Count -1) downto 0 do
TIdContext(lista.items[i]).Connection.Disconnect();
finally
IdCmdTCPOrdenes.Contexts.UnlockList;
end;
sleep(IdCmdTCPOrdenes.TerminateWaitTime);
IdCmdTCPOrdenes.active:=false;
IdCmdTCPOrdenes.Bindings.Clear;
end;

procedure TDataModule1.hallarIPCommand (ASender : TIdCommand );
begin
Asender.Reply.SetReply(301,miservidor.IP);
ASender.SendReply;
end;

procedure TDataModule1.apagarSalvaCommand (ASender : TIdCommand );
const
ORD_APAGAR_SALVA:string='gnome-screensaver-command --exit &';

begin
if ASender.Params.Count<>0 then exit;
if fapagando then exit;
try
Ejecutar(ORD_APAGAR_SALVA);
except
//
end;
end;

procedure TDataModule1.aplicacionCommand (ASender : TIdCommand );
var
orden:String;
begin
try
if ASender.Params.Count=1 then
begin
orden:=HTTPDecode(Asender.Params[0]);
orden:= g_export_display+ orden +' & 2>> /dev/null';
Ejecutar(orden);
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('orden o número de parámetros incorrectos');
end;
end;


procedure TDataModule1.archivoCommand (ASender : TIdCommand );
var
orden,titulo,mensaje,archivo1:string;
stream:TFileStream;
begin
if fapagando then exit;
try
if (Asender.Params.Count=3) then
begin
archivo1:=g_directorio_recogidos+'/'+HTTPDecode(Asender.Params[0]);
mensaje:=Asender.Params[1];
titulo:=Asender.Params[2];
Asender.sendReply;
stream:=TfileStream.create(archivo1,fmcreate);
try
asender.Context.Connection.IOHandler.ReadStream(stream,-1,false);
if CrearArchivoMensaje('1',titulo,mensaje,'1') then
begin
orden:=g_export_display+'/usr/bin/alumnolinex &';
Ejecutar(orden);
end;
finally
freeandnil(stream)
end;
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('Error al enviar archivo');
end;
end;

procedure TDataModule1.carpetaCommand (ASender : TIdCommand );
const
ORD_DESCOMPRIMIR:STRING= '(cd %s; tar xzvf aulalinex-carpeta.tar.gz;rm -f %s) &';
var
titulo:string;
mensaje:string;
orden,archivo:string;
stream:TFileStream;
begin
if fapagando then exit;
try
if (Asender.Params.Count=2) then
begin
archivo:=g_directorio_personal+'/recibidos_profesor/aulalinex-carpeta.tar.gz';
mensaje:=Asender.Params[0];
titulo:=Asender.Params[1];
Asender.sendReply;
stream:=TfileStream.create(archivo,fmcreate);
try
asender.Context.Connection.IOHandler.ReadStream(stream,-1,false);
finally
freeandnil(stream);
sleep(2000);
orden:=Format(ORD_DESCOMPRIMIR,[g_directorio_recogidos,archivo]);
ejecutar1(orden);
if CrearArchivoMensaje('1',titulo,mensaje,'1') then
begin
orden:='/usr/bin/alumnolinex &';
ejecutar1(orden);
end;
end;
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('Error al recibir carpeta');
end;
end;

procedure TDataModule1.cerrarAlumnoCommand(ASender: TIdCommand);
begin
if ASender.Params.Count<>0 then exit;
CerrarServidor;
end;



procedure TDataModule1.activarSonidoCommand(ASender: TIdCommand);
var
orden,estado:String;
begin
try
if ASender.Params.Count =1 then
begin
estado:=Asender.Params[0];
orden:='amixer -c 0 -- sset Master ' + estado +' &';
Asender.Reply.SetReply(258,'correcto');
ASender.SendReply;
Ejecutar(orden);
end
else
begin
writeln(ORD_NUMERO_INCORRECTA)
end;
except
writeln('Error al cambiar el sonido')
end;
end;


procedure TDataModule1.desactivarTRCommand (ASender : TIdCommand );
begin
try
if ASender.Params.Count<>0 then exit;
miservidor.InactivarTecladoRaton();
except
//
end;
end;

procedure TDataModule1.desactivarTRNoCommand (ASender : TIdCommand );
begin
try
if ASender.Params.Count<>0 then exit;
miservidor.ActivarTecladoRaton();
except
//
end;
end;

procedure TDataModule1.desbloquearMonitorOscuroCommand(ASender: TIdCommand);
begin
try
if Asender.Params.Count=0 then
begin
miservidor.DesbloquearMonitorNegro();
end
else
begin
writeln('Orden o número parametros incorrectos');
end;
except
writeln('Error al desbloquear monitor ');
end;
end;

procedure TDataModule1.devolverArchivosCommand (ASender : TIdCommand );
const
ORD_DESCOMPRIMIR:STRING= '(cd %s; tar -xvzf aulalinex-devueltos.tar.gz) &';
var
titulo,mensaje,carpasig,asignatura:string;
orden,archivo:string;
stream:TFileStream;
begin
if fapagando then exit;
try
if (Asender.Params.Count=3) then
begin
asignatura:=Asender.Params[0];
mensaje:=Asender.Params[1];
titulo:=Asender.Params[2];
carpasig:=g_directorio_recogidos+'/'+asignatura;
if not DirectoryExists(carpasig) then
createdir(carpasig);
archivo:=carpasig+'/aulalinex-devueltos.tar.gz';
stream:=TfileStream.create(archivo,fmcreate);
try
Asender.sendReply;
asender.Context.Connection.IOHandler.ReadStream(stream,-1,false);

finally
freeandnil(stream);
sleep(2000);
orden:=Format(ORD_DESCOMPRIMIR,[carpasig]);
Ejecutar(orden);
Ejecutar('chmod 777 -R '+ carpasig);
if CrearArchivoMensaje('1',titulo,mensaje,'1') then
begin
orden:=g_export_display+'/usr/bin/alumnolinex &';
Ejecutar(orden);
end;
if FileExists(archivo) then
DeleteFile(archivo);
end
end
else
begin
writeln('ORD_NUMERO_INCORRECTA');
end;
except
writeln('No se han podido delvolver los archivos');
end;
end;


procedure TDataModule1.hallarUsuarioCommand (ASender : TIdCommand );
var
miusuario:string;
begin
try
miusuario:=miservidor.ObtenerNombreUsuario();
Asender.Reply.SetReply(306,miusuario);
ASender.SendReply;
except
writeln('Error al detectar el usuario ' + miusuario);
end;
end;

procedure TDataModule1.IdCmdTCPOrdenesConnect(AContext: TIdContext);
var
ipOrden:string;
begin
ipOrden:=trim(AContext.Connection.Socket.Binding.PeerIP);
if ((ipOrden<>miservidor.ipprofe) and (miservidor.ipprofe<>'000.000.000.000')) then AContext.Connection.Disconnect;
end;

procedure TDataModule1.iniciarCuestionCommand (ASender : TIdCommand );
const
ORD_ALUMNO_CUESTION:string='/usr/bin/aulalinexcuestion &';
var
archivo1:string;
stream:TFileStream;
begin
if fapagando then exit;
try
archivo1:=g_directorio_recogidos+ '/.cuestion.ini';
if FileExists(archivo1) then DeleteFile(archivo1);
asender.sendReply;
stream:=TfileStream.create(archivo1,fmcreate);
try
asender.Context.Connection.IOHandler.ReadStream(stream,-1,false);
finally
freeandnil(stream);
sleep(2000);
Ejecutar('chmod 777 ' + archivo1);
Ejecutar(g_export_display+ORD_ALUMNO_CUESTION);
end;
except
writeln(ORD_NUMERO_INCORRECTA);
end;
end;


procedure TDataModule1.iniciarExamenCommand (ASender : TIdCommand );
const
ORD_ALUMNO_EXAMEN:string='/usr/bin/aulalinexexamen &';
var
archivo1:string;
stream:TFileStream;
begin
if fapagando then exit;
try
archivo1:=g_directorio_recogidos+'/.examen.ini';
if FileExists(archivo1) then DeleteFile(archivo1);
asender.sendReply;
stream:=TfileStream.create(archivo1,fmcreate);
try
asender.Context.Connection.IOHandler.ReadStream(stream,-1,false);
finally
freeandnil(stream);
sleep(2000);
Ejecutar('chmod 777 ' + archivo1);
Ejecutar(g_export_display+ORD_ALUMNO_EXAMEN);
end;
except
writeln(ORD_NUMERO_INCORRECTA);
end;
end;

procedure TDataModule1.limpiarCommand (ASender : TIdCommand );
const
ORD_LIMPIAR:string='rm -rf %s/*';
var
tipo:integer;
orden:String;
begin
try
if Assigned (ASender.Params) then
begin
tipo:=strtoint(Asender.Params[0]);
case tipo of
0:
begin
orden:=format(ORD_LIMPIAR,[g_directorio_recogidos]);
Ejecutar(orden);
end;
1:
begin
orden:=format(ORD_LIMPIAR,[g_directorio_enviar]);
Ejecutar(orden);
end ;
2:
begin
orden:=format(ORD_LIMPIAR,[g_directorio_recogidos]);
Ejecutar(orden);
orden:=format(ORD_LIMPIAR,[g_directorio_enviar]);
Ejecutar(orden);
end;
end;
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('orden o número de parámetros incorrecto');
end;
end;

procedure TDataModule1.mensajeCommand (ASender : TIdCommand );
var
orden,tipo,asunto, mensaje:String;
begin
try
if ASender.Params.Count =3 then
begin
tipo:=Asender.Params[0];
asunto:= Asender.Params[1];
mensaje:=Asender.Params[2];
if CrearArchivoMensaje(tipo,asunto,mensaje,'0') then
begin
orden:=g_export_display+'/usr/bin/alumnolinex &';
Ejecutar(orden);
end;
end
else
begin
writeln(ORD_NUMERO_INCORRECTA)
end;
except
writeln('Error al enviar el mensaje')
end;
end;

procedure TDataModule1.mensajeNoCommand (ASender : TIdCommand );
begin
try
if ASender.Params.Count<>0 then exit;
MatarAplicacion('alumnolinex');
except
writeln('Error al cerrar mensaje');
end;
end;

procedure TDataModule1.mostrarCommand (ASender : TIdCommand );
const
ORDEN_CREAR_CONTRA:STRING='aulaconlinex %s %s/.seral456512';
ORDEN_MOSTRAR:string='xvnc4viewer -ViewOnly -UseLocalCursor=0 -LowColourLevel=1 -MenuKey F15 -Shared -Fullscreen -passwd %s %s:5989 &';
var
archivocontra,orden,contra,ippro:string;
begin
try
if ASender.Params.count=2 then
begin
contra:=Asender.Params[0];
ippro:=Asender.Params[1];
orden:=Format(ORDEN_CREAR_CONTRA,[contra,g_directorio_personal]);
Ejecutar(orden);
sleep(100);
archivocontra:=g_directorio_personal+'/.seral456512';
orden:=g_export_display+ORDEN_MOSTRAR;
orden:=Format(orden,[archivocontra,ippro]);
Ejecutar(orden);
if miservidor.UsuarioCanon then
miservidor.InactivarTecladoRaton();
Ejecutar('chmod 777 '+archivocontra);
end
else
begin
writeln('numero de parametros incorrecto');
end;
except
writeln('Error al mostrar el profesor');
end;
end;

procedure TDataModule1.mostrarNoCommand (ASender : TIdCommand );
begin
try
if ASender.Params.Count<>0 then exit;
if fapagando then exit;
MatarAplicacion('xvnc4viewer');
except
writeln('Error al detener mostrar alumno');
end;
end;

procedure TDataModule1.navegarCommand (ASender : TIdCommand );
var
navegador,orden, pagina:string;
begin
try
if Assigned (ASender.Params) then
begin
if FileExists('/usr/bin/google-chrome') and (miservidor.UsuarioAlumno <>'root') then
navegador:= 'google-chrome'
else
navegador:='firefox';
pagina:=HTTPDecode(Asender.Params[0]);
orden:=g_export_display+ navegador+' %s &';
orden:= Format(orden,[pagina]);
Ejecutar(orden);
end
else
begin
writeln('orden no admitinda');
end;
except
writeln('Error al ejecutar la orden');
end;
end;



procedure TDataModule1.presentadorCommand (ASender : TIdCommand );
begin
try
if ASender.Params.count=2 then
begin
Ejecutar('killall xvnc4viewer');
iniciarClienteVNC();
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('Error al iniciar presentación');
end;
end;

procedure TDataModule1.ratonCommand (ASender : TIdCommand );
const
ORDEN_CREAR_CONTRA:STRING='aulaconlinex %s %s/.seral456512';
ORDEN_MOSTRAR:string='xvnc4viewer -UseLocalCursor=0 -LowColourLevel=1 -MenuKey F15 -Shared -Fullscreen -passwd %s %s:5989 &';
ORDEN_MATAR_VNC='killall xvnc4viewer 2>> /dev/null';
var
archivocontra, orden,ippro,contra:string;
begin
try
if ASender.Params.count=2 then
begin
Ejecutar(ORDEN_MATAR_VNC);
contra:=Asender.Params[0];
ippro:=Asender.Params[1];
orden:=Format(ORDEN_CREAR_CONTRA,[contra,g_directorio_personal]);
Ejecutar(orden);
sleep(100);
archivocontra:=g_directorio_personal+'/.seral456512';
orden:=g_export_display+ORDEN_MOSTRAR;;
orden:=Format(orden,[archivocontra,ippro]);
Ejecutar(orden);
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('Error al ceder el ratón');
end;
end;

procedure TDataModule1.ratonNoCommand (ASender : TIdCommand );
const
ORDEN_CREAR_CONTRA:STRING='aulaconlinex %s %s/.seral456512';
ORDEN_MOSTRAR:string='xvnc4viewer -UseLocalCursor=0 -LowColourLevel=1 -ViewOnly -MenuKey F15 -Shared -Fullscreen -passwd %s %s:5989 &';
ORDEN_MATAR_VNC='killall xvnc4viewer';
var
archivocontra,orden,ippro,contra:string;
begin
try
if ASender.Params.count=2 then
begin
Ejecutar(ORDEN_MATAR_VNC);
contra:=Asender.Params[0];
ippro:=Asender.Params[1];
orden:=Format(ORDEN_CREAR_CONTRA,[contra,g_directorio_personal]);
Ejecutar(orden);
sleep(100);
archivocontra:=g_directorio_personal+'/.seral456512';
orden:=g_export_display+ORDEN_MOSTRAR;;
orden:=Format(orden,[archivocontra,ippro]);
Ejecutar(orden);

end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('orden o número de parámetros incorrecto');
end;
end;

procedure TDataModule1.recogerArchivosCommand (ASender : TIdCommand );
const
ORDEN_COMPRIMIR:string='(cd %s; tar czvf recogidos_alumno.tar.gz %s;touch %s;chmod 777 %s -R )';
var
orden,asunto, mensaje,archivo:string;
stream:TfileStream;
crcontador:integer=0;
comprimidos:Boolean=false;
arch_recogidos:String;
begin
if fapagando then exit;
try
arch_recogidos:=g_directorio_personal+'/ficheros_recogidos.txt';
if fileExists(arch_recogidos) then Deletefile(arch_recogidos);
archivo:=g_directorio_enviar+'/recogidos_alumno.tar.gz';
if fileExists(archivo) then Deletefile(archivo);
orden:= Format(ORDEN_COMPRIMIR,[g_directorio_enviar,'*',arch_recogidos,g_directorio_enviar]);
ejecutar1(orden);
while ((comprimidos=false) and (crcontador <500)) do
begin
comprimidos:=fileExists(arch_recogidos);
inc(crcontador);
Sleep(100);
end;
Sleep(1000);
if fileExists(archivo) then
begin
stream:=TfileStream.create(archivo, fmOpenRead+ fmShareDenyNone);
try
asunto:=Asender.Params[2];
mensaje:=Asender.Params[1];
ASender.SendReply;
asender.Context.Connection.IOHandler.WriteBufferOpen;
asender.Context.Connection.IOHandler.Write(stream,0,true);
asender.Context.Connection.IOHandler.WriteBufferClose;
if CrearArchivoMensaje('1',asunto,mensaje,'0') then
begin
orden:='/usr/bin/alumnolinex &';
ejecutar1(orden);
end;
finally
begin
stream.free;
if fileExists(archivo) then
Deletefile(archivo);
end;
end;
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('orden o número de parámetros incorrecto');
end;
end;
procedure TDataModule1.Timer1Timer(Sender: TObject);
begin

end;

Procedure TDataModule1.iniciarClienteVNC ( );
const
HALLARX11VNC:string='ps aux | grep %d |head -n 1| grep -v "grep"| grep -v 5989 | grep -c "x11vnc |"';
ORDEN_INICIAR_VNC:STRING='x11vnc -shared -forever -noshm -rfbport %d -passwd 02101067 & 2>> /dev/null';
var
orden:string;
numvnc,vncactivo:string;
begin
try
vncactivo:=Format(HALLARX11VNC,[PUERTO_VNC]);
numvnc:=HallarDatos(vncactivo);
if (numvnc= '0') then
begin
orden:=g_export_display+ORDEN_INICIAR_VNC;
orden:=Format(orden,[PUERTO_VNC]);
//writeln('orden '+ orden);
Ejecutar(orden);
end;
except
//
end;
end;

procedure TDataModule1.verCommand (ASender : TIdCommand );
begin
try
if ASender.Params.count=2 then
iniciarClienteVNC()
else
writeln('Numero parametro incorrectos al iniciar servidor vnc');
except
writeln('Error al mostrar imagen');
end;
end;

procedure TDataModule1.verNoCommand (ASender : TIdCommand );
begin
try
if ASender.Params.Count<>0 then exit;
if fapagando then exit;
Ejecutar('killall x11vnc &');
except
writeln('Error al ocultar al alumno')
end
end;

procedure TDataModule1.videoCommand (ASender : TIdCommand );
const
ORDEN_MIRAR_VIDEO:STRING='cvlc -f --video-on-top udp://@%s &';
var
orden,ipvlc:string;
begin
try
if fapagando then exit;
if Assigned (ASender.Params) then
begin
ipvlc:=HTTPDecode(Asender.Params[0]);
orden:=g_export_display+ORDEN_MIRAR_VIDEO;
orden:=Format(orden,[ipvlc]);
Ejecutar(orden);
end
else
begin
writeln(ORD_NUMERO_INCORRECTA);
end;
except
writeln('Error al iniciar al alumno');
end;
end;

procedure TDataModule1.videoNoCommand (ASender : TIdCommand );
begin
try
if ASender.Params.Count<>0 then exit;
if fapagando then exit;
MatarAplicacion('vlc');
except
writeln('Error al detener el vídeo');
end;
end;
procedure TDataModule1.DesativaSalvaCanon;
begin

end;

initialization
{$I demonio.lrs}
DataModule1:=TDataModule1.create(nil);
miservidor:=Tservidor.create;
fusuario:=miservidor.UsuarioAlumno;
fmipuerto:=miservidor.Puerto;
//writeln(miservidor.IPProfe);
DataModule1.IniciarDemonio('0.0.0.0');
g_directorio_personal:=miservidor.HomeUsuario;
g_directorio_recogidos:=miservidor.DirRecibidos;
g_directorio_enviar:=miservidor.DirEnviar;

if not directoryexists(g_directorio_recogidos) then
begin
ForceDirectories(g_directorio_recogidos);
Ejecutar('chmod 777 ' +g_directorio_recogidos +' -R &');
end;
if not directoryexists(g_directorio_enviar) then
begin
ForceDirectories(g_directorio_enviar);
Ejecutar('chmod 777 ' +g_directorio_enviar +' -R &');
end;
g_home_verdadero:=GetEnvironmentVariable('HOME');
if DirectoryExists(g_home_verdadero+'/Desktop') then
begin
Ejecutar('if [ ! -L '+g_home_verdadero+'/Desktop/enviar_al_profesor ]; then ln -s '+ g_directorio_enviar+ ' '+g_home_verdadero+'/Desktop/enviar_al_profesor; fi');
Ejecutar('if [ ! -L '+g_home_verdadero+'/Desktop/recibidos_del_profesor ]; then ln -s '+ g_directorio_recogidos+' ' +g_home_verdadero+'/Desktop/recibidos_del_profesor; fi');
end;
// writeln('g_home_verdadero ',g_home_verdadero);
if DirectoryExists('/home/'+miservidor.UsuarioAlumno+'/Desktop') then
begin
Ejecutar('if [ ! -L /home/'+miservidor.UsuarioAlumno+'/Desktop/enviar_al_profesor ]; then ln -s '+ g_directorio_enviar+ ' /home/'+miservidor.UsuarioAlumno+'/Desktop/enviar_al_profesor; fi');
Ejecutar('if [ ! -L /home/'+miservidor.UsuarioAlumno+'/Desktop/recibidos_del_profesor ]; then ln -s '+ g_directorio_recogidos+' /home/'+miservidor.UsuarioAlumno+'/Desktop/recibidos_del_profesor; fi');
end;
if DirectoryExists('/home/alumnos/'+miservidor.UsuarioAlumno+'/Desktop') then
begin
Ejecutar('if [ ! -L /home/alumnos/'+miservidor.UsuarioAlumno+'/Desktop/enviar_al_profesor ]; then ln -s '+ g_directorio_enviar+ ' /home/alumnos/'+miservidor.UsuarioAlumno+'/Desktop/enviar_al_profesor; fi');
Ejecutar('if [ ! -L /home/alumnos/'+miservidor.UsuarioAlumno+'/Desktop/recibidos_del_profesor ]; then ln -s '+ g_directorio_recogidos+' /home/alumnos/'+miservidor.UsuarioAlumno+'/Desktop/recibidos_del_profesor; fi');
end;
if DirectoryExists('/homeInst/personal/'+miservidor.UsuarioAlumno+'/Desktop') then
begin
Ejecutar('if [ ! -L /homeInst/personal/'+miservidor.UsuarioAlumno+'/Desktop/enviar_al_profesor ]; then ln -s '+ g_directorio_enviar+ ' /homeInst/personal/'+miservidor.UsuarioAlumno+'/Desktop/enviar_al_profesor; fi');
Ejecutar('if [ ! -L /homeInst/personal/'+miservidor.UsuarioAlumno+'/Desktop/recibidos_del_profesor ]; then ln -s '+ g_directorio_recogidos+' /homeInst/personal/'+miservidor.UsuarioAlumno+'/Desktop/recibidos_del_profesor; fi');
end;
if DirectoryExists(g_home_verdadero+'/Escritorio') then
begin
Ejecutar('if [ ! -L '+g_home_verdadero+'/Escritorio/enviar_al_profesor ]; then ln -s '+ g_directorio_enviar+ ' '+g_home_verdadero+'/Escritorio/enviar_al_profesor; fi');
Ejecutar('if [ ! -L '+g_home_verdadero+'/Escritorio/recibidos_del_profesor ]; then ln -s '+ g_directorio_recogidos+' '+g_home_verdadero+'/Escritorio/recibidos_del_profesor; fi');
end;
// writeln('g_home_verdadero ',g_home_verdadero);
if DirectoryExists('/home/'+miservidor.UsuarioAlumno+'/Escritorio') then
begin
Ejecutar('if [ ! -L /home/'+miservidor.UsuarioAlumno+'/Escritorio/enviar_al_profesor ]; then ln -s '+ g_directorio_enviar+ ' /home/'+miservidor.UsuarioAlumno+'/Escritorio/enviar_al_profesor; fi');
Ejecutar('if [ ! -L /home/'+miservidor.UsuarioAlumno+'/Escritorio/recibidos_del_profesor ]; then ln -s '+ g_directorio_recogidos+' /home/'+miservidor.UsuarioAlumno+'/Escritorio/recibidos_del_profesor; fi');
end;
if DirectoryExists('/home/alumnos/'+miservidor.UsuarioAlumno+'/Escritorio') then
begin
Ejecutar('if [ ! -L /home/alumnos/'+miservidor.UsuarioAlumno+'/Escritorio/enviar_al_profesor ]; then ln -s '+ g_directorio_enviar+ ' /home/alumnos/'+miservidor.UsuarioAlumno+'/Escritorio/enviar_al_profesor; fi');
Ejecutar('if [ ! -L /home/alumnos/'+miservidor.UsuarioAlumno+'/Escritorio/recibidos_del_profesor ]; then ln -s '+ g_directorio_recogidos+' /home/alumnos/'+miservidor.UsuarioAlumno+'/Escritorio/recibidos_del_profesor; fi');
end;
if DirectoryExists('/homeInst/personal/'+miservidor.UsuarioAlumno+'/Escritorio') then
begin
Ejecutar('if [ ! -L /homeInst/personal/'+miservidor.UsuarioAlumno+'/Escritorio/enviar_al_profesor ]; then ln -s '+ g_directorio_enviar+ ' /homeInst/personal/'+miservidor.UsuarioAlumno+'/Escritorio/enviar_al_profesor; fi');
Ejecutar('if [ ! -L /homeInst/personal/'+miservidor.UsuarioAlumno+'/Escritorio/recibidos_del_profesor ]; then ln -s '+ g_directorio_recogidos+' /homeInst/personal/'+miservidor.UsuarioAlumno+'/Escritorio/recibidos_del_profesor; fi');
end;
//writeln('inciando elservidor');
g_dirmensaje:=g_directorio_recogidos+'/.mensaje.ini';
if miservidor.UsuarioCanon then
miservidor.InactivarTecladoRaton;

Finalization
FreeAndNil(DataModule1);
FreeAndNil(miservidor);

end .