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;
64 "a|address|host|hostaddress=s" => \$host, # sip server ip
65 "p|port=s" => \$port, # sip server port
66 "su|sip_user=s" => \$login_user_id, # sip user
67 "sp|sip_pass=s" => \$login_password, # sip password
68 "l|location|location_code=s" => \$location_code, # sip location code
70 "patron=s" => \$patron_identifier, # patron cardnumber or login
71 "password=s" => \$patron_password, # patron's password
73 "i|item=s" => \$item_identifier,
75 "fa|fee-acknowledged" => \$fee_acknowledged,
77 "s|summary=s" => \$summary,
79 "fee-type=s" => \$fee_type,
80 "payment-type=s" => \$payment_type,
81 "currency-type=s" => \$currency_type,
82 "fee-amount=s" => \$fee_amount,
83 "fee-identifier=s" => \$fee_identifier,
84 "transaction-id=s" => \$transaction_id,
85 "pickup-location=s" => \$pickup_location,
86 "hold-mode=s" => \$hold_mode,
87 "n|no-block=s" => \$no_block,
89 "t|terminator=s" => \$terminator,
91 "m|message=s" => \@messages,
108 $terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
110 # Set perl to expect the same record terminator it is sending
113 my $transaction_date = C4::SIP::Sip::timestamp();
115 my $terminal_password = $login_password;
118 print "Attempting socket connection to $host:$port...";
120 my $socket = IO::Socket::INET->new("$host:$port")
121 or die "failed! : $!\n";
127 subroutine => \&build_login_command_message,
129 login_user_id => $login_user_id,
130 login_password => $login_password,
131 location_code => $location_code,
134 sc_status_request => {
136 subroutine => \&build_sc_status_command_message,
140 patron_status_request => {
141 name => 'Patron Status Request',
142 subroutine => \&build_patron_status_request_command_message,
144 transaction_date => $transaction_date,
145 institution_id => $location_code,
146 patron_identifier => $patron_identifier,
147 terminal_password => $terminal_password,
148 patron_password => $patron_password,
150 optional => [ 'patron_password', ],
152 patron_information => {
153 name => 'Patron Information',
154 subroutine => \&build_patron_information_command_message,
156 transaction_date => $transaction_date,
157 institution_id => $location_code,
158 patron_identifier => $patron_identifier,
159 terminal_password => $terminal_password,
160 patron_password => $patron_password,
163 optional => [ 'patron_password', 'summary' ],
165 item_information => {
166 name => 'Item Information',
167 subroutine => \&build_item_information_command_message,
169 transaction_date => $transaction_date,
170 institution_id => $location_code,
171 item_identifier => $item_identifier,
172 terminal_password => $terminal_password,
178 subroutine => \&build_checkout_command_message,
180 SC_renewal_policy => 'Y',
181 no_block => $no_block,
182 transaction_date => $transaction_date,
183 nb_due_date => undef,
184 institution_id => $location_code,
185 patron_identifier => $patron_identifier,
186 item_identifier => $item_identifier,
187 terminal_password => $terminal_password,
188 item_properties => undef,
189 patron_password => $patron_password,
190 fee_acknowledged => $fee_acknowledged,
194 'nb_due_date', # defaults to transaction date
203 subroutine => \&build_checkin_command_message,
205 no_block => $no_block,
206 transaction_date => $transaction_date,
207 return_date => $transaction_date,
208 current_location => $location_code,
209 institution_id => $location_code,
210 item_identifier => $item_identifier,
211 terminal_password => $terminal_password,
212 item_properties => undef,
216 'return_date', # defaults to transaction date
224 subroutine => \&build_renew_command_message,
226 third_party_allowed => 'N',
227 no_block => $no_block,
228 transaction_date => $transaction_date,
229 nb_due_date => undef,
230 institution_id => $location_code,
231 patron_identifier => $patron_identifier,
232 patron_password => $patron_password,
233 item_identifier => $item_identifier,
234 title_identifier => undef,
235 terminal_password => $terminal_password,
236 item_properties => undef,
237 fee_acknowledged => $fee_acknowledged,
240 'nb_due_date', # defaults to transaction date
251 subroutine => \&build_fee_paid_command_message,
253 transaction_date => $transaction_date,
254 fee_type => $fee_type,
255 payment_type => $payment_type,
256 currency_type => $currency_type,
257 fee_amount => $fee_amount,
258 institution_id => $location_code,
259 patron_identifier => $patron_identifier,
260 terminal_password => $terminal_password,
261 patron_password => $patron_password,
262 fee_identifier => $fee_identifier,
263 transaction_id => $transaction_id,
266 'fee_type', # has default
267 'payment_type', # has default
268 'currency_type', #has default
277 subroutine => \&build_hold_command_message,
279 hold_mode => $hold_mode eq '-' ? '-' : '+',
280 transaction_date => $transaction_date,
281 expiration_date => undef,
282 pickup_location => $pickup_location,
284 institution_id => $location_code,
285 patron_identifier => $patron_identifier,
286 patron_password => $patron_password,
287 item_identifier => $item_identifier,
288 title_identifier => undef,
289 terminal_password => $terminal_password,
290 fee_acknowledged => $fee_acknowledged,
305 my $data = run_command_message('login');
307 if ( $data =~ '^941' ) { ## we are logged in
308 foreach my $m (@messages) {
311 my $data = run_command_message($m);
319 sub build_command_message {
322 ##FIXME It would be much better to use exception handling so we aren't priting from subs
323 unless ( $handlers->{$message} ) {
324 say "$message is an unsupported command!";
328 my $subroutine = $handlers->{$message}->{subroutine};
329 my $parameters = $handlers->{$message}->{parameters};
330 my %optional = map { $_ => 1 } @{ $handlers->{$message}->{optional} };
332 foreach my $key ( keys %$parameters ) {
333 unless ( $parameters->{$key} ) {
334 unless ( $optional{$key} ) {
335 say "$key is required for $message";
341 return &$subroutine($parameters);
344 sub run_command_message {
347 my $command_message = build_command_message($message);
349 return unless $command_message;
351 say "SEND: $command_message";
352 print $socket $command_message . $terminator;
354 my $data = <$socket>;
361 sub build_login_command_message {
364 my $login_user_id = $params->{login_user_id};
365 my $login_password = $params->{login_password};
366 my $location_code = $params->{location_code};
370 . build_field( FID_LOGIN_UID, $login_user_id )
371 . build_field( FID_LOGIN_PWD, $login_password )
372 . build_field( FID_LOCATION_CODE, $location_code );
375 sub build_sc_status_command_message {
378 return SC_STATUS . "0" . "030" . "2.00";
381 sub build_patron_status_request_command_message {
384 my $transaction_date = $params->{transaction_date};
385 my $institution_id = $params->{institution_id};
386 my $patron_identifier = $params->{patron_identifier};
387 my $terminal_password = $params->{terminal_password};
388 my $patron_password = $params->{patron_password};
394 . build_field( FID_INST_ID, $institution_id )
395 . build_field( FID_PATRON_ID, $patron_identifier )
396 . build_field( FID_TERMINAL_PWD, $terminal_password )
397 . build_field( FID_PATRON_PWD, $patron_password );
400 sub build_patron_information_command_message {
403 my $transaction_date = $params->{transaction_date};
404 my $institution_id = $params->{institution_id};
405 my $patron_identifier = $params->{patron_identifier};
406 my $terminal_password = $params->{terminal_password};
407 my $patron_password = $params->{patron_password};
408 my $summary = $params->{summary};
417 . build_field( FID_INST_ID, $institution_id )
418 . build_field( FID_PATRON_ID, $patron_identifier )
419 . build_field( FID_TERMINAL_PWD, $terminal_password )
420 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } );
423 sub build_item_information_command_message {
426 my $transaction_date = $params->{transaction_date};
427 my $institution_id = $params->{institution_id};
428 my $item_identifier = $params->{item_identifier};
429 my $terminal_password = $params->{terminal_password};
435 . build_field( FID_INST_ID, $institution_id )
436 . build_field( FID_ITEM_ID, $item_identifier )
437 . build_field( FID_TERMINAL_PWD, $terminal_password );
440 sub build_checkout_command_message {
443 my $SC_renewal_policy = $params->{SC_renewal_policy} || 'N';
444 my $no_block = $params->{no_block} || 'N';
445 my $transaction_date = $params->{transaction_date};
446 my $nb_due_date = $params->{nb_due_date};
447 my $institution_id = $params->{institution_id};
448 my $patron_identifier = $params->{patron_identifier};
449 my $item_identifier = $params->{item_identifier};
450 my $terminal_password = $params->{terminal_password};
451 my $item_properties = $params->{item_properties};
452 my $patron_password = $params->{patron_password};
453 my $fee_acknowledged = $params->{fee_acknowledged} || 'N';
454 my $cancel = $params->{cancel} || 'N';
456 $SC_renewal_policy = $SC_renewal_policy eq 'Y' ? 'Y' : 'N';
457 $no_block = $no_block eq 'Y' ? 'Y' : 'N';
458 $fee_acknowledged = $fee_acknowledged eq 'Y' ? 'Y' : 'N';
459 $cancel = $cancel eq 'Y' ? 'Y' : 'N';
461 $nb_due_date ||= $transaction_date;
469 . build_field( FID_INST_ID, $institution_id )
470 . build_field( FID_PATRON_ID, $patron_identifier )
471 . build_field( FID_ITEM_ID, $item_identifier )
472 . build_field( FID_TERMINAL_PWD, $terminal_password )
473 . build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } )
474 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
475 . build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } )
476 . build_field( FID_CANCEL, $cancel, { optional => 1 } );
479 sub build_checkin_command_message {
482 my $no_block = $params->{no_block} || 'N';
483 my $transaction_date = $params->{transaction_date};
484 my $return_date = $params->{return_date};
485 my $current_location = $params->{current_location};
486 my $institution_id = $params->{institution_id};
487 my $item_identifier = $params->{item_identifier};
488 my $terminal_password = $params->{terminal_password};
489 my $item_properties = $params->{item_properties};
490 my $cancel = $params->{cancel} || 'N';
492 $no_block = $no_block eq 'Y' ? 'Y' : 'N';
493 $cancel = $cancel eq 'Y' ? 'Y' : 'N';
495 $return_date ||= $transaction_date;
502 . build_field( FID_CURRENT_LOCN, $current_location )
503 . build_field( FID_INST_ID, $institution_id )
504 . build_field( FID_ITEM_ID, $item_identifier )
505 . build_field( FID_TERMINAL_PWD, $terminal_password )
506 . build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } )
507 . build_field( FID_CANCEL, $cancel, { optional => 1 } );
510 sub build_hold_command_message {
513 my $hold_mode = $params->{hold_mode};
514 my $transaction_date = $params->{transaction_date};
515 my $expiration_date = $params->{expiration_date};
516 my $pickup_location = $params->{pickup_location};
517 my $hold_type = $params->{hold_type};
518 my $institution_id = $params->{institution_id};
519 my $patron_identifier = $params->{patron_identifier};
520 my $patron_password = $params->{patron_password};
521 my $item_identifier = $params->{item_identifier};
522 my $title_identifier = $params->{title_identifier};
523 my $terminal_password = $params->{terminal_password};
524 my $fee_acknowledged = $params->{fee_acknowledged} || 'N';
530 . build_field( FID_EXPIRATION, $expiration_date, { optional => 1 } )
531 . build_field( FID_PICKUP_LOCN, $pickup_location, { optional => 1 } )
532 . build_field( FID_HOLD_TYPE, $hold_type, { optional => 1 } )
533 . build_field( FID_INST_ID, $institution_id )
534 . build_field( FID_PATRON_ID, $patron_identifier )
535 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
536 . build_field( FID_ITEM_ID, $item_identifier, { optional => 1 } )
537 . build_field( FID_TITLE_ID, $title_identifier, { optional => 1 } )
538 . build_field( FID_TERMINAL_PWD, $terminal_password, { optional => 1 } )
539 . build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } );
542 sub build_renew_command_message {
545 my $third_party_allowed = $params->{third_party_allowed} || 'N';
546 my $no_block = $params->{no_block} || 'N';
547 my $transaction_date = $params->{transaction_date};
548 my $nb_due_date = $params->{nb_due_date};
549 my $institution_id = $params->{institution_id};
550 my $patron_identifier = $params->{patron_identifier};
551 my $patron_password = $params->{patron_password};
552 my $item_identifier = $params->{item_identifier};
553 my $title_identifier = $params->{title_identifier};
554 my $terminal_password = $params->{terminal_password};
555 my $item_properties = $params->{item_properties};
556 my $fee_acknowledged = $params->{fee_acknowledged} || 'N';
558 $third_party_allowed = $third_party_allowed eq 'Y' ? 'Y' : 'N';
559 $no_block = $no_block eq 'Y' ? 'Y' : 'N';
560 $fee_acknowledged = $fee_acknowledged eq 'Y' ? 'Y' : 'N';
562 $nb_due_date ||= $transaction_date;
566 . $third_party_allowed
570 . build_field( FID_INST_ID, $institution_id )
571 . build_field( FID_PATRON_ID, $patron_identifier )
572 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
573 . build_field( FID_ITEM_ID, $item_identifier )
574 . build_field( FID_TITLE_ID, $title_identifier )
575 . build_field( FID_TERMINAL_PWD, $terminal_password )
576 . build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } )
577 . build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } );
580 sub build_fee_paid_command_message {
583 my $transaction_date = $params->{transaction_date};
584 my $fee_type = $params->{fee_type} || '01';
585 my $payment_type = $params->{payment_type} || '00';
586 my $currency_type = $params->{currency_type} || 'USD';
587 my $fee_amount = $params->{fee_amount};
588 my $institution_id = $params->{location_code};
589 my $patron_identifier = $params->{patron_identifier};
590 my $terminal_password = $params->{terminal_password};
591 my $patron_password = $params->{patron_password};
592 my $fee_identifier = $params->{fee_identifier};
593 my $transaction_id = $params->{transaction_id};
601 . build_field( FID_FEE_AMT, $fee_amount )
602 . build_field( FID_INST_ID, $institution_id )
603 . build_field( FID_PATRON_ID, $patron_identifier )
604 . build_field( FID_TERMINAL_PWD, $terminal_password, { optional => 1 } )
605 . build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } )
606 . build_field( FID_FEE_ID, $fee_identifier, { optional => 1 } )
607 . build_field( FID_TRANSACTION_ID, $transaction_id, { optional => 1 } );
611 my ( $field_identifier, $value, $params ) = @_;
615 return q{} if ( $params->{optional} && !$value );
617 return $field_identifier . (($value) ? $value : '') . '|';
621 say q/sip_cli_emulator.pl - SIP command line emulator
623 Test a SIP2 service by sending patron status and patron
624 information requests.
627 sip_cli_emulator.pl [OPTIONS]
630 --help display help message
632 -a --address SIP server ip address or host name
633 -p --port SIP server port
635 -su --sip_user SIP server login username
636 -sp --sip_pass SIP server login password
638 -l --location SIP location code
640 --patron ILS patron cardnumber or username
641 --password ILS patron password
643 -s --summary Optionally define the patron information request summary field.
644 Please refer to the SIP2 protocol specification for details
646 --item ILS item identifier ( item barcode )
648 -t --terminator SIP2 message terminator, either CR, or CRLF
651 -fa --fee-acknowledged Sends a confirmation of checkout fee
653 --fee-type Fee type for Fee Paid message, defaults to '01'
654 --payment-type Payment type for Fee Paid message, default to '00'
655 --currency-type Currency type for Fee Paid message, defaults to 'USD'
656 --fee-amount Fee amount for Fee Paid message, required
657 --fee-identifier Fee identifier for Fee Paid message, optional
658 --transaction-id Transaction id for Fee Paid message, optional
659 --pickup-location Pickup location (branchcode) for Hold message, optional
660 --hold-mode Accepts "+" to add hold or "-" to cancel hold, defaults to +
661 -n --no-block Accepts "N" for standard operatoin, "Y" for no-block, defaults to "N"
663 -m --message SIP2 message to execute
665 Implemented Messages:
672 patron_status_request