Code

- Corrected various perl errors due to buf fixes and merging of patches
[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;
25 use strict;
26 use warnings;
28 use Exporter;
29 use GOSA::GosaSupportDaemon;
30 use Data::Dumper;
31 use Time::HiRes qw( usleep);
32 use MIME::Base64;
34 our @ISA = qw(Exporter);
36 my @events = (
37     "get_events",
38     "mailqueue_query",
39     "mailqueue_header",
40 );
42 our @EXPORT = @events;
44 BEGIN {}
46 END {}
48 ### Start ######################################################################
50 =pod
52 =over 4
54 =item get_events ( )
56 Returns a list of functions which are exported by the module.
58 =back
60 =cut
62 sub get_events {
63     return \@events;
64 }
67 =pod
69 =over 4
71 =item mailqueue_query( $msg, $msg_hash, $session_id )
73 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.
75 Returns the answer of the client.
77 =back
79 =cut
81 sub mailqueue_query {
82     my ($msg, $msg_hash, $session_id) = @_ ;
83     my $header = @{$msg_hash->{header}}[0];
84     my $target = @{$msg_hash->{target}}[0];
85     my $source = @{$msg_hash->{source}}[0];
86     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
87     my $error = 0;
88     my $error_string;
89     my $answer_msg;
90     my ($sql, $res);
92     if( defined $jobdb_id) {
93         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
94         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
95         my $res = $main::job_db->exec_statement($sql_statement);
96     }
98     # search for the correct target address
99     $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))"; 
100     $res = $main::known_clients_db->exec_statement($sql);
101     my ($host_name, $host_key);  # sanity check of db result
102     if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {
103         $host_name = @{@$res[0]}[0];
104         $host_key = @{@$res[0]}[2];
105     } else {
106         &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db\n$msg", 1);
107         $error_string = "Cannot determine host_name and host_key from known_clients_db";
108         $error = 1;
109     }
111     # send message to target
112     if (not $error) {
113         $msg =~ s/<source>GOSA<\/source>/<source>$main::server_address<\/source>/g; 
114         $msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
115         &main::send_msg_to_target($msg, $host_name, $host_key, $header, $session_id);
116     }
118     # waiting for answer
119     if (not $error) {
120         my $message_id;
121         my $i = 0;
122         while (1) {
123             $i++;
124             $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
125             $res = $main::incoming_db->exec_statement($sql);
126             if (ref @$res[0] eq "ARRAY") { 
127                 $message_id = @{@$res[0]}[0];
128                 last;
129             }
130             if ($i > 100) { last; } # do not run into a endless loop
131             usleep(100000);
132         }
133         # if answer exists
134         if (defined $message_id) {
135             $answer_msg = decode_base64(@{@$res[0]}[4]);
136             $answer_msg =~ s/<target>\S+<\/target>/<target>$source<\/target>/;
137             $answer_msg =~ s/<header>\S+<\/header>/<header>$header<\/header>/;
139             my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
140             if (defined $forward_to_gosa){
141                 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
142             }
143             $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id"; 
144             $res = $main::incoming_db->exec_statement($sql);
145         }
146     }
148     return ( $answer_msg );
151 =pod
153 =over 4
155 =item mailqueue_header ( $msg, $msg_hash, $session_id )
157 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.
159 Returns the answer of the client.
161 =back
163 =cut
164 sub mailqueue_header {
165     my ($msg, $msg_hash, $session_id) = @_ ;
166     my $header = @{$msg_hash->{header}}[0];
167     my $target = @{$msg_hash->{target}}[0];
168     my $source = @{$msg_hash->{source}}[0];
169     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
170     my $error = 0;
171     my $error_string;
172     my $answer_msg;
173     my ($sql, $res);
175     if( defined $jobdb_id) {
176         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
177         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
178         my $res = $main::job_db->exec_statement($sql_statement);
179     }
181     # search for the correct target address
182     $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))"; 
183     $res = $main::known_clients_db->exec_statement($sql);
184     my ($host_name, $host_key);
185     if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {   # sanity check of db result
186         $host_name = @{@$res[0]}[0];
187         $host_key = @{@$res[0]}[2];
188     } else {
189         &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db\n$msg", 1);
190         $error_string = "Cannot determine host_name and host_key from known_clients_db";
191         $error = 1;
192     }
194     # send message to target
195     if (not $error) {
196         $msg =~ s/<source>GOSA<\/source>/<source>$main::server_address<\/source>/g; 
197         $msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
198         &main::send_msg_to_target($msg, $host_name, $host_key, $header, $session_id);
199     }
201     # waiting for answer
202     if (not $error) {
203         my $message_id;
204         my $i = 0;
205         while (1) {
206             $i++;
207             $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
208             $res = $main::incoming_db->exec_statement($sql);
209             if (ref @$res[0] eq "ARRAY") { 
210                 $message_id = @{@$res[0]}[0];
211                 last;
212             }
213             if ($i > 100) { last; } # do not run into a endless loop
214             usleep(100000);
215         }
216         # if answer exists
217         if (defined $message_id) {
218             $answer_msg = decode_base64(@{@$res[0]}[4]);
219             $answer_msg =~ s/<target>\S+<\/target>/<target>$source<\/target>/;
220             $answer_msg =~ s/<header>\S+<\/header>/<header>$header<\/header>/;
222             my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
223             if (defined $forward_to_gosa){
224                 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
225             }
226             $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id"; 
227             $res = $main::incoming_db->exec_statement($sql);
228         }
229     }
231     return ( $answer_msg );
235 =pod
237 =head1 BUGS
239 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>
241 =head1 COPYRIGHT
243 This code is part of GOsa (L<http://www.gosa-project.org>)
245 Copyright (C) 2003-2008 GONICUS GmbH
247 ID: $$Id$$
249 This program is free software; you can redistribute it and/or modify
250 it under the terms of the GNU General Public License as published by
251 the Free Software Foundation; either version 2 of the License, or
252 (at your option) any later version.
254 This program is distributed in the hope that it will be useful,
255 but WITHOUT ANY WARRANTY; without even the implied warranty of
256 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
257 GNU General Public License for more details.
259 You should have received a copy of the GNU General Public License
260 along with this program; if not, write to the Free Software
261 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
264 =cut
266 # vim:ts=4:shiftwidth:expandtab
267 1;