Code

Added optional offset arguments (seconds) to get_time.
[gosa.git] / gosa-si / server / events / mailqueue_com.pm
1 =pod
3 =head1 NAME
5 mailqueue_com - Implementation of a GOsa-SI event module. 
7 =head1 SYNOPSIS
9  use GOSA::GosaSupportDaemon;
10  use Time::HiRes qw(usleep);
11  use MIME::Base64;
13 =head1 DESCRIPTION
15 A GOsa-SI event module containing all functions used by GOsa mail queue. This module will
16 be automatically imported by GOsa-SI if it is under F</usr/lib/gosa-si/server/E<lt>PACKAGEMODULEE<gt>/> .
19 =head1 METHODS
21 =cut
23 package mailqueue_com;
24 use Exporter;
25 @ISA = qw(Exporter);
26 my @events = (
27     "get_events",
28     "mailqueue_query",
29     "mailqueue_header",
30 );
31 @EXPORT = @events;
33 use strict;
34 use warnings;
35 use GOSA::GosaSupportDaemon;
36 use Data::Dumper;
37 use Time::HiRes qw( usleep);
38 use MIME::Base64;
41 BEGIN {}
43 END {}
45 ### Start ######################################################################
47 =pod
49 =over 4
51 =item get_events ( )
53 Returns a list of functions which are exported by the module.
55 =back
57 =cut
59 sub get_events {
60     return \@events;
61 }
64 =pod
66 =over 4
68 =item mailqueue_query( $msg, $msg_hash, $session_id )
70 This function do for incoming messages with header 'mailqueue_query' the target translation from mac address to ip:port address, updates job_queue, send message to client and wait for client answer.
72 Returns the answer of the client.
74 =back
76 =cut
78 sub mailqueue_query {
79     my ($msg, $msg_hash, $session_id) = @_ ;
80     my $header = @{$msg_hash->{header}}[0];
81     my $target = @{$msg_hash->{target}}[0];
82     my $source = @{$msg_hash->{source}}[0];
83     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
84     my $error = 0;
85     my $error_string;
86     my $answer_msg;
87     my ($sql, $res);
89     if( defined $jobdb_id) {
90         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
91         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
92         my $res = $main::job_db->exec_statement($sql_statement);
93     }
95     # search for the correct target address
96     $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))"; 
97     $res = $main::known_clients_db->exec_statement($sql);
98     my ($host_name, $host_key);  # sanity check of db result
99     if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {
100         $host_name = @{@$res[0]}[0];
101         $host_key = @{@$res[0]}[2];
102     } else {
103         &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db\n$msg", 1);
104         $error_string = "Cannot determine host_name and host_key from known_clients_db";
105         $error = 1;
106     }
108     # send message to target
109     if (not $error) {
110         $msg =~ s/<source>GOSA<\/source>/<source>$main::server_address<\/source>/g; 
111         $msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
112         &main::send_msg_to_target($msg, $host_name, $host_key, $header, $session_id);
113     }
115     # waiting for answer
116     if (not $error) {
117         my $message_id;
118         my $i = 0;
119         while (1) {
120             $i++;
121             $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
122             $res = $main::incoming_db->exec_statement($sql);
123             if (ref @$res[0] eq "ARRAY") { 
124                 $message_id = @{@$res[0]}[0];
125                 last;
126             }
127             if ($i > 100) { last; } # do not run into a endless loop
128             usleep(100000);
129         }
130         # if answer exists
131         if (defined $message_id) {
132             $answer_msg = decode_base64(@{@$res[0]}[4]);
133             $answer_msg =~ s/<target>\S+<\/target>/<target>$source<\/target>/;
134             $answer_msg =~ s/<header>\S+<\/header>/<header>$header<\/header>/;
136             my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
137             if (defined $forward_to_gosa){
138                 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
139             }
140             $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id"; 
141             $res = $main::incoming_db->exec_statement($sql);
142         }
143     }
145     return ( $answer_msg );
148 =pod
150 =over 4
152 =item mailqueue_header ( $msg, $msg_hash, $session_id )
154 This function do for incoming messages with header 'mailqueue_header' the target translation from mac address to ip:port address, updates job_queue, send message to client and wait for client answer.
156 Returns the answer of the client.
158 =back
160 =cut
161 sub mailqueue_header {
162     my ($msg, $msg_hash, $session_id) = @_ ;
163     my $header = @{$msg_hash->{header}}[0];
164     my $target = @{$msg_hash->{target}}[0];
165     my $source = @{$msg_hash->{source}}[0];
166     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
167     my $error = 0;
168     my $error_string;
169     my $answer_msg;
170     my ($sql, $res);
172     if( defined $jobdb_id) {
173         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
174         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
175         my $res = $main::job_db->exec_statement($sql_statement);
176     }
178     # search for the correct target address
179     $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))"; 
180     $res = $main::known_clients_db->exec_statement($sql);
181     my ($host_name, $host_key);
182     if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {   # sanity check of db result
183         $host_name = @{@$res[0]}[0];
184         $host_key = @{@$res[0]}[2];
185     } else {
186         &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db\n$msg", 1);
187         $error_string = "Cannot determine host_name and host_key from known_clients_db";
188         $error = 1;
189     }
191     # send message to target
192     if (not $error) {
193         $msg =~ s/<source>GOSA<\/source>/<source>$main::server_address<\/source>/g; 
194         $msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
195         &main::send_msg_to_target($msg, $host_name, $host_key, $header, $session_id);
196     }
198     # waiting for answer
199     if (not $error) {
200         my $message_id;
201         my $i = 0;
202         while (1) {
203             $i++;
204             $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
205             $res = $main::incoming_db->exec_statement($sql);
206             if (ref @$res[0] eq "ARRAY") { 
207                 $message_id = @{@$res[0]}[0];
208                 last;
209             }
210             if ($i > 100) { last; } # do not run into a endless loop
211             usleep(100000);
212         }
213         # if answer exists
214         if (defined $message_id) {
215             $answer_msg = decode_base64(@{@$res[0]}[4]);
216             $answer_msg =~ s/<target>\S+<\/target>/<target>$source<\/target>/;
217             $answer_msg =~ s/<header>\S+<\/header>/<header>$header<\/header>/;
219             my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
220             if (defined $forward_to_gosa){
221                 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
222             }
223             $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id"; 
224             $res = $main::incoming_db->exec_statement($sql);
225         }
226     }
228     return ( $answer_msg );
232 =pod
234 =head1 BUGS
236 Please report any bugs, or post any suggestions, to the GOsa mailing list E<lt>gosa-devel@oss.gonicus.deE<gt> or to L<https://oss.gonicus.de/labs/gosa>
238 =head1 COPYRIGHT
240 This code is part of GOsa (L<http://www.gosa-project.org>)
242 Copyright (C) 2003-2008 GONICUS GmbH
244 ID: $$Id$$
246 This program is free software; you can redistribute it and/or modify
247 it under the terms of the GNU General Public License as published by
248 the Free Software Foundation; either version 2 of the License, or
249 (at your option) any later version.
251 This program is distributed in the hope that it will be useful,
252 but WITHOUT ANY WARRANTY; without even the implied warranty of
253 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
254 GNU General Public License for more details.
256 You should have received a copy of the GNU General Public License
257 along with this program; if not, write to the Free Software
258 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
261 =cut
263 # vim:ts=4:shiftwidth:expandtab
264 1;