| 1 | # ***** BEGIN LICENSE BLOCK ***** | ||
| 2 | # Zimbra Collaboration Suite Server | ||
| 3 | # Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Zimbra, Inc. | ||
| 4 | # | ||
| 5 | # The contents of this file are subject to the Zimbra Public License | ||
| 6 | # Version 1.3 ("License"); you may not use this file except in | ||
| 7 | # compliance with the License. You may obtain a copy of the License at | ||
| 8 | # http://www.zimbra.com/license. | ||
| 9 | # | ||
| 10 | # Software distributed under the License is distributed on an "AS IS" | ||
| 11 | # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. | ||
| 12 | # ***** END LICENSE BLOCK ***** | ||
| 13 | |||
| 14 | package ZCS::LocalConfig::_base; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | our $VERSION = 1.0; | ||
| 20 | |||
| 21 | { | ||
| 22 | my ( $Err, $Errstr ); | ||
| 23 | sub err { return $Err; } | ||
| 24 | sub errstr { return $Errstr; } | ||
| 25 | |||
| 26 | sub _set_err { | ||
| 27 | my $self = shift; | ||
| 28 | ( $Err, $Errstr ) = @_; | ||
| 29 | return undef if $Err; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _set_conf { | ||
| 34 | my ( $self, $conf ) = @_; | ||
| 35 | $self->{__conf} = $conf if ( $#_ == 1 ); | ||
| 36 | return $self->{__conf}; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _conf { | ||
| 40 | my ($self) = @_; | ||
| 41 | |||
| 42 | # load localconf into memory first time through | ||
| 43 | unless ( $self->{__conf} ) { | ||
| 44 | $self->load or return undef; | ||
| 45 | } | ||
| 46 | |||
| 47 | return $self->{__conf}; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my ( $class, @args ) = @_; | ||
| 52 | my $self = {@args}; | ||
| 53 | bless( $self, $class ); | ||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub get { | ||
| 58 | my ( $self, @args ) = @_; | ||
| 59 | |||
| 60 | my $conf = $self->_conf or return undef; | ||
| 61 | if ( @args == 1 ) { | ||
| 62 | return $conf->{ $args[0] }; | ||
| 63 | } | ||
| 64 | elsif ( @args > 1 ) { | ||
| 65 | my @ret = @{$conf}{@args}; | ||
| 66 | return wantarray ? @ret : \@ret; | ||
| 67 | } | ||
| 68 | elsif ( @args == 0 ) { | ||
| 69 | return $conf; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | return $self->_set_err( 1, "get: invalid arguments" ); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub set { | ||
| 77 | my ( $self, @args ) = @_; | ||
| 78 | |||
| 79 | if ( @args == 2 ) { | ||
| 80 | my ( $tag, $value ) = @args; | ||
| 81 | my $conf = $self->_conf or return $self->err; | ||
| 82 | return $conf->{$tag} = $value; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | Carp::confess("set: invalid arguments"); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub load { | ||
| 90 | Carp::confess("load: not implemented!"); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub store { | ||
| 94 | Carp::confess("store: not implemented!"); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _dump { | ||
| 98 | my $self = shift; | ||
| 99 | my $conf = $self->get or return $self->err; | ||
| 100 | |||
| 101 | my @data; | ||
| 102 | foreach my $k ( sort keys %$conf ) { | ||
| 103 | next if ( $k =~ /^_/ or $k =~ /password/i ); | ||
| 104 | my $v = $conf->{$k}; | ||
| 105 | if ( ref($v) eq "HASH" ) { | ||
| 106 | $v = join( | ||
| 107 | ", ", | ||
| 108 | map( "$_ => " | ||
| 109 | . ( | ||
| 110 | ref( $v->{$_} ) | ||
| 111 | ? join( ",", @{ $v->{$_} } ) | ||
| 112 | : $v->{$_} | ||
| 113 | ), | ||
| 114 | sort keys %$v ) | ||
| 115 | ); | ||
| 116 | } | ||
| 117 | elsif ( ref($v) eq "ARRAY" ) { | ||
| 118 | $v = join( ", ", @$v ); | ||
| 119 | } | ||
| 120 | push( @data, "$k = $v" ) if ( defined($v) and $v !~ /^\s*$/ ); | ||
| 121 | } | ||
| 122 | return wantarray ? @data : \@data; | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; |