Code

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