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;
27 use vars qw(@ISA @EXPORT_OK);
29 use constant INVALID_CARD => 'Invalid cardnumber';
30 use constant INVALID_PW => 'Invalid password';
34 @EXPORT_OK = qw(handle login_core);
37 # Predeclare handler subroutines
38 use subs qw(handle_patron_status handle_checkout handle_checkin
39 handle_block_patron handle_sc_status handle_request_acs_resend
40 handle_login handle_patron_info handle_end_patron_session
41 handle_fee_paid handle_item_information handle_item_status_update
42 handle_patron_enable handle_hold handle_renew handle_renew_all);
45 # For the most part, Version 2.00 of the protocol just adds new
46 # variable fields, but sometimes it changes the fixed header.
48 # In general, if there's no '2.00' protocol entry for a handler, that's
49 # because 2.00 didn't extend the 1.00 version of the protocol. This will
50 # be handled by the module initialization code following the declaration,
51 # which goes through the handlers table and creates a '2.00' entry that
52 # points to the same place as the '1.00' entry. If there's a 2.00 entry
53 # but no 1.00 entry, then that means that it's a completely new service
54 # in 2.00, so 1.00 shouldn't recognize it.
57 (PATRON_STATUS_REQ) => {
58 name => "Patron Status Request",
59 handler => \&handle_patron_status,
64 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD) ],
70 handler => \&handle_checkout,
73 template => "CCA18A18",
75 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
78 template => "CCA18A18",
80 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_PATRON_PWD), (FID_FEE_ACK), (FID_CANCEL) ],
86 handler => \&handle_checkin,
89 template => "CA18A18",
91 fields => [ (FID_CURRENT_LOCN), (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
94 template => "CA18A18",
96 fields => [ (FID_CURRENT_LOCN), (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS), (FID_CANCEL) ],
101 name => "Block Patron",
102 handler => \&handle_block_patron,
107 fields => [ (FID_INST_ID), (FID_BLOCKED_CARD_MSG), (FID_PATRON_ID), (FID_TERMINAL_PWD) ],
113 handler => \&handle_sc_status,
122 (REQUEST_ACS_RESEND) => {
123 name => "Request ACS Resend",
124 handler => \&handle_request_acs_resend,
135 handler => \&handle_login,
140 fields => [ (FID_LOGIN_UID), (FID_LOGIN_PWD), (FID_LOCATION_CODE) ],
145 name => "Patron Info",
146 handler => \&handle_patron_info,
149 template => "A3A18A10",
151 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD), (FID_START_ITEM), (FID_END_ITEM) ],
155 (END_PATRON_SESSION) => {
156 name => "End Patron Session",
157 handler => \&handle_end_patron_session,
162 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD) ],
168 handler => \&handle_fee_paid,
171 template => "A18A2A2A3",
173 fields => [ (FID_FEE_AMT), (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD), (FID_FEE_ID), (FID_TRANSACTION_ID) ],
177 (ITEM_INFORMATION) => {
178 name => "Item Information",
179 handler => \&handle_item_information,
184 fields => [ (FID_INST_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD) ],
188 (ITEM_STATUS_UPDATE) => {
189 name => "Item Status Update",
190 handler => \&handle_item_status_update,
195 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_ITEM_ID), (FID_TERMINAL_PWD), (FID_ITEM_PROPS) ],
200 name => "Patron Enable",
201 handler => \&handle_patron_enable,
206 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_TERMINAL_PWD), (FID_PATRON_PWD) ],
212 handler => \&handle_hold,
218 (FID_EXPIRATION), (FID_PICKUP_LOCN), (FID_HOLD_TYPE), (FID_INST_ID), (FID_PATRON_ID), (FID_PATRON_PWD),
219 (FID_ITEM_ID), (FID_TITLE_ID), (FID_TERMINAL_PWD), (FID_FEE_ACK)
226 handler => \&handle_renew,
229 template => "CCA18A18",
231 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) ],
237 handler => \&handle_renew_all,
242 fields => [ (FID_INST_ID), (FID_PATRON_ID), (FID_PATRON_PWD), (FID_TERMINAL_PWD), (FID_FEE_ACK) ],
249 # Now, initialize some of the missing bits of %handlers
251 foreach my $i ( keys(%handlers) ) {
252 if ( !exists( $handlers{$i}->{protocol}->{2} ) ) {
253 $handlers{$i}->{protocol}->{2} = $handlers{$i}->{protocol}->{1};
258 my ( $class, $msg, $seqno ) = @_;
260 my $msgtag = substr( $msg, 0, 2 );
262 if ( $msgtag eq LOGIN ) {
264 # If the client is using the 2.00-style "Login" message
265 # to authenticate to the server, then we get the Login message
266 # _before_ the client has indicated that it supports 2.00, but
267 # it's using the 2.00 login process, so it must support 2.00.
268 $protocol_version = 2;
270 siplog( "LOG_DEBUG", "Sip::MsgType::new('%s', '%s...', '%s'): seq.no '%s', protocol %s", $class, substr( $msg, 0, 10 ), $msgtag, $seqno, $protocol_version );
272 # warn "SIP PROTOCOL: $protocol_version";
273 if ( !exists( $handlers{$msgtag} ) ) {
274 siplog( "LOG_WARNING", "new Sip::MsgType: Skipping message of unknown type '%s' in '%s'", $msgtag, $msg );
276 } elsif ( !exists( $handlers{$msgtag}->{protocol}->{$protocol_version} ) ) {
277 siplog( "LOG_WARNING", "new Sip::MsgType: Skipping message '%s' unsupported by protocol rev. '%d'", $msgtag, $protocol_version );
283 $self->{seqno} = $seqno;
284 $self->_initialize( substr( $msg, 2 ), $handlers{$msgtag} );
290 my ( $self, $msg, $control_block ) = @_;
292 my $proto = $control_block->{protocol}->{$protocol_version};
294 $self->{name} = $control_block->{name};
295 $self->{handler} = $control_block->{handler};
297 $self->{fields} = {};
298 $self->{fixed_fields} = [];
300 chomp($msg); # These four are probably unnecessary now.
305 foreach my $field ( @{ $proto->{fields} } ) {
306 $self->{fields}->{$field} = undef;
309 siplog( "LOG_DEBUG", "Sip::MsgType::_initialize('%s', '%s', '%s', '%s', ...)", $self->{name}, $msg, $proto->{template}, $proto->{template_len} );
311 $self->{fixed_fields} = [ unpack( $proto->{template}, $msg ) ]; # see http://perldoc.perl.org/5.8.8/functions/unpack.html
313 # Skip over the fixed fields and the split the rest of
314 # the message into fields based on the delimiter and parse them
315 foreach my $field ( split( quotemeta($field_delimiter), substr( $msg, $proto->{template_len} ) ) ) {
316 $fn = substr( $field, 0, 2 );
318 if ( !exists( $self->{fields}->{$fn} ) ) {
319 siplog( "LOG_WARNING", "Unsupported field '%s' in %s message '%s'", $fn, $self->{name}, $msg );
320 } elsif ( defined( $self->{fields}->{$fn} ) ) {
321 siplog( "LOG_WARNING", "Duplicate field '%s' (previous value '%s') in %s message '%s'", $fn, $self->{fields}->{$fn}, $self->{name}, $msg );
323 $self->{fields}->{$fn} = substr( $field, 2 );
331 my ( $msg, $server, $req ) = @_;
332 my $config = $server->{config};
335 # Set system preference overrides, first global, then account level
336 # Clear overrides from previous message handling first
337 foreach my $key ( keys %ENV ) {
338 delete $ENV{$key} if index($key, 'OVERRIDE_SYSPREF_') > 0;
340 foreach my $key ( keys %{ $config->{'syspref_overrides'} } ) {
341 $ENV{"OVERRIDE_SYSPREF_$key"} = $config->{'syspref_overrides'}->{$key};
343 foreach my $key ( keys %{ $server->{account}->{'syspref_overrides'} } ) {
344 $ENV{"OVERRIDE_SYSPREF_$key"} =
345 $server->{account}->{'syspref_overrides'}->{$key};
349 # What's the field delimiter for variable length fields?
350 # This can't be based on the account, since we need to know
351 # the field delimiter to parse a SIP login message
353 if ( defined( $server->{config}->{delimiter} ) ) {
354 $field_delimiter = $server->{config}->{delimiter};
357 # error detection is active if this is a REQUEST_ACS_RESEND
358 # message with a checksum, or if the message is long enough
359 # and the last nine characters begin with a sequence number
361 if ( $msg eq REQUEST_ACS_RESEND_CKSUM ) {
364 $error_detection = 1;
365 $self = C4::SIP::Sip::MsgType->new( (REQUEST_ACS_RESEND), 0 );
366 } elsif ( ( length($msg) > 11 ) && ( substr( $msg, -9, 2 ) eq "AY" ) ) {
367 $error_detection = 1;
369 if ( !verify_cksum($msg) ) {
370 siplog( "LOG_WARNING", "Checksum failed on message '%s'", $msg );
372 # REQUEST_SC_RESEND with error detection
373 $last_response = REQUEST_SC_RESEND_CKSUM;
374 print("$last_response\r");
375 return REQUEST_ACS_RESEND;
378 # Save the sequence number, then strip off the
379 # error detection data to process the message
380 $self = C4::SIP::Sip::MsgType->new( substr( $msg, 0, -9 ), substr( $msg, -7, 1 ) );
382 } elsif ($error_detection) {
384 # We received a non-ED message when ED is supposed to be active.
385 # Warn about this problem, then process the message anyway.
386 siplog( "LOG_WARNING", "Received message without error detection: '%s'", $msg );
387 $error_detection = 0;
388 $self = C4::SIP::Sip::MsgType->new( $msg, 0 );
390 $self = C4::SIP::Sip::MsgType->new( $msg, 0 );
393 if ( ( substr( $msg, 0, 2 ) ne REQUEST_ACS_RESEND )
395 && ( substr( $msg, 0, 2 ) ne $req ) ) {
396 return substr( $msg, 0, 2 );
398 unless ( $self->{handler} ) {
399 siplog( "LOG_WARNING", "No handler defined for '%s'", $msg );
400 $last_response = REQUEST_SC_RESEND;
401 print("$last_response\r");
402 return REQUEST_ACS_RESEND;
404 return ( $self->{handler}->( $self, $server ) ); # FIXME
405 # FIXME: Use of uninitialized value in subroutine entry
406 # Can't use string ("") as a subroutine ref while "strict refs" in use
414 # Patron status messages are produced in response to both
415 # "Request Patron Status" and "Block Patron"
417 # Request Patron Status requires a patron password, but
418 # Block Patron doesn't (since the patron may never have
419 # provided one before attempting some illegal action).
421 # ASSUMPTION: If the patron password field is present in the
422 # message, then it must match, otherwise incomplete patron status
423 # information will be returned to the terminal.
425 sub build_patron_status {
426 my ( $patron, $lang, $fields, $server ) = @_;
428 my $patron_pwd = $fields->{ (FID_PATRON_PWD) };
429 my $resp = (PATRON_STATUS_RESP);
434 $password_rc = $patron->check_password($patron_pwd);
437 $resp .= patron_status_string( $patron, $server );
438 $resp .= $lang . timestamp();
439 if ( defined $server->{account}->{ae_field_template} ) {
440 $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template}, $server ) );
442 $resp .= add_field( FID_PERSONAL_NAME, $patron->name, $server );
446 # while the patron ID we got from the SC is valid, let's
447 # use the one returned from the ILS, just in case...
448 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
450 if ( $protocol_version >= 2 ) {
451 $resp .= add_field( FID_VALID_PATRON, 'Y', $server );
453 # Patron password is a required field.
454 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool($password_rc), $server );
455 $resp .= maybe_add( FID_CURRENCY, $patron->currency, $server );
456 $resp .= maybe_add( FID_FEE_AMT, $patron->fee_amount, $server );
459 my $msg = $patron->screen_msg;
460 $msg .= ' -- '. INVALID_PW if $patron_pwd && !$password_rc;
461 $resp .= maybe_add( FID_SCREEN_MSG, $msg, $server );
463 $resp .= maybe_add( FID_SCREEN_MSG, $patron->{branchcode}, $server )
464 if ( $server->{account}->{send_patron_home_library_in_af} );
465 $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
467 $resp .= $patron->build_custom_field_string( $server );
468 $resp .= $patron->build_patron_attributes_string( $server );
471 # Invalid patron (cardnumber)
472 # Report that the user has no privs.
474 # no personal name, and is invalid (if we're using 2.00)
475 $resp .= 'YYYY' . ( ' ' x 10 ) . $lang . timestamp();
476 $resp .= add_field( FID_PERSONAL_NAME, '', $server );
478 # the patron ID is invalid, but it's a required field, so
480 $resp .= add_field( FID_PATRON_ID, $fields->{ (FID_PATRON_ID) }, $server );
482 ( $protocol_version >= 2 )
483 and $resp .= add_field( FID_VALID_PATRON, 'N', $server );
485 $resp .= maybe_add( FID_SCREEN_MSG, INVALID_CARD, $server );
488 $resp .= add_field( FID_INST_ID, $fields->{ (FID_INST_ID) }, $server );
492 sub handle_patron_status {
493 my ( $self, $server ) = @_;
494 my $ils = $server->{ils};
496 my $resp = (PATRON_STATUS_RESP);
497 my $account = $server->{account};
498 my ( $lang, $date ) = @{ $self->{fixed_fields} };
499 my $fields = $self->{fields};
501 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_patron_status" );
502 $patron = $ils->find_patron( $fields->{ (FID_PATRON_ID) } );
503 $resp = build_patron_status( $patron, $lang, $fields, $server );
504 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
505 return (PATRON_STATUS_REQ);
508 sub handle_checkout {
509 my ( $self, $server ) = @_;
510 my $account = $server->{account};
511 my $ils = $server->{ils};
512 my $inst = $ils->institution;
513 my ( $sc_renewal_policy, $no_block, $trans_date, $nb_due_date );
515 my ( $patron_id, $item_id, $status );
516 my ( $item, $patron );
519 ( $sc_renewal_policy, $no_block, $trans_date, $nb_due_date ) = @{ $self->{fixed_fields} };
520 $fields = $self->{fields};
522 $patron_id = $fields->{ (FID_PATRON_ID) };
523 $item_id = $fields->{ (FID_ITEM_ID) };
524 my $fee_ack = $fields->{ (FID_FEE_ACK) };
526 if ( $no_block eq 'Y' ) {
528 # Off-line transactions need to be recorded, but there's
529 # not a lot we can do about it
530 siplog( "LOG_WARNING", "received no-block checkout from terminal '%s'", $account->{id} );
532 $status = $ils->checkout_no_block( $patron_id, $item_id, $sc_renewal_policy, $trans_date, $nb_due_date );
535 # Does the transaction date really matter for items that are
536 # checkout out while the terminal is online? I'm guessing 'no'
537 $status = $ils->checkout( $patron_id, $item_id, $sc_renewal_policy, $fee_ack, $account );
540 $item = $status->item;
541 $patron = $status->patron;
545 # Item successfully checked out
547 $resp = CHECKOUT_RESP . '1';
548 $resp .= sipbool( $status->renew_ok );
549 if ( $ils->supports('magnetic media') ) {
550 $resp .= sipbool( $item->magnetic_media );
555 # We never return the obsolete 'U' value for 'desensitize'
556 $resp .= sipbool( $status->desensitize );
559 # Now for the variable fields
560 $resp .= add_field( FID_INST_ID, $inst, $server );
561 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
562 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
563 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
564 if ( $item->due_date ) {
565 $resp .= add_field( FID_DUE_DATE, timestamp( $item->due_date ), $server );
567 $resp .= add_field( FID_DUE_DATE, q{}, $server );
570 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
571 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
573 if ( $protocol_version >= 2 ) {
574 if ( $ils->supports('security inhibit') ) {
575 $resp .= add_field( FID_SECURITY_INHIBIT, $status->security_inhibit, $server );
577 $resp .= maybe_add( FID_MEDIA_TYPE, $item->sip_media_type, $server );
578 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
586 # Checkout Response: not ok, no renewal, don't know mag. media,
588 $resp = sprintf( "120NUN%s", timestamp );
589 $resp .= add_field( FID_INST_ID, $inst, $server );
590 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
591 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
593 # If the item is valid, provide the title, otherwise
595 $resp .= add_field( FID_TITLE_ID, $item ? $item->title_id : '', $server );
597 # Due date is required. Since it didn't get checked out,
598 # it's not due, so leave the date blank
599 $resp .= add_field( FID_DUE_DATE, '', $server );
601 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
602 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
604 if ( $protocol_version >= 2 ) {
606 # Is the patron ID valid?
607 $resp .= add_field( FID_VALID_PATRON, sipbool($patron), $server );
609 if ( $patron && exists( $fields->{FID_PATRON_PWD} ) ) {
611 # Password provided, so we can tell if it was valid or not
612 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password( $fields->{ (FID_PATRON_PWD) } ) ), $server );
617 $resp .= $item->build_additional_item_fields_string( $server ) if $item;
619 if ( $protocol_version >= 2 ) {
621 # Financials : return irrespective of ok status
622 if ( $status->fee_amount ) {
623 $resp .= add_field( FID_FEE_AMT, $status->fee_amount, $server );
624 $resp .= maybe_add( FID_CURRENCY, $status->sip_currency, $server );
625 $resp .= maybe_add( FID_FEE_TYPE, $status->sip_fee_type, $server );
626 $resp .= maybe_add( FID_TRANSACTION_ID, $status->transaction_id, $server );
630 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
635 my ( $self, $server ) = @_;
636 my $account = $server->{account};
637 my $ils = $server->{ils};
638 my $my_branch = $ils->institution;
639 my ( $current_loc, $inst_id, $item_id, $terminal_pwd, $item_props, $cancel );
640 my ( $patron, $item, $status );
641 my $resp = CHECKIN_RESP;
642 my ( $no_block, $trans_date, $return_date ) = @{ $self->{fixed_fields} };
643 my $fields = $self->{fields};
645 $current_loc = $fields->{ (FID_CURRENT_LOCN) };
646 $inst_id = $fields->{ (FID_INST_ID) };
647 $item_id = $fields->{ (FID_ITEM_ID) };
648 $item_props = $fields->{ (FID_ITEM_PROPS) };
649 $cancel = $fields->{ (FID_CANCEL) };
651 $my_branch = $current_loc; # most scm do not set $current_loc
654 $ils->check_inst_id( $inst_id, "handle_checkin" );
656 if ( $no_block eq 'Y' ) {
658 # Off-line transactions, ick.
659 siplog( "LOG_WARNING", "received no-block checkin from terminal '%s'", $account->{id} );
660 $status = $ils->checkin_no_block( $item_id, $trans_date, $return_date, $item_props, $cancel );
662 $status = $ils->checkin( $item_id, $trans_date, $return_date, $my_branch, $item_props, $cancel, $account );
665 $patron = $status->patron;
666 $item = $status->item;
668 $resp .= $status->ok ? '1' : '0';
669 $resp .= $status->resensitize ? 'Y' : 'N';
670 if ( $item && $ils->supports('magnetic media') ) {
671 $resp .= sipbool( $item->magnetic_media );
674 # item barcode is invalid or system doesn't support 'magnetic media' indicator
678 $resp .= $status->alert ? 'Y' : 'N';
680 $resp .= add_field( FID_INST_ID, $inst_id, $server );
681 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
684 $resp .= add_field( FID_PERM_LOCN, $item->permanent_location, $server );
685 $resp .= maybe_add( FID_TITLE_ID, $item->title_id, $server );
686 $resp .= $item->build_additional_item_fields_string( $server );
689 if ( $protocol_version >= 2 ) {
690 $resp .= maybe_add( FID_SORT_BIN, $status->sort_bin, $server );
692 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
695 $resp .= maybe_add( FID_MEDIA_TYPE, $item->sip_media_type, $server );
696 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
697 $resp .= maybe_add( FID_CALL_NUMBER, $item->call_number, $server );
698 $resp .= maybe_add( FID_HOLD_PATRON_ID, $item->hold_patron_bcode, $server );
699 $resp .= add_field( FID_DESTINATION_LOCATION, $item->destination_loc, $server ) if ( $item->destination_loc || $server->{account}->{ct_always_send} );
700 $resp .= maybe_add( FID_HOLD_PATRON_NAME, $item->hold_patron_name( $server->{account}->{da_field_template} ), $server );
702 if ( my $CR = $server->{account}->{cr_item_field} ) {
703 $resp .= maybe_add( FID_COLLECTION_CODE, $item->{$CR}, $server );
705 $resp .= maybe_add( FID_COLLECTION_CODE, $item->collection_code, $server );
708 if ( $status->hold and $status->hold->{branchcode} ne $item->destination_loc ) {
709 warn 'SIP hold mismatch: $status->hold->{branchcode}=' . $status->hold->{branchcode} . '; $item->destination_loc=' . $item->destination_loc;
711 # just me being paranoid.
716 if ( $status->alert && $status->alert_type ) {
717 $resp .= maybe_add( FID_ALERT_TYPE, $status->alert_type, $server );
718 } elsif ( $server->{account}->{cv_send_00_on_success} ) {
719 $resp .= add_field( FID_ALERT_TYPE, '00', $server );
721 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
722 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
724 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
729 sub handle_block_patron {
730 my ( $self, $server ) = @_;
731 my $account = $server->{account};
732 my $ils = $server->{ils};
733 my ( $card_retained, $trans_date );
734 my ( $inst_id, $blocked_card_msg, $patron_id, $terminal_pwd );
735 my ( $fields, $resp, $patron );
737 ( $card_retained, $trans_date ) = @{ $self->{fixed_fields} };
738 $fields = $self->{fields};
739 $inst_id = $fields->{ (FID_INST_ID) };
740 $blocked_card_msg = $fields->{ (FID_BLOCKED_CARD_MSG) };
741 $patron_id = $fields->{ (FID_PATRON_ID) };
742 $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
744 # Terminal passwords are different from account login
745 # passwords, but I have no idea what to do with them. So,
746 # I'll just ignore them for now.
750 $ils->check_inst_id( $inst_id, "block_patron" );
751 $patron = $ils->find_patron($patron_id);
753 # The correct response for a "Block Patron" message is a
754 # "Patron Status Response", so use that handler to generate
755 # the message, but then return the correct code from here.
757 # Normally, the language is provided by the "Patron Status"
758 # fixed field, but since we're not responding to one of those
759 # we'll just say, "Unspecified", as per the spec. Let the
760 # terminal default to something that, one hopes, will be
765 $patron->block( $card_retained, $blocked_card_msg );
768 $resp = build_patron_status( $patron, $patron->language, $fields, $server );
769 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
770 return (BLOCK_PATRON);
773 sub handle_sc_status {
774 my ( $self, $server ) = @_;
775 ($server) or warn "handle_sc_status error: no \$server argument received.";
776 my ( $status, $print_width, $sc_protocol_version ) = @{ $self->{fixed_fields} };
779 if ( $sc_protocol_version =~ /^1\./ ) {
781 } elsif ( $sc_protocol_version =~ /^2\./ ) {
784 siplog( "LOG_WARNING", "Unrecognized protocol revision '%s', falling back to '1'", $sc_protocol_version );
788 if ( $new_proto != $protocol_version ) {
789 siplog( "LOG_INFO", "Setting protocol level to $new_proto" );
790 $protocol_version = $new_proto;
793 if ( $status == SC_STATUS_PAPER ) {
794 siplog( "LOG_WARNING", "Self-Check unit '%s@%s' out of paper", $self->{account}->{id}, $self->{account}->{institution} );
795 } elsif ( $status == SC_STATUS_SHUTDOWN ) {
796 siplog( "LOG_WARNING", "Self-Check unit '%s@%s' shutting down", $self->{account}->{id}, $self->{account}->{institution} );
799 $self->{account}->{print_width} = $print_width;
800 return ( send_acs_status( $self, $server ) ? SC_STATUS : '' );
803 sub handle_request_acs_resend {
804 my ( $self, $server ) = @_;
806 if ( !$last_response ) {
808 # We haven't sent anything yet, so respond with a
809 # REQUEST_SC_RESEND msg (p. 16)
810 $self->write_msg( REQUEST_SC_RESEND, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
811 } elsif ( ( length($last_response) < 9 )
812 || substr( $last_response, -9, 2 ) ne 'AY' ) {
814 # When resending a message, we aren't supposed to include
815 # a sequence number, even if the original had one (p. 4).
816 # If the last message didn't have a sequence number, then
817 # we can just send it.
818 print("$last_response\r"); # not write_msg?
821 # Cut out the sequence number and checksum, since the old
822 # checksum is wrong for the resent message.
823 my $rebuilt = substr( $last_response, 0, -9 );
824 $self->write_msg( $rebuilt, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
827 return REQUEST_ACS_RESEND;
831 my $server = shift or return;
834 my $status = 1; # Assume it all works
835 if ( !exists( $server->{config}->{accounts}->{$uid} ) ) {
836 siplog( "LOG_WARNING", "MsgType::login_core: Unknown login '$uid'" );
838 } elsif ( $server->{config}->{accounts}->{$uid}->{password} ne $pwd ) {
839 siplog( "LOG_WARNING", "MsgType::login_core: Invalid password for login '$uid'" );
843 # Store the active account someplace handy for everybody else to find.
844 $server->{account} = $server->{config}->{accounts}->{$uid};
845 my $inst = $server->{account}->{institution};
846 $server->{institution} = $server->{config}->{institutions}->{$inst};
847 $server->{policy} = $server->{institution}->{policy};
848 $server->{sip_username} = $uid;
849 $server->{sip_password} = $pwd;
851 my $auth_status = api_auth( $uid, $pwd, $inst );
852 if ( !$auth_status or $auth_status !~ /^ok$/i ) {
853 siplog( "LOG_WARNING", "api_auth failed for SIP terminal '%s' of '%s': %s", $uid, $inst, ( $auth_status || 'unknown' ) );
856 siplog( "LOG_INFO", "Successful login/auth for '%s' of '%s'", $server->{account}->{id}, $inst );
859 # initialize connection to ILS
861 my $module = $server->{config}->{institutions}->{$inst}->{implementation};
862 siplog( "LOG_DEBUG", 'login_core: ' . Dumper($module) );
864 # Suspect this is always ILS but so we don't break any eccentic install (for now)
865 if ( $module eq 'ILS' ) {
866 $module = 'C4::SIP::ILS';
870 siplog( "LOG_ERR", "%s: Loading ILS implementation '%s' for institution '%s' failed", $server->{service}, $module, $inst );
871 die("Failed to load ILS implementation '$module' for $inst");
874 # like ILS->new(), I think.
875 $server->{ils} = $module->new( $server->{institution}, $server->{account} );
876 if ( !$server->{ils} ) {
877 siplog( "LOG_ERR", "%s: ILS connection to '%s' failed", $server->{service}, $inst );
878 die("Unable to connect to ILS '$inst'");
886 my ( $self, $server ) = @_;
887 my ( $uid_algorithm, $pwd_algorithm );
891 my $status = 1; # Assume it all works
893 $fields = $self->{fields};
894 ( $uid_algorithm, $pwd_algorithm ) = @{ $self->{fixed_fields} };
896 $uid = $fields->{ (FID_LOGIN_UID) }; # Terminal ID, not patron ID.
897 $pwd = $fields->{ (FID_LOGIN_PWD) }; # Terminal PWD, not patron PWD.
899 if ( $uid_algorithm || $pwd_algorithm ) {
900 siplog( "LOG_ERR", "LOGIN: Unsupported non-zero encryption method(s): uid = $uid_algorithm, pwd = $pwd_algorithm" );
903 $status = login_core( $server, $uid, $pwd );
906 $self->write_msg( LOGIN_RESP . $status, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
907 return $status ? LOGIN : '';
911 # Build the detailed summary information for the Patron
912 # Information Response message based on the first 'Y' that appears
913 # in the 'summary' field of the Patron Information request. The
914 # specification says that only one 'Y' can appear in that field,
915 # and we're going to believe it.
918 my ( $ils, $patron, $summary, $start, $end, $server ) = @_;
922 # Map from offsets in the "summary" field of the Patron Information
923 # message to the corresponding field and handler
926 { func => $patron->can("hold_items"), fid => FID_HOLD_ITEMS },
927 { func => $patron->can("overdue_items"), fid => FID_OVERDUE_ITEMS },
928 { func => $patron->can("charged_items"), fid => FID_CHARGED_ITEMS },
929 { func => $patron->can("fine_items"), fid => FID_FINE_ITEMS },
930 { func => $patron->can("recall_items"), fid => FID_RECALL_ITEMS },
931 { func => $patron->can("unavail_holds"), fid => FID_UNAVAILABLE_HOLD_ITEMS },
934 my $summary_type = index( $summary, 'Y' );
935 return q{} if $summary_type == -1; # No detailed information required.
936 return q{} if $summary_type > 5; # Positions 6-9 are not defined in the sip spec,
937 # and we have no extensions to handle them.
939 siplog( "LOG_DEBUG", "Summary_info: index == '%d', field '%s'", $summary_type, $summary_map[$summary_type]->{fid} );
941 my $func = $summary_map[$summary_type]->{func};
942 my $fid = $summary_map[$summary_type]->{fid};
943 my $itemlist = &$func( $patron, $start, $end, $server );
945 siplog( "LOG_DEBUG", "summary_info: list = (%s)", join( ", ", map{ $_->{barcode} } @{$itemlist} ) );
946 foreach my $i ( @{$itemlist} ) {
947 $resp .= add_field( $fid, $i->{barcode}, $server );
953 sub handle_patron_info {
954 my ( $self, $server ) = @_;
955 my $ils = $server->{ils};
956 my ( $lang, $trans_date, $summary ) = @{ $self->{fixed_fields} };
957 my $fields = $self->{fields};
958 my ( $inst_id, $patron_id, $terminal_pwd, $patron_pwd, $start, $end );
959 my ( $resp, $patron );
961 $inst_id = $fields->{ (FID_INST_ID) };
962 $patron_id = $fields->{ (FID_PATRON_ID) };
963 $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
964 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
965 $start = $fields->{ (FID_START_ITEM) };
966 $end = $fields->{ (FID_END_ITEM) };
968 $patron = $ils->find_patron($patron_id);
970 $resp = (PATRON_INFO_RESP);
972 $patron->update_lastseen();
973 $resp .= patron_status_string( $patron, $server );
974 $resp .= ( defined($lang) and length($lang) == 3 ) ? $lang : $patron->language;
975 $resp .= timestamp();
977 $resp .= add_count( 'patron_info/hold_items', scalar @{ $patron->hold_items } );
978 $resp .= add_count( 'patron_info/overdue_items', scalar @{ $patron->overdue_items } );
979 $resp .= add_count( 'patron_info/charged_items', scalar @{ $patron->charged_items } );
980 $resp .= add_count( 'patron_info/fine_items', scalar @{ $patron->fine_items } );
981 $resp .= add_count( 'patron_info/recall_items', scalar @{ $patron->recall_items } );
982 $resp .= add_count( 'patron_info/unavail_holds', scalar @{ $patron->unavail_holds } );
984 $resp .= add_field( FID_INST_ID, ( $ils->institution_id || 'SIP2' ), $server );
986 # while the patron ID we got from the SC is valid, let's
987 # use the one returned from the ILS, just in case...
988 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
989 if ( defined $server->{account}->{ae_field_template} ) {
990 $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ), $server );
992 $resp .= add_field( FID_PERSONAL_NAME, $patron->name, $server );
995 # TODO: add code for the fields
997 # overdue items limit
998 # charged items limit
1000 $resp .= add_field( FID_VALID_PATRON, 'Y', $server );
1002 if ( defined($patron_pwd) ) {
1004 # If patron password was provided, report whether it was right or not.
1005 if ( $patron_pwd eq q{} && $server->{account}->{allow_empty_passwords} ) {
1008 $password_rc = $patron->check_password($patron_pwd);
1010 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $password_rc ), $server );
1013 $resp .= maybe_add( FID_CURRENCY, $patron->currency, $server );
1014 $resp .= maybe_add( FID_FEE_AMT, $patron->fee_amount, $server );
1015 $resp .= add_field( FID_FEE_LMT, $patron->fee_limit, $server );
1017 # TODO: zero or more item details for 2.0 can go here:
1024 $resp .= summary_info( $ils, $patron, $summary, $start, $end, $server );
1026 $resp .= maybe_add( FID_HOME_ADDR, $patron->address, $server );
1027 $resp .= maybe_add( FID_EMAIL, $patron->email_addr, $server );
1028 $resp .= maybe_add( FID_HOME_PHONE, $patron->home_phone, $server );
1030 # SIP 2.0 extensions used by Envisionware
1031 # Other terminals will ignore unrecognized fields (unrecognized field identifiers)
1032 $resp .= maybe_add( FID_PATRON_BIRTHDATE, $patron->birthdate, $server );
1033 $resp .= maybe_add( FID_PATRON_CLASS, $patron->ptype, $server );
1035 # Custom protocol extension to report patron internet privileges
1036 $resp .= maybe_add( FID_INET_PROFILE, $patron->inet_privileges, $server );
1038 my $msg = $patron->screen_msg;
1039 if( defined( $patron_pwd ) && !$password_rc ) {
1040 $msg .= ' -- ' . INVALID_PW;
1042 $resp .= maybe_add( FID_SCREEN_MSG, $msg, $server );
1043 if ( $server->{account}->{send_patron_home_library_in_af} ) {
1044 $resp .= maybe_add( FID_SCREEN_MSG, $patron->{branchcode}, $server);
1046 $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
1048 $resp .= $patron->build_custom_field_string( $server );
1049 $resp .= $patron->build_patron_attributes_string( $server );
1052 # Invalid patron ID:
1053 # no privileges, no items associated,
1054 # no personal name, and is invalid (if we're using 2.00)
1055 $resp .= 'YYYY' . ( ' ' x 10 ) . $lang . timestamp();
1056 $resp .= '0000' x 6;
1058 $resp .= add_field( FID_INST_ID, ( $ils->institution_id || 'SIP2' ), $server );
1060 # patron ID is invalid, but field is required, so just echo it back
1061 $resp .= add_field( FID_PATRON_ID, $fields->{ (FID_PATRON_ID) }, $server );
1062 $resp .= add_field( FID_PERSONAL_NAME, '', $server );
1064 if ( $protocol_version >= 2 ) {
1065 $resp .= add_field( FID_VALID_PATRON, 'N', $server );
1067 $resp .= maybe_add( FID_SCREEN_MSG, INVALID_CARD, $server );
1070 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1071 return (PATRON_INFO);
1074 sub handle_end_patron_session {
1075 my ( $self, $server ) = @_;
1076 my $ils = $server->{ils};
1078 my $fields = $self->{fields};
1079 my $resp = END_SESSION_RESP;
1080 my ( $status, $screen_msg, $print_line );
1082 ($trans_date) = @{ $self->{fixed_fields} };
1084 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, 'handle_end_patron_session' );
1086 ( $status, $screen_msg, $print_line ) = $ils->end_patron_session( $fields->{ (FID_PATRON_ID) } );
1088 $resp .= $status ? 'Y' : 'N';
1089 $resp .= timestamp();
1091 $resp .= add_field( FID_INST_ID, $server->{ils}->institution, $server );
1092 $resp .= add_field( FID_PATRON_ID, $fields->{ (FID_PATRON_ID) }, $server );
1094 $resp .= maybe_add( FID_SCREEN_MSG, $screen_msg, $server );
1095 $resp .= maybe_add( FID_PRINT_LINE, $print_line, $server );
1097 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1099 return (END_PATRON_SESSION);
1102 sub handle_fee_paid {
1103 my ( $self, $server ) = @_;
1104 my $ils = $server->{ils};
1105 my ( $trans_date, $fee_type, $pay_type, $currency ) = @{ $self->{fixed_fields} };
1106 my $fields = $self->{fields};
1107 my ( $fee_amt, $inst_id, $patron_id, $terminal_pwd, $patron_pwd );
1108 my ( $fee_id, $trans_id );
1110 my $resp = FEE_PAID_RESP;
1112 my $disallow_overpayment = $server->{account}->{disallow_overpayment};
1113 my $payment_type_writeoff = $server->{account}->{payment_type_writeoff} || q{};
1114 my $register_id = $server->{account}->{register_id};
1116 my $is_writeoff = $pay_type eq $payment_type_writeoff;
1118 $fee_amt = $fields->{ (FID_FEE_AMT) };
1119 $inst_id = $fields->{ (FID_INST_ID) };
1120 $patron_id = $fields->{ (FID_PATRON_ID) };
1121 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1122 $fee_id = $fields->{ (FID_FEE_ID) };
1123 $trans_id = $fields->{ (FID_TRANSACTION_ID) };
1125 $ils->check_inst_id( $inst_id, "handle_fee_paid" );
1127 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 );
1128 $status = $pay_result->{status};
1129 my $pay_response = $pay_result->{pay_response};
1132 "no_item" => "No matching item could be found",
1133 "no_checkout" => "Item is not checked out",
1134 "too_soon" => "Cannot yet be renewed",
1135 "too_many" => "Renewed the maximum number of times",
1136 "auto_too_soon" => "Scheduled for automatic renewal and cannot yet be renewed",
1137 "auto_too_late" => "Scheduled for automatic renewal and cannot yet be any more",
1138 "auto_account_expired" => "Scheduled for automatic renewal and cannot be renewed because the patron's account has expired",
1139 "auto_renew" => "Scheduled for automatic renewal",
1140 "auto_too_much_oweing" => "Scheduled for automatic renewal",
1141 "on_reserve" => "On hold for another patron",
1142 "patron_restricted" => "Patron is currently restricted",
1143 "item_denied_renewal" => "Item is not allowed renewal",
1144 "onsite_checkout" => "Item is an onsite checkout"
1148 foreach my $result( @{$pay_response->{renew_result}} ) {
1149 my $item = Koha::Items->find({ itemnumber => $result->{itemnumber} });
1150 if ($result->{success}) {
1151 push @success, '"' . $item->biblio->title . '"';
1153 push @fail, '"' . $item->biblio->title . '" : ' . $failmap->{$result->{error}};
1158 if (scalar @success > 0) {
1159 $msg.="The following items were renewed: " . join(", ", @success) . ". ";
1161 if (scalar @fail > 0) {
1162 $msg.="The following items were not renewed: " . join(", ", @fail) . ".";
1164 if (length $msg > 0) {
1165 $status->screen_msg($status->screen_msg . " $msg");
1168 $resp .= ( $status->ok ? 'Y' : 'N' ) . timestamp;
1169 $resp .= add_field( FID_INST_ID, $inst_id, $server );
1170 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
1171 $resp .= maybe_add( FID_TRANSACTION_ID, $status->transaction_id, $server );
1172 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1173 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1175 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1180 sub handle_item_information {
1181 my ( $self, $server ) = @_;
1182 my $ils = $server->{ils};
1184 my $fields = $self->{fields};
1185 my $resp = ITEM_INFO_RESP;
1189 ($trans_date) = @{ $self->{fixed_fields} };
1191 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_item_information" );
1193 $item = $ils->find_item( $fields->{ (FID_ITEM_ID) } );
1195 if ( !defined($item) ) {
1198 # "Other" circ stat, "Other" security marker, "Unknown" fee type
1202 # Just echo back the invalid item id
1203 $resp .= add_field( FID_ITEM_ID, $fields->{ (FID_ITEM_ID) }, $server );
1205 # title id is required, but we don't have one
1206 $resp .= add_field( FID_TITLE_ID, '', $server );
1209 # Valid Item ID, send the good stuff
1210 $resp .= $item->sip_circulation_status;
1211 $resp .= $item->sip_security_marker;
1212 $resp .= $item->sip_fee_type;
1215 $resp .= add_field( FID_ITEM_ID, $item->id, $server );
1216 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
1218 $resp .= maybe_add( FID_MEDIA_TYPE, $item->sip_media_type, $server );
1219 $resp .= maybe_add( FID_PERM_LOCN, $item->permanent_location, $server );
1220 $resp .= maybe_add( FID_CURRENT_LOCN, $item->current_location, $server );
1221 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
1223 if ( my $CR = $server->{account}->{cr_item_field} ) {
1224 $resp .= maybe_add( FID_COLLECTION_CODE, $item->{$CR}, $server );
1226 $resp .= maybe_add( FID_COLLECTION_CODE, $item->collection_code, $server );
1229 if ( ( $i = $item->fee ) != 0 ) {
1230 $resp .= add_field( FID_CURRENCY, $item->fee_currency, $server );
1231 $resp .= add_field( FID_FEE_AMT, $i, $server );
1233 $resp .= maybe_add( FID_OWNER, $item->owner, $server );
1235 if ( ( $i = scalar @{ $item->hold_queue } ) > 0 ) {
1236 $resp .= add_field( FID_HOLD_QUEUE_LEN, $i, $server );
1238 if ( $item->due_date ) {
1239 $resp .= add_field( FID_DUE_DATE, timestamp( $item->due_date ), $server );
1241 if ( ( $i = $item->recall_date ) != 0 ) {
1242 $resp .= add_field( FID_RECALL_DATE, timestamp($i), $server );
1244 if ( ( $i = $item->hold_pickup_date ) != 0 ) {
1245 $resp .= add_field( FID_HOLD_PICKUP_DATE, timestamp($i), $server );
1248 $resp .= maybe_add( FID_SCREEN_MSG, $item->screen_msg, $server );
1249 $resp .= maybe_add( FID_PRINT_LINE, $item->print_line, $server );
1251 $resp .= $item->build_additional_item_fields_string( $server );
1254 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1256 return (ITEM_INFORMATION);
1259 sub handle_item_status_update {
1260 my ( $self, $server ) = @_;
1261 my $ils = $server->{ils};
1262 my ( $trans_date, $item_id, $terminal_pwd, $item_props );
1263 my $fields = $self->{fields};
1266 my $resp = ITEM_STATUS_UPDATE_RESP;
1268 ($trans_date) = @{ $self->{fixed_fields} };
1270 $ils->check_inst_id( $fields->{ (FID_INST_ID) } );
1272 $item_id = $fields->{ (FID_ITEM_ID) };
1273 $item_props = $fields->{ (FID_ITEM_PROPS) };
1275 if ( !defined($item_id) ) {
1276 siplog( "LOG_WARNING", "handle_item_status: received message without Item ID field" );
1278 $item = $ils->find_item($item_id);
1286 $resp .= add_field( FID_ITEM_ID, $item_id, $server );
1291 $status = $item->status_update($item_props);
1293 $resp .= $status->ok ? '1' : '0';
1296 $resp .= add_field( FID_ITEM_ID, $item->id, $server );
1297 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
1298 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
1301 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1302 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1304 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1306 return (ITEM_STATUS_UPDATE);
1309 sub handle_patron_enable {
1310 my ( $self, $server ) = @_;
1311 my $ils = $server->{ils};
1312 my $fields = $self->{fields};
1313 my ( $trans_date, $patron_id, $terminal_pwd, $patron_pwd );
1314 my ( $status, $patron );
1315 my $resp = PATRON_ENABLE_RESP;
1317 ($trans_date) = @{ $self->{fixed_fields} };
1318 $patron_id = $fields->{ (FID_PATRON_ID) };
1319 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1321 siplog( "LOG_DEBUG", "handle_patron_enable: patron_id: '%s', patron_pwd: '%s'", $patron_id, $patron_pwd );
1323 $patron = $ils->find_patron($patron_id);
1325 if ( !defined($patron) ) {
1328 $resp .= 'YYYY' . ( ' ' x 10 ) . '000' . timestamp();
1329 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
1330 $resp .= add_field( FID_PERSONAL_NAME, '', $server );
1331 $resp .= add_field( FID_VALID_PATRON, 'N', $server );
1332 $resp .= add_field( FID_VALID_PATRON_PWD, 'N', $server );
1336 if ( !defined($patron_pwd) || $patron->check_password($patron_pwd) ) {
1338 # Don't enable the patron if there was an invalid password
1339 $status = $patron->enable;
1341 $resp .= patron_status_string( $patron, $server );
1342 $resp .= $patron->language . timestamp();
1344 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
1345 $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ), $server );
1346 if ( defined($patron_pwd) ) {
1347 $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password($patron_pwd) ), $server );
1349 $resp .= add_field( FID_VALID_PATRON, 'Y', $server );
1350 $resp .= maybe_add( FID_SCREEN_MSG, $patron->screen_msg, $server );
1351 $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
1354 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1356 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1358 return (PATRON_ENABLE);
1362 my ( $self, $server ) = @_;
1363 my $ils = $server->{ils};
1364 my ( $hold_mode, $trans_date );
1365 my ( $expiry_date, $pickup_locn, $hold_type, $patron_id, $patron_pwd );
1366 my ( $item_id, $title_id, $fee_ack );
1367 my $fields = $self->{fields};
1369 my $resp = HOLD_RESP;
1371 ( $hold_mode, $trans_date ) = @{ $self->{fixed_fields} };
1373 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_hold" );
1375 $patron_id = $fields->{ (FID_PATRON_ID) };
1376 $expiry_date = $fields->{ (FID_EXPIRATION) } || '';
1377 $pickup_locn = $fields->{ (FID_PICKUP_LOCN) } || '';
1378 $hold_type = $fields->{ (FID_HOLD_TYPE) } || '2'; # Any copy of title
1379 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1380 $item_id = $fields->{ (FID_ITEM_ID) } || '';
1381 $title_id = $fields->{ (FID_TITLE_ID) } || '';
1382 $fee_ack = $fields->{ (FID_FEE_ACK) } || 'N';
1384 if ( $hold_mode eq '+' ) {
1385 $status = $ils->add_hold( $patron_id, $patron_pwd, $item_id, $title_id, $expiry_date, $pickup_locn, $hold_type, $fee_ack );
1386 } elsif ( $hold_mode eq '-' ) {
1387 $status = $ils->cancel_hold( $patron_id, $patron_pwd, $item_id, $title_id );
1388 } elsif ( $hold_mode eq '*' ) {
1389 $status = $ils->alter_hold( $patron_id, $patron_pwd, $item_id, $title_id, $expiry_date, $pickup_locn, $hold_type, $fee_ack );
1391 siplog( "LOG_WARNING", "handle_hold: Unrecognized hold mode '%s' from terminal '%s'", $hold_mode, $server->{account}->{id} );
1392 $status = $ils->Transaction::Hold; # new?
1393 $status->screen_msg("System error. Please contact library staff.");
1396 $resp .= $status->ok;
1397 $resp .= sipbool( $status->item && $status->item->available($patron_id) );
1400 if ( $status->ok ) {
1401 $resp .= add_field( FID_PATRON_ID, $status->patron->id, $server );
1403 ( $status->expiration_date )
1404 and $resp .= maybe_add( FID_EXPIRATION, timestamp( $status->expiration_date ), $server );
1405 $resp .= maybe_add( FID_QUEUE_POS, $status->queue_position, $server );
1406 $resp .= maybe_add( FID_PICKUP_LOCN, $status->pickup_location, $server );
1407 $resp .= maybe_add( FID_ITEM_ID, $status->item->id, $server );
1408 $resp .= maybe_add( FID_TITLE_ID, $status->item->title_id, $server );
1411 # Not ok. still need required fields
1412 $resp .= add_field( FID_PATRON_ID, $patron_id, $server );
1415 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1416 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1417 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1419 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1425 my ( $self, $server ) = @_;
1426 my $ils = $server->{ils};
1427 my ( $third_party, $no_block, $trans_date, $nb_due_date );
1428 my ( $patron_id, $patron_pwd, $item_id, $title_id, $item_props, $fee_ack );
1429 my $fields = $self->{fields};
1431 my ( $patron, $item );
1432 my $resp = RENEW_RESP;
1434 ( $third_party, $no_block, $trans_date, $nb_due_date ) = @{ $self->{fixed_fields} };
1436 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_renew" );
1438 if ( $no_block eq 'Y' ) {
1439 siplog( "LOG_WARNING", "handle_renew: received 'no block' renewal from terminal '%s'", $server->{account}->{id} );
1442 $patron_id = $fields->{ (FID_PATRON_ID) };
1443 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1444 $item_id = $fields->{ (FID_ITEM_ID) };
1445 $title_id = $fields->{ (FID_TITLE_ID) };
1446 $item_props = $fields->{ (FID_ITEM_PROPS) };
1447 $fee_ack = $fields->{ (FID_FEE_ACK) };
1449 $status = $ils->renew( $patron_id, $patron_pwd, $item_id, $title_id, $no_block, $nb_due_date, $third_party, $item_props, $fee_ack );
1451 $patron = $status->patron;
1452 $item = $status->item;
1454 if ( $status->renewal_ok ) {
1456 $resp .= $status->renewal_ok ? 'Y' : 'N';
1457 if ( $ils->supports('magnetic media') ) {
1458 $resp .= sipbool( $item->magnetic_media );
1462 $resp .= sipbool( $status->desensitize );
1464 $resp .= add_field( FID_PATRON_ID, $patron->id, $server );
1465 $resp .= add_field( FID_ITEM_ID, $item->id, $server );
1466 $resp .= add_field( FID_TITLE_ID, $item->title_id, $server );
1467 if ( $item->due_date ) {
1468 $resp .= add_field( FID_DUE_DATE, timestamp( $item->due_date ), $server );
1470 $resp .= add_field( FID_DUE_DATE, q{}, $server );
1472 if ( $ils->supports('security inhibit') ) {
1473 $resp .= add_field( FID_SECURITY_INHIBIT, $status->security_inhibit, $server );
1475 $resp .= add_field( FID_MEDIA_TYPE, $item->sip_media_type, $server );
1476 $resp .= maybe_add( FID_ITEM_PROPS, $item->sip_item_properties, $server );
1479 # renew failed for some reason
1480 # not OK, renewal not OK, Unknown media type (why bother checking?)
1484 # If we found the patron or the item, the return the ILS
1485 # information, otherwise echo back the information we received
1487 $resp .= add_field( FID_PATRON_ID, $patron ? $patron->id : $patron_id, $server );
1488 $resp .= add_field( FID_ITEM_ID, $item ? $item->id : $item_id, $server );
1489 $resp .= add_field( FID_TITLE_ID, $item ? $item->title_id : $title_id, $server );
1490 $resp .= add_field( FID_DUE_DATE, '', $server );
1493 if ( $status->fee_amount ) {
1494 $resp .= add_field( FID_FEE_AMT, $status->fee_amount, $server );
1495 $resp .= maybe_add( FID_CURRENCY, $status->sip_currency, $server );
1496 $resp .= maybe_add( FID_FEE_TYPE, $status->sip_fee_type, $server );
1497 $resp .= maybe_add( FID_TRANSACTION_ID, $status->transaction_id, $server );
1500 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1501 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1502 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1504 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1509 sub handle_renew_all {
1511 # my ($third_party, $no_block, $nb_due_date, $fee_ack, $patron);
1513 my ( $self, $server ) = @_;
1514 my $ils = $server->{ils};
1515 my ( $trans_date, $patron_id, $patron_pwd, $terminal_pwd, $fee_ack );
1516 my $fields = $self->{fields};
1517 my $resp = RENEW_ALL_RESP;
1519 my ( @renewed, @unrenewed );
1521 $ils->check_inst_id( $fields->{ (FID_INST_ID) }, "handle_renew_all" );
1523 ($trans_date) = @{ $self->{fixed_fields} };
1525 $patron_id = $fields->{ (FID_PATRON_ID) };
1526 $patron_pwd = $fields->{ (FID_PATRON_PWD) };
1527 $terminal_pwd = $fields->{ (FID_TERMINAL_PWD) };
1528 $fee_ack = $fields->{ (FID_FEE_ACK) };
1530 $status = $ils->renew_all( $patron_id, $patron_pwd, $fee_ack );
1532 $resp .= $status->ok ? '1' : '0';
1534 if ( !$status->ok ) {
1535 $resp .= add_count( "renew_all/renewed_count", 0 );
1536 $resp .= add_count( "renew_all/unrenewed_count", 0 );
1540 @renewed = ( @{ $status->renewed } );
1541 @unrenewed = ( @{ $status->unrenewed } );
1542 $resp .= add_count( "renew_all/renewed_count", scalar @renewed );
1543 $resp .= add_count( "renew_all/unrenewed_count", scalar @unrenewed );
1547 $resp .= add_field( FID_INST_ID, $ils->institution, $server );
1549 $resp .= join( '', map( add_field( FID_RENEWED_ITEMS, $_ ), @renewed ), $server );
1550 $resp .= join( '', map( add_field( FID_UNRENEWED_ITEMS, $_ ), @unrenewed ), $server );
1552 $resp .= maybe_add( FID_SCREEN_MSG, $status->screen_msg, $server );
1553 $resp .= maybe_add( FID_PRINT_LINE, $status->print_line, $server );
1555 $self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1561 # send_acs_status($self, $server)
1563 # Send an ACS Status message, which is contains lots of little fields
1564 # of information gleaned from all sorts of places.
1567 my @message_type_names = (
1568 "patron status request",
1573 "request sc/acs resend",
1575 "patron information",
1576 "end patron session",
1579 "item status update",
1586 sub send_acs_status {
1587 my ( $self, $server, $screen_msg, $print_line ) = @_;
1589 my $msg = ACS_STATUS;
1590 ($server) or die "send_acs_status error: no \$server argument received";
1591 my $account = $server->{account} or die "send_acs_status error: no 'account' in \$server object:\n" . Dumper($server);
1592 my $policy = $server->{policy} or die "send_acs_status error: no 'policy' in \$server object:\n" . Dumper($server);
1593 my $ils = $server->{ils} or die "send_acs_status error: no 'ils' in \$server object:\n" . Dumper($server);
1594 my $sip_username = $server->{sip_username} or die "send_acs_status error: no 'sip_username' in \$server object:\n" . Dumper($server);
1595 my ( $online_status, $checkin_ok, $checkout_ok, $ACS_renewal_policy );
1596 my ( $status_update_ok, $offline_ok, $timeout, $retries );
1597 my $sip_user = Koha::Patrons->find({ userid => $sip_username });
1598 die "send_acs_status error: sip_username cannot be found in DB or DB cannot be reached" unless $sip_user;
1600 $online_status = 'Y';
1601 $checkout_ok = sipbool( $ils->checkout_ok );
1602 $checkin_ok = sipbool( $ils->checkin_ok );
1603 $ACS_renewal_policy = sipbool( $policy->{renewal} );
1604 $status_update_ok = sipbool( $ils->status_update_ok );
1605 $offline_ok = sipbool( $ils->offline_ok );
1606 $timeout = $server->get_timeout({ policy => 1 });
1607 $retries = sprintf( "%03d", $policy->{retries} );
1609 if ( length($retries) != 3 ) {
1610 siplog( "LOG_ERR", "handle_acs_status: retries field wrong size: '%s'", $retries );
1614 $msg .= "$online_status$checkin_ok$checkout_ok$ACS_renewal_policy";
1615 $msg .= "$status_update_ok$offline_ok$timeout$retries";
1616 $msg .= timestamp();
1618 if ( $protocol_version == 1 ) {
1620 } elsif ( $protocol_version == 2 ) {
1623 siplog( "LOG_ERR", 'Bad setting for $protocol_version, "%s" in send_acs_status', $protocol_version );
1628 $msg .= add_field( FID_INST_ID, $account->{institution}, $server );
1630 if ( $protocol_version >= 2 ) {
1632 # Supported messages: we do it all
1633 my $supported_msgs = '';
1635 foreach my $msg_name (@message_type_names) {
1636 if ( $msg_name eq 'request sc/acs resend' ) {
1637 $supported_msgs .= sipbool(1);
1639 $supported_msgs .= sipbool( $ils->supports($msg_name) );
1642 if ( length($supported_msgs) < 16 ) {
1643 siplog( "LOG_ERR", 'send_acs_status: supported messages "%s" too short', $supported_msgs );
1645 $msg .= add_field( FID_SUPPORTED_MSGS, $supported_msgs, $server );
1648 $msg .= maybe_add( FID_SCREEN_MSG, $screen_msg, $server );
1650 if ( defined( $account->{print_width} )
1651 && defined($print_line)
1652 && $account->{print_width} < length($print_line) ) {
1653 siplog( "LOG_WARNING", "send_acs_status: print line '%s' too long. Truncating", $print_line );
1654 $print_line = substr( $print_line, 0, $account->{print_width} );
1657 $msg .= maybe_add( FID_PRINT_LINE, $print_line, $server );
1659 # Do we want to tell the terminal its location?
1661 $self->write_msg( $msg, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );
1666 # build_patron_status: create the 14-char patron status
1667 # string for the Patron Status message
1669 sub patron_status_string {
1675 siplog( "LOG_DEBUG", "patron_status_string: %s charge_ok: %s", $patron->id, $patron->charge_ok );
1676 $patron_status = sprintf(
1677 '%s%s%s%s%s%s%s%s%s%s%s%s%s%s',
1678 denied( $patron->charge_ok ),
1679 denied( $patron->renew_ok ),
1680 denied( $patron->recall_ok ),
1681 denied( $patron->hold_ok ),
1682 boolspace( $patron->card_lost ),
1683 boolspace( $patron->too_many_charged ),
1684 $server->{account}->{overdues_block_checkout} ? boolspace( $patron->too_many_overdue ) : q{ },
1685 boolspace( $patron->too_many_renewal ),
1686 boolspace( $patron->too_many_claim_return ),
1687 boolspace( $patron->too_many_lost ),
1688 boolspace( $patron->excessive_fines ),
1689 boolspace( $patron->excessive_fees ),
1690 boolspace( $patron->recall_overdue ),
1691 boolspace( $patron->too_many_billed )
1693 return $patron_status;
1697 my ( $username, $password, $branch ) = @_;
1698 $ENV{REMOTE_USER} = $username;
1699 my $query = CGI->new();
1700 $query->param( userid => $username );
1701 $query->param( password => $password );
1703 $query->param( branch => $branch );
1705 my ( $status, $cookie, $sessionID ) = check_api_auth( $query, { circulate => 1 }, 'intranet' );