| 1 | package VUser::CORE; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | # Copyright 2004 Randy Smith | ||
| 6 | # $Id: CORE.pm,v 1.26 2007-09-21 20:40:19 perlstalker Exp $ | ||
| 7 | |||
| 8 | use vars qw(@ISA); | ||
| 9 | |||
| 10 | our $REVISION = (split (' ', '$Revision: 1.26 $'))[1]; | ||
| 11 | our $VERSION = "0.4.4"; | ||
| 12 | |||
| 13 | use Pod::Usage; | ||
| 14 | |||
| 15 | use VUser::Meta; | ||
| 16 | use VUser::ResultSet; | ||
| 17 | use VUser::Extension; | ||
| 18 | push @ISA, 'VUser::Extension'; | ||
| 19 | |||
| 20 | sub config_file | ||
| 21 | { | ||
| 22 | my $cfg = shift; | ||
| 23 | my $opts = shift; | ||
| 24 | |||
| 25 | print ("Current config file: ", tied (%$cfg)->GetFileName, "\n"); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub config_sample | ||
| 29 | { | ||
| 30 | my $cfg = shift; | ||
| 31 | my $opts = shift; | ||
| 32 | |||
| 33 | my $fh; | ||
| 34 | if (defined $opts->{file}) { | ||
| 35 | open ($fh, ">".$opts->{file}) | ||
| 36 | or die "Can't open '".$opts->{file}."': $!\n"; | ||
| 37 | } else { | ||
| 38 | $fh = \*STDOUT; | ||
| 39 | } | ||
| 40 | |||
| 41 | print $fh <<'CONFIG'; | ||
| 42 | [vuser] | ||
| 43 | # Enable debugging | ||
| 44 | debug = yes | ||
| 45 | |||
| 46 | # Space delimited list of extensions to load | ||
| 47 | # extensions = asterisk courier | ||
| 48 | extensions = courier | ||
| 49 | |||
| 50 | CONFIG | ||
| 51 | |||
| 52 | if (defined $opts->{file}) { | ||
| 53 | close $fh; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | sub config_inc { | ||
| 58 | my $cfg = shift; | ||
| 59 | my $opts = shift; | ||
| 60 | |||
| 61 | my $rs = VUser::ResultSet->new(); | ||
| 62 | $rs->add_meta(VUser::Meta->new('name' => 'path', | ||
| 63 | 'type' => 'string', | ||
| 64 | 'description' => 'Include path') | ||
| 65 | ); | ||
| 66 | foreach my $path (@INC) { | ||
| 67 | $rs->add_data([$path]); | ||
| 68 | } | ||
| 69 | |||
| 70 | return $rs; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub version | ||
| 74 | { | ||
| 75 | my $cfg = shift; | ||
| 76 | my $opts = shift; | ||
| 77 | |||
| 78 | my $rs = VUser::ResultSet->new(); | ||
| 79 | $rs->add_meta(VUser::Meta->new('name' => 'extension', | ||
| 80 | 'type' => 'string', | ||
| 81 | 'description' => 'Extension name')); | ||
| 82 | $rs->add_meta(VUser::Meta->new('name' => 'version', | ||
| 83 | 'type' => 'string', | ||
| 84 | 'description' => 'Version number')); | ||
| 85 | $rs->add_data(['CORE', $VERSION]); | ||
| 86 | return $rs; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub revision | ||
| 90 | { | ||
| 91 | my $cfg = shift; | ||
| 92 | my $opts = shift; | ||
| 93 | |||
| 94 | my $rs = VUser::ResultSet->new(); | ||
| 95 | $rs->add_meta(VUser::Meta->new('name' => 'extension', | ||
| 96 | 'type' => 'string', | ||
| 97 | 'description' => 'Extension name')); | ||
| 98 | $rs->add_meta(VUser::Meta->new('name' => 'version', | ||
| 99 | 'type' => 'string', | ||
| 100 | 'description' => 'Revision number')); | ||
| 101 | $rs->add_data(['CORE', $REVISION]); | ||
| 102 | return $rs; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub help | ||
| 106 | { | ||
| 107 | # pod2usage('-verbose' => 1); | ||
| 108 | my $cfg = shift; | ||
| 109 | my $opts = shift; | ||
| 110 | my $keyword = shift; # The 'action' for help is the keyword see details of | ||
| 111 | my $eh = shift; | ||
| 112 | |||
| 113 | use FindBin; | ||
| 114 | |||
| 115 | my @keywords = $eh->get_keywords(); | ||
| 116 | if ($keyword and $eh->is_keyword($keyword)) { | ||
| 117 | my $descr = $eh->get_description($keyword); | ||
| 118 | print "Run '$FindBin::Script help' to see all available keywords.\n"; | ||
| 119 | print "Options marked with '*' are required.\n"; | ||
| 120 | printf "** $keyword - %s\n", defined $descr? $descr : 'No description'; | ||
| 121 | my @actions = $eh->get_actions($keyword); | ||
| 122 | foreach my $action (@actions) { | ||
| 123 | $descr = $eh->get_description($keyword, $action) | ||
| 124 | || 'No description'; | ||
| 125 | printf ("%8s - %s\n", $action, $descr); | ||
| 126 | my @opts = $eh->get_options($keyword, $action); | ||
| 127 | foreach my $opt (@opts) { | ||
| 128 | $descr = $eh->get_description($keyword, $action, $opt) | ||
| 129 | || 'No Description'; | ||
| 130 | printf("\t%-16s %s - %s\n", | ||
| 131 | "--$opt", | ||
| 132 | ($eh->is_required($keyword, $action, $opt))? '*' : ' ', | ||
| 133 | $descr); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } else { | ||
| 137 | print "Run '$FindBin::Script help <keyword>' for more details.\n"; | ||
| 138 | print "Keywords: \n"; | ||
| 139 | foreach my $keyword (@keywords) { | ||
| 140 | my $descr = $eh->get_description($keyword) || 'No description'; | ||
| 141 | printf ("%18s - %s\n", $keyword, $descr); | ||
| 142 | |||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | sub man | ||
| 148 | { | ||
| 149 | my $cfg = shift; | ||
| 150 | my $opts = shift; | ||
| 151 | my $keyword = shift; # The 'action' for help is the keyword see details of | ||
| 152 | my $eh = shift; | ||
| 153 | |||
| 154 | if ($keyword) { | ||
| 155 | local $ENV{PERL5LIB} .= ':'.join ':',@INC; | ||
| 156 | system('perldoc', 'VUser::'.$keyword); | ||
| 157 | } else { | ||
| 158 | pod2usage('-verbose' => 2); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | sub init | ||
| 163 | { | ||
| 164 | my $eh = shift; # ExtHandler | ||
| 165 | my %cfg = @_; | ||
| 166 | |||
| 167 | # Config | ||
| 168 | $eh->register_keyword('config', 'Get information about the configuration.'); | ||
| 169 | $eh->register_action('config', 'file', 'Print the current config file.'); | ||
| 170 | $eh->register_task('config', 'file', \&config_file, 0); | ||
| 171 | |||
| 172 | # $eh->register_action('config', 'sample', 'Print a sample config file.'); | ||
| 173 | # $eh->register_task('config', 'sample', \&config_sample, 0); | ||
| 174 | # $eh->register_option('config', 'sample', 'file', '=s', 0, 'Write the sample to this file.'); | ||
| 175 | |||
| 176 | $eh->register_action('config', 'inc', 'Get the include paths'); | ||
| 177 | $eh->register_task('config', 'inc', \&config_inc, 0); | ||
| 178 | |||
| 179 | # Help | ||
| 180 | $eh->register_keyword('help', 'Print help/usage information.'); | ||
| 181 | $eh->register_action('help', '*', 'Get help for specific keyword.'); | ||
| 182 | $eh->register_task('help', '*', \&help); | ||
| 183 | |||
| 184 | # Man | ||
| 185 | $eh->register_keyword('man', 'Print documentation'); | ||
| 186 | $eh->register_action('man', '*'); | ||
| 187 | $eh->register_task('man', '*', \&man); | ||
| 188 | |||
| 189 | # Version | ||
| 190 | $eh->register_keyword('version', 'Show version information.'); | ||
| 191 | $eh->register_action('version', ''); | ||
| 192 | $eh->register_task('version', '', \&version); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub unload { }; | ||
| 196 | |||
| 197 | 1; | ||
| 198 | |||
| 199 | __END__ |