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