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