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