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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
package XXV::MODULES::CONFIG;
use strict;
use Tools;
# This module method must exist for XXV
# ------------------
sub module {
# ------------------
my $obj = shift || return error('No object defined!');
my $args = {
Name => 'CONFIG',
Prereq => {
'Module::Reload' => 'Reload %INC files when updated on disk ',
},
Description => gettext('This module edits, writes and saves the configuration.'),
Commands => {
configedit => {
description => gettext("Edit configuration 'section'"),
short => 'ce',
callback => sub{ $obj->edit(@_) },
Level => 'admin',
},
configget => {
description => gettext("Get configuration from 'modname'"),
short => 'cg',
callback => sub{ $obj->get(@_) },
Level => 'admin',
},
help => {
description => gettext("This will display all commands or description of module 'name'."),
short => 'h',
callback => sub{
return $obj->usage(@_);
},
},
reload => {
description => gettext("Restart all modules."),
short => 'rel',
callback => sub{
my ($console, $config, $l) = @_;
$Module::Reload::Debug = CORE::int(($Tools::VERBOSE+.5)/2);
# my %Status = %Module::Reload->Stat;
my $cnt = Module::Reload->check();
# my $files;
# foreach my $file (keys %Module::Reload::Stat) {
# push(@$files, $file)
# if($Module::Reload::Stat{$file} ne $Status{$file});
# }
if($cnt) {
$console->message(sprintf(gettext("Reload %d modules."),$cnt));
} else {
$console->message(gettext("There none module reloaded."));
}
},
Level => 'admin'
},
},
};
return $args;
}
# ------------------
sub new {
# ------------------
my($class, %attr) = @_;
my $self = {};
bless($self, $class);
$self->{charset} = delete $attr{'-charset'};
if($self->{charset} eq 'UTF-8'){
eval 'use utf8';
}
# paths
$self->{paths} = delete $attr{'-paths'};
# who am I
$self->{MOD} = $self->module;
# Try to use the Requirments
map {
eval "use $_";
if($@) {
my $m = (split(/ /, $_))[0];
return panic("\nCouldn't load perl module: $m\nPlease install this module on your system:\nperl -MCPAN -e 'install $m'");
}
} keys %{$self->{MOD}->{Prereq}};
# read the Configdata
$self->{config} = $attr{'-config'};
return $self;
}
# ------------------
sub _menu {
# ------------------
my $obj = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift;
my $sector = shift || 0;
my $ret = {};
$ret->{title} = gettext("Global settings");
$ret->{highlight} = $sector;
my $mods = main::getModules;
foreach my $module (sort keys %{$mods}) {
my $name = $mods->{$module}->{MOD}->{Name};
error(sprintf("Missing real modul name %s",$module)) unless($name);
next unless($name && exists $obj->{config}->{$name});
$ret->{links}->{$name} = {
text => $name,
link => "?cmd=configedit&data=$name",
};
}
return $console->littlemenu($ret);
}
# ------------------
sub edit {
# ------------------
my $obj = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift;
my $sector = shift || 0;
my $data = shift || 0;
$obj->_menu( $console, $config, $sector )
if($console->{TYP} eq 'HTML' or ($console->{TYP} ne 'HTML' and not $sector));
return unless $sector;
$sector = uc($sector) unless($sector eq 'General');
my $cfg = $obj->{config}->{$sector}
or return con_err($console, sprintf(gettext("Sorry, but section %s does not exist in the configuration!"),$sector));
my $mod = main::getModule($sector);
my $prefs = $mod->{MOD}->{Preferences}
or return con_err($console, sprintf(gettext("Sorry, but the settings in module: %s do not exist!"),$sector));
my $questions = [];
foreach my $name (sort { lc($a) cmp lc($b) } keys(%{$prefs})) {
my $def = $prefs->{$name}->{default};
$def = $cfg->{$name}
if(defined $cfg->{$name} && $cfg->{$name} ne "");
push(@$questions, $name,
{
typ => $prefs->{$name}->{type} || 'string',
options => $prefs->{$name}->{options},
msg => sprintf("%s:\n%s", ucfirst($name), ($prefs->{$name}->{description} || gettext('No description'))),
def => $def,
req => $prefs->{$name}->{required},
choices => $prefs->{$name}->{choices},
check => $prefs->{$name}->{check},
readonly => $prefs->{$name}->{readonly} || 0,
}
);
}
my $desc = $mod->{MOD}->{Description};
$desc =~ s/[\.\r\n].*$//g; # remove last part
$console->link({text => $desc . " - " . sprintf(gettext("%s manual") , $sector) . " ..." , url => "?cmd=doc&data=$sector"})
if($console->typ eq 'HTML');
$cfg = $console->question(sprintf(gettext('Edit configuration %s'), $sector), $questions, $data);
if(ref $cfg eq 'HASH') {
$obj->{config}->{$sector} = $cfg;
con_msg($console, sprintf(gettext("Section: '%s' saving ... please wait."), $sector));
my $success = $obj->write($console);
$console->redirect({url => '?cmd=configedit', wait => 1})
if($success eq 'ok'
&& $console->typ eq 'HTML');
}
}
# ------------------
sub write {
# ------------------
my $obj = shift || return error('No object defined!');
my $console = shift;
my $success = $obj->reconfigure($console);
my $configfile = main::getUsrConfigFile;
if($success eq 'ok'
&& !$obj->{config}->write( $configfile )) {
con_err($console, sprintf ("Couldn't write '%s': %s", $configfile , $! ));
return 'failed';
}
con_msg($console, sprintf(gettext("Configuration written to '%s'."), $configfile));
return $success;
}
# ------------------
sub get {
# ------------------
my $obj = shift || return error('No object defined!');
my $console = shift;
my $config = shift;
my $modname = shift || 0;
return con_err($console, gettext('Need a name of the module to display the configuration!'))
unless($modname);
$modname = uc($modname) unless($modname eq 'General');
my $cfg = $obj->{config}->{$modname};
con_err($console, sprintf(gettext("Sorry, but section %s does not exist in the configuration!"),$modname))
if(! $cfg);
if(ref $console) {
return $console->table($cfg);
} else {
return $cfg;
}
}
# ------------------
sub reconfigure {
# ------------------
my $obj = shift || return error('No object defined!');
my $console = shift;
my $success = 'ok';
my $cfg = $obj->{config};
foreach my $moduleName (keys %$cfg) {
if($moduleName eq 'General') {
main::reconfigure();
} else {
my $mod = main::getModule($moduleName)
or (error("$moduleName does not exist!") && next);
foreach my $parameter (keys %{$mod->{MOD}->{Preferences}}) {
if(defined $mod->{$parameter}) {
$cfg->{$moduleName}->{$parameter} = $mod->{MOD}->{Preferences}->{$parameter}->{default}
if(not defined $cfg->{$moduleName}->{$parameter});
$mod->{$parameter} = $cfg->{$moduleName}->{$parameter};
# Check this input
if(my $check = $mod->{MOD}->{Preferences}->{$parameter}->{check}) {
if(ref $check eq 'CODE') {
my ($ok, $err) = &$check($mod->{$parameter});
unless($ok || not $err) {
my $message = sprintf("Config -> %s -> %s: %s %s", $moduleName, $parameter, $mod->{$parameter}, $err);
con_err($console, $message);
$success = 'failed';
}
}
}
} else {
con_err($console, sprintf(gettext("Couldn't find %s in %s!"), $parameter, $moduleName));
$success = 'failed';
}
}
}
}
con_msg($console, gettext('Edit successful!'))
if($success eq 'ok');
return $success;
}
# ------------------
sub realModNames {
# ------------------
my $obj = shift || return error('No object defined!');
my $mods = main::getModules();
my @realModName;
# Search for command and display the Description
foreach my $modName (sort keys %{$mods}) {
my $modCfg = $mods->{$modName}->{MOD};
push(@realModName, $mods->{$modName}->{MOD}->{Name})
if(exists $mods->{$modName}->{MOD}->{Name});
}
return sort @realModName;
}
# ------------------
sub usage {
# ------------------
my $obj = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift;
my $modulename = shift || 0;
my $hint = shift || '';
my $user = shift || $console->{USER};
my $u = main::getModule('USER');
unless($user) {
my $loginObj = $obj;
$loginObj = main::getModule('HTTPD')
if ($console->{TYP} eq 'HTML') ;
$loginObj = main::getModule('WAPD')
if ($console->{TYP} eq 'WML') ;
$user = $loginObj->{USER};
}
my $ret;
if($console->typ eq 'HTML') {
push(@$ret, sprintf(gettext("%sThis is the xxv %s server.\nPlease use the following commands:\n"),
($hint ? "$hint\n\n" : ''), $console->typ));
}
$console->setCall('help');
my $mods = main::getModules();
my @realModName;
# Search for command and display the Description
foreach my $modName (sort keys %{$mods}) {
my $modCfg = $mods->{$modName}->{MOD};
my $name = $modCfg->{Name};
unless($name) {
error(sprintf("Missing real name from modul %s",$modName));
next;
}
next if($modulename and uc($modulename) ne $name);
push(@realModName, $name);
my $addcmd = undef;
foreach my $cmdName (sort keys %{$modCfg->{Commands}}) {
next if($modCfg->{Commands}->{$cmdName}->{hidden});
my ($ccmdobj, $ccmdname, $cshorterr, $cerror) = $u->checkCommand($console, $cmdName,"1");
next unless($ccmdobj);
push(@$ret,
[
$modCfg->{Commands}->{$cmdName}->{short},
$cmdName,
$name,
$modCfg->{Commands}->{$cmdName}->{description}
]
);
$addcmd = 'ok';
}
unless($addcmd) {
push(@$ret,
[
undef,
undef,
$name,
gettext("None active commands")
]
);
}
}
my $info = {
rows => scalar @$ret
};
if($console->typ eq 'HTML') {
$info->{periods} = $mods->{'XXV::MODULES::EPG'}->{periods};
$info->{CHANNELS} = $mods->{'XXV::MODULES::CHANNELS'}->ChannelWithGroup('c.name,c.hash');
$info->{CONFIGS} = [ sort @realModName ];
}
$console->table( $ret, $info );
}
1;
|