Code

Removed smarty plugin loading
[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 Data::Dumper;
29 use Time::HiRes qw( usleep);
30 use MIME::Base64;
31 use GOsaSI::GosaSupportDaemon;
33 use Exporter;
35 our @ISA = qw(Exporter);
37 my @events = (
38     "get_events",
39     "mailqueue_query",
40     "mailqueue_header",
41 );
43 our @EXPORT = @events;
45 BEGIN {}
47 END {}
49 ### Start ######################################################################
51 =pod
53 =over 4
55 =item get_events ( )
57 Returns a list of functions which are exported by the module.
59 =back
61 =cut
63 sub get_events {
64     return \@events;
65 }
68 =pod
70 =over 4
72 =item mailqueue_query( $msg, $msg_hash, $session_id )
74 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.
76 Returns the answer of the client.
78 =back
80 =cut
82 sub mailqueue_query {
83     my ($msg, $msg_hash, $session_id) = @_ ;
84     my $header = @{$msg_hash->{header}}[0];
85     my $target = @{$msg_hash->{target}}[0];
86     my $source = @{$msg_hash->{source}}[0];
87     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
88     my $error = 0;
89     my $error_string;
90     my $answer_msg;
91     my ($sql, $res);
93     if( defined $jobdb_id) {
94         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
95         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
96         my $res = $main::job_db->exec_statement($sql_statement);
97     }
99     # search for the correct target address
100     $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))"; 
101     $res = $main::known_clients_db->exec_statement($sql);
102     my ($host_name, $host_key);  # sanity check of db result
103     if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {
104         $host_name = @{@$res[0]}[0];
105         $host_key = @{@$res[0]}[2];
106     } else {
107         &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db\n$msg", 1);
108         $error_string = "Cannot determine host_name and host_key from known_clients_db";
109         $error = 1;
110     }
112     # send message to target
113     if (not $error) {
114         $msg =~ s/<source>GOSA<\/source>/<source>$main::server_address<\/source>/g; 
115         $msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
116         &main::send_msg_to_target($msg, $host_name, $host_key, $header, $session_id);
117     }
119     # waiting for answer
120     if (not $error) {
121         my $message_id;
122         my $i = 0;
123         while (1) {
124             $i++;
125             $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
126             $res = $main::incoming_db->exec_statement($sql);
127             if (ref @$res[0] eq "ARRAY") { 
128                 $message_id = @{@$res[0]}[0];
129                 last;
130             }
131             if ($i > 100) { last; } # do not run into a endless loop
132             usleep(100000);
133         }
134         # if answer exists
135         if (defined $message_id) {
136             $answer_msg = decode_base64(@{@$res[0]}[4]);
137             $answer_msg =~ s/<target>\S+<\/target>/<target>$source<\/target>/;
138             $answer_msg =~ s/<header>\S+<\/header>/<header>$header<\/header>/;
140             my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
141             if (defined $forward_to_gosa){
142                 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
143             }
144             $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id"; 
145             $res = $main::incoming_db->exec_statement($sql);
146         }
147     }
149     return ( $answer_msg );
152 =pod
154 =over 4
156 =item mailqueue_header ( $msg, $msg_hash, $session_id )
158 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.
160 Returns the answer of the client.
162 =back
164 =cut
165 sub mailqueue_header {
166     my ($msg, $msg_hash, $session_id) = @_ ;
167     my $header = @{$msg_hash->{header}}[0];
168     my $target = @{$msg_hash->{target}}[0];
169     my $source = @{$msg_hash->{source}}[0];
170     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
171     my $error = 0;
172     my $error_string;
173     my $answer_msg;
174     my ($sql, $res);
176     if( defined $jobdb_id) {
177         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
178         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
179         my $res = $main::job_db->exec_statement($sql_statement);
180     }
182     # search for the correct target address
183     $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))"; 
184     $res = $main::known_clients_db->exec_statement($sql);
185     my ($host_name, $host_key);
186     if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {   # sanity check of db result
187         $host_name = @{@$res[0]}[0];
188         $host_key = @{@$res[0]}[2];
189     } else {
190         &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db\n$msg", 1);
191         $error_string = "Cannot determine host_name and host_key from known_clients_db";
192         $error = 1;
193     }
195     # send message to target
196     if (not $error) {
197         $msg =~ s/<source>GOSA<\/source>/<source>$main::server_address<\/source>/g; 
198         $msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
199         &main::send_msg_to_target($msg, $host_name, $host_key, $header, $session_id);
200     }
202     # waiting for answer
203     if (not $error) {
204         my $message_id;
205         my $i = 0;
206         while (1) {
207             $i++;
208             $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
209             $res = $main::incoming_db->exec_statement($sql);
210             if (ref @$res[0] eq "ARRAY") { 
211                 $message_id = @{@$res[0]}[0];
212                 last;
213             }
214             if ($i > 100) { last; } # do not run into a endless loop
215             usleep(100000);
216         }
217         # if answer exists
218         if (defined $message_id) {
219             $answer_msg = decode_base64(@{@$res[0]}[4]);
220             $answer_msg =~ s/<target>\S+<\/target>/<target>$source<\/target>/;
221             $answer_msg =~ s/<header>\S+<\/header>/<header>$header<\/header>/;
223             my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
224             if (defined $forward_to_gosa){
225                 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
226             }
227             $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id"; 
228             $res = $main::incoming_db->exec_statement($sql);
229         }
230     }
232     return ( $answer_msg );
236 =pod
238 =head1 BUGS
240 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>
242 =head1 COPYRIGHT
244 This code is part of GOsa (L<http://www.gosa-project.org>)
246 Copyright (C) 2003-2008 GONICUS GmbH
248 ID: $$Id$$
250 This program is free software; you can redistribute it and/or modify
251 it under the terms of the GNU General Public License as published by
252 the Free Software Foundation; either version 2 of the License, or
253 (at your option) any later version.
255 This program is distributed in the hope that it will be useful,
256 but WITHOUT ANY WARRANTY; without even the implied warranty of
257 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
258 GNU General Public License for more details.
260 You should have received a copy of the GNU General Public License
261 along with this program; if not, write to the Free Software
262 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
265 =cut
267 # vim:ts=4:shiftwidth:expandtab
268 1;