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