Code

bugfix: user messages adapted to server-server communication
[gosa.git] / gosa-si / client / events / gosaTriggered.pm
2 =head1 NAME
4 gosaTriggered.pm
6 =head1 SYNOPSIS
8 use GOSA::GosaSupportDaemon;
10 use Data::Dumper;
12 use MIME::Base64
14 =head1 DESCRIPTION
16 This module contains all GOsa-SI-client processing instructions concerning actions controllable from GOsa.
18 =head1 VERSION
20 Version 1.0
22 =head1 AUTHOR
24 Andreas Rettenberger <rettenberger at gonicus dot de>
26 =head1 FUNCTIONS
28 =cut
31 package gosaTriggered;
32 use Exporter;
33 @ISA = qw(Exporter);
34 my @events = (
35     "get_events",
36     "usr_msg",
37     "trigger_action_localboot",
38     "trigger_action_halt",
39     "trigger_action_faireboot",
40     "trigger_action_reboot",
41     "trigger_action_reinstall",
42     "trigger_action_update",
43     "trigger_action_instant_update",
44     );
45 @EXPORT = @events;
47 use strict;
48 use warnings;
49 use GOSA::GosaSupportDaemon;
50 use Data::Dumper;
51 use MIME::Base64;
53 BEGIN {}
55 END {}
57 ###############################################################################
58 =over 
60 =item B<get_events ()>
62 =over
64 =item description 
66     Reports all provided functions.
68 =item parameter
70     None.
72 =item return 
74     \@events - ARRAYREF - array containing all functions 
76 =back
78 =back
80 =cut
81 ###############################################################################
82 sub get_events { return \@events; }
85 ###############################################################################
86 =over 
88 =item B<usr_msg ($$)>
90 =over
92 =item description 
94     Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
96 =item parameter
98     $msg - STRING - complete GOsa-si message
99     $msg_hash - HASHREF - content of GOsa-si message in a hash
101 =item GOsa-si message xml content
102     
103     <to> - STRING - username message should be deliverd to
104     <subject> - STRING - subject of the message, base64 encoded
105     <message> - STRING - message itself, base64 encoded
107 =item return 
109     $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
111 =back
113 =back
115 =cut
116 ###############################################################################
117 sub usr_msg {
118     my ($msg, $msg_hash) = @_;
119     my $header = @{$msg_hash->{'header'}}[0];
120     my $source = @{$msg_hash->{'source'}}[0];
121     my $target = @{$msg_hash->{'target'}}[0];
123     my $to = @{$msg_hash->{'usr'}}[0];
124     my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
125     my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
127     system( "/usr/bin/goto-notify user-message '$to' '$subject' '$message'" );
129     # give gosa-si-server feedback, that msg was received
130     $msg =~ s/<header>usr_msg<\/header>/<header>confirm_usr_msg<\/header>/g;
131     my $out_hash = &create_xml_hash("confirm_usr_msg", $target, $source);
132     &add_content2xml_hash($out_hash, 'usr', $to);
133     &add_content2xml_hash($out_hash, 'subject', @{$msg_hash->{'subject'}}[0]);
134     &add_content2xml_hash($out_hash, 'message', @{$msg_hash->{'message'}}[0]);
137     return &create_xml_string($out_hash);
141 ###############################################################################
142 =over 
144 =item B<trigger_action_localboot ($$)>
146 =over
148 =item description 
150     Executes '/sbin/shutdown -r' if  no user is logged in otherwise write 
151     'trigger_action_localboot' to '/etc/gosa-si/event'
153 =item parameter
155     $msg - STRING - complete GOsa-si message
156     $msg_hash - HASHREF - content of GOsa-si message in a hash
158 =item GOsa-si message xml content
160     <timeout> - INTEGER - timeout to wait befor restart, default 0
162 =item return 
163     
164     Nothing.
166 =back
168 =back
170 =cut
171 ###############################################################################
172 sub trigger_action_localboot {
173     my ($msg, $msg_hash) = @_;
174     my $timeout;
176     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
177         $timeout = -1;
178     } 
179     else {
180         $timeout = @{$msg_hash->{timeout}}[0];
181     }
183     # check logged in user
184     my $logged_in_user = 1;
185     if( $logged_in_user ) {
186         # TODO do something
187     }
188     else {
189         $timeout = 0;
190     }
191         
192     # execute function
193     if( $timeout == 0 ) {
194         print STDERR ("shutdown -r +$timeout\n");
195     }
196     elsif( $timeout > 0 ) {
197         print STDERR ("shutdown -r +$timeout\n");
198     }
199     elsif( $timeout < 0 ) {
200         print STDERR "The administrator has sent a signal to reboot this workstation. It will reboot after you've logged out.\n";
201         open(FILE, "> /etc/gosa-si/event");
202         print FILE "trigger_action_localboot\n";
203         close(FILE);
204     }
205     else {
206         # TODO do something, error handling, logging
207     }
209     return;
213 ###############################################################################
214 =over 
216 =item B<trigger_action_faireboot ($$)>
218 =over
220 =item description 
222     Executes '/usr/sbin/faireboot'.
224 =item parameter
226     $msg - STRING - complete GOsa-si message
227     $msg_hash - HASHREF - content of GOsa-si message in a hash
229 =item GOsa-si message xml content
231     None.
233 =item return 
234     
235     Nothing.
237 =back
239 =back
241 =cut
242 ###############################################################################
243 sub trigger_action_faireboot {
244     my ($msg, $msg_hash) = @_;
245         &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7); 
246     system("/usr/sbin/faireboot");
247     return;
251 ###############################################################################
252 =over 
254 =item B<trigger_action_reboot ($$)>
256 =over
258 =item description 
260     Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r'  if  no 
261     user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
263 =item parameter
265     $msg - STRING - complete GOsa-si message
266     $msg_hash - HASHREF - content of GOsa-si message in a hash
268 =item GOsa-si message xml content
270     <timeout> - INTEGER - timeout to wait befor reboot, default 0
272 =item return 
273     
274     Nothing.
276 =back
278 =back
280 =cut
281 ###############################################################################
282 sub trigger_action_reboot {
283     my ($msg, $msg_hash) = @_;
284     my $timeout;
286     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
287         $timeout = 0;
288     } 
289     else {
290         $timeout = @{$msg_hash->{timeout}}[0];
291     }
293     # check logged in user
294     my @user_list = &get_logged_in_users;
295     if( @user_list >= 1 ) {
296         system( "/usr/bin/goto-notify reboot" );
297         open(FILE, "> /etc/gosa-si/event");
298         print FILE "reboot\n";
299         close(FILE);
300     }
301     else {
302         system( "/sbin/shutdown -r +$timeout &" );
303     }
305     return;
309 ###############################################################################
310 =over 
312 =item B<trigger_action_halt ($$)>
314 =over
316 =item description 
318     Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if  no 
319     user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
321 =item parameter
323     $msg - STRING - complete GOsa-si message
324     $msg_hash - HASHREF - content of GOsa-si message in a hash
326 =item GOsa-si message xml content
328     <timeout> - INTEGER - timeout to wait befor halt, default 0
330 =item return 
331     
332     Nothing.    
334 =back
336 =back
338 =cut
339 ###############################################################################
340 sub trigger_action_halt {
341     my ($msg, $msg_hash) = @_;
342     my $timeout;
344     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
345         $timeout = 0;
346     } 
347     else {
348         $timeout = @{$msg_hash->{timeout}}[0];
349     }
351     # check logged in user
352     my @user_list = &get_logged_in_users;
353     if( @user_list >= 1 ) {
354         system( "/usr/bin/goto-notify halt" );
355         open(FILE, "> /etc/gosa-si/event");
356         print FILE "halt\n";
357         close(FILE);
358     }
359     else {
360         system( "/sbin/shutdown -h +$timeout &" );
361     }
363     return;
367 ###############################################################################
368 =over 
370 =item B<trigger_action_reinstall>
372 =over
374 =item description 
376     Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no 
377     user is logged in otherwise write 'install' to '/etc/gosa-si/event'
379 =item parameter
381     $msg - STRING - complete GOsa-si message
382     $msg_hash - HASHREF - content of GOsa-si message in a hash
384 =item GOsa-si message xml content
386     None.
388 =item return 
389     
390     Nothing.
392 =back
394 =back
396 =cut
397 ###############################################################################
398 sub trigger_action_reinstall {
399     my ($msg, $msg_hash) = @_;
401     # check logged in user
402     my @user_list = &get_logged_in_users;
403     if( @user_list >= 1 ) {
404         system( "/usr/bin/goto-notify install" );
405         open(FILE, "> /etc/gosa-si/event");
406         print FILE "install\n";
407         close(FILE);
408     }
409     else {
410         system( "/sbin/shutdown -r now &" );
411     }
413     return;
417 ###############################################################################
418 =over 
420 =item B<trigger_action_updae>
422 =over
424 =item description 
426     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
428 =item parameter
430     $msg - STRING - complete GOsa-si message
431     $msg_hash - HASHREF - content of GOsa-si message in a hash
433 =item GOsa-si message xml content
435     None.
437 =item return 
438     
439     Nothing
441 =back
443 =back
445 =cut
446 ###############################################################################
447 # Backward compatibility
448 sub trigger_action_update {
449     my ($msg, $msg_hash) = @_;
451     # Execute update
452     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
454     return;
458 ###############################################################################
459 =over 
461 =item B<trigger_action_instant_update ($$)>
463 =over
465 =item description 
467     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
469 =item parameter
471     $msg - STRING - complete GOsa-si message
472     $msg_hash - HASHREF - content of GOsa-si message in a hash
474 =item GOsa-si message xml content
476     None.
478 =item return 
479     
480     Nothing.
482 =back
484 =back
486 =cut
487 ###############################################################################
488 # Backward compatibility
489 sub trigger_action_instant_update {
490     my ($msg, $msg_hash) = @_;
492     # Execute update
493     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
495     return;
499 1;