--- ast_extstated 2009-12-18 17:54:34.000000000 +0100 +++ ast_extstated 2009-12-21 14:51:11.000000000 +0100 @@ -63,6 +63,7 @@ our $nobody_exten = '95'; our $skip_nobody = 1; our $conn_time = 25; +our $use_hint_state = 1; # ######################################################### @@ -96,6 +97,7 @@ our %lastchanged : shared = (); our @ami_threads; our $sel; +our %hintstates : shared = (); use IO::Socket; use IO::Select; @@ -171,12 +173,18 @@ my $state = defined($1) ? int($1) : int(AST_EXTSTATE_UNKNOWN); if ($ignore_context) { + if ($use_hint_state && $state == int(AST_EXTSTATE_UNKNOWN) && exists($hintstates{ $exten }) && !($hintstates{ $exten } =~ /^Unavailable/ ) ) { + $state = int(AST_EXTSTATE_IDLE); + } &handle_extstate(\$exten, $state); } else { if ($pkt !~ m/^Context:\s*([a-z0-9\\-_]+)/mio) {return;} if (! defined($1)) {return;} my $context = $1; my $exten_at_context = $exten .'@'. $context; + if ($use_hint_state && $state == int(AST_EXTSTATE_UNKNOWN) && exists($hintstates{ $exten_at_context }) && !($hintstates{ $exten_at_context } =~ /^Unavailable/ ) ) { + $state = int(AST_EXTSTATE_IDLE); + } &handle_extstate(\$exten_at_context, $state); } } @@ -199,23 +207,39 @@ elsif ($message eq 'follows' && $pkt =~ m/^ActionID:\s*hints/mio) { my $exten = ''; my $context = ''; - while ($pkt =~ m/([0-9*a-z\-_]+)@([a-z\-_0-9]+)/gcio) { + my $hintstate = ''; + my $state = -2; + # while ($pkt =~ m/([0-9*a-z\-_]+)@([a-z\-_0-9]+)/gcio) { + while ($pkt =~ m/([0-9*a-z\-_]+)@([a-z\-_0-9]+).*State:([A-Za-z]+)/gcio) { $exten = $1; $context = $2; + $hintstate = $3; foreach (@known_contexts) { if ($context eq substr($_, 0, length($context))) { $context = $_; last; } } - #print 'HINT: '. $exten .'@'. $context ."\n"; + #print 'HINT: '. $exten .'@'. $context ." Hintstate:" . $hintstate . "\n"; + if ($use_hint_state && ($hintstate =~ /^Idle/ ) ) { + $state = 0; + } else { + $state = -2; + } if ($ignore_context) { - &handle_extstate(\$exten, -2); + &handle_extstate(\$exten, $state); } else { my $exten_at_context = $exten .'@'. $context; - &handle_extstate(\$exten_at_context, -2); + &handle_extstate(\$exten_at_context, $state); } - $ami->request_extstate($exten, $context); + + if ($ignore_context) { + &handle_hintstate(\$exten, $hintstate); + } else { + my $exten_at_context = $exten .'@'. $context; + &handle_hintstate(\$exten_at_context, $hintstate); + } + # $ami->request_extstate($exten, $context); } } } @@ -243,13 +267,23 @@ if (! defined($1)) {return;} my $state = defined($1) ? int($1) : int(AST_EXTSTATE_UNKNOWN); + if ($use_hint_state && $state == int(AST_EXTSTATE_UNKNOWN) && !($hintstates{ $exten } =~ /^Unavailable/ ) ) { + $state = int(AST_EXTSTATE_IDLE); + } + if ($ignore_context) { + if ($use_hint_state && $state == int(AST_EXTSTATE_UNKNOWN) && exists($hintstates{ $exten }) && !($hintstates{ $exten } =~ /^Unavailable/ ) ) { + $state = int(AST_EXTSTATE_IDLE); + } &handle_extstate(\$exten, $state); } else { if ($pkt !~ m/^Context:\s*([a-z0-9\\-_]+)/mio) {return;} if (! defined($1)) {return;} my $context = $1; my $exten_at_context = $exten .'@'. $context; + if ($use_hint_state && $state == int(AST_EXTSTATE_UNKNOWN) && exists($hintstates{ $exten_at_context }) && !($hintstates{ $exten_at_context } =~ /^Unavailable/ ) ) { + $state = int(AST_EXTSTATE_IDLE); + } &handle_extstate(\$exten_at_context, $state); } } @@ -381,6 +415,27 @@ #print "----\n"; } +sub handle_hintstate : locked +{ + my $ext_at_context = ${$_[0]}; + my $state = $_[1]; + + our %hintstates; + our $skip_nobody; + our $nobody_exten; + + if ($ext_at_context !~ m/^[0-9]+(?:@|$)/o) {return;} + + if ($skip_nobody && $ext_at_context =~ m/^$nobody_exten/io) { + #print "SKIPPING NOBODY EXTENSION\n"; + return; + } + + # store + #print "STORE HINTSTATE ".$ext_at_context.": ".$state ." - TIME: ". time() ."\n"; + $hintstates { $ext_at_context } = $state; +} + sub handle_channel_up_down : locked { my $uniqueid = ${$_[0]}; @@ -650,7 +705,7 @@ while ($run) { $ami->run_one_timeslice(); ++$cnt; - if ($cnt==6000) { + if ($cnt==600) { $cnt = 0; #$ami->request_extstate('2001', $ask_in_context); #$ami->request_extstate('4001', $ask_in_context);