Dateiencodings auf UTF-8 geändert
[kivitendo-erp.git] / scripts / console
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use 5.008;                          # too much magic in here to include perl 5.6
6
7 BEGIN {
8   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
9   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
10 }
11
12 use Config::Std;
13 use Data::Dumper;
14 use Devel::REPL 1.002001;
15 use Term::ReadLine::Perl::Bind;     # use sane key binding for rxvt users
16
17 read_config 'config/console.conf' => my %config;# if -f 'config/console.conf';
18
19 my $login        = shift || $config{Console}{login}        || 'demo';
20 my $history_file =          $config{Console}{history_file} || '/tmp/lxoffice_console_history.log'; # fallback if users is not writable
21 my $autorun      =          $config{Console}{autorun};
22
23 # will be configed eventually
24 my @plugins      = qw(History LexEnv Colors MultiLine::PPI FancyPrompt PermanentHistory AutoloadModules);
25
26 my $repl = Devel::REPL->new;
27 $repl->load_plugin($_) for @plugins;
28 $repl->load_history($history_file);
29 $repl->eval('help');
30 $repl->print("trying to auto login as '$login'...");
31 $repl->print($repl->eval("lxinit '$login'"));
32 $repl->print($repl->eval($autorun)) if $autorun;
33 $repl->run;
34
35 package Devel::REPL;
36
37 use utf8;
38 use CGI qw( -no_xhtml);
39 use SL::Auth;
40 use SL::Form;
41 use SL::Locale;
42 use SL::LXDebug;
43 use Data::Dumper;
44
45 # this is a cleaned up version of am.pl
46 # it lacks redirection, some html setup and most of the authentication process.
47 # it is assumed that anyone with physical access and execution rights on this script
48 # won't be hindered by authentication anyway.
49 sub lxinit {
50   my $login = shift;
51
52   die 'need login' unless $login;
53
54   package main;
55
56   { no warnings 'once';
57     $::userspath  = "users";
58     $::templates  = "templates";
59     $::sendmail   = "| /usr/sbin/sendmail -t";
60   }
61
62   $::lxdebug = LXDebug->new;
63
64   eval { require "config/lx-erp.conf"; };
65   eval { require "config/lx-erp-local.conf"; } if -f "config/lx-erp-local.conf";
66
67   $::locale = Locale->new($::language);
68   $::cgi    = CGI->new qw();
69   $::form   = Form->new;
70   $::auth   = SL::Auth->new;
71
72   die 'cannot reach auth db'               unless $::auth->session_tables_present;
73
74   $::auth->restore_session;
75
76   require "bin/mozilla/common.pl";
77
78   die "cannot find user $login"            unless %::myconfig = $::auth->read_user($login);
79   die "cannot find locale for user $login" unless $::locale   = Locale->new($::myconfig{countrycode});
80
81   return "logged in as $login";
82 }
83
84 # these function provides a load command to slurp in a lx-office module
85 # since it's seldomly useful, it's not documented in help
86 sub load {
87   my $module = shift;
88   $module =~ s/[^\w]//g;
89   require "bin/mozilla/$module.pl";
90 }
91
92 sub reload {
93   require Module::Reload;
94   Module::Reload->check();
95
96   return "modules reloaded";
97 }
98
99 sub quit {
100   exit;
101 }
102
103 sub help {
104   print <<EOL;
105
106   Lx-Office Konsole
107
108   ./scripts/console [login]
109
110 Spezielle Kommandos:
111
112   help                - zeigt diese Hilfe an.
113   lxinit 'login'      - lädt das Lx-Office Environment für den User 'login'.
114   reload              - lädt modifizierte Module neu.
115   pp DATA             - zeigt die Datenstruktur mit Data::Dumper an.
116   quit                - beendet die Konsole
117
118 EOL
119 #  load   'module'     - läd das angegebene Modul, d.h. bin/mozilla/module.pl und SL/Module.pm.
120 }
121
122 sub pp {
123   $Data::Dumper::Indent   = 2;
124   $Data::Dumper::Maxdepth = 2;
125   Data::Dumper::Dumper(@_);
126 }
127
128 1;
129
130 __END__
131
132 =head1 NAME
133
134 scripts/console - Lx Office Console
135
136 =head1 SYNOPSIS
137
138   ./script/console
139   > help               # displays a brief documentation
140
141 =head1 DESCRIPTION
142
143 Users of Ruby on Rails will recognize this as a perl reimplementation of the
144 rails scripts/console. It's intend is to provide a shell environment to the
145 lx-office internals. This will mostly not interest you if you just want to do
146 your ERP stuff with lx-office, but will be invaluable for those who wish to
147 make changes to lx-office itself.
148
149 =head1 FUNCTIONS
150
151 You can do most things in the console that you could do in an actual perl
152 script. Certain helper functions will aid you in debugging the state of the
153 program:
154
155 =head2 pp C<DATA>
156
157 Named after the rails pretty print gem, this will call Data::Dumper on the
158 given C<DATA>. Use it to see what is going on.
159
160 Currently C<pp> will set the Data::Dumper depth to 2, so if you need a
161 different depth, you'll have to change that. A nice feature would be to
162 configure that, or at least to be able to change it at runtime.
163
164 =head2 lxinit C<login>
165
166 Login into lx-office using a specified login. No password will be required, and
167 security mechanisms will mostly be inactive. form, locale, myconfig will be
168 correctly set.
169
170 =head2 reload
171
172 Attempts to reload modules that changed since last reload (or inital startup).
173 This will mostly work just fine, except for Moose classes that have been made
174 immutable. Keep in mind that existing objects will continue to have the methods
175 of the classes they were created with.
176
177 =head1 BUGS
178
179  - Reload on immutable Moose classes is buggy.
180  - Logging in more than once is not supported by the program, and thus not by
181    the console. It seems to work, but strange things may happen.
182
183 =head1 SEE ALSO
184
185 Configuration of this script is located in:
186
187  config/console.conf
188  config/console.conf.default
189
190 See there for interesting options.
191
192 =head1 AUTHOR
193
194   Sven Schöling <s.schoeling@linet-services.de>
195
196 =cut