-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDCBSettings.pm
79 lines (66 loc) · 1.75 KB
/
DCBSettings.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
package DCBSettings;
use strict;
use warnings;
use Cwd 'abs_path';
use File::Basename;
use YAML::AppConfig;
my $config_file = 'odchbot.yml';
sub new { return bless {}, shift }
sub config_init() {
my ($class, $config_name) = @_;
my $yaml = config_load($config_name);
our $config = $yaml->get('config');
}
sub config_load {
my $config = shift // $config_file;
our ( $settings, $cwd, $suffix ) = fileparse( abs_path(__FILE__) );
# Create a new YAML object and get the config settings from file
# Nested variables may be used: $config->{variables}->{timezone}
return YAML::AppConfig->new( file => $cwd . $config );
}
sub config_set {
my $variable = shift;
my $value = shift;
my $yaml = config_load();
$DCBSettings::config->{$variable} = $value;
$yaml->{config}->{config}->{$variable} = $value;
return config_save($yaml);
}
sub config_get {
my $variable = shift;
if ($DCBSettings::config->{$variable}) {
return $DCBSettings::config->{$variable};
}
return 0;
}
sub config_delete {
my $variable = shift;
my $yaml = config_load();
if ($DCBSettings::config->{$variable}) {
delete($DCBSettings::config->{$variable});
delete($yaml->{config}->{config}->{$variable});
return config_save($yaml);
}
return 0;
}
sub config_reload {
my $yaml = config_load();
my $conf = $yaml->get('config');
foreach my $key (keys %{$conf}) {
# Definitely do not override the db.
if ($key ne 'db' && $key ne 'jabber') {
if (!exists($DCBSettings::config->{$key}) || $DCBSettings::config->{$key} ne $conf->{$key}) {
$DCBSettings::config->{$key} = $conf->{$key};
}
}
}
return 1;
}
sub config_save {
my $yaml = shift;
if ($yaml->dump($DCBSettings::cwd . $config_file)) {
return 1;
}
return 0;
}
1;