source: A2P/a2p/A2P/EService/Tools.pm @ 3

Last change on this file since 3 was 3, checked in by guillaume, 17 years ago
  • AUTHORS: Ajout des différents contributeurs
  • COPYING: Ajout de la licence GPL v3
  • a2p: Préparation des sources pour leur publication sous GPL
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1#
2# Copyright (c) 2004-2007 - Consultas, PKG.fr
3#
4# This file is part of A2P.
5#
6# A2P is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# A2P is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with A2P; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19#
20# $Id: Tools.pm 3 2007-10-18 16:20:19Z guillaume $
21#
22
23package A2P::EService::Tools ;
24
25# Package of tools API to create E-Service dedicated scripts
26
27use strict ;
28use integer ;
29use Carp qw(longmess);
30use XML::LibXML ;
31
32use A2P::Globals ;
33use A2P::Syslog ;
34
35BEGIN {
36    use Exporter ();
37
38    our $VERSION = sprintf "%s", q$Rev: 885 $ =~ /(\d[0-9.]+)\s+/ ;
39    our @ISA = qw(Exporter);
40    our @EXPORT_OK = qw( &try &abort &get_conf &get_id &get_lib &get_work
41                         &get_useragent );
42}
43
44our $VERSION ;
45our $perllib = '/etc/afp2print/perl5lib' ;
46
47my $try   = "" ;
48my $tries = 0 ;
49my $service = $0 ;
50
51sub try {
52    # Handle Perl signals
53    $SIG{'__DIE__'} = $SIG{'__WARN__'} = sub {
54        &abort(&longmess(@_));
55        };
56
57    $try = shift ;
58    ++ $tries ;
59    print sprintf("Check #%03d: %s\n", $tries, $try )
60        if (exists($ENV{SHOW_TRIES}));
61}
62
63sub abort {
64    # If one argument is given, add it to try string
65    $try .= ": " . shift if @_ ;
66    $try .= " ($!)" if ($!);
67    # Print message only in some environment
68    print "$try\n"
69        if (exists($ENV{A2P_PATH}) or (defined($ENV{DEBUG}) and $ENV{DEBUG}));
70    &Error($try);
71    print STDERR "ABTERM: $try\n" if (defined($ENV{JOB}) and $ENV{JOB});
72    exit($tries);
73}
74
75sub get_conf {
76    $service = shift ;
77    my @required = @_ ;
78
79    # Prepare logging api
80    our $Progname = $0 = $service ;
81    A2P::Syslog::SetLogger(0);
82
83    my $conf_file = $service . '.conf' ;
84
85    # Configuration folder can be over-rided with EADRESSE_CONF environment
86    my $conf_path = defined($ENV{'EADRESSE_CONF'}) ?
87        $ENV{'EADRESSE_CONF'} : '/etc/afp2print/eservice' ;
88
89    # Prepare path to configuration file to 'require' it
90    $conf_path .= '/' unless ( $conf_path =~ m|/$| );
91    $conf_file = $conf_path . $conf_file ;
92
93    abort "No configuration found at $conf_file"
94        unless -e $conf_file ;
95
96    my $conf = {} ;
97    my ( $req, $eval ) = ( "", "" ) ;
98    map {
99        $req  .= 'our %' . $_ . ' ;' ;
100        $eval .= '$conf->{' . $_ . '} = \%' . $_ . ' ; ' ;
101    } @required ;
102
103    eval "$req require '$conf_file' ; $eval"
104        or abort "Can't read '$conf_file' configuration file" ;
105
106    return $conf ;
107}
108
109sub _cons {
110    return substr('bcdfgjklmnprstvxz',rand(17),1);
111}
112
113sub _vowl {
114    return substr('aeiou',rand(5),1);
115}
116
117sub get_id {
118    my $reference = shift || _cons . _vowl . _cons . _vowl . _cons . _vowl ;
119    my $job = $ENV{JOB} || _cons . _vowl . _cons . _vowl . _cons . _vowl ;
120    my $host = lc($LOCKID) ;
121    return sprintf('%s-%s-%x@%s.%s', $job, $reference, $$, $service, $host );
122}
123
124sub get_lib {
125    my $library = shift or return 0 ;
126    my $wanted  = shift || 0 ;
127
128    try "Loading '$library'" ;
129    eval "use lib '$perllib' ; use $library" ;
130
131    my $version = eval '$' . $library . '::VERSION' ;
132    abort "$library version not found" unless (defined($version) and $version);
133    abort "Too old $library version $version not supported"
134        unless ( $version >= $wanted );
135
136    # Reset errno as it is often set during library load
137    $! = 0 ;
138
139    return 1 ;
140}
141
142sub get_useragent {
143    return $service . " v" . A2P_RPM_VERSION ;
144}
145
146sub get_work {
147    # The work to do is provided as pdf and file attribut in a <a2p/> XML
148    my ( $pdf, $file ) = ( "", "" );
149    my $xml ;
150    my @XML ;
151
152    my $parser = new XML::LibXML ;
153    abort "Unable to get an XML parser" unless (defined($parser));
154
155    # Firstly expect job as argument as mode 1
156    if (@ARGV) {
157        # Still abort if got wrong count of arguments
158        abort "Bad argument count with '@ARGV'" unless ( @ARGV == 1 );
159
160        # Argument must be a file
161        my $file = shift @ARGV ;
162        abort "Bad file found" unless -s $file ;
163
164        $xml = $parser->parse_file( $file );
165        abort "Can't parse '$file' as XML" unless (defined($xml));
166
167    } else {
168        # Mode 0
169        my @XML = <STDIN> ;
170        local $" = '' ;
171        $xml = $parser->parse_string("@XML") if @XML ;
172        abort "Can't parse '@XML' string as XML" unless (defined($xml));
173    }
174
175    my $root = $xml->lastChild ;
176    abort "No XML root found in given content" unless (defined($root));
177
178    my $rootname = $root->nodeName() ;
179    abort "XML is not an a2P work to do "
180        unless ( defined($rootname) and $rootname and $rootname =~ /^a2p$/i );
181
182    $pdf  = $root->getAttribute('pdf') ;
183    abort "No 'pdf' attribut found in a2p XML"
184        unless ( defined($pdf) and $pdf );
185
186    $file = $root->getAttribute('file') ;
187    abort "No 'file' attribut found in a2p XML"
188        unless ( defined($file) and $file );
189
190    # return job to do
191    return ( $pdf, $file ) ;
192}
193
1941;
Note: See TracBrowser for help on using the repository browser.