Code

mailqueue.pm with POD lines
[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;
11  use Time::HiRes qw(usleep);
13  use MIME::Base64;
15 =head1 DESCRIPTION
17 A GOsa-SI event module containing all functions used by GOsa mail queue. This module will
18 be automatically imported by GOsa-SI if it is under F</usr/lib/gosa-si/server/E<lt>PACKAGEMODULEE<gt>/> .
21 =head1 METHODS
23 =cut
25 package mailqueue_com;
26 use Exporter;
27 @ISA = qw(Exporter);
28 my @events = (
29     "get_events",
30     "mailqueue_query",
31     "mailqueue_header",
32 );
33 @EXPORT = @events;
35 use strict;
36 use warnings;
37 use GOSA::GosaSupportDaemon;
38 use Data::Dumper;
39 use Time::HiRes qw( usleep);
40 use MIME::Base64;
43 BEGIN {}
45 END {}
47 ### Start ######################################################################
49 =pod
51 =over 4
53 =item get_events ( )
55 Returns a list of functions which are exported by the module.
57 =back
59 =cut
61 sub get_events {
62     return \@events;
63 }
66 =pod
68 =over 4
70 =item mailqueue_query( $msg, $msg_hash, $session_id )
72 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.
74 Returns the answer of the client.
76 =back
78 =cut
80 sub mailqueue_query {
81     my ($msg, $msg_hash, $session_id) = @_ ;
82     my $header = @{$msg_hash->{header}}[0];
83     my $target = @{$msg_hash->{target}}[0];
84     my $source = @{$msg_hash->{source}}[0];
85     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
86     my $error = 0;
87     my $error_string;
88     my $answer_msg;
89     my ($sql, $res);
91     if( defined $jobdb_id) {
92         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
93         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
94         my $res = $main::job_db->exec_statement($sql_statement);
95     }
97     # search for the correct target address
98     $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))"; 
99     $res = $main::known_clients_db->exec_statement($sql);
100     my ($host_name, $host_key);  # sanity check of db result
101     if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {
102         $host_name = @{@$res[0]}[0];
103         $host_key = @{@$res[0]}[2];
104     } else {
105         &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db\n$msg", 1);
106         $error_string = "Cannot determine host_name and host_key from known_clients_db";
107         $error = 1;
108     }
110     # send message to target
111     if (not $error) {
112         $msg =~ s/<source>GOSA<\/source>/<source>$main::server_address<\/source>/g; 
113         $msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
114         &main::send_msg_to_target($msg, $host_name, $host_key, $header, $session_id);
115     }
117     # waiting for answer
118     if (not $error) {
119         my $message_id;
120         my $i = 0;
121         while (1) {
122             $i++;
123             $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
124             $res = $main::incoming_db->exec_statement($sql);
125             if (ref @$res[0] eq "ARRAY") { 
126                 $message_id = @{@$res[0]}[0];
127                 last;
128             }
129             if ($i > 100) { last; } # do not run into a endless loop
130             usleep(100000);
131         }
132         # if answer exists
133         if (defined $message_id) {
134             $answer_msg = decode_base64(@{@$res[0]}[4]);
135             $answer_msg =~ s/<target>\S+<\/target>/<target>$source<\/target>/;
136             $answer_msg =~ s/<header>\S+<\/header>/<header>$header<\/header>/;
138             my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
139             if (defined $forward_to_gosa){
140                 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
141             }
142             $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id"; 
143             $res = $main::incoming_db->exec_statement($sql);
144         }
145     }
147     return ( $answer_msg );
150 =pod
152 =over 4
154 =item mailqueue_header ( $msg, $msg_hash, $session_id )
156 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.
158 Returns the answer of the client.
160 =back
162 =cut
163 sub mailqueue_header {
164     my ($msg, $msg_hash, $session_id) = @_ ;
165     my $header = @{$msg_hash->{header}}[0];
166     my $target = @{$msg_hash->{target}}[0];
167     my $source = @{$msg_hash->{source}}[0];
168     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
169     my $error = 0;
170     my $error_string;
171     my $answer_msg;
172     my ($sql, $res);
174     if( defined $jobdb_id) {
175         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
176         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
177         my $res = $main::job_db->exec_statement($sql_statement);
178     }
180     # search for the correct target address
181     $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))"; 
182     $res = $main::known_clients_db->exec_statement($sql);
183     my ($host_name, $host_key);
184     if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {   # sanity check of db result
185         $host_name = @{@$res[0]}[0];
186         $host_key = @{@$res[0]}[2];
187     } else {
188         &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db\n$msg", 1);
189         $error_string = "Cannot determine host_name and host_key from known_clients_db";
190         $error = 1;
191     }
193     # send message to target
194     if (not $error) {
195         $msg =~ s/<source>GOSA<\/source>/<source>$main::server_address<\/source>/g; 
196         $msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
197         &main::send_msg_to_target($msg, $host_name, $host_key, $header, $session_id);
198     }
200     # waiting for answer
201     if (not $error) {
202         my $message_id;
203         my $i = 0;
204         while (1) {
205             $i++;
206             $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
207             $res = $main::incoming_db->exec_statement($sql);
208             if (ref @$res[0] eq "ARRAY") { 
209                 $message_id = @{@$res[0]}[0];
210                 last;
211             }
212             if ($i > 100) { last; } # do not run into a endless loop
213             usleep(100000);
214         }
215         # if answer exists
216         if (defined $message_id) {
217             $answer_msg = decode_base64(@{@$res[0]}[4]);
218             $answer_msg =~ s/<target>\S+<\/target>/<target>$source<\/target>/;
219             $answer_msg =~ s/<header>\S+<\/header>/<header>$header<\/header>/;
221             my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
222             if (defined $forward_to_gosa){
223                 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
224             }
225             $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id"; 
226             $res = $main::incoming_db->exec_statement($sql);
227         }
228     }
230     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;