3 # This file is part of Koha.
5 # Copyright (C) 2012-2013 ByWater Solutions
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
26 use C4::SIP::Sip::Constants qw(:all);
29 use constant { LANGUAGE => '001' };
40 my $patron_identifier;
47 my $fee_acknowledged = 0;
54 "a|address|host|hostaddress=s" => \$host, # sip server ip
55 "p|port=s" => \$port, # sip server port
56 "su|sip_user=s" => \$login_user_id, # sip user
57 "sp|sip_pass=s" => \$login_password, # sip password
58 "l|location|location_code=s" => \$location_code, # sip location code
60 "patron=s" => \$patron_identifier, # patron cardnumber or login
61 "password=s" => \$patron_password, # patron's password
63 "i|item=s" => \$item_identifier,
65 "fa|fee-acknowledged" => \$fee_acknowledged,
67 "s|summary=s" => \$summary,
69 "t|terminator=s" => \$terminator,
71 "m|message=s" => \@messages,
86 $terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
88 # Set perl to expect the same record terminator it is sending
91 my $transaction_date = C4::SIP::Sip::timestamp();
93 my $terminal_password = $login_password;
96 print "Attempting socket connection to $host:$port...";
98 my $socket = IO::Socket::INET->new("$host:$port")
99 or die "failed! : $!\n";
105 subroutine => \&build_login_command_message,
107 login_user_id => $login_user_id,
108 login_password => $login_password,
109 location_code => $location_code,
112 patron_status_request => {
113 name => 'Patron Status Request',
114 subroutine => \&build_patron_status_request_command_message,
116 transaction_date => $transaction_date,
117 institution_id => $location_code,
118 patron_identifier => $patron_identifier,
119 terminal_password => $terminal_password,
120 patron_password => $patron_password,
122 optional => [ 'patron_password', ],
124 patron_information => {
125 name => 'Patron Information',
126 subroutine => \&build_patron_information_command_message,
128 transaction_date => $transaction_date,
129 institution_id => $location_code,
130 patron_identifier => $patron_identifier,
131 terminal_password => $terminal_password,
132 patron_password => $patron_password,
135 optional => [ 'patron_password', 'summary' ],
137 item_information => {
138 name => 'Item Information',
139 subroutine => \&build_item_information_command_message,
141 transaction_date => $transaction_date,
142 institution_id => $location_code,
143 item_identifier => $item_identifier,
144 terminal_password => $terminal_password,
150 subroutine => \&build_checkout_command_message,
152 SC_renewal_policy => 'Y',
154 transaction_date => $transaction_date,
155 nb_due_date => undef,
156 institution_id => $location_code,
157 patron_identifier => $patron_identifier,
158 item_identifier => $item_identifier,
159 terminal_password => $terminal_password,
160 item_properties => undef,
161 patron_password => $patron_password,
162 fee_acknowledged => $fee_acknowledged,
166 'nb_due_date', # defaults to transaction date
175 subroutine => \&build_checkin_command_message,
178 transaction_date => $transaction_date,
179 return_date => $transaction_date,
180 current_location => $location_code,
181 institution_id => $location_code,
182 item_identifier => $item_identifier,
183 terminal_password => $terminal_password,
184 item_properties => undef,
188 'return_date', # defaults to transaction date
196 subroutine => \&build_renew_command_message,
198 third_party_allowed => 'N',
200 transaction_date => $transaction_date,
201 nb_due_date => undef,
202 institution_id => $location_code,
203 patron_identifier => $patron_identifier,
204 patron_password => $patron_password,
205 item_identifier => $item_identifier,
206 title_identifier => undef,
207 terminal_password => $terminal_password,
208 item_properties => undef,
209 fee_acknowledged => $fee_acknowledged,
212 'nb_due_date', # defaults to transaction date
223 my $data = run_command_message('login');
225 if ( $data =~ '^941' ) { ## we are logged in
226 foreach my $m (@messages) {
229 my $data = run_command_message($m);
237 sub build_command_message {
240 ##FIXME It would be much better to use exception handling so we aren't priting from subs
241 unless ( $handlers->{$message} ) {
242 say "$message is an unsupported command!";
246 my $subroutine = $handlers->{$message}->{subroutine};
247 my $parameters = $handlers->{$message}->{parameters};
248 my %optional = map { $_ => 1 } @{ $handlers->{$message}->{optional} };
250 foreach my $key ( keys %$parameters ) {
251 unless ( $parameters->{$key} ) {
252 unless ( $optional{$key} ) {
253 say "$key is required for $message";
259 return &$subroutine($parameters);
262 sub run_command_message {
265 my $command_message = build_command_message($message);
267 return unless $command_message;
269 say "SEND: $command_message";
270 print $socket $command_message . $terminator;
272 my $data = <$socket>;
279 sub build_login_command_message {
282 my $login_user_id = $params->{login_user_id};
283 my $login_password = $params->{login_password};
284 my $location_code = $params->{location_code};
288 . build_field( FID_LOGIN_UID, $login_user_id )
289 . build_field( FID_LOGIN_PWD, $login_password )
290 . build_field( FID_LOCATION_CODE, $location_code );
293 sub build_patron_status_request_command_message {
296 my $transaction_date = $params->{transaction_date};
297 my $institution_id = $params->{institution_id};
298 my $patron_identifier = $params->{patron_identifier};
299 my $terminal_password = $params->{terminal_password};
300 my $patron_password = $params->{patron_password};
306 . build_field( FID_INST_ID, $institution_id )
307 . build_field( FID_PATRON_ID, $patron_identifier )
308 . build_field( FID_TERMINAL_PWD, $terminal_password )
309 . build_field( FID_PATRON_PWD, $patron_password );
312 sub build_patron_information_command_message {
315 my $transaction_date = $params->{transaction_date};
316 my $institution_id = $params->{institution_id};
317 my $patron_identifier = $params->{patron_identifier};
318 my $terminal_password = $params->{terminal_password};
319 my $patron_password = $params->{patron_password};
320 my $summary = $params->{summary};
329 . build_field( FID_INST_ID, $institution_id )
330 . build_field( FID_PATRON_ID, $patron_identifier )
331 . build_field( FID_TERMINAL_PWD, $terminal_password )
332 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } );
335 sub build_item_information_command_message {
338 my $transaction_date = $params->{transaction_date};
339 my $institution_id = $params->{institution_id};
340 my $item_identifier = $params->{item_identifier};
341 my $terminal_password = $params->{terminal_password};
347 . build_field( FID_INST_ID, $institution_id )
348 . build_field( FID_ITEM_ID, $item_identifier )
349 . build_field( FID_TERMINAL_PWD, $terminal_password );
352 sub build_checkout_command_message {
355 my $SC_renewal_policy = $params->{SC_renewal_policy} || 'N';
356 my $no_block = $params->{no_block} || 'N';
357 my $transaction_date = $params->{transaction_date};
358 my $nb_due_date = $params->{nb_due_date};
359 my $institution_id = $params->{institution_id};
360 my $patron_identifier = $params->{patron_identifier};
361 my $item_identifier = $params->{item_identifier};
362 my $terminal_password = $params->{terminal_password};
363 my $item_properties = $params->{item_properties};
364 my $patron_password = $params->{patron_password};
365 my $fee_acknowledged = $params->{fee_acknowledged} || 'N';
366 my $cancel = $params->{cancel} || 'N';
368 $SC_renewal_policy = $SC_renewal_policy eq 'Y' ? 'Y' : 'N';
369 $no_block = $no_block eq 'Y' ? 'Y' : 'N';
370 $fee_acknowledged = $fee_acknowledged eq 'Y' ? 'Y' : 'N';
371 $cancel = $cancel eq 'Y' ? 'Y' : 'N';
373 $nb_due_date ||= $transaction_date;
381 . build_field( FID_INST_ID, $institution_id )
382 . build_field( FID_PATRON_ID, $patron_identifier )
383 . build_field( FID_ITEM_ID, $item_identifier )
384 . build_field( FID_TERMINAL_PWD, $terminal_password )
385 . build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } )
386 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
387 . build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } )
388 . build_field( FID_CANCEL, $cancel, { optional => 1 } );
391 sub build_checkin_command_message {
394 my $no_block = $params->{no_block} || 'N';
395 my $transaction_date = $params->{transaction_date};
396 my $return_date = $params->{return_date};
397 my $current_location = $params->{current_location};
398 my $institution_id = $params->{institution_id};
399 my $item_identifier = $params->{item_identifier};
400 my $terminal_password = $params->{terminal_password};
401 my $item_properties = $params->{item_properties};
402 my $cancel = $params->{cancel} || 'N';
404 $no_block = $no_block eq 'Y' ? 'Y' : 'N';
405 $cancel = $cancel eq 'Y' ? 'Y' : 'N';
407 $return_date ||= $transaction_date;
414 . build_field( FID_CURRENT_LOCN, $current_location )
415 . build_field( FID_INST_ID, $institution_id )
416 . build_field( FID_ITEM_ID, $item_identifier )
417 . build_field( FID_TERMINAL_PWD, $terminal_password )
418 . build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } )
419 . build_field( FID_CANCEL, $cancel, { optional => 1 } );
422 sub build_renew_command_message {
425 my $third_party_allowed = $params->{third_party_allowed} || 'N';
426 my $no_block = $params->{no_block} || 'N';
427 my $transaction_date = $params->{transaction_date};
428 my $nb_due_date = $params->{nb_due_date};
429 my $institution_id = $params->{institution_id};
430 my $patron_identifier = $params->{patron_identifier};
431 my $patron_password = $params->{patron_password};
432 my $item_identifier = $params->{item_identifier};
433 my $title_identifier = $params->{title_identifier};
434 my $terminal_password = $params->{terminal_password};
435 my $item_properties = $params->{item_properties};
436 my $fee_acknowledged = $params->{fee_acknowledged} || 'N';
438 $third_party_allowed = $third_party_allowed eq 'Y' ? 'Y' : 'N';
439 $no_block = $no_block eq 'Y' ? 'Y' : 'N';
440 $fee_acknowledged = $fee_acknowledged eq 'Y' ? 'Y' : 'N';
442 $nb_due_date ||= $transaction_date;
446 . $third_party_allowed
450 . build_field( FID_INST_ID, $institution_id )
451 . build_field( FID_PATRON_ID, $patron_identifier )
452 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
453 . build_field( FID_ITEM_ID, $item_identifier )
454 . build_field( FID_TITLE_ID, $title_identifier )
455 . build_field( FID_TERMINAL_PWD, $terminal_password )
456 . build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } )
457 . build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } );
461 my ( $field_identifier, $value, $params ) = @_;
465 return q{} if ( $params->{optional} && !$value );
467 return $field_identifier . (($value) ? $value : '') . '|';
471 say q/sip_cli_emulator.pl - SIP command line emulator
473 Test a SIP2 service by sending patron status and patron
474 information requests.
477 sip_cli_emulator.pl [OPTIONS]
480 --help display help message
482 -a --address SIP server ip address or host name
483 -p --port SIP server port
485 -su --sip_user SIP server login username
486 -sp --sip_pass SIP server login password
488 -l --location SIP location code
490 --patron ILS patron cardnumber or username
491 --password ILS patron password
493 -s --summary Optionally define the patron information request summary field.
494 Please refer to the SIP2 protocol specification for details
496 --item ILS item identifier ( item barcode )
498 -t --terminator SIP2 message terminator, either CR, or CRLF
501 -fa --fee-acknowledged Sends a confirmation of checkout fee
503 -m --message SIP2 message to execute
505 Implemented Messages:
506 patron_status_request