1 package OpenILS::Application::Trigger::Reactor::AstCall;
2 use base 'OpenILS::Application::Trigger::Reactor';
3 use OpenSRF::Utils::Logger qw($logger);
4 # use OpenILS::Application::AppUtils;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
7 use strict; use warnings;
11 use OpenSRF::Utils::SettingsClient;
13 $Data::Dumper::Indent = 0;
15 my $U = 'OpenILS::Application::AppUtils';
17 my $e = new_editor(xact => 1);
19 # $last_channel_used is:
20 # ~ index (not literal value) of last channel used in a callfile
21 # ~ index is of position in @channels (zero-based)
22 # ~ cached at package level
23 # ~ typically for Zap (PSTN), not VOIP
26 our $last_channel_used = 0;
32 The AstCall reactor module creates a callfile for Asterisk, given a
33 template describing the message and an environment defining
34 necessary information for contacting the Asterisk server and scheduling
41 # $logger->info(__PACKAGE__ . ": get_conf()");
42 $telephony and return $telephony;
43 my $config = OpenSRF::Utils::SettingsClient->new;
44 # config object cached by package
45 $telephony = $config->config_value('notifications', 'telephony');
50 @channels and return @channels;
51 my $config = get_conf(); # populated $telephony object
52 @channels = @{ $config->{channels} };
57 # Increments $last_channel_used, or resets it to zero, as necessary.
58 # Returns appropriate value from channels array.
59 my @chans = get_channels();
61 $logger->error(__PACKAGE__ . ": Cannot build call using " .
63 ", no notifications.telephony.channels found in config!");
66 if (++$last_channel_used > $#chans) {
67 $last_channel_used = 0;
69 return $chans[$last_channel_used]; # say, 'Zap/1' or 'Zap/12'
73 my $tech = get_conf()->{driver} || 'SIP';
74 if ($tech !~ /^SIP/) {
75 return next_channel($tech);
77 return $tech; # say, 'SIP' or 'SIP/ubab33'
81 my $lines = get_conf()->{callfile_lines} or return '';
83 foreach (split "\n", $lines) {
84 s/^\s*//g; # strip leading spaces
85 /\S/ or next; # skip empty lines
88 (scalar @fixed) or return '';
89 return join("\n", @fixed) . "\n";
93 my $conf = get_conf();
94 my $host = $conf->{host};
96 $logger->error(__PACKAGE__ . ": No telephony/host in config.");
100 # prepend http:// if no protocol specified
101 $host =~ /^\S+:\/\// or $host = 'http://' . $host;
102 # append port number if specified
103 $conf->{port} and $host .= ":" . $conf->{port};
108 # TODO: caching? (would take testing to ensure memory and
109 # connections are clean/stable)
110 my $host = (@_ ? shift : host_string()) or return;
111 return new RPC::XML::Client($host);
115 my ($self, $env) = @_;
117 $logger->info(__PACKAGE__ . ": entered handler");
119 # assignment, not comparison
120 unless ($env->{channel_prefix} = channel()) {
121 $logger->error(__PACKAGE__ . ": Cannot find tech/resource in config");
125 $env->{extra_lines} = get_extra_lines() || '';
126 my $tmpl_output = $self->run_TT($env);
127 if (not $tmpl_output) {
128 $logger->error(__PACKAGE__ . ": no template input");
132 my @eventids = map {$_->id} @{$env->{event}};
133 @eventids or push @eventids, '';
135 my $eo = Fieldmapper::action_trigger::event_output->new;
137 # XXX we have to actually create this in the DB now if we expect to use the
140 $eo = $e->create_action_trigger_event_output($eo) or return $e->die_event;
141 if ($env->{"extra_lines"}) {
142 $tmpl_output .= ";; added by handler:\n";
143 $tmpl_output .= $env->{"extra_lines"};
146 # or would we prefer distinct lines instead of comma-separated?
147 $tmpl_output .= "; event_ids = " . join(",",@eventids) . "\n";
148 $tmpl_output .= "; event_output = " . $eo->id . "\n";
150 #my $filename_fragment = $userid . '_' . $eventids[0] . 'uniq' . time;
152 # the event_output.id tells us all we need to know
153 # XXX why is id in here twice?
154 my $filename_fragment = $eo->id . '_' . $eo->id;
156 # TODO: add scheduling intelligence and use it here... or not if
157 # relying only on crontab
158 my $client = rpc_client();
159 my $resp = $client->send_request(
160 'inject', $tmpl_output, $filename_fragment, 0
161 ); # FIXME: 0 could be seconds-from-epoch UTC if deferred call needed
164 ref $resp ? ("Response: " . Dumper($resp->value)) : "Error: $resp"
167 if ($resp->{code} and $resp->{code}->value == 200) {
169 $eo->data('filename: ' . $resp->{spooled_filename}->value);
170 # could look for the file that replaced it
173 my $msg = $resp->{faultcode} ? $resp->{faultcode}->value :
174 $resp->{ code} ? $resp->{ code}->value :
175 " -- UNKNOWN response '$resp'";
176 $msg .= " for $filename_fragment";
177 $eo->data("Error " . $msg);
178 $logger->error(__PACKAGE__ . ": Mediator Error " . $msg);
181 # Now point all our events' async_output to the newly made row
182 # $eo = $env->{EventProcessor}->editor->
183 # create_action_trigger_event_output( $eo );
184 $e->update_action_trigger_event_output($eo) or return $e->die_event;
185 foreach (@eventids) {
186 my $event = $e->retrieve_action_trigger_event($_);
187 $event->async_output($eo->id);
188 $e->update_action_trigger_event($event);
190 $e->commit; # defer till after loop?
192 # TODO: a sub for saving async_output might belong in Trigger.pm
197 my $response = shift or return;
198 return map {$response->{$_}} sort grep {/^file_\d*/} keys %$response;
201 =head1 EXAMPLE CALFILES
203 Note: all lines start flush left (no leading whitespace)
205 =head2 Example callfile (successful)
207 Channel: SIP/ubab33/17707775555
208 Context: overdue-test
215 Set: titlestring=chez nos gens;; added by OpenILS::Application::Trigger::Reactor::AstCall handler:
216 ; event_ids = 123,145
217 ; event_output = 14;; added by inject() in the mediator
218 Set: callfilename=EG_1258060382_6.call
220 StartRetry: 2139 1 (1258060442)
222 Channel: SIP/ubab33/17707775555
224 =head2 Example callfile (FAILED)
226 CallerID: "Jack Jackson" <17707775555>
227 Context: overdue-test
234 Set: titlestring=Land Before Time;; added by OpenILS::Application::Trigger::Reactor::AstCall handler:
236 Set: callfilename=EG_joe_20091109145355.call
238 StartRetry: 2139 1 (1257907526)
241 EndRetry: 2139 1 (1257907496)
243 StartRetry: 2139 2 (1257907617)
247 =head2 Possible data structure:
251 attempts => [ $attempt1, $attempt2 ... $attemptN ],
252 anything_else => scalar,
256 time => secs from epoch (UTC) for the BEGINNING of the call,
264 # parses the done callfile comments from Mediator
266 my $content = shift or return;
269 my @lines = split "\n", $content;
270 foreach (shift @lines) {
271 s/^\s*(Set:\s*)?//i; # strip leading whitespace, and possible "Set:"
272 if (/^StartRetry: \d+ (\d+) \((\d+)\)/) {
273 # go parse an attempt;
274 # go record an attempt;
276 if (/^(Status):\s*(\S+)/i or /^;+\s*(FAILED):\s*(\S*)/i) {
281 /^;+\s*(\S+)\s*[=:]\s*([^;]*)$/ and $hash{lc $1} = $2;
283 if (exists $hash{failed}) {
284 $hash{failcode} = $hash{failed};
285 # b/c "0" is a common failcode and we want a more binary indicator
292 my $self = shift or return;
293 my $files = join(',',@_) or return;
294 my $client = rpc_client();
295 return $client->send_request('cleanup', $files);
296 # TODO: more error checking
300 my $self = shift or return;
301 my $client = rpc_client();
302 my $resp = $client->send_request('retrieve');
303 unless ($resp and ref $resp) {
305 __PACKAGE__ . ": Mediator Error: " .
306 ($resp ? 'Bad' : 'No') . " response to retrieve request"
311 # my $count = $resp{match_count}; # how many files we should have
313 my @files = _files($resp);
315 my $content = $resp->{$_}->content;
316 my $filename = $resp->{$_}->filename;
318 $logger->error(__PACKAGE__ .
319 ": Mediator sent incomplete/unintelligible message for " .
320 "filename " . ($filename || 'UNKNOWN'));
323 my $feedback = feedback_hash($content);
324 my $output = $e->retrieve_action_trigger_event_output(
325 $feedback->{event_output}
327 if ($content == $output->data) {
329 __PACKAGE__ . ": Mediator sent duplicate file "
330 . $resp->{$_}->filename . " for event_output " .
331 $feedback->{event_output}
334 $output->data($content);
336 $e->commit; # defer until after loop? probably not
337 my $clean = $client->send_request('cleanup', $filename);
338 # TODO: deletion by (comma-separated) filenames in chunks
339 # instead of individually?
340 # push @rm_list, $_; $client->send_request('cleanup', join(',',@rm_list));
341 unless ($clean and ref $clean) {
343 __PACKAGE__ . ": Mediator Error: " .
344 ($clean ? 'Bad' : 'No') .
345 " response to cleanup $filename request");
348 unless ($clean->{code}->value == 200 and $clean->{delete_count}) {
349 $logger->error(__PACKAGE__ . ": cleanup $filename returned " . (
350 $resp->{faultcode} ? $resp->{faultcode}->value :
351 $resp->{ code} ? $resp->{ code}->value :
352 " -- UNKNOWN response '$resp'"
353 ) . " with delete_count " .
354 (defined $clean->{delete_count} ? $clean->{delete_count} : 'UNDEF'));