This repository was archived by the owner on Aug 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComando.pm
89 lines (66 loc) · 2.11 KB
/
Comando.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl
#==============================================================================
# ARCHIVO: Comando.pm
# AUTOR: Victor Rodriguez - victor <at> vrdominguez.es
# DESCRIPCION: Clase base para los comandos del contestador Raspberry Pi
#==============================================================================
package Comando;
use strict;
use YAML;
use Cwd qw(abs_path);
use File::Basename;
# --- metodos de clase -------------------------------------------------------------
sub instanciarComando {
my $comando = shift;
#comando siempre debe tener el mismo formato: todo mininusculas menos la primera letra
$comando = ucfirst(lc($comando));
my $class = "Comando::$comando";
my $ruta = "Comando/$comando\.pm";
eval {
require $ruta;
};
if ($@) {
die("Error cargando la clase necesaria para ejecutar $comando. Detalles: $@");
}
return $class->new();
}
sub comandosDisponibles {
my $ruta_base = dirname(abs_path(__FILE__));
my @lista_comandos = `ls $ruta_base/Comando/`;
foreach my $comando (@lista_comandos) {
chomp $comando;
$comando =~ s/\.pm$//g;
$comando = lc($comando);
}
return \@lista_comandos;
}
# --- metodos para objetos ---------------------------------------------------------
sub new {
my $self = bless({'usuario' => $ENV{CONTACT_NICK}}, $_[0]);
$self->{configuraciones} = $self->__cargaConfiguraciones();
return $self;
}
sub __cargaConfiguraciones {
# Calculamos la ruta al fichero de configuracion
my $fichero_config = dirname(abs_path(__FILE__)) . '/config.yml';
# Abrimos el fichero de configuracion
open(my $fichero, '<', $fichero_config)
|| die('No se pudo cargar la configuracion');
my $config = &YAML::LoadFile($fichero);
close($fichero);
return $config;
}
sub params {
my $self = $_[0];
die("Metodo no definido, es necesario reescribirlo en cada comando");
}
sub ejecutar {
my $self = $_[0];
die("Metodo no definido, es necesario reescribirlo en cada comando");
}
sub soloAdministradores {
my $self = $_[0];
die "Permisos insuficinetes" unless ( $self->{usuario} ~~ @{$self->{configuraciones}->{administradores}} );
return 1;
}
1;