Code

* change of client/event/gosaTriggered.pm to trigger the 'seen'-feedback
[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     "trigger_goto_settings_reload",
45     );
46 @EXPORT = @events;
48 use strict;
49 use warnings;
50 use GOSA::GosaSupportDaemon;
51 use Data::Dumper;
52 use MIME::Base64;
53 use Data::Random qw(:all);
55 BEGIN {}
57 END {}
59 ###############################################################################
60 =over 
62 =item B<get_events ()>
64 =over
66 =item description 
68     Reports all provided functions.
70 =item parameter
72     None.
74 =item return 
76     \@events - ARRAYREF - array containing all functions 
78 =back
80 =back
82 =cut
83 ###############################################################################
84 sub get_events { return \@events; }
87 ###############################################################################
88 =over 
90 =item B<usr_msg ($$)>
92 =over
94 =item description 
96     Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
98 =item parameter
100     $msg - STRING - complete GOsa-si message
101     $msg_hash - HASHREF - content of GOsa-si message in a hash
103 =item GOsa-si message xml content
104     
105     <to> - STRING - username message should be deliverd to
106     <subject> - STRING - subject of the message, base64 encoded
107     <message> - STRING - message itself, base64 encoded
109 =item return 
111     $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
113 =back
115 =back
117 =cut
118 ###############################################################################
119 sub usr_msg {
120     my ($msg, $msg_hash) = @_;
121     my $header = @{$msg_hash->{'header'}}[0];
122     my $source = @{$msg_hash->{'source'}}[0];
123     my $target = @{$msg_hash->{'target'}}[0];
125     my $to = @{$msg_hash->{'usr'}}[0];
126     my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
127     my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
129     my $rand_file = "/tmp/goto_notify_".join("",rand_chars(set=>'alphanumeric', min=>16, max=>16));
130     open(DATEI, ">$rand_file");
131     print DATEI "source:$source\ntarget:$target\nusr:$to\nsubject:".@{$msg_hash->{'subject'}}[0]."\nmessage:".@{$msg_hash->{'message'}}[0]."\n";
132     close DATEI;
134     my $feedback = system("/usr/bin/goto-notify user-message '$to' '$subject' '$message' '$rand_file' &" );
136     # give gosa-si-server feedback, that msg was received
137 #    $msg =~ s/<header>usr_msg<\/header>/<header>confirm_usr_msg<\/header>/g;
138 #    my $out_hash = &create_xml_hash("confirm_usr_msg", $target, $source);
139 #    &add_content2xml_hash($out_hash, 'usr', $to);
140 #    &add_content2xml_hash($out_hash, 'subject', @{$msg_hash->{'subject'}}[0]);
141 #    &add_content2xml_hash($out_hash, 'message', @{$msg_hash->{'message'}}[0]);
144 #    return &create_xml_string($out_hash);
145     return
149 ###############################################################################
150 =over 
152 =item B<trigger_action_localboot ($$)>
154 =over
156 =item description 
158     Executes '/sbin/shutdown -r' if  no user is logged in otherwise write 
159     'trigger_action_localboot' to '/etc/gosa-si/event'
161 =item parameter
163     $msg - STRING - complete GOsa-si message
164     $msg_hash - HASHREF - content of GOsa-si message in a hash
166 =item GOsa-si message xml content
168     <timeout> - INTEGER - timeout to wait befor restart, default 0
170 =item return 
171     
172     Nothing.
174 =back
176 =back
178 =cut
179 ###############################################################################
180 sub trigger_action_localboot {
181     my ($msg, $msg_hash) = @_;
182     my $timeout;
184     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
185         $timeout = -1;
186     } 
187     else {
188         $timeout = @{$msg_hash->{timeout}}[0];
189     }
191     # check logged in user
192     my $logged_in_user = 1;
193     if( $logged_in_user ) {
194         # TODO do something
195     }
196     else {
197         $timeout = 0;
198     }
199         
200     # execute function
201     if( $timeout == 0 ) {
202         print STDERR ("shutdown -r +$timeout\n");
203     }
204     elsif( $timeout > 0 ) {
205         print STDERR ("shutdown -r +$timeout\n");
206     }
207     elsif( $timeout < 0 ) {
208         print STDERR "The administrator has sent a signal to reboot this workstation. It will reboot after you've logged out.\n";
209         open(FILE, "> /etc/gosa-si/event");
210         print FILE "trigger_action_localboot\n";
211         close(FILE);
212     }
213     else {
214         # TODO do something, error handling, logging
215     }
217     return;
221 ###############################################################################
222 =over 
224 =item B<trigger_action_faireboot ($$)>
226 =over
228 =item description 
230     Executes '/usr/sbin/faireboot'.
232 =item parameter
234     $msg - STRING - complete GOsa-si message
235     $msg_hash - HASHREF - content of GOsa-si message in a hash
237 =item GOsa-si message xml content
239     None.
241 =item return 
242     
243     Nothing.
245 =back
247 =back
249 =cut
250 ###############################################################################
251 sub trigger_action_faireboot {
252     my ($msg, $msg_hash) = @_;
253         &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7); 
254     system("/usr/sbin/faireboot");
255     return;
259 ###############################################################################
260 =over 
262 =item B<trigger_action_reboot ($$)>
264 =over
266 =item description 
268     Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r'  if  no 
269     user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
271 =item parameter
273     $msg - STRING - complete GOsa-si message
274     $msg_hash - HASHREF - content of GOsa-si message in a hash
276 =item GOsa-si message xml content
278     <timeout> - INTEGER - timeout to wait befor reboot, default 0
280 =item return 
281     
282     Nothing.
284 =back
286 =back
288 =cut
289 ###############################################################################
290 sub trigger_action_reboot {
291     my ($msg, $msg_hash) = @_;
292     my $timeout;
294     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
295         $timeout = 0;
296     } 
297     else {
298         $timeout = @{$msg_hash->{timeout}}[0];
299     }
301     # check logged in user
302     my @user_list = &get_logged_in_users;
303     if( @user_list >= 1 ) {
304         system( "/usr/bin/goto-notify reboot" );
305         open(FILE, "> /etc/gosa-si/event");
306         print FILE "reboot\n";
307         close(FILE);
308     }
309     else {
310         system( "/sbin/shutdown -r +$timeout &" );
311     }
313     return;
317 ###############################################################################
318 =over 
320 =item B<trigger_action_halt ($$)>
322 =over
324 =item description 
326     Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if  no 
327     user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
329 =item parameter
331     $msg - STRING - complete GOsa-si message
332     $msg_hash - HASHREF - content of GOsa-si message in a hash
334 =item GOsa-si message xml content
336     <timeout> - INTEGER - timeout to wait befor halt, default 0
338 =item return 
339     
340     Nothing.    
342 =back
344 =back
346 =cut
347 ###############################################################################
348 sub trigger_action_halt {
349     my ($msg, $msg_hash) = @_;
350     my $timeout;
352     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
353         $timeout = 0;
354     } 
355     else {
356         $timeout = @{$msg_hash->{timeout}}[0];
357     }
359     # check logged in user
360     my @user_list = &get_logged_in_users;
361     if( @user_list >= 1 ) {
362         system( "/usr/bin/goto-notify halt" );
363         open(FILE, "> /etc/gosa-si/event");
364         print FILE "halt\n";
365         close(FILE);
366     }
367     else {
368         system( "/sbin/shutdown -h +$timeout &" );
369     }
371     return;
375 ###############################################################################
376 =over 
378 =item B<trigger_action_reinstall>
380 =over
382 =item description 
384     Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no 
385     user is logged in otherwise write 'install' to '/etc/gosa-si/event'
387 =item parameter
389     $msg - STRING - complete GOsa-si message
390     $msg_hash - HASHREF - content of GOsa-si message in a hash
392 =item GOsa-si message xml content
394     None.
396 =item return 
397     
398     Nothing.
400 =back
402 =back
404 =cut
405 ###############################################################################
406 sub trigger_action_reinstall {
407     my ($msg, $msg_hash) = @_;
409     # check logged in user
410     my @user_list = &get_logged_in_users;
411     if( @user_list >= 1 ) {
412         system( "/usr/bin/goto-notify install" );
413         open(FILE, "> /etc/gosa-si/event");
414         print FILE "install\n";
415         close(FILE);
416     }
417     else {
418         system( "/sbin/shutdown -r now &" );
419     }
421     return;
425 ###############################################################################
426 =over 
428 =item B<trigger_action_updae>
430 =over
432 =item description 
434     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
436 =item parameter
438     $msg - STRING - complete GOsa-si message
439     $msg_hash - HASHREF - content of GOsa-si message in a hash
441 =item GOsa-si message xml content
443     None.
445 =item return 
446     
447     Nothing
449 =back
451 =back
453 =cut
454 ###############################################################################
455 # Backward compatibility
456 sub trigger_action_update {
457     my ($msg, $msg_hash) = @_;
459     # Execute update
460     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
462     return;
466 ###############################################################################
467 =over 
469 =item B<trigger_action_instant_update ($$)>
471 =over
473 =item description 
475     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
477 =item parameter
479     $msg - STRING - complete GOsa-si message
480     $msg_hash - HASHREF - content of GOsa-si message in a hash
482 =item GOsa-si message xml content
484     None.
486 =item return 
487     
488     Nothing.
490 =back
492 =back
494 =cut
495 ###############################################################################
496 # Backward compatibility
497 sub trigger_action_instant_update {
498     my ($msg, $msg_hash) = @_;
500     # Execute update
501     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
503     return;
506 sub trigger_goto_settings_reload {
507     my ($msg, $msg_hash) = @_;
509     # Execute goto settings reload
510     my $cmd = "/etc/init.d/goto-agents";
511     my $pram = "start";
512     if (-f $cmd){
513         my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
514     } else {
515         &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
516     }
518     return;
522 1;