⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.pm

📁 Astercon2 开源软交换 2.2.0
💻 PM
📖 第 1 页 / 共 2 页
字号:
package Asterisk::config;############################################################		read and write asterisk config files############################################################	Copyright (c) 2005-2006  hoowa sun	P.R.China##	See COPYRIGHT section in pod text below for usage and distribution rights.##	<hoowa.sun@gmail.com>#	www.perlchina.org / www.openpbx.cn#	last modify 2006-6-2###########################################################$Asterisk::config::VERSION='0.9';use strict;#0.6-use vars qw/@commit_list/;use Fcntl ':flock';sub new {#0.6-	my $self = {};	my $self = {		commit_list => [],	};	bless $self;	return $self;}###############################  METHOD#  load config from file or from stream datasub load_config {	my $self = shift;	my %args = @_;#	my $filename = shift;#	my $stream_data = shift;	my @DATA;	if (!$args{'stream_data'}) {		open(DATA,"<$args{'filename'}") or die "$!";		@DATA = <DATA>;		close(DATA);	} else {		@DATA = split(/\n/,$args{'stream_data'});	}	chomp(@DATA);	my (%DATA,$last_section_name,%DATA_CHUNK);	$DATA{'[unsection]'}={};	$DATA_CHUNK{'[unsection]'}={};	foreach my $one_line (@DATA) {		my $line_sp=&clean_string($one_line);		next if ($line_sp eq '');#next if just comment		#right [section]???		if ($line_sp =~ /^\[(.+)\]/) {			$DATA{$1}={};			$last_section_name = $1;			next;			$DATA_CHUNK{$1}=[];		}		#right sharp "#" ???		if ($line_sp =~ /^\#/) {			my $section_name = $last_section_name;			$section_name = '[unsection]' if (!$section_name);			$DATA{$section_name}{$line_sp}=[] if (!$DATA{$section_name}{$line_sp});			push(@{$DATA{$section_name}{$line_sp}},$line_sp);			#copying source chunk to data_chunk			push(@{$DATA_CHUNK{$section_name}},[$line_sp]);						next;		}		#right key/value???		if ($line_sp =~ /\=/) {			#split data and key			my ($key,$value)=split(/\=(.*)/,$line_sp);			$key =~ s/^(\s+)//;		$key =~ s/(\s+)$//;			$value=~ s/^\>//g;	$value =~ s/^(\s+)//;	$value =~ s/(\s+)$//;			my $section_name = $last_section_name;			$section_name = '[unsection]' if (!$section_name);			$DATA{$section_name}{$key}=[] if (!$DATA{$section_name}{$key});			push(@{$DATA{$section_name}{$key}},$value);                        #copying source chunk to data_chunk                        push(@{$DATA_CHUNK{$section_name}},[$key=>$value]);			next;		}	}	return(\%DATA,\@DATA,\%DATA_CHUNK);}######################	cookie for our#####################sub check_nvd {	if (shift=~/[^a-zA-Z0-9\.]/) {		return(0);	} else {		return(1);	}}sub check_value {	if (shift=~/[^a-zA-Z0-9]/) {		return(0);	} else {		return(1);	}}sub check_digits {	if (shift=~/[^0-9\*\#]/) {		return(0);	} else {		return(1);	}}sub check_number {	if (shift=~/[^0-9]/) {		return(0);	} else {		return(1);	}}sub check_import_number {	if (shift=~/[^0-9,\s]/) {		return(0);	} else {		return(1);	}}############################### clean ; data from string endsub clean_string {	my $string = shift;	return '' unless $string;	($string,undef)=split(/\;/,$string);#0.6-	$string =~ s/^(\s+)//;#0.6-	$string =~ s/(\s+)$//;#0.6-return($string);	$string =~ s/^\s+//;	$string =~ s/\s+$//;return($string);}# split key value of datasub clean_keyvalue {	my $string = shift;	my ($key,$value)=split(/\=(.*)/,$string);	$key =~ s/^(\s+)//;		$key =~ s/(\s+)$//;	if ($value) {		$value=~ s/^\>//g;		$value =~ s/^(\s+)//;	$value =~ s/(\s+)$//;	}return($key,$value);}# income scalar,array ref,hash ref output array datasub format_convert {	my $string = shift;	if (ref($string) eq 'ARRAY') {		return(@$string);	} elsif (ref($string) eq 'HASH') {		my @tmp;		foreach  (keys(%$string)) {			push(@tmp,"$_=".$string->{$_});		}		return(@tmp);	} else {		return($string);	}}###############################  METHOD#  clean all assign beforesub clean_assign {	my $self = shift;	undef($self->{commit_list});#0.6-	undef(@commit_list);}###############################  METHOD#  assign_cleanfile ; all data from filesub assign_cleanfile {	my $self = shift;	my %hash = @_;	$hash{'action'}='cleanfile';	push(@{$self->{commit_list}},\%hash);#0.6-	push(@commit_list,\%hash);}###############################  METHOD#  replace data when matched#  assign_matchreplace(match=>,replace=>);sub assign_matchreplace {	my $self = shift;	my %hash = @_;	$hash{'action'}='matchreplace';	push(@{$self->{commit_list}},\%hash);#0.6-	push(@commit_list,\%hash);}###############################  METHOD#  assign append in anywhere#  any section: up/down#  assign_append(point=>'up'|'down',section=>,data=>[key=value,key=value]|{key=>value,key=>value}|'key=value');#  any section&key-value: up/down/over#  assign_append(point=>'up'|'down'|'over',section=>,comkey=>[key,value],data=>[key=value,key=value]|{key=>value,key=>value}|'key=value');  #  no section:#  assign_append(point=>'up'|'down',data=>[key=value,key=value]|{key=>value,key=>value}|'key=value');sub assign_append {	my $self = shift;	my %hash = @_;	$hash{'action'}='append';	push(@{$self->{commit_list}},\%hash);#0.6-	push(@commit_list,\%hash);}###############################  METHOD#  replace the section except sharp "#"#  any section/[unsection]:#  assign_replacesection(section=>,data=>[key=value,key=value]|{key=>value,key=>value}|'key=value');sub assign_replacesection {	my $self = shift;	my %hash = @_;	$hash{'action'}='replacesection';	push(@{$self->{commit_list}},\%hash);#0.6-	push(@commit_list,\%hash);}###############################  METHOD#  delete section#  any section/[unsection]:#  assign_delsection(section=>);sub assign_delsection {	my $self = shift;	my %hash = @_;	$hash{'action'}='delsection';	push(@{$self->{commit_list}},\%hash);#0.6-	push(@commit_list,\%hash);}###############################  METHOD#  add section#  assign_addsection(section=>sectionname)sub assign_addsection {	my $self = shift;	my %hash = @_;	$hash{action} = 'addsection';	push(@{$self->{commit_list}}, \%hash);}###############################  METHOD#  edit key#  any section/[unsection]: change all matched key when key value are null.#  assign_editkey(section=>,key=>,value=>,new_value=>);sub assign_editkey {	my $self = shift;	my %hash = @_;	$hash{'action'}='editkey';	push(@{$self->{commit_list}},\%hash);#0.6-	push(@commit_list,\%hash);}###############################  METHOD#  delete key#  any section/[unsection]: change all matched key when key value are null.#  assign_delkey(section=>,key=>,$value=>);sub assign_delkey {	my $self = shift;	my %hash = @_;	$hash{'action'}='delkey';	push(@{$self->{commit_list}},\%hash);#0.6-	push(@commit_list,\%hash);}###############################  METHOD#  save to file#  filename: run assign rules and save to file#  save_file(filename=>,resource=>);sub save_file {	my $self = shift;	my %args = @_;	if (!$args{'resource'}) {		open(DATA,"<$args{'filename'}") or die "Failed to open resou$!";		my @DATA = <DATA>;		close(DATA);		chomp(@DATA);		$args{'resource'}=\@DATA;	}#0.6-	foreach my $one_case (@commit_list) {	foreach my $one_case (@{$self->{commit_list}}) {		$args{'resource'} = &do_editkey($one_case,$args{'resource'}) if ($one_case->{'action'} eq 'editkey' || $one_case->{'action'} eq 'delkey');		$args{'resource'} = &do_delsection($one_case,$args{'resource'}) if ($one_case->{'action'} eq 'delsection' || $one_case->{'action'} eq 'replacesection');		$args{'resource'} = &do_addsection($one_case,$args{'resource'}) if ($one_case->{'action'} eq 'addsection');		$args{'resource'} = &do_append($one_case,$args{'resource'}) if ($one_case->{'action'} eq 'append');		$args{'resource'} = &do_matchreplace($one_case,$args{'resource'}) if ($one_case->{'action'} eq 'matchreplace');		if ($one_case->{'action'} eq 'cleanfile') {			undef($args{'resource'});			last;		}	}	#save file	open(SAVE,">$args{'filename'}") or die ("$!");	flock(SAVE,LOCK_EX);	print SAVE grep{$_.="\n"} @{$args{'resource'}};	flock(SAVE,LOCK_UN);	close(SAVE);return(1);}########################### kernel dosub do_matchreplace {	my $one_case = shift;	my $data = shift;	my @NEW;	foreach my $one_line (@$data) {		if ($one_line =~ /$one_case->{'match'}/) {			$one_line = $one_case->{'replace'};		}		push(@NEW,$one_line);	}return(\@NEW);}sub do_append {	my $one_case = shift;	my $data = shift;	my @NEW;	if ($one_case->{'section'} eq '') {	#Append data head of source data/foot of source data		if ($one_case->{'point'} eq 'up') {			push(@NEW,&format_convert($one_case->{'data'}),@$data);		} else {			push(@NEW,@$data,&format_convert($one_case->{'data'}));		}	} elsif ($one_case->{'comkey'} eq '') {#0.7-		my $auto_save=0;#0.7-		foreach my $one_line (@$data) {			#tune on auto save#0.7-			if ($auto_save) {			push(@NEW,$one_line);			next;		}			#check section#0.7-			my $line_sp=&clean_string($one_line);#0.7-			my ($section_name) = $line_sp =~ /^\[(.+)\]/;#0.7-			if ($one_case->{'section'} eq $section_name & $one_case->{'point'} eq 'up') {#0.7-				push(@NEW,&format_convert($one_case->{'data'}));	$auto_save=1;#0.7-			} elsif ($one_case->{'section'} eq $section_name & $one_case->{'point'} eq 'down') {#0.7-				push(@NEW,$one_line);	push(@NEW,&format_convert($one_case->{'data'}));#0.7-				$one_line=undef;		$auto_save=1;#0.7-			}#0.7-			push(@NEW,$one_line);#0.7-		}	#Append data head/foot of section_name		my $auto_save=0;	my	$save_tmpmem=0;	my	$offset=0;		foreach my $one_line (@$data) {			#tune on auto save			if ($auto_save) {			push(@NEW,$one_line);			$offset++;	next;		}			#check section			my $line_sp=&clean_string($one_line);			my ($section_name) = $line_sp =~ /^\[(.+)\]/;			# for up / down			if ($one_case->{'section'} eq $section_name & $one_case->{'point'} eq 'up') {				push(@NEW,&format_convert($one_case->{'data'}));	$auto_save=1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -