4 # A Class for handing SIP messages
7 package C4::SIP::Sip::MsgType;
13 use C4::SIP::Sip qw(:all);
14 use C4::SIP::Sip::Constants qw(:all);
15 use C4::SIP::Sip::Checksum qw(verify_cksum);
19 use C4::Auth qw(&check_api_auth);
22 use Koha::Patron::Attributes;
25 use Koha::DateUtils qw( output_pref );
29 use vars qw(@ISA @EXPORT_OK);
31 use constant INVALID_CARD => 'Invalid cardnumber';
32 use constant INVALID_PW => 'Invalid password';
36 @EXPORT_OK = qw(handle login_core);
39 # Predeclare handler subroutines
40 use subs qw(handle_patron_status handle_checkout handle_checkin
41 handle_block_patron handle_sc_status handle_request_acs_resend
42 handle_login handle_patron_info handle_end_patron_session
43 handle_fee_paid handle_item_information handle_item_status_update
44 handle_patron_enable handle_hold handle_renew handle_renew_all);
47 # For the most part, Version 2.00 of the protocol just adds new
48 # variable fields, but sometimes it changes the fixed header.
50 # In general, if there's no '2.00' protocol entry for a handler, that's
51 # because 2.00 didn't extend the 1.00 version of the protocol. This will
52 # be handled by the module initialization code following the declaration,
53 # which goes through the handlers table and creates a '2.00' entry that
54 # points to the same place as the '1.00' entry. If there's a 2.00 entry
55 # but no 1.00 entry, then that means that it's a completely new service
56 # in 2.00, so 1.00 shouldn't recognize it.
59 (PATRON_STATUS_REQ) => {
60 name => "Patron Status Request",
61 handler => \&handle_patron_status,
66 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD) ],
72 handler => \&handle_checkout,
75 template => "A1A1A18A18",
77 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
80 template => "A1A1A18A18",
82 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_PATRON_PWD), (FID_FEE_ACK), (FID_CANCEL) ],
88 handler => \&handle_checkin,
91 template => "A1A18A18",
93 fields => [ (FID_CURRENT_LOCN), (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
96 template => "A1A18A18",
98 fields => [ (FID_CURRENT_LOCN), (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_CANCEL) ],
103 name => "Block Patron",
104 handler => \&handle_block_patron,
109 fields => [ (FID_INST_ID), (FID_BLOCKED_CARD_MSG), (FID_PATRON_ID), (FID_TERMINAL_PWD) ],
115 handler => \&handle_sc_status,
118 template => "A1A3A4",
124 (REQUEST_ACS_RESEND) => {
125 name => "Request ACS Resend",
126 handler => \&handle_request_acs_resend,
137 handler => \&handle_login,
142 fields => [ (FID_LOGIN_UID), (FID_LOGIN_PWD), (FID_LOCATION_CODE) ],
147 name => "Patron Info",
148 handler => \&handle_patron_info,
151 template => "A3A18A10",
153 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD), (FID_START_ITEM), (FID_END_ITEM) ],
157 (END_PATRON_SESSION) => {
158 name => "End Patron Session",
159 handler => \&handle_end_patron_session,
164 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD) ],
170 handler => \&handle_fee_paid,
173 template => "A18A2A2A3",
175 fields => [ (FID_FEE_AMT), (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD), (FID_FEE_ID), (FID_TRANSACTION_ID) ],
179 (ITEM_INFORMATION) => {
180 name => "Item Information",
181 handler => \&handle_item_information,
186 fields => [ (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
190 (ITEM_STATUS_UPDATE) => {
191 name => "Item Status Update",
192 handler => \&handle_item_status_update,
197 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS) ],
202 name => "Patron Enable",
203 handler => \&handle_patron_enable,
208 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD) ],
214 handler => \&handle_hold,
220 (FID_EXPIRATION), (FID_PICKUP_LOCN), (FID_HOLD_TYPE), (FID_INST_ID), (FID_PATRON_ID), (FID_PATRON_PWD),
221 (FID_ITEM_ID), (FID_TITLE_ID), (FID_TERMINAL_PWD), (FID_FEE_ACK)
228 handler => \&handle_renew,
231 template => "A1A1A18A18",
233 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_PATRON_PWD), (FID_ITEM_ID), (FID_TITLE_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_FEE_ACK) ],
239 handler => \&handle_renew_all,
244 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_PATRON_PWD), (FID_TERMINAL_PWD), (FID_FEE_ACK) ],
251 # Now, initialize some of the missing bits of %handlers
253 foreach my $i ( keys(%handlers) ) {
254 if ( !exists( $handlers{$i}->{protocol}->{2} ) ) {
255 $handlers{$i}->{protocol}->{2} = $handlers{$i}->{protocol}->{1};
260 my ( $class, $msg, $seqno ) = @_;
262 my $msgtag = substr( $msg, 0, 2 );
264 if ( $msgtag eq LOGIN ) {
266 # If the client is using the 2.00-style "Login" message
267 # to authenticate to the server, then we get the Login message
268 # _before_ the client has indicated that it supports 2.00, but
269 # it's using the 2.00 login process, so it must support 2.00.
270 $protocol_version = 2;
272 siplog( "LOG_DEBUG", "Sip::MsgType::new('%s', '%s...', '%s'): seq.no '%s', protocol %s", $class, substr( $msg, 0, 10 ), $msgtag, $seqno, $protocol_version );
274 # warn "SIP PROTOCOL: $protocol_version";
275 if ( !exists( $handlers{$msgtag} ) ) {
276 siplog( "LOG_WARNING", "new Sip::MsgType: Skipping message of unknown type '%s' in '%s'", $msgtag, $msg );
278 } elsif ( !exists( $handlers{$msgtag}->{protocol}->{$protocol_version} ) ) {
279 siplog( "LOG_WARNING", "new Sip::MsgType: Skipping message '%s' unsupported by protocol rev. '%d'", $msgtag, $protocol_version );
285 $self->{seqno} = $seqno;
286 $self->_initialize( substr( $msg, 2 ), $handlers{$msgtag} );
292 my ( $self, $msg, $control_block ) = @_;
294 my $proto = $control_block->{protocol}->{$protocol_version};
296 $self->{name} = $control_block->{name};
297 $self->{handler} = $control_block->{handler};
299 $self->{fields} = {};
300 $self->{fixed_fields} = [];
302 chomp($msg); # These four are probably unnecessary now.
307 foreach my $field ( @{ $proto->{fields} } ) {
308 $self->{fields}->{$field} = undef;
311 siplog( "LOG_DEBUG", "Sip::MsgType::_initialize('%s', '%s', '%s', '%s', ...)", $self->{name}, $msg, $proto->{template}, $proto->{template_len} );
313 $self->{fixed_fields} = [ unpack( $proto->{template}, $msg ) ]; # see http://perldoc.perl.org/5.8.8/functions/unpack.html
315 # Skip over the fixed fields and the split the rest of
316 # the message into fields based on the delimiter and parse them
317 foreach my $field ( split( quotemeta($field_delimiter), substr( $msg, $proto->{template_len} ) ) ) {
318 $fn = substr( $field, 0, 2 );
320 if ( !exists( $self->{fields}->{$fn} ) ) {
321 siplog( "LOG_WARNING", "Unsupported field '%s' in %s message '%s'", $fn, $self->{name}, $msg );
322 } elsif ( defined( $self->{fields}->{$fn} ) ) {
323 siplog( "LOG_WARNING", "Duplicate field '%s' (previous value '%s') in %s message '%s'", $fn, $self->{fields}->{$fn}, $self->{name}, $msg );
325 $self->{fields}->{$fn} = substr( $field, 2 );
333 my ( $msg, $server, $req ) = @_;
334 my $config = $server->{config};
337 # Set system preference overrides, first global, then account level
338 # Clear overrides from previous message handling first
339 foreach my $key ( keys %ENV ) {
340 delete $ENV{$key} if index($key, 'OVERRIDE_SYSPREF_') > 0;
342 foreach my $key ( keys %{ $config->{'syspref_overrides'} } ) {
343 $ENV{"OVERRIDE_SYSPREF_$key"} = $config->{'syspref_overrides'}->{$key};
345 foreach my $key ( keys %{ $server->{account}->{'syspref_overrides'} } ) {
346 $ENV{"OVERRIDE_SYSPREF_$key"} =
347 $server->{account}->{'syspref_overrides'}->{$key};
351 # What's the field delimiter for variable length fields?
352 # This can't be based on the account, since we need to know
353 # the field delimiter to parse a SIP login message
355 if ( defined( $server->{config}->{delimiter} ) ) {
356 $field_delimiter = $server->{config}->{delimiter};
359 # error detection is active if this is a REQUEST_ACS_RESEND
360 # message with a checksum, or if the message is long enough
361 # and the last nine characters begin with a sequence number
363 if ( $msg eq REQUEST_ACS_RESEND_CKSUM ) {
366 $error_detection = 1;
367 $self = C4::SIP::Sip::MsgType->new( (REQUEST_ACS_RESEND), 0 );
368 } elsif ( ( length($msg) > 11 ) && ( substr( $msg, -9, 2 ) eq "AY" ) ) {
369 $error_detection = 1;
371 if ( !verify_cksum($msg) ) {
372 siplog( "LOG_WARNING", "Checksum failed on message '%s'", $msg );
374 # REQUEST_SC_RESEND with error detection
375 $last_response = REQUEST_SC_RESEND_CKSUM;
376 print("$last_response\r");
377 return REQUEST_ACS_RESEND;
380 # Save the sequence number, then strip off the
381 # error detection data to process the message
382 $self = C4::SIP::Sip::MsgType->new( substr( $msg, 0, -9 ), substr( $msg, -7, 1 ) );
384 } elsif ($error_detection) {
386 # We received a non-ED message when ED is supposed to be active.
387 # Warn about this problem, then process the message anyway.
388 siplog( "LOG_WARNING", "Received message without error detection: '%s'", $msg );
389 $error_detection = 0;
390 $self = C4::SIP::Sip::MsgType->new( $msg, 0 );
392 $self = C4::SIP::Sip::MsgType->new( $msg, 0 );
395 if ( ( substr( $msg, 0, 2 ) ne REQUEST_ACS_RESEND )
397 && ( substr( $msg, 0, 2 ) ne $req ) ) {
398 return substr( $msg, 0, 2 );
400 unless ( $self->{handler} ) {
401 siplog( "LOG_WARNING", "No handler defined for '%s'", $msg );
402 $last_response = REQUEST_SC_RESEND;
403 print("$last_response\r");
404 return REQUEST_ACS_RESEND;
406 return ( $self->{handler}->( $self, $server ) ); # FIXME
407 # FIXME: Use of uninitialized value in subroutine entry
408 # Can't use string ("") as a subroutine ref while "strict refs" in use
416 # Patron status messages are produced in response to both
417 # "Request Patron Status" and "Block Patron"
419 # Request Patron Status requires a patron password, but
420 # Block Patron doesn't (since the patron may never have
421 # provided one before attempting some illegal action).
423 # ASSUMPTION: If the patron password field is present in the
424 # message, then it must match, otherwise incomplete patron status
425 # information will be returned to the terminal.
427 sub build_patron_status {
428 my ( $patron, $lang, $fields, $server ) = @_;
430 my $patron_pwd = $fields->{ (FID_PATRON_PWD) };
431 my $resp = (PATRON_STATUS_RESP);
436 $password_rc = $patron->check_password($patron_pwd);
439 $resp .= patron_status_string( $patron, $server );
440 $resp .= $lang . timestamp();
441 if ( defined $server->{account}->{ae_field_template} ) {
442 $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template}, $server ) );
444 $resp .= add_field( FID_PERSONAL_NAME, $patron->name, $server );
448 # while the patron ID we got from the SC is valid, let's
449 # use the one returned from the ILS, just in case...
450 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
452 if ( $protocol_version >= 2 ) {
453 $resp .= add_field( FID_VALID_PATRON, 'Y', $server );
455 # Patron password is a required field.
456 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool($password_rc), $server );
457 $resp .= maybe_add( FID_CURRENCY, $patron->currency, $server );
458 $resp .= maybe_add( FID_FEE_AMT, $patron->fee_amount, $server );
461 my $msg = $patron->screen_msg;
462 $msg .= ' -- '. INVALID_PW if $patron_pwd && !$password_rc;
463 $resp .= maybe_add( FID_SCREEN_MSG, $msg, $server );
465 $resp .= maybe_add( FID_SCREEN_MSG, $patron->{branchcode}, $server )
466 if ( $server->{account}->{send_patron_home_library_in_af} );
467 $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
469 $resp .= $patron->build_custom_field_string( $server );
470 $resp .= $patron->build_patron_attributes_string( $server );
473 # Invalid patron (cardnumber)
474 # Report that the user has no privs.
476 # no personal name, and is invalid (if we're using 2.00)
477 $resp .= 'YYYY' . ( ' ' x 10 ) . $lang . timestamp();
478 $resp .= add_field( FID_PERSONAL_NAME, '', $server );
480 # the patron ID is invalid, but it's a required field, so
482 $resp .= add_field( FID_PATRON_ID, $fields->{ (FID_PATRON_ID) }, $server );
484 ( $protocol_version >= 2 )
485 and $resp .= add_field( FID_VALID_PATRON, 'N', $server );
487 $resp .= maybe_add( FID_SCREEN_MSG, INVALID_CARD, $server );
490 $resp .= add_field( FID_INST_ID, $fields->{ (FID_INST_ID) }, $server );
494 sub handle_patron_status {
495 my ( $self, $server ) = @_;
496 my $ils = $server->{ils};
498 my $resp = (PATRON_STATUS_RESP);
499 my $account = $server->{account};
500 my ( $lang, $date ) = @{ $self->{fixed_fields} };
501 my $fields = $self->{fields};
503 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_patron_status" );
504 $patron = $ils->find_patron( $fields->{ (FID_PATRON_ID) } );
505 $resp = build_patron_status( $patron, $lang, $fields, $server );
506 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
507 return (PATRON_STATUS_REQ);
510 sub handle_checkout {
511 my ( $self, $server ) = @_;
512 my $account = $server->{account};
513 my $ils = $server->{ils};
514 my $inst = $ils->institution;
515 my ( $sc_renewal_policy, $no_block, $trans_date, $nb_due_date );
517 my ( $patron_id, $item_id, $status );
518 my ( $item, $patron );
521 ( $sc_renewal_policy, $no_block, $trans_date, $nb_due_date ) = @{ $self->{fixed_fields} };
522 $fields = $self->{fields};
524 $patron_id = $fields->{ (FID_PATRON_ID) };
525 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
526 $item_id = $fields->{ (FID_ITEM_ID) };
527 my $fee_ack = $fields->{ (FID_FEE_ACK) };
529 if ( $no_block eq 'Y' ) {
531 # Off-line transactions need to be recorded, but there's
532 # not a lot we can do about it
533 siplog( "LOG_WARNING", "received no-block checkout from terminal '%s'", $account->{id} );
535 $status = $ils->checkout( $patron_id, $item_id, $sc_renewal_policy, $fee_ack, $account, $nb_due_date );
538 # Does the transaction date really matter for items that are
539 # checkout out while the terminal is online? I'm guessing 'no'
540 $status = $ils->checkout( $patron_id, $item_id, $sc_renewal_policy, $fee_ack, $account );
543 $item = $status->item;
544 $patron = $status->patron;
548 # Item successfully checked out
550 $resp = CHECKOUT_RESP . '1';
551 $resp .= sipbool( $status->renew_ok );
552 if ( $ils->supports('magnetic media') ) {
553 $resp .= sipbool( $item->magnetic_media );
558 # We never return the obsolete 'U' value for 'desensitize'
571 # Now for the variable fields
572 $resp .= add_field( FID_INST_ID, $inst, $server );
573 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
574 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
575 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
576 if ( $item->due_date ) {
578 if( $account->{format_due_date} ){
579 $due_date = output_pref({ str => $item->due_date, as_due_date => 1 });
581 $due_date = timestamp( $item->due_date );
583 $resp .= add_field( FID_DUE_DATE, $due_date, $server );
585 $resp .= add_field( FID_DUE_DATE, q{}, $server );
588 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
589 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
591 if ( $protocol_version >= 2 ) {
592 if ( $ils->supports('security inhibit') ) {
593 $resp .= add_field( FID_SECURITY_INHIBIT, $status->security_inhibit, $server );
595 $resp .= maybe_add( FID_MEDIA_TYPE, $item->sip_media_type, $server );
596 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
604 # Checkout Response: not ok, no renewal, don't know mag. media,
606 $resp = sprintf( "120NUN%s", timestamp );
607 $resp .= add_field( FID_INST_ID, $inst, $server );
608 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
609 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
611 # If the item is valid, provide the title, otherwise
613 $resp .= add_field( FID_TITLE_ID, $item ? $item->title_id : '', $server );
615 # Due date is required. Since it didn't get checked out,
616 # it's not due, so leave the date blank
617 $resp .= add_field( FID_DUE_DATE, '', $server );
619 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
620 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
622 if ( $protocol_version >= 2 ) {
624 # Is the patron ID valid?
625 $resp .= add_field( FID_VALID_PATRON, sipbool($patron), $server );
627 if ( $patron && exists( $fields->{FID_PATRON_PWD} ) ) {
629 # Password provided, so we can tell if it was valid or not
630 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password( $fields->{ (FID_PATRON_PWD) } ) ), $server );
635 $resp .= $item->build_additional_item_fields_string( $server ) if $item;
637 if ( $protocol_version >= 2 ) {
639 # Financials : return irrespective of ok status
640 if ( $status->fee_amount ) {
641 $resp .= add_field( FID_FEE_AMT, $status->fee_amount, $server );
642 $resp .= maybe_add( FID_CURRENCY, $status->sip_currency, $server );
643 $resp .= maybe_add( FID_FEE_TYPE, $status->sip_fee_type, $server );
644 $resp .= maybe_add( FID_TRANSACTION_ID, $status->transaction_id, $server );
648 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
653 my ( $self, $server ) = @_;
654 my $account = $server->{account};
655 my $ils = $server->{ils};
656 my $my_branch = $ils->institution;
657 my ( $current_loc, $inst_id, $item_id, $terminal_pwd, $item_props, $cancel );
658 my ( $patron, $item, $status );
659 my $resp = CHECKIN_RESP;
660 my ( $no_block, $trans_date, $return_date ) = @{ $self->{fixed_fields} };
661 my $fields = $self->{fields};
663 $current_loc = $fields->{ (FID_CURRENT_LOCN) };
664 $inst_id = $fields->{ (FID_INST_ID) };
665 $item_id = $fields->{ (FID_ITEM_ID) };
666 $item_props = $fields->{ (FID_ITEM_PROPS) };
667 $cancel = $fields->{ (FID_CANCEL) };
669 $my_branch = $current_loc; # most scm do not set $current_loc
672 $ils->check_inst_id( $inst_id, "handle_checkin" );
674 if ( $no_block eq 'Y' ) {
676 # Off-line transactions, ick.
677 siplog( "LOG_WARNING", "received no-block checkin from terminal '%s'", $account->{id} );
678 $status = $ils->checkin_no_block( $item_id, $trans_date, $return_date, $item_props, $cancel );
680 $status = $ils->checkin( $item_id, $trans_date, $return_date, $my_branch, $item_props, $cancel, $account );
683 $patron = $status->patron;
684 $item = $status->item;
686 $resp .= $status->ok ? '1' : '0';
687 $resp .= $status->resensitize ? 'Y' : 'N';
688 if ( $item && $ils->supports('magnetic media') ) {
689 $resp .= sipbool( $item->magnetic_media );
692 # item barcode is invalid or system doesn't support 'magnetic media' indicator
696 $resp .= $status->alert ? 'Y' : 'N';
698 $resp .= add_field( FID_INST_ID, $inst_id, $server );
699 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
702 $resp .= add_field( FID_PERM_LOCN, $item->permanent_location, $server );
703 $resp .= maybe_add( FID_TITLE_ID, $item->title_id, $server );
704 $resp .= $item->build_additional_item_fields_string( $server );
707 if ( $protocol_version >= 2 ) {
708 $resp .= maybe_add( FID_SORT_BIN, $status->sort_bin, $server );
710 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
713 $resp .= maybe_add( FID_MEDIA_TYPE, $item->sip_media_type, $server );
714 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
715 $resp .= maybe_add( FID_CALL_NUMBER, $item->call_number, $server );
716 $resp .= maybe_add( FID_HOLD_PATRON_ID, $item->hold_patron_bcode, $server );
717 $resp .= add_field( FID_DESTINATION_LOCATION, $item->destination_loc, $server ) if ( $item->destination_loc || $server->{account}->{ct_always_send} );
718 $resp .= maybe_add( FID_HOLD_PATRON_NAME, $item->hold_patron_name( $server->{account}->{da_field_template} ), $server );
720 if ( my $CR = $server->{account}->{cr_item_field} ) {
721 $resp .= maybe_add( FID_COLLECTION_CODE, $item->{$CR}, $server );
723 $resp .= maybe_add( FID_COLLECTION_CODE, $item->collection_code, $server );
726 if ( $status->hold and $status->hold->{branchcode} ne $item->destination_loc ) {
727 warn 'SIP hold mismatch: $status->hold->{branchcode}=' . $status->hold->{branchcode} . '; $item->destination_loc=' . $item->destination_loc;
729 # just me being paranoid.
734 if ( $status->alert && $status->alert_type ) {
735 $resp .= maybe_add( FID_ALERT_TYPE, $status->alert_type, $server );
736 } elsif ( $server->{account}->{cv_send_00_on_success} ) {
737 $resp .= add_field( FID_ALERT_TYPE, '00', $server );
739 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
740 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
742 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
747 sub handle_block_patron {
748 my ( $self, $server ) = @_;
749 my $account = $server->{account};
750 my $ils = $server->{ils};
751 my ( $card_retained, $trans_date );
752 my ( $inst_id, $blocked_card_msg, $patron_id, $terminal_pwd );
753 my ( $fields, $resp, $patron );
755 ( $card_retained, $trans_date ) = @{ $self->{fixed_fields} };
756 $fields = $self->{fields};
757 $inst_id = $fields->{ (FID_INST_ID) };
758 $blocked_card_msg = $fields->{ (FID_BLOCKED_CARD_MSG) };
759 $patron_id = $fields->{ (FID_PATRON_ID) };
760 $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
762 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
764 # Terminal passwords are different from account login
765 # passwords, but I have no idea what to do with them. So,
766 # I'll just ignore them for now.
770 $ils->check_inst_id( $inst_id, "block_patron" );
771 $patron = $ils->find_patron($patron_id);
773 # The correct response for a "Block Patron" message is a
774 # "Patron Status Response", so use that handler to generate
775 # the message, but then return the correct code from here.
777 # Normally, the language is provided by the "Patron Status"
778 # fixed field, but since we're not responding to one of those
779 # we'll just say, "Unspecified", as per the spec. Let the
780 # terminal default to something that, one hopes, will be
785 $patron->block( $card_retained, $blocked_card_msg );
788 $resp = build_patron_status( $patron, $patron->language, $fields, $server );
789 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
790 return (BLOCK_PATRON);
793 sub handle_sc_status {
794 my ( $self, $server ) = @_;
795 ($server) or warn "handle_sc_status error: no \$server argument received.";
796 my ( $status, $print_width, $sc_protocol_version ) = @{ $self->{fixed_fields} };
799 if ( $sc_protocol_version =~ /^1\./ ) {
801 } elsif ( $sc_protocol_version =~ /^2\./ ) {
804 siplog( "LOG_WARNING", "Unrecognized protocol revision '%s', falling back to '1'", $sc_protocol_version );
808 if ( $new_proto != $protocol_version ) {
809 siplog( "LOG_INFO", "Setting protocol level to $new_proto" );
810 $protocol_version = $new_proto;
813 if ( $status == SC_STATUS_PAPER ) {
814 siplog( "LOG_WARNING", "Self-Check unit '%s@%s' out of paper", $self->{account}->{id}, $self->{account}->{institution} );
815 } elsif ( $status == SC_STATUS_SHUTDOWN ) {
816 siplog( "LOG_WARNING", "Self-Check unit '%s@%s' shutting down", $self->{account}->{id}, $self->{account}->{institution} );
819 $self->{account}->{print_width} = $print_width;
820 return ( send_acs_status( $self, $server ) ? SC_STATUS : '' );
823 sub handle_request_acs_resend {
824 my ( $self, $server ) = @_;
826 if ( !$last_response ) {
828 # We haven't sent anything yet, so respond with a
829 # REQUEST_SC_RESEND msg (p. 16)
830 $self->write_msg( REQUEST_SC_RESEND, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
831 } elsif ( ( length($last_response) < 9 )
832 || substr( $last_response, -9, 2 ) ne 'AY' ) {
834 # When resending a message, we aren't supposed to include
835 # a sequence number, even if the original had one (p. 4).
836 # If the last message didn't have a sequence number, then
837 # we can just send it.
838 print("$last_response\r"); # not write_msg?
841 # Cut out the sequence number and checksum, since the old
842 # checksum is wrong for the resent message.
843 my $rebuilt = substr( $last_response, 0, -9 );
844 $self->write_msg( $rebuilt, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
847 return REQUEST_ACS_RESEND;
851 my $server = shift or return;
854 my $status = 1; # Assume it all works
855 if ( !exists( $server->{config}->{accounts}->{$uid} ) ) {
856 siplog( "LOG_WARNING", "MsgType::login_core: Unknown login '$uid'" );
858 } elsif ( $server->{config}->{accounts}->{$uid}->{password} ne $pwd ) {
859 siplog( "LOG_WARNING", "MsgType::login_core: Invalid password for login '$uid'" );
863 # Store the active account someplace handy for everybody else to find.
864 $server->{account} = $server->{config}->{accounts}->{$uid};
865 my $inst = $server->{account}->{institution};
866 $server->{institution} = $server->{config}->{institutions}->{$inst};
867 $server->{policy} = $server->{institution}->{policy};
868 $server->{sip_username} = $uid;
869 $server->{sip_password} = $pwd;
871 my $auth_status = api_auth( $uid, $pwd, $inst );
872 if ( !$auth_status or $auth_status !~ /^ok$/i ) {
873 siplog( "LOG_WARNING", "api_auth failed for SIP terminal '%s' of '%s': %s", $uid, $inst, ( $auth_status || 'unknown' ) );
876 siplog( "LOG_INFO", "Successful login/auth for '%s' of '%s'", $server->{account}->{id}, $inst );
879 # initialize connection to ILS
881 my $module = $server->{config}->{institutions}->{$inst}->{implementation};
882 siplog( "LOG_DEBUG", 'login_core: ' . Dumper($module) );
884 # Suspect this is always ILS but so we don't break any eccentic install (for now)
885 if ( $module eq 'ILS' ) {
886 $module = 'C4::SIP::ILS';
890 siplog( "LOG_ERR", "%s: Loading ILS implementation '%s' for institution '%s' failed", $server->{service}, $module, $inst );
891 die("Failed to load ILS implementation '$module' for $inst");
894 # like ILS->new(), I think.
895 $server->{ils} = $module->new( $server->{institution}, $server->{account} );
896 if ( !$server->{ils} ) {
897 siplog( "LOG_ERR", "%s: ILS connection to '%s' failed", $server->{service}, $inst );
898 die("Unable to connect to ILS '$inst'");
906 my ( $self, $server ) = @_;
907 my ( $uid_algorithm, $pwd_algorithm );
911 my $status = 1; # Assume it all works
913 $fields = $self->{fields};
914 ( $uid_algorithm, $pwd_algorithm ) = @{ $self->{fixed_fields} };
916 $uid = $fields->{ (FID_LOGIN_UID) }; # Terminal ID, not patron ID.
917 $pwd = $fields->{ (FID_LOGIN_PWD) }; # Terminal PWD, not patron PWD.
919 if ( $uid_algorithm || $pwd_algorithm ) {
920 siplog( "LOG_ERR", "LOGIN: Unsupported non-zero encryption method(s): uid = $uid_algorithm, pwd = $pwd_algorithm" );
923 $status = login_core( $server, $uid, $pwd );
926 $self->write_msg( LOGIN_RESP . $status, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
927 return $status ? LOGIN : '';
931 # Build the detailed summary information for the Patron
932 # Information Response message based on the first 'Y' that appears
933 # in the 'summary' field of the Patron Information request. The
934 # specification says that only one 'Y' can appear in that field,
935 # and we're going to believe it.
938 my ( $ils, $patron, $summary, $start, $end, $server ) = @_;
942 # Map from offsets in the "summary" field of the Patron Information
943 # message to the corresponding field and handler
946 { func => $patron->can("hold_items"), fid => FID_HOLD_ITEMS },
947 { func => $patron->can("overdue_items"), fid => FID_OVERDUE_ITEMS },
948 { func => $patron->can("charged_items"), fid => FID_CHARGED_ITEMS },
949 { func => $patron->can("fine_items"), fid => FID_FINE_ITEMS },
950 { func => $patron->can("recall_items"), fid => FID_RECALL_ITEMS },
951 { func => $patron->can("unavail_holds"), fid => FID_UNAVAILABLE_HOLD_ITEMS },
954 my $summary_type = index( $summary, 'Y' );
955 return q{} if $summary_type == -1; # No detailed information required.
956 return q{} if $summary_type > 5; # Positions 6-9 are not defined in the sip spec,
957 # and we have no extensions to handle them.
959 siplog( "LOG_DEBUG", "Summary_info: index == '%d', field '%s'", $summary_type, $summary_map[$summary_type]->{fid} );
961 my $func = $summary_map[$summary_type]->{func};
962 my $fid = $summary_map[$summary_type]->{fid};
963 my $itemlist = &$func( $patron, $start, $end, $server );
965 siplog( "LOG_DEBUG", "summary_info: list = (%s)", join( ", ", map{ $_->{barcode} } @{$itemlist} ) );
966 foreach my $i ( @{$itemlist} ) {
967 $resp .= add_field( $fid, $i->{barcode}, $server );
973 sub handle_patron_info {
974 my ( $self, $server ) = @_;
975 my $ils = $server->{ils};
976 my ( $lang, $trans_date, $summary ) = @{ $self->{fixed_fields} };
977 my $fields = $self->{fields};
978 my ( $inst_id, $patron_id, $terminal_pwd, $patron_pwd, $start, $end );
979 my ( $resp, $patron );
981 $inst_id = $fields->{ (FID_INST_ID) };
982 $patron_id = $fields->{ (FID_PATRON_ID) };
983 $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
984 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
985 $start = $fields->{ (FID_START_ITEM) };
986 $end = $fields->{ (FID_END_ITEM) };
988 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
990 $patron = $ils->find_patron($patron_id);
992 $resp = (PATRON_INFO_RESP);
994 $patron->update_lastseen();
995 $resp .= patron_status_string( $patron, $server );
996 $resp .= ( defined($lang) and length($lang) == 3 ) ? $lang : $patron->language;
997 $resp .= timestamp();
999 $resp .= add_count( 'patron_info/hold_items', scalar @{ $patron->hold_items } );
1000 $resp .= add_count( 'patron_info/overdue_items', scalar @{ $patron->overdue_items } );
1001 $resp .= add_count( 'patron_info/charged_items', scalar @{ $patron->charged_items } );
1002 $resp .= add_count( 'patron_info/fine_items', scalar @{ $patron->fine_items } );
1003 $resp .= add_count( 'patron_info/recall_items', scalar @{ $patron->recall_items } );
1004 $resp .= add_count( 'patron_info/unavail_holds', scalar @{ $patron->unavail_holds } );
1006 $resp .= add_field( FID_INST_ID, ( $ils->institution_id || 'SIP2' ), $server );
1008 # while the patron ID we got from the SC is valid, let's
1009 # use the one returned from the ILS, just in case...
1010 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
1011 if ( defined $server->{account}->{ae_field_template} ) {
1012 $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ), $server );
1014 $resp .= add_field( FID_PERSONAL_NAME, $patron->name, $server );
1017 # TODO: add code for the fields
1019 # overdue items limit
1020 # charged items limit
1022 $resp .= add_field( FID_VALID_PATRON, 'Y', $server );
1024 if ( defined($patron_pwd) ) {
1026 # If patron password was provided, report whether it was right or not.
1027 if ( $patron_pwd eq q{} && $server->{account}->{allow_empty_passwords} ) {
1030 $password_rc = $patron->check_password($patron_pwd);
1032 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $password_rc ), $server );
1035 $resp .= maybe_add( FID_CURRENCY, $patron->currency, $server );
1036 $resp .= maybe_add( FID_FEE_AMT, $patron->fee_amount, $server );
1037 $resp .= add_field( FID_FEE_LMT, $patron->fee_limit, $server );
1039 # TODO: zero or more item details for 2.0 can go here:
1046 $resp .= summary_info( $ils, $patron, $summary, $start, $end, $server );
1048 $resp .= maybe_add( FID_HOME_ADDR, $patron->address, $server );
1049 $resp .= maybe_add( FID_EMAIL, $patron->email_addr, $server );
1050 $resp .= maybe_add( FID_HOME_PHONE, $patron->home_phone, $server );
1052 # SIP 2.0 extensions used by Envisionware
1053 # Other terminals will ignore unrecognized fields (unrecognized field identifiers)
1054 $resp .= maybe_add( FID_PATRON_BIRTHDATE, $patron->birthdate, $server );
1055 $resp .= maybe_add( FID_PATRON_CLASS, $patron->ptype, $server );
1057 # Custom protocol extension to report patron internet privileges
1058 $resp .= maybe_add( FID_INET_PROFILE, $patron->inet_privileges, $server );
1060 my $msg = $patron->screen_msg;
1061 if( defined( $patron_pwd ) && !$password_rc ) {
1062 $msg .= ' -- ' . INVALID_PW;
1064 $resp .= maybe_add( FID_SCREEN_MSG, $msg, $server );
1065 if ( $server->{account}->{send_patron_home_library_in_af} ) {
1066 $resp .= maybe_add( FID_SCREEN_MSG, $patron->{branchcode}, $server);
1068 $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
1070 $resp .= $patron->build_custom_field_string( $server );
1071 $resp .= $patron->build_patron_attributes_string( $server );
1074 # Invalid patron ID:
1075 # no privileges, no items associated,
1076 # no personal name, and is invalid (if we're using 2.00)
1077 $resp .= 'YYYY' . ( ' ' x 10 ) . $lang . timestamp();
1078 $resp .= '0000' x 6;
1080 $resp .= add_field( FID_INST_ID, ( $ils->institution_id || 'SIP2' ), $server );
1082 # patron ID is invalid, but field is required, so just echo it back
1083 $resp .= add_field( FID_PATRON_ID, $fields->{ (FID_PATRON_ID) }, $server );
1084 $resp .= add_field( FID_PERSONAL_NAME, '', $server );
1086 if ( $protocol_version >= 2 ) {
1087 $resp .= add_field( FID_VALID_PATRON, 'N', $server );
1089 $resp .= maybe_add( FID_SCREEN_MSG, INVALID_CARD, $server );
1092 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1093 return (PATRON_INFO);
1096 sub handle_end_patron_session {
1097 my ( $self, $server ) = @_;
1098 my $ils = $server->{ils};
1100 my $fields = $self->{fields};
1101 my $resp = END_SESSION_RESP;
1102 my ( $status, $screen_msg, $print_line );
1104 ($trans_date) = @{ $self->{fixed_fields} };
1106 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, 'handle_end_patron_session' );
1108 ( $status, $screen_msg, $print_line ) = $ils->end_patron_session( $fields->{ (FID_PATRON_ID) } );
1110 $resp .= $status ? 'Y' : 'N';
1111 $resp .= timestamp();
1113 $resp .= add_field( FID_INST_ID, $server->{ils}->institution, $server );
1114 $resp .= add_field( FID_PATRON_ID, $fields->{ (FID_PATRON_ID) }, $server );
1116 $resp .= maybe_add( FID_SCREEN_MSG, $screen_msg, $server );
1117 $resp .= maybe_add( FID_PRINT_LINE, $print_line, $server );
1119 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1121 return (END_PATRON_SESSION);
1124 sub handle_fee_paid {
1125 my ( $self, $server ) = @_;
1126 my $ils = $server->{ils};
1127 my ( $trans_date, $fee_type, $pay_type, $currency ) = @{ $self->{fixed_fields} };
1128 my $fields = $self->{fields};
1129 my ( $fee_amt, $inst_id, $patron_id, $terminal_pwd, $patron_pwd );
1130 my ( $fee_id, $trans_id );
1132 my $resp = FEE_PAID_RESP;
1134 my $disallow_overpayment = $server->{account}->{disallow_overpayment};
1135 my $payment_type_writeoff = $server->{account}->{payment_type_writeoff} || q{};
1136 my $register_id = $server->{account}->{register_id};
1138 my $is_writeoff = $pay_type eq $payment_type_writeoff;
1140 $fee_amt = $fields->{ (FID_FEE_AMT) };
1141 $inst_id = $fields->{ (FID_INST_ID) };
1142 $patron_id = $fields->{ (FID_PATRON_ID) };
1143 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1144 $fee_id = $fields->{ (FID_FEE_ID) };
1145 $trans_id = $fields->{ (FID_TRANSACTION_ID) };
1147 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1149 $ils->check_inst_id( $inst_id, "handle_fee_paid" );
1151 my $pay_result = $ils->pay_fee( $patron_id, $patron_pwd, $fee_amt, $fee_type, $pay_type, $fee_id, $trans_id, $currency, $is_writeoff, $disallow_overpayment, $register_id );
1152 $status = $pay_result->{status};
1153 my $pay_response = $pay_result->{pay_response};
1156 "no_item" => "No matching item could be found",
1157 "no_checkout" => "Item is not checked out",
1158 "too_soon" => "Cannot yet be renewed",
1159 "too_many" => "Renewed the maximum number of times",
1160 "auto_too_soon" => "Scheduled for automatic renewal and cannot yet be renewed",
1161 "auto_too_late" => "Scheduled for automatic renewal and cannot yet be any more",
1162 "auto_account_expired" => "Scheduled for automatic renewal and cannot be renewed because the patron's account has expired",
1163 "auto_renew" => "Scheduled for automatic renewal",
1164 "auto_too_much_oweing" => "Scheduled for automatic renewal",
1165 "on_reserve" => "On hold for another patron",
1166 "patron_restricted" => "Patron is currently restricted",
1167 "item_denied_renewal" => "Item is not allowed renewal",
1168 "onsite_checkout" => "Item is an onsite checkout"
1172 foreach my $result( @{$pay_response->{renew_result}} ) {
1173 my $item = Koha::Items->find({ itemnumber => $result->{itemnumber} });
1174 if ($result->{success}) {
1175 push @success, '"' . $item->biblio->title . '"';
1177 push @fail, '"' . $item->biblio->title . '" : ' . $failmap->{$result->{error}};
1182 if (scalar @success > 0) {
1183 $msg.="The following items were renewed: " . join(", ", @success) . ". ";
1185 if (scalar @fail > 0) {
1186 $msg.="The following items were not renewed: " . join(", ", @fail) . ".";
1188 if (length $msg > 0) {
1189 $status->screen_msg($status->screen_msg . " $msg");
1192 $resp .= ( $status->ok ? 'Y' : 'N' ) . timestamp;
1193 $resp .= add_field( FID_INST_ID, $inst_id, $server );
1194 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
1195 $resp .= maybe_add( FID_TRANSACTION_ID, $status->transaction_id, $server );
1196 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1197 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1199 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1204 sub handle_item_information {
1205 my ( $self, $server ) = @_;
1206 my $ils = $server->{ils};
1208 my $fields = $self->{fields};
1209 my $resp = ITEM_INFO_RESP;
1213 ($trans_date) = @{ $self->{fixed_fields} };
1215 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_item_information" );
1217 $item = $ils->find_item( $fields->{ (FID_ITEM_ID) } );
1219 if ( !defined($item) ) {
1222 # "Other" circ stat, "Other" security marker, "Unknown" fee type
1226 # Just echo back the invalid item id
1227 $resp .= add_field( FID_ITEM_ID, $fields->{ (FID_ITEM_ID) }, $server );
1229 # title id is required, but we don't have one
1230 $resp .= add_field( FID_TITLE_ID, '', $server );
1233 # Valid Item ID, send the good stuff
1234 my $circulation_status = $item->sip_circulation_status;
1235 $resp .= $circulation_status;
1236 $resp .= $item->sip_security_marker;
1237 $resp .= $item->sip_fee_type;
1240 if ( $circulation_status eq '01' ) {
1241 $resp .= maybe_add( FID_SCREEN_MSG, "Item is damaged", $server );
1244 $resp .= add_field( FID_ITEM_ID, $item->id, $server );
1245 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
1247 $resp .= maybe_add( FID_MEDIA_TYPE, $item->sip_media_type, $server );
1248 $resp .= maybe_add( FID_PERM_LOCN, $item->permanent_location, $server );
1249 $resp .= maybe_add( FID_CURRENT_LOCN, $item->current_location, $server );
1250 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
1253 if ( my $CR = $server->{account}->{cr_item_field} ) {
1254 $resp .= maybe_add( FID_COLLECTION_CODE, $item->{$CR}, $server );
1256 $resp .= maybe_add( FID_COLLECTION_CODE, $item->collection_code, $server );
1259 if ( ( $i = $item->fee ) != 0 ) {
1260 $resp .= add_field( FID_CURRENCY, $item->fee_currency, $server );
1261 $resp .= add_field( FID_FEE_AMT, $i, $server );
1263 $resp .= maybe_add( FID_OWNER, $item->owner, $server );
1265 if ( ( $i = scalar @{ $item->hold_queue } ) > 0 ) {
1266 $resp .= add_field( FID_HOLD_QUEUE_LEN, $i, $server );
1268 if ( $item->due_date ) {
1269 $resp .= add_field( FID_DUE_DATE, timestamp( $item->due_date ), $server );
1271 if ( ( $i = $item->recall_date ) != 0 ) {
1272 $resp .= add_field( FID_RECALL_DATE, timestamp($i), $server );
1274 if ( ( $i = $item->hold_pickup_date ) != 0 ) {
1275 $resp .= add_field( FID_HOLD_PICKUP_DATE, timestamp($i), $server );
1278 $resp .= maybe_add( FID_SCREEN_MSG, $item->screen_msg, $server );
1279 $resp .= maybe_add( FID_PRINT_LINE, $item->print_line, $server );
1281 $resp .= $item->build_additional_item_fields_string( $server );
1282 $resp .= $item->build_custom_field_string( $server );
1285 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1287 return (ITEM_INFORMATION);
1290 sub handle_item_status_update {
1291 my ( $self, $server ) = @_;
1292 my $ils = $server->{ils};
1293 my ( $trans_date, $item_id, $terminal_pwd, $item_props );
1294 my $fields = $self->{fields};
1297 my $resp = ITEM_STATUS_UPDATE_RESP;
1299 ($trans_date) = @{ $self->{fixed_fields} };
1301 $ils->check_inst_id( $fields->{ (FID_INST_ID) } );
1303 $item_id = $fields->{ (FID_ITEM_ID) };
1304 $item_props = $fields->{ (FID_ITEM_PROPS) };
1306 if ( !defined($item_id) ) {
1307 siplog( "LOG_WARNING", "handle_item_status: received message without Item ID field" );
1309 $item = $ils->find_item($item_id);
1317 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
1322 $status = $item->status_update($item_props);
1324 $resp .= $status->ok ? '1' : '0';
1327 $resp .= add_field( FID_ITEM_ID, $item->id, $server );
1328 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
1329 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
1332 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1333 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1335 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1337 return (ITEM_STATUS_UPDATE);
1340 sub handle_patron_enable {
1341 my ( $self, $server ) = @_;
1342 my $ils = $server->{ils};
1343 my $fields = $self->{fields};
1344 my ( $trans_date, $patron_id, $terminal_pwd, $patron_pwd );
1345 my ( $status, $patron );
1346 my $resp = PATRON_ENABLE_RESP;
1348 ($trans_date) = @{ $self->{fixed_fields} };
1349 $patron_id = $fields->{ (FID_PATRON_ID) };
1350 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1352 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1354 siplog( "LOG_DEBUG", "handle_patron_enable: patron_id: '%s', patron_pwd: '%s'", $patron_id, $patron_pwd );
1356 $patron = $ils->find_patron($patron_id);
1358 if ( !defined($patron) ) {
1361 $resp .= 'YYYY' . ( ' ' x 10 ) . '000' . timestamp();
1362 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
1363 $resp .= add_field( FID_PERSONAL_NAME, '', $server );
1364 $resp .= add_field( FID_VALID_PATRON, 'N', $server );
1365 $resp .= add_field( FID_VALID_PATRON_PWD, 'N', $server );
1369 if ( !defined($patron_pwd) || $patron->check_password($patron_pwd) ) {
1371 # Don't enable the patron if there was an invalid password
1372 $status = $patron->enable;
1374 $resp .= patron_status_string( $patron, $server );
1375 $resp .= $patron->language . timestamp();
1377 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
1378 $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ), $server );
1379 if ( defined($patron_pwd) ) {
1380 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password($patron_pwd) ), $server );
1382 $resp .= add_field( FID_VALID_PATRON, 'Y', $server );
1383 $resp .= maybe_add( FID_SCREEN_MSG, $patron->screen_msg, $server );
1384 $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
1387 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1389 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1391 return (PATRON_ENABLE);
1395 my ( $self, $server ) = @_;
1396 my $ils = $server->{ils};
1397 my ( $hold_mode, $trans_date );
1398 my ( $expiry_date, $pickup_locn, $hold_type, $patron_id, $patron_pwd );
1399 my ( $item_id, $title_id, $fee_ack );
1400 my $fields = $self->{fields};
1402 my $resp = HOLD_RESP;
1404 ( $hold_mode, $trans_date ) = @{ $self->{fixed_fields} };
1406 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_hold" );
1408 $patron_id = $fields->{ (FID_PATRON_ID) };
1409 $expiry_date = $fields->{ (FID_EXPIRATION) } || '';
1410 $pickup_locn = $fields->{ (FID_PICKUP_LOCN) } || '';
1411 $hold_type = $fields->{ (FID_HOLD_TYPE) } || '2'; # Any copy of title
1412 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1413 $item_id = $fields->{ (FID_ITEM_ID) } || '';
1414 $title_id = $fields->{ (FID_TITLE_ID) } || '';
1415 $fee_ack = $fields->{ (FID_FEE_ACK) } || 'N';
1417 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1419 if ( $hold_mode eq '+' ) {
1420 $status = $ils->add_hold( $patron_id, $patron_pwd, $item_id, $title_id, $expiry_date, $pickup_locn, $hold_type, $fee_ack );
1421 } elsif ( $hold_mode eq '-' ) {
1422 $status = $ils->cancel_hold( $patron_id, $patron_pwd, $item_id, $title_id );
1423 } elsif ( $hold_mode eq '*' ) {
1424 $status = $ils->alter_hold( $patron_id, $patron_pwd, $item_id, $title_id, $expiry_date, $pickup_locn, $hold_type, $fee_ack );
1426 siplog( "LOG_WARNING", "handle_hold: Unrecognized hold mode '%s' from terminal '%s'", $hold_mode, $server->{account}->{id} );
1427 $status = $ils->Transaction::Hold; # new?
1428 $status->screen_msg("System error. Please contact library staff.");
1431 $resp .= $status->ok;
1432 $resp .= sipbool( $status->item && $status->item->available($patron_id) );
1435 if ( $status->ok ) {
1436 $resp .= add_field( FID_PATRON_ID, $status->patron->id, $server );
1438 ( $status->expiration_date )
1439 and $resp .= maybe_add( FID_EXPIRATION, timestamp( $status->expiration_date ), $server );
1440 $resp .= maybe_add( FID_QUEUE_POS, $status->queue_position, $server );
1441 $resp .= maybe_add( FID_PICKUP_LOCN, $status->pickup_location, $server );
1442 $resp .= maybe_add( FID_ITEM_ID, $status->item->id, $server );
1443 $resp .= maybe_add( FID_TITLE_ID, $status->item->title_id, $server );
1446 # Not ok. still need required fields
1447 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
1450 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1451 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1452 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1454 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1460 my ( $self, $server ) = @_;
1461 my $ils = $server->{ils};
1462 my ( $third_party, $no_block, $trans_date, $nb_due_date );
1463 my ( $patron_id, $patron_pwd, $item_id, $title_id, $item_props, $fee_ack );
1464 my $fields = $self->{fields};
1466 my ( $patron, $item );
1467 my $resp = RENEW_RESP;
1469 ( $third_party, $no_block, $trans_date, $nb_due_date ) = @{ $self->{fixed_fields} };
1471 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_renew" );
1473 if ( $no_block eq 'Y' ) {
1474 siplog( "LOG_WARNING", "handle_renew: received 'no block' renewal from terminal '%s'", $server->{account}->{id} );
1477 $patron_id = $fields->{ (FID_PATRON_ID) };
1478 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1479 $item_id = $fields->{ (FID_ITEM_ID) };
1480 $title_id = $fields->{ (FID_TITLE_ID) };
1481 $item_props = $fields->{ (FID_ITEM_PROPS) };
1482 $fee_ack = $fields->{ (FID_FEE_ACK) };
1484 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1486 $status = $ils->renew( $patron_id, $patron_pwd, $item_id, $title_id, $no_block, $nb_due_date, $third_party, $item_props, $fee_ack );
1488 $patron = $status->patron;
1489 $item = $status->item;
1491 if ( $status->renewal_ok ) {
1493 $resp .= $status->renewal_ok ? 'Y' : 'N';
1494 if ( $ils->supports('magnetic media') ) {
1495 $resp .= sipbool( $item->magnetic_media );
1499 $resp .= sipbool( desensitize( { status => $status, patron => $patron, server => $server } ) );
1501 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
1502 $resp .= add_field( FID_ITEM_ID, $item->id, $server );
1503 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
1504 if ( $item->due_date ) {
1505 $resp .= add_field( FID_DUE_DATE, timestamp( $item->due_date ), $server );
1507 $resp .= add_field( FID_DUE_DATE, q{}, $server );
1509 if ( $ils->supports('security inhibit') ) {
1510 $resp .= add_field( FID_SECURITY_INHIBIT, $status->security_inhibit, $server );
1512 $resp .= add_field( FID_MEDIA_TYPE, $item->sip_media_type, $server );
1513 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
1516 # renew failed for some reason
1517 # not OK, renewal not OK, Unknown media type (why bother checking?)
1521 # If we found the patron or the item, the return the ILS
1522 # information, otherwise echo back the information we received
1524 $resp .= add_field( FID_PATRON_ID, $patron ? $patron->id : $patron_id, $server );
1525 $resp .= add_field( FID_ITEM_ID, $item ? $item->id : $item_id, $server );
1526 $resp .= add_field( FID_TITLE_ID, $item ? $item->title_id : $title_id, $server );
1527 $resp .= add_field( FID_DUE_DATE, '', $server );
1530 if ( $status->fee_amount ) {
1531 $resp .= add_field( FID_FEE_AMT, $status->fee_amount, $server );
1532 $resp .= maybe_add( FID_CURRENCY, $status->sip_currency, $server );
1533 $resp .= maybe_add( FID_FEE_TYPE, $status->sip_fee_type, $server );
1534 $resp .= maybe_add( FID_TRANSACTION_ID, $status->transaction_id, $server );
1537 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1538 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1539 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1541 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1546 sub handle_renew_all {
1548 # my ($third_party, $no_block, $nb_due_date, $fee_ack, $patron);
1550 my ( $self, $server ) = @_;
1551 my $ils = $server->{ils};
1552 my ( $trans_date, $patron_id, $patron_pwd, $terminal_pwd, $fee_ack );
1553 my $fields = $self->{fields};
1554 my $resp = RENEW_ALL_RESP;
1556 my ( @renewed, @unrenewed );
1558 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_renew_all" );
1560 ($trans_date) = @{ $self->{fixed_fields} };
1562 $patron_id = $fields->{ (FID_PATRON_ID) };
1563 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1564 $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
1565 $fee_ack = $fields->{ (FID_FEE_ACK) };
1567 Koha::Plugins->call('patron_barcode_transform', \$patron_id );
1569 $status = $ils->renew_all( $patron_id, $patron_pwd, $fee_ack );
1571 $resp .= $status->ok ? '1' : '0';
1573 if ( !$status->ok ) {
1574 $resp .= add_count( "renew_all/renewed_count", 0 );
1575 $resp .= add_count( "renew_all/unrenewed_count", 0 );
1579 @renewed = ( @{ $status->renewed } );
1580 @unrenewed = ( @{ $status->unrenewed } );
1581 $resp .= add_count( "renew_all/renewed_count", scalar @renewed );
1582 $resp .= add_count( "renew_all/unrenewed_count", scalar @unrenewed );
1586 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1588 $resp .= join( '', map( add_field( FID_RENEWED_ITEMS, $_ ), @renewed ), $server );
1589 $resp .= join( '', map( add_field( FID_UNRENEWED_ITEMS, $_ ), @unrenewed ), $server );
1591 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1592 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1594 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1600 # send_acs_status($self, $server)
1602 # Send an ACS Status message, which is contains lots of little fields
1603 # of information gleaned from all sorts of places.
1606 my @message_type_names = (
1607 "patron status request",
1612 "request sc/acs resend",
1614 "patron information",
1615 "end patron session",
1618 "item status update",
1625 sub send_acs_status {
1626 my ( $self, $server, $screen_msg, $print_line ) = @_;
1628 my $msg = ACS_STATUS;
1629 ($server) or die "send_acs_status error: no \$server argument received";
1630 my $account = $server->{account} or die "send_acs_status error: no 'account' in \$server object:\n" . Dumper($server);
1631 my $policy = $server->{policy} or die "send_acs_status error: no 'policy' in \$server object:\n" . Dumper($server);
1632 my $ils = $server->{ils} or die "send_acs_status error: no 'ils' in \$server object:\n" . Dumper($server);
1633 my $sip_username = $server->{sip_username} or die "send_acs_status error: no 'sip_username' in \$server object:\n" . Dumper($server);
1634 my ( $online_status, $checkin_ok, $checkout_ok, $ACS_renewal_policy );
1635 my ( $status_update_ok, $offline_ok, $timeout, $retries );
1636 my $sip_user = Koha::Patrons->find({ userid => $sip_username });
1637 die "send_acs_status error: sip_username cannot be found in DB or DB cannot be reached" unless $sip_user;
1639 $online_status = 'Y';
1640 $checkout_ok = sipbool( $ils->checkout_ok );
1641 $checkin_ok = sipbool( $ils->checkin_ok );
1642 $ACS_renewal_policy = sipbool( $policy->{renewal} );
1643 $status_update_ok = sipbool( $ils->status_update_ok );
1644 $offline_ok = sipbool( $ils->offline_ok );
1645 $timeout = $server->get_timeout({ policy => 1 });
1646 $retries = sprintf( "%03d", $policy->{retries} );
1648 if ( length($retries) != 3 ) {
1649 siplog( "LOG_ERR", "handle_acs_status: retries field wrong size: '%s'", $retries );
1653 $msg .= "$online_status$checkin_ok$checkout_ok$ACS_renewal_policy";
1654 $msg .= "$status_update_ok$offline_ok$timeout$retries";
1655 $msg .= timestamp();
1657 if ( $protocol_version == 1 ) {
1659 } elsif ( $protocol_version == 2 ) {
1662 siplog( "LOG_ERR", 'Bad setting for $protocol_version, "%s" in send_acs_status', $protocol_version );
1667 $msg .= add_field( FID_INST_ID, $account->{institution}, $server );
1669 if ( $protocol_version >= 2 ) {
1671 # Supported messages: we do it all
1672 my $supported_msgs = '';
1674 foreach my $msg_name (@message_type_names) {
1675 if ( $msg_name eq 'request sc/acs resend' ) {
1676 $supported_msgs .= sipbool(1);
1678 $supported_msgs .= sipbool( $ils->supports($msg_name) );
1681 if ( length($supported_msgs) < 16 ) {
1682 siplog( "LOG_ERR", 'send_acs_status: supported messages "%s" too short', $supported_msgs );
1684 $msg .= add_field( FID_SUPPORTED_MSGS, $supported_msgs, $server );
1687 $msg .= maybe_add( FID_SCREEN_MSG, $screen_msg, $server );
1689 if ( defined( $account->{print_width} )
1690 && defined($print_line)
1691 && $account->{print_width} < length($print_line) ) {
1692 siplog( "LOG_WARNING", "send_acs_status: print line '%s' too long. Truncating", $print_line );
1693 $print_line = substr( $print_line, 0, $account->{print_width} );
1696 $msg .= maybe_add( FID_PRINT_LINE, $print_line, $server );
1698 # Do we want to tell the terminal its location?
1700 $self->write_msg( $msg, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1705 # build_patron_status: create the 14-char patron status
1706 # string for the Patron Status message
1708 sub patron_status_string {
1714 siplog( "LOG_DEBUG", "patron_status_string: %s charge_ok: %s", $patron->id, $patron->charge_ok );
1715 $patron_status = sprintf(
1716 '%s%s%s%s%s%s%s%s%s%s%s%s%s%s',
1717 denied( $patron->charge_ok ),
1718 denied( $patron->renew_ok ),
1719 denied( $patron->recall_ok ),
1720 denied( $patron->hold_ok ),
1721 boolspace( $patron->card_lost ),
1722 boolspace( $patron->too_many_charged ),
1723 $server->{account}->{overdues_block_checkout} ? boolspace( $patron->too_many_overdue ) : q{ },
1724 boolspace( $patron->too_many_renewal ),
1725 boolspace( $patron->too_many_claim_return ),
1726 boolspace( $patron->too_many_lost ),
1727 boolspace( $patron->excessive_fines ),
1728 boolspace( $patron->excessive_fees ),
1729 boolspace( $patron->recall_overdue ),
1730 boolspace( $patron->too_many_billed )
1732 return $patron_status;
1736 my ( $username, $password, $branch ) = @_;
1737 $ENV{REMOTE_USER} = $username;
1738 my $query = CGI->new();
1739 $query->param( userid => $username );
1740 $query->param( password => $password );
1742 $query->param( branch => $branch );
1744 my ( $status, $cookie, $sessionID ) = check_api_auth( $query, { circulate => 1 }, 'intranet' );
1751 my $status = $params->{status};
1752 my $desensitize = $status->desensitize();
1754 # If desenstize is already false, no need to do anything
1755 return unless $desensitize;
1757 my $patron = $params->{patron};
1758 my $item = $params->{item};
1759 my $server = $params->{server};
1761 my $patron_categories = $server->{account}->{inhouse_patron_categories} // q{};
1762 my $item_types = $server->{account}->{inhouse_item_types} // q{};
1764 # If no patron categories or item types are set for never desensitize, no need to do anything
1765 return $desensitize unless $patron_categories || $item_types;
1767 my $patron_category = $patron->ptype();
1768 my @patron_categories = split( /,/, $patron_categories );
1769 my $found_patron_category = grep( /^$patron_category$/, @patron_categories );
1770 return 0 if $found_patron_category;
1772 my $item_type = $item->itemtype;
1773 my @item_types = split( /,/, $item_types );
1774 my $found_item_type = grep( /^$item_type$/, @item_types );
1775 return 0 if $found_item_type;