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;
63 "a|address|host|hostaddress=s" => \$host, # sip server ip
64 "p|port=s" => \$port, # sip server port
65 "su|sip_user=s" => \$login_user_id, # sip user
66 "sp|sip_pass=s" => \$login_password, # sip password
67 "l|location|location_code=s" => \$location_code, # sip location code
69 "patron=s" => \$patron_identifier, # patron cardnumber or login
70 "password=s" => \$patron_password, # patron's password
72 "i|item=s" => \$item_identifier,
74 "fa|fee-acknowledged" => \$fee_acknowledged,
76 "s|summary=s" => \$summary,
78 "fee-type=s" => \$fee_type,
79 "payment-type=s" => \$payment_type,
80 "currency-type=s" => \$currency_type,
81 "fee-amount=s" => \$fee_amount,
82 "fee-identifier=s" => \$fee_identifier,
83 "transaction-id=s" => \$transaction_id,
84 "pickup-location=s" => \$pickup_location,
85 "hold-mode=s" => \$hold_mode,
87 "t|terminator=s" => \$terminator,
89 "m|message=s" => \@messages,
104 $terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
106 # Set perl to expect the same record terminator it is sending
109 my $transaction_date = C4::SIP::Sip::timestamp();
111 my $terminal_password = $login_password;
114 print "Attempting socket connection to $host:$port...";
116 my $socket = IO::Socket::INET->new("$host:$port")
117 or die "failed! : $!\n";
123 subroutine => \&build_login_command_message,
125 login_user_id => $login_user_id,
126 login_password => $login_password,
127 location_code => $location_code,
130 sc_status_request => {
132 subroutine => \&build_sc_status_command_message,
136 patron_status_request => {
137 name => 'Patron Status Request',
138 subroutine => \&build_patron_status_request_command_message,
140 transaction_date => $transaction_date,
141 institution_id => $location_code,
142 patron_identifier => $patron_identifier,
143 terminal_password => $terminal_password,
144 patron_password => $patron_password,
146 optional => [ 'patron_password', ],
148 patron_information => {
149 name => 'Patron Information',
150 subroutine => \&build_patron_information_command_message,
152 transaction_date => $transaction_date,
153 institution_id => $location_code,
154 patron_identifier => $patron_identifier,
155 terminal_password => $terminal_password,
156 patron_password => $patron_password,
159 optional => [ 'patron_password', 'summary' ],
161 item_information => {
162 name => 'Item Information',
163 subroutine => \&build_item_information_command_message,
165 transaction_date => $transaction_date,
166 institution_id => $location_code,
167 item_identifier => $item_identifier,
168 terminal_password => $terminal_password,
174 subroutine => \&build_checkout_command_message,
176 SC_renewal_policy => 'Y',
178 transaction_date => $transaction_date,
179 nb_due_date => undef,
180 institution_id => $location_code,
181 patron_identifier => $patron_identifier,
182 item_identifier => $item_identifier,
183 terminal_password => $terminal_password,
184 item_properties => undef,
185 patron_password => $patron_password,
186 fee_acknowledged => $fee_acknowledged,
190 'nb_due_date', # defaults to transaction date
199 subroutine => \&build_checkin_command_message,
202 transaction_date => $transaction_date,
203 return_date => $transaction_date,
204 current_location => $location_code,
205 institution_id => $location_code,
206 item_identifier => $item_identifier,
207 terminal_password => $terminal_password,
208 item_properties => undef,
212 'return_date', # defaults to transaction date
220 subroutine => \&build_renew_command_message,
222 third_party_allowed => 'N',
224 transaction_date => $transaction_date,
225 nb_due_date => undef,
226 institution_id => $location_code,
227 patron_identifier => $patron_identifier,
228 patron_password => $patron_password,
229 item_identifier => $item_identifier,
230 title_identifier => undef,
231 terminal_password => $terminal_password,
232 item_properties => undef,
233 fee_acknowledged => $fee_acknowledged,
236 'nb_due_date', # defaults to transaction date
247 subroutine => \&build_fee_paid_command_message,
249 transaction_date => $transaction_date,
250 fee_type => $fee_type,
251 payment_type => $payment_type,
252 currency_type => $currency_type,
253 fee_amount => $fee_amount,
254 institution_id => $location_code,
255 patron_identifier => $patron_identifier,
256 terminal_password => $terminal_password,
257 patron_password => $patron_password,
258 fee_identifier => $fee_identifier,
259 transaction_id => $transaction_id,
262 'fee_type', # has default
263 'payment_type', # has default
264 'currency_type', #has default
273 subroutine => \&build_hold_command_message,
275 hold_mode => $hold_mode eq '-' ? '-' : '+',
276 transaction_date => $transaction_date,
277 expiration_date => undef,
278 pickup_location => $pickup_location,
280 institution_id => $location_code,
281 patron_identifier => $patron_identifier,
282 patron_password => $patron_password,
283 item_identifier => $item_identifier,
284 title_identifier => undef,
285 terminal_password => $terminal_password,
286 fee_acknowledged => $fee_acknowledged,
301 my $data = run_command_message('login');
303 if ( $data =~ '^941' ) { ## we are logged in
304 foreach my $m (@messages) {
307 my $data = run_command_message($m);
315 sub build_command_message {
318 ##FIXME It would be much better to use exception handling so we aren't priting from subs
319 unless ( $handlers->{$message} ) {
320 say "$message is an unsupported command!";
324 my $subroutine = $handlers->{$message}->{subroutine};
325 my $parameters = $handlers->{$message}->{parameters};
326 my %optional = map { $_ => 1 } @{ $handlers->{$message}->{optional} };
328 foreach my $key ( keys %$parameters ) {
329 unless ( $parameters->{$key} ) {
330 unless ( $optional{$key} ) {
331 say "$key is required for $message";
337 return &$subroutine($parameters);
340 sub run_command_message {
343 my $command_message = build_command_message($message);
345 return unless $command_message;
347 say "SEND: $command_message";
348 print $socket $command_message . $terminator;
350 my $data = <$socket>;
357 sub build_login_command_message {
360 my $login_user_id = $params->{login_user_id};
361 my $login_password = $params->{login_password};
362 my $location_code = $params->{location_code};
366 . build_field( FID_LOGIN_UID, $login_user_id )
367 . build_field( FID_LOGIN_PWD, $login_password )
368 . build_field( FID_LOCATION_CODE, $location_code );
371 sub build_sc_status_command_message {
374 return SC_STATUS . "0" . "030" . "2.00";
377 sub build_patron_status_request_command_message {
380 my $transaction_date = $params->{transaction_date};
381 my $institution_id = $params->{institution_id};
382 my $patron_identifier = $params->{patron_identifier};
383 my $terminal_password = $params->{terminal_password};
384 my $patron_password = $params->{patron_password};
390 . build_field( FID_INST_ID, $institution_id )
391 . build_field( FID_PATRON_ID, $patron_identifier )
392 . build_field( FID_TERMINAL_PWD, $terminal_password )
393 . build_field( FID_PATRON_PWD, $patron_password );
396 sub build_patron_information_command_message {
399 my $transaction_date = $params->{transaction_date};
400 my $institution_id = $params->{institution_id};
401 my $patron_identifier = $params->{patron_identifier};
402 my $terminal_password = $params->{terminal_password};
403 my $patron_password = $params->{patron_password};
404 my $summary = $params->{summary};
413 . build_field( FID_INST_ID, $institution_id )
414 . build_field( FID_PATRON_ID, $patron_identifier )
415 . build_field( FID_TERMINAL_PWD, $terminal_password )
416 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } );
419 sub build_item_information_command_message {
422 my $transaction_date = $params->{transaction_date};
423 my $institution_id = $params->{institution_id};
424 my $item_identifier = $params->{item_identifier};
425 my $terminal_password = $params->{terminal_password};
431 . build_field( FID_INST_ID, $institution_id )
432 . build_field( FID_ITEM_ID, $item_identifier )
433 . build_field( FID_TERMINAL_PWD, $terminal_password );
436 sub build_checkout_command_message {
439 my $SC_renewal_policy = $params->{SC_renewal_policy} || 'N';
440 my $no_block = $params->{no_block} || 'N';
441 my $transaction_date = $params->{transaction_date};
442 my $nb_due_date = $params->{nb_due_date};
443 my $institution_id = $params->{institution_id};
444 my $patron_identifier = $params->{patron_identifier};
445 my $item_identifier = $params->{item_identifier};
446 my $terminal_password = $params->{terminal_password};
447 my $item_properties = $params->{item_properties};
448 my $patron_password = $params->{patron_password};
449 my $fee_acknowledged = $params->{fee_acknowledged} || 'N';
450 my $cancel = $params->{cancel} || 'N';
452 $SC_renewal_policy = $SC_renewal_policy eq 'Y' ? 'Y' : 'N';
453 $no_block = $no_block eq 'Y' ? 'Y' : 'N';
454 $fee_acknowledged = $fee_acknowledged eq 'Y' ? 'Y' : 'N';
455 $cancel = $cancel eq 'Y' ? 'Y' : 'N';
457 $nb_due_date ||= $transaction_date;
465 . build_field( FID_INST_ID, $institution_id )
466 . build_field( FID_PATRON_ID, $patron_identifier )
467 . build_field( FID_ITEM_ID, $item_identifier )
468 . build_field( FID_TERMINAL_PWD, $terminal_password )
469 . build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } )
470 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
471 . build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } )
472 . build_field( FID_CANCEL, $cancel, { optional => 1 } );
475 sub build_checkin_command_message {
478 my $no_block = $params->{no_block} || 'N';
479 my $transaction_date = $params->{transaction_date};
480 my $return_date = $params->{return_date};
481 my $current_location = $params->{current_location};
482 my $institution_id = $params->{institution_id};
483 my $item_identifier = $params->{item_identifier};
484 my $terminal_password = $params->{terminal_password};
485 my $item_properties = $params->{item_properties};
486 my $cancel = $params->{cancel} || 'N';
488 $no_block = $no_block eq 'Y' ? 'Y' : 'N';
489 $cancel = $cancel eq 'Y' ? 'Y' : 'N';
491 $return_date ||= $transaction_date;
498 . build_field( FID_CURRENT_LOCN, $current_location )
499 . build_field( FID_INST_ID, $institution_id )
500 . build_field( FID_ITEM_ID, $item_identifier )
501 . build_field( FID_TERMINAL_PWD, $terminal_password )
502 . build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } )
503 . build_field( FID_CANCEL, $cancel, { optional => 1 } );
506 sub build_hold_command_message {
509 my $hold_mode = $params->{hold_mode};
510 my $transaction_date = $params->{transaction_date};
511 my $expiration_date = $params->{expiration_date};
512 my $pickup_location = $params->{pickup_location};
513 my $hold_type = $params->{hold_type};
514 my $institution_id = $params->{institution_id};
515 my $patron_identifier = $params->{patron_identifier};
516 my $patron_password = $params->{patron_password};
517 my $item_identifier = $params->{item_identifier};
518 my $title_identifier = $params->{title_identifier};
519 my $terminal_password = $params->{terminal_password};
520 my $fee_acknowledged = $params->{fee_acknowledged} || 'N';
526 . build_field( FID_EXPIRATION, $expiration_date, { optional => 1 } )
527 . build_field( FID_PICKUP_LOCN, $pickup_location, { optional => 1 } )
528 . build_field( FID_HOLD_TYPE, $hold_type, { optional => 1 } )
529 . build_field( FID_INST_ID, $institution_id )
530 . build_field( FID_PATRON_ID, $patron_identifier )
531 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
532 . build_field( FID_ITEM_ID, $item_identifier, { optional => 1 } )
533 . build_field( FID_TITLE_ID, $title_identifier, { optional => 1 } )
534 . build_field( FID_TERMINAL_PWD, $terminal_password, { optional => 1 } )
535 . build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } );
538 sub build_renew_command_message {
541 my $third_party_allowed = $params->{third_party_allowed} || 'N';
542 my $no_block = $params->{no_block} || 'N';
543 my $transaction_date = $params->{transaction_date};
544 my $nb_due_date = $params->{nb_due_date};
545 my $institution_id = $params->{institution_id};
546 my $patron_identifier = $params->{patron_identifier};
547 my $patron_password = $params->{patron_password};
548 my $item_identifier = $params->{item_identifier};
549 my $title_identifier = $params->{title_identifier};
550 my $terminal_password = $params->{terminal_password};
551 my $item_properties = $params->{item_properties};
552 my $fee_acknowledged = $params->{fee_acknowledged} || 'N';
554 $third_party_allowed = $third_party_allowed eq 'Y' ? 'Y' : 'N';
555 $no_block = $no_block eq 'Y' ? 'Y' : 'N';
556 $fee_acknowledged = $fee_acknowledged eq 'Y' ? 'Y' : 'N';
558 $nb_due_date ||= $transaction_date;
562 . $third_party_allowed
566 . build_field( FID_INST_ID, $institution_id )
567 . build_field( FID_PATRON_ID, $patron_identifier )
568 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
569 . build_field( FID_ITEM_ID, $item_identifier )
570 . build_field( FID_TITLE_ID, $title_identifier )
571 . build_field( FID_TERMINAL_PWD, $terminal_password )
572 . build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } )
573 . build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } );
576 sub build_fee_paid_command_message {
579 my $transaction_date = $params->{transaction_date};
580 my $fee_type = $params->{fee_type} || '01';
581 my $payment_type = $params->{payment_type} || '00';
582 my $currency_type = $params->{currency_type} || 'USD';
583 my $fee_amount = $params->{fee_amount};
584 my $institution_id = $params->{location_code};
585 my $patron_identifier = $params->{patron_identifier};
586 my $terminal_password = $params->{terminal_password};
587 my $patron_password = $params->{patron_password};
588 my $fee_identifier = $params->{fee_identifier};
589 my $transaction_id = $params->{transaction_id};
597 . build_field( FID_FEE_AMT, $fee_amount )
598 . build_field( FID_INST_ID, $institution_id )
599 . build_field( FID_PATRON_ID, $patron_identifier )
600 . build_field( FID_TERMINAL_PWD, $terminal_password, { optional => 1 } )
601 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
602 . build_field( FID_FEE_ID, $fee_identifier, { optional => 1 } )
603 . build_field( FID_TRANSACTION_ID, $transaction_id, { optional => 1 } );
607 my ( $field_identifier, $value, $params ) = @_;
611 return q{} if ( $params->{optional} && !$value );
613 return $field_identifier . (($value) ? $value : '') . '|';
617 say q/sip_cli_emulator.pl - SIP command line emulator
619 Test a SIP2 service by sending patron status and patron
620 information requests.
623 sip_cli_emulator.pl [OPTIONS]
626 --help display help message
628 -a --address SIP server ip address or host name
629 -p --port SIP server port
631 -su --sip_user SIP server login username
632 -sp --sip_pass SIP server login password
634 -l --location SIP location code
636 --patron ILS patron cardnumber or username
637 --password ILS patron password
639 -s --summary Optionally define the patron information request summary field.
640 Please refer to the SIP2 protocol specification for details
642 --item ILS item identifier ( item barcode )
644 -t --terminator SIP2 message terminator, either CR, or CRLF
647 -fa --fee-acknowledged Sends a confirmation of checkout fee
649 --fee-type Fee type for Fee Paid message, defaults to '01'
650 --payment-type Payment type for Fee Paid message, default to '00'
651 --currency-type Currency type for Fee Paid message, defaults to 'USD'
652 --fee-amount Fee amount for Fee Paid message, required
653 --fee-identifier Fee identifier for Fee Paid message, optional
654 --transaction-id Transaction id for Fee Paid message, optional
655 --pickup-location Pickup location (branchcode) for Hold message, optional
656 --hold-mode Accepts "+" to add hold or "-" to cancel hold, defaults to +
658 -m --message SIP2 message to execute
660 Implemented Messages:
667 patron_status_request