Bug 8216: Allow SIP modules to pass critic tests
[koha.git] / C4 / SIP / Sip / Configuration.pm
1
2 # parse-config: Parse an XML-format
3 # ACS configuration file and build the configuration
4 # structure.
5 #
6
7 package Sip::Configuration;
8
9 use strict;
10 use English;
11 use warnings;
12 use XML::Simple qw(:strict);
13
14 use Sip::Configuration::Institution;
15 use Sip::Configuration::Account;
16 use Sip::Configuration::Service;
17
18 my $parser = new XML::Simple( KeyAttr   => { login => '+id',
19                                              institution => '+id',
20                                              service => '+port' },
21                               GroupTags =>  { listeners => 'service',
22                                               accounts => 'login',
23                                               institutions => 'institution', },
24                               ForceArray=> [ 'service',
25                                              'login',
26                                              'institution' ],
27                               ValueAttr =>  { 'error-detect' => 'enabled',
28                                              'min_servers' => 'value',
29                                              'max_servers' => 'value'} );
30
31 sub new {
32     my ($class, $config_file) = @_;
33     my $cfg = $parser->XMLin($config_file);
34     my %listeners;
35
36     foreach my $acct (values %{$cfg->{accounts}}) {
37                 new Sip::Configuration::Account $acct;
38     }
39
40     # The key to the listeners hash is the 'port' component of the
41     # configuration, which is of the form '[host]:[port]/proto', and
42     # the 'proto' component could be upper-, lower-, or mixed-cased.
43     # Regularize it here to lower-case, and then do the same below in
44     # find_server() when building the keys to search the hash.
45
46     foreach my $service (values %{$cfg->{listeners}}) {
47                 new Sip::Configuration::Service $service;
48                 $listeners{lc $service->{port}} = $service;
49     }
50     $cfg->{listeners} = \%listeners;
51
52     foreach my $inst (values %{$cfg->{institutions}}) {
53                 new Sip::Configuration::Institution $inst;
54     }
55     return bless $cfg, $class;
56 }
57
58 sub error_detect {
59     my $self = shift;
60     return $self->{'error-detect'};
61 }
62 sub accounts {
63     my $self = shift;
64     return values %{$self->{accounts}};
65 }
66
67 # sub policy {
68 #     my $self = shift;
69 #     return values %{$self->{policy}};
70 # }
71
72 sub find_service {
73     my ($self, $sockaddr, $port, $proto) = @_;
74     my $portstr;
75         foreach my $addr ('', '*:', "$sockaddr:") {
76                 $portstr = sprintf("%s%s/%s", $addr, $port, lc $proto);
77                 Sys::Syslog::syslog("LOG_DEBUG", "Configuration::find_service: Trying $portstr");
78                 last if (exists(($self->{listeners})->{$portstr}));
79         }
80     return $self->{listeners}->{$portstr};
81 }
82
83 1;
84 __END__
85
86     my $config = new Sip::Configuration $ARGV[0];
87
88
89 foreach my $acct ($config->accounts) {
90     print "Found account '", $acct->name, "', part of '"
91     print $acct->institution, "'\n";
92 }
93
94 1;