2 # Copyright (C) 2006-2008 Georgia Public Library Service
4 # Author: David J. Fiander
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # A Class for handing SIP messages
29 use Sys::Syslog qw(syslog);
30 use UNIVERSAL qw(can);
33 use Sip::Constants qw(:all);
34 use Sip::Checksum qw(verify_cksum);
38 our (@ISA, @EXPORT_OK, $VERSION);
41 @EXPORT_OK = qw(handle);
44 # Predeclare handler subroutines
45 use subs qw(handle_patron_status handle_checkout handle_checkin
46 handle_block_patron handle_sc_status handle_request_acs_resend
47 handle_login handle_patron_info handle_end_patron_session
48 handle_fee_paid handle_item_information handle_item_status_update
49 handle_patron_enable handle_hold handle_renew handle_renew_all);
52 # For the most part, Version 2.00 of the protocol just adds new
53 # variable fields, but sometimes it changes the fixed header.
55 # In general, if there's no '2.00' protocol entry for a handler, that's
56 # because 2.00 didn't extend the 1.00 version of the protocol. This will
57 # be handled by the module initialization code following the declaration,
58 # which goes through the handlers table and creates a '2.00' entry that
59 # points to the same place as the '1.00' entry. If there's a 2.00 entry
60 # but no 1.00 entry, then that means that it's a completely new service
61 # in 2.00, so 1.00 shouldn't recognize it.
64 (PATRON_STATUS_REQ) => {
65 name => "Patron Status Request",
66 handler => \&handle_patron_status,
71 fields => [(FID_INST_ID), (FID_PATRON_ID),
72 (FID_TERMINAL_PWD), (FID_PATRON_PWD)],
78 handler => \&handle_checkout,
81 template => "CCA18A18",
83 fields => [(FID_INST_ID), (FID_PATRON_ID),
84 (FID_ITEM_ID), (FID_TERMINAL_PWD)],
87 template => "CCA18A18",
89 fields => [(FID_INST_ID), (FID_PATRON_ID),
90 (FID_ITEM_ID), (FID_TERMINAL_PWD),
91 (FID_ITEM_PROPS), (FID_PATRON_PWD),
92 (FID_FEE_ACK), (FID_CANCEL)],
98 handler => \&handle_checkin,
101 template => "CA18A18",
103 fields => [(FID_CURRENT_LOCN), (FID_INST_ID),
104 (FID_ITEM_ID), (FID_TERMINAL_PWD)],
107 template => "CA18A18",
109 fields => [(FID_CURRENT_LOCN), (FID_INST_ID),
110 (FID_ITEM_ID), (FID_TERMINAL_PWD),
111 (FID_ITEM_PROPS), (FID_CANCEL)],
116 name => "Block Patron",
117 handler => \&handle_block_patron,
122 fields => [(FID_INST_ID), (FID_BLOCKED_CARD_MSG),
123 (FID_PATRON_ID), (FID_TERMINAL_PWD)],
129 handler => \&handle_sc_status,
138 (REQUEST_ACS_RESEND) => {
139 name => "Request ACS Resend",
140 handler => \&handle_request_acs_resend,
151 handler => \&handle_login,
156 fields => [(FID_LOGIN_UID), (FID_LOGIN_PWD),
157 (FID_LOCATION_CODE)],
162 name => "Patron Info",
163 handler => \&handle_patron_info,
166 template => "A3A18A10",
168 fields => [(FID_INST_ID), (FID_PATRON_ID),
169 (FID_TERMINAL_PWD), (FID_PATRON_PWD),
170 (FID_START_ITEM), (FID_END_ITEM)],
174 (END_PATRON_SESSION) => {
175 name => "End Patron Session",
176 handler => \&handle_end_patron_session,
181 fields => [(FID_INST_ID), (FID_PATRON_ID),
182 (FID_TERMINAL_PWD), (FID_PATRON_PWD)],
188 handler => \&handle_fee_paid,
191 template => "A18A2A2A3",
193 fields => [(FID_FEE_AMT), (FID_INST_ID),
194 (FID_PATRON_ID), (FID_TERMINAL_PWD),
195 (FID_PATRON_PWD), (FID_FEE_ID),
196 (FID_TRANSACTION_ID)],
200 (ITEM_INFORMATION) => {
201 name => "Item Information",
202 handler => \&handle_item_information,
207 fields => [(FID_INST_ID), (FID_ITEM_ID),
212 (ITEM_STATUS_UPDATE) => {
213 name => "Item Status Update",
214 handler => \&handle_item_status_update,
219 fields => [(FID_INST_ID), (FID_PATRON_ID),
220 (FID_ITEM_ID), (FID_TERMINAL_PWD),
226 name => "Patron Enable",
227 handler => \&handle_patron_enable,
232 fields => [(FID_INST_ID), (FID_PATRON_ID),
233 (FID_TERMINAL_PWD), (FID_PATRON_PWD)],
239 handler => \&handle_hold,
244 fields => [(FID_EXPIRATION), (FID_PICKUP_LOCN),
245 (FID_HOLD_TYPE), (FID_INST_ID),
246 (FID_PATRON_ID), (FID_PATRON_PWD),
247 (FID_ITEM_ID), (FID_TITLE_ID),
248 (FID_TERMINAL_PWD), (FID_FEE_ACK)],
254 handler => \&handle_renew,
257 template => "CCA18A18",
259 fields => [(FID_INST_ID), (FID_PATRON_ID),
260 (FID_PATRON_PWD), (FID_ITEM_ID),
261 (FID_TITLE_ID), (FID_TERMINAL_PWD),
262 (FID_ITEM_PROPS), (FID_FEE_ACK)],
268 handler => \&handle_renew_all,
273 fields => [(FID_INST_ID), (FID_PATRON_ID),
274 (FID_PATRON_PWD), (FID_TERMINAL_PWD),
282 # Now, initialize some of the missing bits of %handlers
284 foreach my $i (keys(%handlers)) {
285 if (!exists($handlers{$i}->{protocol}->{2})) {
287 $handlers{$i}->{protocol}->{2} = $handlers{$i}->{protocol}->{1};
291 # Stolen from ../ILS.pm, useful in here as well
295 # If it's defined, and matches a true sort of string, or is
296 # a non-zero number, then it's true.
297 return defined($bool) && (($bool =~ /true|y|yes/i) || $bool != 0);
301 my ($class, $msg, $seqno) = @_;
303 my $msgtag = substr($msg, 0, 2);
305 syslog("LOG_DEBUG", "Sip::MsgType::new('%s', '%s', '%s'): msgtag '%s'",
306 $class, substr($msg, 0, 10), $msgtag, $seqno);
307 if ($msgtag eq LOGIN) {
308 # If the client is using the 2.00-style "Login" message
309 # to authenticate to the server, then we get the Login message
310 # _before_ the client has indicated that it supports 2.00, but
311 # it's using the 2.00 login process, so it must support 2.00,
312 # so we'll just do it.
313 $protocol_version = 2;
315 if (!exists($handlers{$msgtag})) {
316 syslog("LOG_WARNING",
317 "new Sip::MsgType: Skipping message of unknown type '%s' in '%s'",
320 } elsif (!exists($handlers{$msgtag}->{protocol}->{$protocol_version})) {
321 syslog("LOG_WARNING", "new Sip::MsgType: Skipping message '%s' unsupported by protocol rev. '%d'",
322 $msgtag, $protocol_version);
328 $self->{seqno} = $seqno;
329 $self->_initialize(substr($msg,2), $handlers{$msgtag});
335 my ($self, $msg, $control_block) = @_;
337 my $proto = $control_block->{protocol}->{$protocol_version};
339 $self->{name} = $control_block->{name};
340 $self->{handler} = $control_block->{handler};
342 $self->{fields} = {};
343 $self->{fixed_fields} = [];
345 syslog("LOG_DEBUG", "Sip::MsgType::_initialize('%s', '%s...')", $self->{name}, substr($msg,0,20));
348 foreach my $field (@{$proto->{fields}}) {
349 $self->{fields}->{$field} = undef;
353 "Sip::MsgType::_initialize('%s', '%s', '%s', '%s', ...",
354 $self->{name}, $msg, $proto->{template},
355 $proto->{template_len});
357 $self->{fixed_fields} = [ unpack($proto->{template}, $msg) ];
359 # Skip over the fixed fields and the split the rest of
360 # the message into fields based on the delimiter and parse them
361 foreach my $field (split(quotemeta($field_delimiter), substr($msg, $proto->{template_len}))) {
362 $fn = substr($field, 0, 2);
364 if (!exists($self->{fields}->{$fn})) {
365 syslog("LOG_WARNING",
366 "Unsupported field '%s' in %s message '%s'",
367 $fn, $self->{name}, $msg);
368 } elsif (defined($self->{fields}->{$fn})) {
369 syslog("LOG_WARNING",
370 "Duplicate field '%s' (previous value '%s') in %s message '%s'",
371 $fn, $self->{fields}->{$fn}, $self->{name}, $msg);
373 $self->{fields}->{$fn} = substr($field, 2);
381 my ($msg, $server, $req) = @_;
382 my $config = $server->{config};
387 # What's the field delimiter for variable length fields?
388 # This can't be based on the account, since we need to know
389 # the field delimiter to parse a SIP login message
391 if (defined($server->{config}->{delimiter})) {
392 $field_delimiter = $server->{config}->{delimiter};
395 # error detection is active if this is a REQUEST_ACS_RESEND
396 # message with a checksum, or if the message is long enough
397 # and the last nine characters begin with a sequence number
399 if ($msg eq REQUEST_ACS_RESEND_CKSUM) {
402 $error_detection = 1;
403 $self = new Sip::MsgType ((REQUEST_ACS_RESEND), 0);
404 } elsif((length($msg) > 11) && (substr($msg, -9, 2) eq "AY")) {
405 $error_detection = 1;
407 if (!verify_cksum($msg)) {
408 syslog("LOG_WARNING", "Checksum failed on message '%s'", $msg);
409 # REQUEST_SC_RESEND with error detection
410 $last_response = REQUEST_SC_RESEND_CKSUM;
411 print("$last_response\r");
412 return REQUEST_ACS_RESEND;
414 # Save the sequence number, then strip off the
415 # error detection data to process the message
416 $self = new Sip::MsgType (substr($msg, 0, -9), substr($msg, -7, 1));
418 } elsif ($error_detection) {
419 # We've receive a non-ED message when ED is supposed
420 # to be active. Warn about this problem, then process
421 # the message anyway.
422 syslog("LOG_WARNING",
423 "Received message without error detection: '%s'", $msg);
424 $error_detection = 0;
425 $self = new Sip::MsgType ($msg, 0);
427 $self = new Sip::MsgType ($msg, 0);
430 if ((substr($msg, 0, 2) ne REQUEST_ACS_RESEND) &&
431 $req && (substr($msg, 0, 2) ne $req)) {
432 return substr($msg, 0, 2);
434 return($self->{handler}->($self, $server));
442 # Patron status messages are produced in response to both
443 # "Request Patron Status" and "Block Patron"
445 # Request Patron Status requires a patron password, but
446 # Block Patron doesn't (since the patron may never have
447 # provided one before attempting some illegal action).
449 # ASSUMPTION: If the patron password field is present in the
450 # message, then it must match, otherwise incomplete patron status
451 # information will be returned to the terminal.
453 sub build_patron_status {
454 my ($patron, $lang, $fields, $server)= @_;
456 my $patron_pwd = $fields->{(FID_PATRON_PWD)};
457 my $resp = (PATRON_STATUS_RESP);
460 $resp .= patron_status_string($patron);
461 $resp .= $lang . Sip::timestamp();
463 # while the patron ID we got from the SC is valid, let's
464 # use the one returned from the ILS, just in case...
465 $resp .= add_field(FID_INST_ID, $fields->{(FID_INST_ID)});
466 $resp .= add_field(FID_PATRON_ID, $patron->id);
467 $resp .= add_field(FID_PERSONAL_NAME, $patron->name);
468 if ($protocol_version >= 2) {
469 $resp .= add_field(FID_VALID_PATRON, 'Y');
470 # Patron password is a required field.
471 $resp .= add_field(FID_VALID_PATRON_PWD, sipbool($patron->check_password($patron_pwd)));
472 $resp .= maybe_add(FID_CURRENCY, $patron->currency);
473 $resp .= maybe_add(FID_FEE_AMT, $patron->fee_amount);
476 if ($server->{institution}->relais_extensions_to_msg24()) {
477 $resp .= maybe_add(FID_HOME_ADDR, $patron->address );
478 $resp .= maybe_add(FID_EMAIL, $patron->email_addr);
479 $resp .= maybe_add(FID_HOME_PHONE, $patron->home_phone);
484 $resp .= maybe_add(FID_SCREEN_MSG, $patron->screen_msg);
485 $resp .= maybe_add(FID_PRINT_LINE, $patron->print_line);
487 # Invalid patron id. Report that the user has no privs.,
488 # no personal name, and is invalid (if we're using 2.00)
489 $resp .= 'YYYY' . (' ' x 10) . $lang . Sip::timestamp();
490 $resp .= add_field(FID_INST_ID, $fields->{(FID_INST_ID)});
492 # the patron ID is invalid, but it's a required field, so
494 $resp .= add_field(FID_PATRON_ID, $fields->{(FID_PATRON_ID)});
495 $resp .= add_field(FID_PERSONAL_NAME, '');
497 if ($protocol_version >= 2) {
498 $resp .= add_field(FID_VALID_PATRON, 'N');
506 sub handle_patron_status {
507 my ($self, $server) = @_;
508 my $ils = $server->{ils};
512 my $resp = (PATRON_STATUS_RESP);
513 my $account = $server->{account};
515 ($lang, $date) = @{$self->{fixed_fields}};
516 $fields = $self->{fields};
518 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_patron_status");
520 $patron = $ils->find_patron($fields->{(FID_PATRON_ID)});
522 $resp = build_patron_status($patron, $lang, $fields, $server);
524 $self->write_msg($resp);
526 return (PATRON_STATUS_REQ);
529 sub handle_checkout {
530 my ($self, $server) = @_;
531 my $account = $server->{account};
532 my $ils = $server->{ils};
533 my $inst = $ils->institution;
534 my ($sc_renewal_policy, $no_block, $trans_date, $nb_due_date);
536 my ($patron_id, $item_id, $status, $fee_ack);
540 ($sc_renewal_policy, $no_block, $trans_date, $nb_due_date) =
541 @{$self->{fixed_fields}};
542 $fields = $self->{fields};
544 $patron_id = $fields->{(FID_PATRON_ID)};
545 $item_id = $fields->{(FID_ITEM_ID)};
546 $fee_ack = $fields->{(FID_FEE_ACK)};
549 if ($no_block eq 'Y') {
550 # Off-line transactions need to be recorded, but there's
551 # not a lot we can do about it
552 syslog("LOG_WARNING", "received no-block checkout from terminal '%s'",
555 $status = $ils->checkout_no_block($patron_id, $item_id,
557 $trans_date, $nb_due_date);
559 # Does the transaction date really matter for items that are
560 # checkout out while the terminal is online? I'm guessing 'no'
561 $status = $ils->checkout($patron_id, $item_id, $sc_renewal_policy, $fee_ack);
565 $item = $status->item;
566 $patron = $status->patron;
569 # Item successfully checked out
571 $resp = CHECKOUT_RESP . '1';
572 $resp .= sipbool($status->renew_ok);
573 if ($ils->supports('magnetic media')) {
574 $resp .= sipbool($item->magnetic);
578 # We never return the obsolete 'U' value for 'desensitize'
579 $resp .= sipbool($status->desensitize);
580 $resp .= Sip::timestamp;
582 # Now for the variable fields
583 $resp .= add_field(FID_INST_ID, $inst);
584 $resp .= add_field(FID_PATRON_ID, $patron_id);
585 $resp .= add_field(FID_ITEM_ID, $item_id);
586 $resp .= add_field(FID_TITLE_ID, $item->title_id);
587 $resp .= add_field(FID_DUE_DATE, $item->due_date);
589 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
590 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
592 if ($protocol_version >= 2) {
593 if ($ils->supports('security inhibit')) {
594 $resp .= add_field(FID_SECURITY_INHIBIT, $status->security_inhibit);
596 $resp .= maybe_add(FID_MEDIA_TYPE, $item->sip_media_type);
597 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
600 if ($status->fee_amount) {
601 $resp .= add_field(FID_FEE_AMT, $status->fee_amount);
602 $resp .= maybe_add(FID_CURRENCY, $status->sip_currency);
603 $resp .= maybe_add(FID_FEE_TYPE, $status->sip_fee_type);
604 $resp .= maybe_add(FID_TRANSACTION_ID,
605 $status->transaction_id);
611 # Checkout Response: not ok, no renewal, don't know mag. media,
613 $resp = sprintf("120%sUN%s", sipbool($status->renew_ok), Sip::timestamp);
614 $resp .= add_field(FID_INST_ID, $inst);
615 $resp .= add_field(FID_PATRON_ID, $patron_id);
616 $resp .= add_field(FID_ITEM_ID, $item_id);
618 # If the item is valid, provide the title, otherwise
620 $resp .= add_field(FID_TITLE_ID, $item ? $item->title_id : '');
621 # Due date is required. Since it didn't get checked out,
622 # it's not due, so leave the date blank
623 $resp .= add_field(FID_DUE_DATE, '');
625 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
626 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
628 if ($protocol_version >= 2) {
629 # Is the patron ID valid?
630 $resp .= add_field(FID_VALID_PATRON, sipbool($patron));
632 if ($patron && exists($fields->{FID_PATRON_PWD})) {
633 # Password provided, so we can tell if it was valid or not
634 $resp .= add_field(FID_VALID_PATRON_PWD,
635 sipbool($patron->check_password($fields->{(FID_PATRON_PWD)})));
637 # For the patron to accept a fee in chargeable loans, we
638 # need to return fee information.
639 if ($status->fee_amount) {
640 $resp .= add_field(FID_FEE_AMT, $status->fee_amount);
641 $resp .= maybe_add(FID_CURRENCY, $status->sip_currency);
642 $resp .= maybe_add(FID_FEE_TYPE, $status->sip_fee_type);
647 $self->write_msg($resp);
652 my ($self, $server) = @_;
653 my $account = $server->{account};
654 my $ils = $server->{ils};
655 my ($current_loc, $inst_id, $item_id, $terminal_pwd, $item_props, $cancel);
656 my ($patron, $item, $status);
657 my $resp = CHECKIN_RESP;
659 my ($no_block, $trans_date, $return_date) = @{$self->{fixed_fields}};
660 my $fields = $self->{fields};
662 $current_loc = $fields->{(FID_CURRENT_LOCN)};
663 $inst_id = $fields->{(FID_INST_ID) };
664 $item_id = $fields->{(FID_ITEM_ID) };
665 $item_props = $fields->{(FID_ITEM_PROPS) };
666 $cancel = $fields->{(FID_CANCEL) };
668 $ils->check_inst_id($inst_id, "handle_checkin");
670 if ($no_block eq 'Y') {
671 # Off-line transactions, ick.
672 syslog("LOG_WARNING", "received no-block checkin from terminal '%s'", $account->{id});
673 $status = $ils->checkin_no_block($item_id, $trans_date, $return_date, $item_props, $cancel);
675 $status = $ils->checkin($item_id, $inst_id, $trans_date, $return_date, $current_loc, $item_props, $cancel);
678 $patron = $status->patron;
679 $item = $status->item;
681 $resp .= $status->ok ? '1' : '0';
682 $resp .= $status->resensitize ? 'Y' : 'N';
683 if ($item && $ils->supports('magnetic media')) {
684 $resp .= sipbool($item->magnetic);
686 # The item barcode was invalid or the system doesn't support
687 # the 'magnetic media' indicator
690 $resp .= $status->alert ? 'Y' : 'N';
691 $resp .= Sip::timestamp;
692 $resp .= add_field(FID_INST_ID, $inst_id);
693 $resp .= add_field(FID_ITEM_ID, $item_id);
696 $resp .= add_field(FID_PERM_LOCN, $item->permanent_location);
697 $resp .= maybe_add(FID_TITLE_ID, $item->title_id);
700 if ($protocol_version >= 2) {
701 $resp .= maybe_add(FID_SORT_BIN, $status->sort_bin);
703 $resp .= add_field(FID_PATRON_ID, $patron->id);
706 $resp .= maybe_add(FID_MEDIA_TYPE, $item->sip_media_type );
707 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
708 $resp .= maybe_add(FID_COLLECTION_CODE, $item->collection_code );
709 $resp .= maybe_add(FID_CALL_NUMBER, $item->call_number );
710 $resp .= maybe_add(FID_DESTINATION_LOCATION, $item->destination_loc );
711 $resp .= maybe_add(FID_HOLD_PATRON_ID, $item->hold_patron_bcode );
712 $resp .= maybe_add(FID_HOLD_PATRON_NAME, $item->hold_patron_name );
716 $resp .= maybe_add(FID_ALERT_TYPE, $status->alert_type) if $status->alert;
717 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
718 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
720 $self->write_msg($resp);
725 sub handle_block_patron {
726 my ($self, $server) = @_;
727 my $account = $server->{account};
728 my $ils = $server->{ils};
729 my ($card_retained, $trans_date);
730 my ($inst_id, $blocked_card_msg, $patron_id, $terminal_pwd);
735 ($card_retained, $trans_date) = @{$self->{fixed_fields}};
736 $fields = $self->{fields};
737 $inst_id = $fields->{(FID_INST_ID)};
738 $blocked_card_msg = $fields->{(FID_BLOCKED_CARD_MSG)};
739 $patron_id = $fields->{(FID_PATRON_ID)};
740 $terminal_pwd = $fields->{(FID_TERMINAL_PWD)};
742 # Terminal passwords are different from account login
743 # passwords, but I have no idea what to do with them. So,
744 # I'll just ignore them for now.
746 $ils->check_inst_id($inst_id, "block_patron");
748 $patron = $ils->find_patron($patron_id);
750 # The correct response for a "Block Patron" message is a
751 # "Patron Status Response", so use that handler to generate
752 # the message, but then return the correct code from here.
754 # Normally, the language is provided by the "Patron Status"
755 # fixed field, but since we're not responding to one of those
756 # we'll just say, "Unspecified", as per the spec. Let the
757 # terminal default to something that, one hopes, will be
759 my $language = $patron ? $patron->language : '000';
762 $patron->block($card_retained, $blocked_card_msg);
765 $resp = build_patron_status($patron, $language, $fields, $server);
767 $self->write_msg($resp);
768 return(BLOCK_PATRON);
771 sub handle_sc_status {
772 my ($self, $server) = @_;
773 my ($status, $print_width, $sc_protocol_version, $new_proto);
775 ($status, $print_width, $sc_protocol_version) = @{$self->{fixed_fields}};
777 if ($sc_protocol_version =~ /^1\./) {
779 } elsif ($sc_protocol_version =~ /^2\./) {
782 syslog("LOG_WARNING", "Unrecognized protocol revision '%s', falling back to '1'", $sc_protocol_version);
786 if ($new_proto != $protocol_version) {
787 syslog("LOG_INFO", "Setting protocol level to $new_proto");
788 $protocol_version = $new_proto;
791 unless (defined $server->{account}) {
792 # If we haven't logged in yet, go ahead and
793 # return the SC status anyway, arbitrarily using the
794 # first account in Perl string sort order to specify
795 # the account, institution, and ILS. This supports
796 # raw clients such as Relais that insist on sending 99 first
798 syslog('LOG_INFO', 'sending SC status without logging in first');
800 $mock_server->{config} = $server->{config};
801 my $uid = (sort keys %{ $server->{config}->{accounts} })[0];
802 _load_ils_handler($mock_server, $uid);
803 return send_acs_status($self, $mock_server) ? SC_STATUS : '';
806 if ($status == SC_STATUS_PAPER) {
807 syslog("LOG_WARNING", "Self-Check unit '%s@%s' out of paper",
808 $server->{account}->{id}, $server->{account}->{institution});
809 } elsif ($status == SC_STATUS_SHUTDOWN) {
810 syslog("LOG_WARNING", "Self-Check unit '%s@%s' shutting down",
811 $server->{account}->{id}, $server->{account}->{institution});
814 $server->{account}->{print_width} = $print_width;
816 return send_acs_status($self, $server) ? SC_STATUS : '';
819 sub handle_request_acs_resend {
820 my ($self, $server) = @_;
822 if (!$last_response) {
823 # We haven't sent anything yet, so respond with a
824 # REQUEST_SC_RESEND msg (p. 16)
825 $self->write_msg(REQUEST_SC_RESEND);
826 } elsif ((length($last_response) < 9)
827 || substr($last_response, -9, 2) ne 'AY') {
828 # When resending a message, we aren't supposed to include
829 # a sequence number, even if the original had one (p. 4).
830 # If the last message didn't have a sequence number, then
831 # we can just send it.
832 print("$last_response\r");
834 # Cut out the sequence number and checksum, since the old
835 # checksum is wrong for the resent message.
836 $self->write_msg(substr($last_response, 0, -9));
838 return REQUEST_ACS_RESEND;
842 my ($self, $server) = @_;
843 my ($uid_algorithm, $pwd_algorithm, $sc_loc);
847 my $status = 1; # Assume it all works
849 $fields = $self->{fields};
850 ($uid_algorithm, $pwd_algorithm) = @{$self->{fixed_fields}};
852 $uid = $fields->{(FID_LOGIN_UID)};
853 $pwd = $fields->{(FID_LOGIN_PWD)};
854 $sc_loc = $fields->{(FID_LOCATION_CODE)};
856 if ($uid_algorithm || $pwd_algorithm) {
857 syslog("LOG_ERR", "LOGIN: Can't cope with non-zero encryption methods: uid = $uid_algorithm, pwd = $pwd_algorithm");
861 if (!exists($server->{config}->{accounts}->{$uid})) {
862 syslog("LOG_WARNING", "MsgType::handle_login: Unknown login '$uid'");
864 } elsif ($server->{config}->{accounts}->{$uid}->{password} ne $pwd) {
865 syslog("LOG_WARNING", "MsgType::handle_login: Invalid password for login '$uid'");
872 ->{ $server->{config}->{accounts}->{$uid}->{institution} }
874 ->{client_location_code}
876 $sc_loc ||= $server->{config}->{accounts}->{$uid}->{location_code};
878 # Only use config file value
879 $sc_loc = $server->{config}->{accounts}->{$uid}->{location_code};
881 _load_ils_handler($server, $uid, $sc_loc);
884 $server->{login_complete}->($status) if $server->{login_complete};
886 $self->write_msg(LOGIN_RESP . $status);
888 return $status ? LOGIN : '';
891 sub _load_ils_handler {
892 my ($server, $uid, $sc_loc) = @_;
894 # Store the active account someplace handy for everybody else to find.
895 $server->{account} = $server->{config}->{accounts}->{$uid};
896 my $inst = $server->{account}->{institution};
897 $server->{institution} = $server->{config}->{institutions}->{$inst};
898 $server->{policy} = $server->{institution}->{policy};
899 $server->{account}->{location} = $sc_loc if $sc_loc;
901 syslog("LOG_INFO", "Successful login for '%s' of '%s'", $server->{account}->{id}, $inst);
903 # initialize connection to ILS
905 my $module = $server->{config}->{institutions}->{$inst}->{implementation};
909 syslog("LOG_ERR", "%s: Loading ILS implementation '%s' for institution '%s' failed",
910 $server->{service}, $module, $inst);
911 die("Failed to load ILS implementation '$module'");
914 $server->{ils} = $module->new($server->{institution}, $server->{account});
916 if (!$server->{ils}) {
917 syslog("LOG_ERR", "%s: ILS connection to '%s' failed", $server->{service}, $inst);
918 die("Unable to connect to ILS '$inst'");
923 # Build the detailed summary information for the Patron
924 # Information Response message based on the first 'Y' that appears
925 # in the 'summary' field of the Patron Information reqest. The
926 # specification says that only one 'Y' can appear in that field,
927 # and we're going to believe it.
930 my ($ils, $patron, $summary, $start, $end) = @_;
936 # Map from offsets in the "summary" field of the Patron Information
937 # message to the corresponding field and handler
940 { func => $patron->can("hold_items"), fid => FID_HOLD_ITEMS },
941 { func => $patron->can("overdue_items"), fid => FID_OVERDUE_ITEMS },
942 { func => $patron->can("charged_items"), fid => FID_CHARGED_ITEMS },
943 { func => $patron->can("fine_items"), fid => FID_FINE_ITEMS },
944 { func => $patron->can("recall_items"), fid => FID_RECALL_ITEMS },
945 { func => $patron->can("unavail_holds"), fid => FID_UNAVAILABLE_HOLD_ITEMS },
949 if (($summary_type = index($summary, 'Y')) == -1) {
950 # No detailed information required
954 if ($summary_type > $#summary_map || not defined $summary_map[$summary_type]->{func}) {
955 # Huh, we don't have any code to handle the requested summary information.
956 # Pretend nothing was asked for instead.
960 syslog("LOG_DEBUG", "Summary_info: index == '%d', field '%s'",
961 $summary_type, $summary_map[$summary_type]->{fid});
963 $func = $summary_map[$summary_type]->{func};
964 $fid = $summary_map[$summary_type]->{fid};
965 $itemlist = &$func($patron, $start, $end);
967 syslog("LOG_DEBUG", "summary_info: list = (%s)", join(", ", @{$itemlist}));
968 foreach my $i (@{$itemlist}) {
969 $resp .= add_field($fid, $i);
975 sub handle_patron_info {
976 my ($self, $server) = @_;
977 my $ils = $server->{ils};
978 my ($lang, $trans_date, $summary) = @{$self->{fixed_fields}};
979 my $fields = $self->{fields};
980 my ($inst_id, $patron_id, $terminal_pwd, $patron_pwd, $start, $end);
981 my ($resp, $patron, $count);
982 $lang ||= '000'; # unspecified
984 $inst_id = $fields->{(FID_INST_ID)};
985 $patron_id = $fields->{(FID_PATRON_ID)};
986 $terminal_pwd = $fields->{(FID_TERMINAL_PWD)};
987 $patron_pwd = $fields->{(FID_PATRON_PWD)};
988 $start = $fields->{(FID_START_ITEM)};
989 $end = $fields->{(FID_END_ITEM)};
991 $patron = $ils->find_patron($patron_id);
993 $resp = (PATRON_INFO_RESP);
995 $resp .= patron_status_string($patron);
997 $lang = $patron->language if $patron->language;
998 $resp .= $lang . Sip::timestamp();
1000 $resp .= add_count('patron_info/hold_items', scalar @{$patron->hold_items(undef,undef,1) });
1001 $resp .= add_count('patron_info/overdue_items', scalar @{$patron->overdue_items(undef,undef,1)});
1002 $resp .= add_count('patron_info/charged_items', scalar @{$patron->charged_items(undef,undef,1)});
1003 $resp .= add_count('patron_info/fine_items', scalar @{$patron->fine_items(undef,undef,1) });
1004 $resp .= add_count('patron_info/recall_items', scalar @{$patron->recall_items(undef,undef,1) });
1005 $resp .= add_count('patron_info/unavail_holds', scalar @{$patron->unavail_holds(undef,undef,1)});
1007 $resp .= add_field(FID_INST_ID, $server->{ils}->institution);
1009 # while the patron ID we got from the SC is valid, let's
1010 # use the one returned from the ILS, just in case...
1011 $resp .= add_field(FID_PATRON_ID, $patron->id);
1013 $resp .= add_field(FID_PERSONAL_NAME, $patron->name);
1015 # TODO: add code for the fields
1017 # overdue items limit
1018 # charged items limit
1021 $resp .= maybe_add(FID_CURRENCY, $patron->currency );
1022 $resp .= maybe_add(FID_FEE_AMT, $patron->fee_amount);
1023 $resp .= maybe_add(FID_HOME_ADDR, $patron->address );
1024 $resp .= maybe_add(FID_EMAIL, $patron->email_addr);
1025 $resp .= maybe_add(FID_HOME_PHONE, $patron->home_phone);
1027 # Extension requested by PINES. Report the home system for
1028 # the patron in the 'AQ' field. This is normally the "permanent
1029 # location" field for an ITEM, but it's not used in PATRON info.
1030 # Apparently TLC systems do this.
1031 $resp .= maybe_add(FID_HOME_LIBRARY, $patron->home_library);
1033 $resp .= summary_info($ils, $patron, $summary, $start, $end);
1035 $resp .= add_field(FID_VALID_PATRON, 'Y');
1036 if (defined($patron_pwd)) {
1037 # If the patron password was provided, report on if it was right.
1038 $resp .= add_field(FID_VALID_PATRON_PWD,
1039 sipbool($patron->check_password($patron_pwd)));
1042 # SIP 2.0 extensions used by Envisionware
1043 # Other types of terminals will ignore the fields, if
1044 # they don't recognize the codes
1045 if ($patron->can('sip_expire')) {
1046 $resp .= maybe_add(FID_PATRON_EXPIRE, $patron->sip_expire);
1048 $resp .= maybe_add(FID_PATRON_BIRTHDATE, $patron->sip_birthdate);
1049 $resp .= maybe_add(FID_PATRON_CLASS, $patron->ptype);
1051 # Custom protocol extension to report patron internet privileges
1052 $resp .= maybe_add(FID_INET_PROFILE, $patron->inet_privileges);
1054 $resp .= maybe_add(FID_PATRON_INTERNAL_ID, $patron->internal_id); # another extension
1056 $resp .= maybe_add(FID_SCREEN_MSG, $patron->screen_msg);
1057 $resp .= maybe_add(FID_PRINT_LINE, $patron->print_line);
1059 # Custom ILS-defined protocol extensions
1060 if ($patron->can('extra_fields')) {
1061 my $extra_fields = $patron->extra_fields();
1062 foreach my $field (keys %$extra_fields) {
1063 foreach my $value (@{$extra_fields->{ $field }}) {
1064 $resp .= maybe_add($field, $value);
1070 # He has no privileges, no items associated with him,
1071 # no personal name, and is invalid (if we're using 2.00)
1072 $resp .= 'YYYY' . (' ' x 10) . $lang . Sip::timestamp();
1073 $resp .= '0000' x 6;
1075 $resp .= add_field(FID_INST_ID, $server->{ils}->institution);
1076 # the patron ID is invalid, but it's a required field, so
1078 $resp .= add_field(FID_PATRON_ID, $fields->{(FID_PATRON_ID)});
1079 $resp .= add_field(FID_PERSONAL_NAME, '');
1081 if ($protocol_version >= 2) {
1082 $resp .= add_field(FID_VALID_PATRON, 'N');
1086 $self->write_msg($resp);
1088 return(PATRON_INFO);
1091 sub handle_end_patron_session {
1092 my ($self, $server) = @_;
1093 my $ils = $server->{ils};
1095 my $fields = $self->{fields};
1096 my $resp = END_SESSION_RESP;
1097 my ($status, $screen_msg, $print_line);
1099 ($trans_date) = @{$self->{fixed_fields}};
1101 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_end_patron_session");
1103 ($status, $screen_msg, $print_line) = $ils->end_patron_session($fields->{(FID_PATRON_ID)});
1105 $resp .= $status ? 'Y' : 'N';
1106 $resp .= Sip::timestamp();
1108 $resp .= add_field(FID_INST_ID, $server->{ils}->institution);
1109 $resp .= add_field(FID_PATRON_ID, $fields->{(FID_PATRON_ID)});
1111 $resp .= maybe_add(FID_SCREEN_MSG, $screen_msg);
1112 $resp .= maybe_add(FID_PRINT_LINE, $print_line);
1114 $self->write_msg($resp);
1116 return(END_PATRON_SESSION);
1119 sub handle_fee_paid {
1120 my ($self, $server) = @_;
1121 my $ils = $server->{ils};
1122 my ($trans_date, $fee_type, $pay_type, $currency) = @{$self->{fixed_fields}};
1123 my $fields = $self->{fields};
1124 my ($fee_amt, $inst_id, $patron_id, $terminal_pwd, $patron_pwd);
1125 my ($fee_id, $trans_id);
1127 my $resp = FEE_PAID_RESP;
1129 $fee_amt = $fields->{(FID_FEE_AMT)};
1130 $inst_id = $fields->{(FID_INST_ID)};
1131 $patron_id = $fields->{(FID_PATRON_ID)};
1132 $patron_pwd = $fields->{(FID_PATRON_PWD)};
1133 $fee_id = $fields->{(FID_FEE_ID)};
1134 $trans_id = $fields->{(FID_TRANSACTION_ID)};
1136 $ils->check_inst_id($inst_id, "handle_fee_paid");
1138 $status = $ils->pay_fee($patron_id, $patron_pwd, $fee_amt, $fee_type,
1139 $pay_type, $fee_id, $trans_id, $currency);
1141 $resp .= ($status->ok ? 'Y' : 'N') . Sip::timestamp;
1142 $resp .= add_field(FID_INST_ID, $inst_id);
1143 $resp .= add_field(FID_PATRON_ID, $patron_id);
1144 $resp .= maybe_add(FID_TRANSACTION_ID, $status->transaction_id);
1145 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1146 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1148 $self->write_msg($resp);
1153 sub handle_item_information {
1154 my ($self, $server) = @_;
1155 my $ils = $server->{ils};
1157 my $fields = $self->{fields};
1158 my $resp = ITEM_INFO_RESP;
1161 ($trans_date) = @{$self->{fixed_fields}};
1163 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_item_information");
1165 $item = $ils->find_item($fields->{(FID_ITEM_ID)});
1167 if (!defined($item)) {
1169 # "Other" circ stat, "Other" security marker, "Unknown" fee type
1171 $resp .= Sip::timestamp;
1172 # Just echo back the invalid item id
1173 $resp .= add_field(FID_ITEM_ID, $fields->{(FID_ITEM_ID)});
1174 # title id is required, but we don't have one
1175 $resp .= add_field(FID_TITLE_ID, '');
1177 # Valid Item ID, send the good stuff
1178 $resp .= $item->sip_circulation_status;
1179 $resp .= $item->sip_security_marker;
1180 $resp .= $item->sip_fee_type;
1181 $resp .= Sip::timestamp;
1183 $resp .= add_field(FID_ITEM_ID, $item->id);
1184 $resp .= add_field(FID_TITLE_ID, $item->title_id);
1186 $resp .= maybe_add(FID_MEDIA_TYPE, $item->sip_media_type);
1187 $resp .= maybe_add(FID_PERM_LOCN, $item->permanent_location);
1188 $resp .= maybe_add(FID_CURRENT_LOCN, $item->current_location);
1189 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
1192 $resp .= add_field(FID_CURRENCY, $item->fee_currency);
1193 $resp .= add_field(FID_FEE_AMT, $item->fee);
1195 $resp .= maybe_add(FID_OWNER, $item->owner);
1196 $resp .= maybe_add(FID_HOLD_QUEUE_LEN, scalar @{$item->hold_queue});
1197 $resp .= maybe_add(FID_DUE_DATE, $item->due_date);
1198 $resp .= maybe_add(FID_RECALL_DATE, $item->recall_date);
1199 $resp .= maybe_add(FID_HOLD_PICKUP_DATE, $item->hold_pickup_date);
1200 $resp .= maybe_add(FID_DESTINATION_LOCATION, $item->destination_loc); # Extension for AMH sorting
1201 $resp .= maybe_add(FID_CALL_NUMBER, $item->call_number); # Extension for AMH sorting
1202 $resp .= maybe_add(FID_SCREEN_MSG, $item->screen_msg);
1203 $resp .= maybe_add(FID_PRINT_LINE, $item->print_line);
1205 # Custom ILS-defined protocol extensions
1206 if ($item->can('extra_fields')) {
1207 my $extra_fields = $item->extra_fields();
1208 foreach my $field (keys %$extra_fields) {
1209 foreach my $value (@{$extra_fields->{ $field }}) {
1210 $resp .= maybe_add($field, $value);
1216 $self->write_msg($resp);
1218 return(ITEM_INFORMATION);
1221 sub handle_item_status_update {
1222 my ($self, $server) = @_;
1223 my $ils = $server->{ils};
1224 my ($trans_date, $item_id, $terminal_pwd, $item_props);
1225 my $fields = $self->{fields};
1228 my $resp = ITEM_STATUS_UPDATE_RESP;
1230 ($trans_date) = @{$self->{fixed_fields}};
1232 $ils->check_inst_id($fields->{(FID_INST_ID)});
1234 $item_id = $fields->{(FID_ITEM_ID)};
1235 $item_props = $fields->{(FID_ITEM_PROPS)};
1237 if (!defined($item_id)) {
1238 syslog("LOG_WARNING", "handle_item_status: received message without Item ID field");
1240 $item = $ils->find_item($item_id);
1246 $resp .= Sip::timestamp;
1247 $resp .= add_field(FID_ITEM_ID, $item_id);
1250 $status = $item->status_update($item_props);
1252 $resp .= $status->ok ? '1' : '0';
1253 $resp .= Sip::timestamp;
1255 $resp .= add_field(FID_ITEM_ID, $item->id);
1256 $resp .= add_field(FID_TITLE_ID, $item->title_id);
1257 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
1260 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1261 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1263 $self->write_msg($resp);
1265 return(ITEM_STATUS_UPDATE);
1268 sub handle_patron_enable {
1269 my ($self, $server) = @_;
1270 my $ils = $server->{ils};
1271 my $fields = $self->{fields};
1272 my ($trans_date, $patron_id, $terminal_pwd, $patron_pwd);
1273 my ($status, $patron);
1274 my $resp = PATRON_ENABLE_RESP;
1276 ($trans_date) = @{$self->{fixed_fields}};
1277 $patron_id = $fields->{(FID_PATRON_ID)};
1278 $patron_pwd = $fields->{(FID_PATRON_PWD)};
1280 syslog("LOG_DEBUG", "handle_patron_enable: patron_id: '%s', patron_pwd: '%s'",
1281 $patron_id, $patron_pwd);
1283 $patron = $ils->find_patron($patron_id);
1285 if (!defined($patron)) {
1287 $resp .= 'YYYY' . (' ' x 10) . '000' . Sip::timestamp();
1288 $resp .= add_field(FID_PATRON_ID, $patron_id);
1289 $resp .= add_field(FID_PERSONAL_NAME, '' );
1290 $resp .= add_field(FID_VALID_PATRON, 'N');
1291 $resp .= add_field(FID_VALID_PATRON_PWD, 'N');
1294 if (!defined($patron_pwd) || $patron->check_password($patron_pwd)) {
1295 # Don't enable the patron if there was an invalid password
1296 $status = $patron->enable;
1298 $resp .= patron_status_string($patron);
1299 $resp .= $patron->language . Sip::timestamp();
1301 $resp .= add_field(FID_PATRON_ID, $patron->id);
1302 $resp .= add_field(FID_PERSONAL_NAME, $patron->name);
1303 if (defined($patron_pwd)) {
1304 $resp .= add_field(FID_VALID_PATRON_PWD,
1305 sipbool($patron->check_password($patron_pwd)));
1307 $resp .= add_field(FID_VALID_PATRON, 'Y');
1308 $resp .= maybe_add(FID_SCREEN_MSG, $patron->screen_msg);
1309 $resp .= maybe_add(FID_PRINT_LINE, $patron->print_line);
1312 $resp .= add_field(FID_INST_ID, $ils->institution);
1314 $self->write_msg($resp);
1316 return(PATRON_ENABLE);
1320 my ($self, $server) = @_;
1321 my $ils = $server->{ils};
1322 my ($hold_mode, $trans_date);
1323 my ($expiry_date, $pickup_locn, $hold_type, $patron_id, $patron_pwd);
1324 my ($item_id, $title_id, $fee_ack);
1325 my $fields = $self->{fields};
1327 my $resp = HOLD_RESP;
1329 ($hold_mode, $trans_date) = @{$self->{fixed_fields}};
1331 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_hold");
1333 $patron_id = $fields->{(FID_PATRON_ID) };
1334 $expiry_date = $fields->{(FID_EXPIRATION) } || '';
1335 $pickup_locn = $fields->{(FID_PICKUP_LOCN)} || '';
1336 $hold_type = $fields->{(FID_HOLD_TYPE) } || '2'; # Any copy of title
1337 $patron_pwd = $fields->{(FID_PATRON_PWD) };
1338 $item_id = $fields->{(FID_ITEM_ID) } || '';
1339 $title_id = $fields->{(FID_TITLE_ID) } || '';
1340 $fee_ack = $fields->{(FID_FEE_ACK) } || 'N';
1342 if ($hold_mode eq '+') {
1343 $status = $ils->add_hold($patron_id, $patron_pwd,
1344 $item_id, $title_id,
1345 $expiry_date, $pickup_locn, $hold_type,
1347 } elsif ($hold_mode eq '-') {
1348 $status = $ils->cancel_hold($patron_id, $patron_pwd,
1349 $item_id, $title_id);
1350 } elsif ($hold_mode eq '*') {
1351 $status = $ils->alter_hold($patron_id, $patron_pwd,
1352 $item_id, $title_id,
1353 $expiry_date, $pickup_locn, $hold_type,
1356 syslog("LOG_WARNING", "handle_hold: Unrecognized hold mode '%s' from terminal '%s'",
1357 $hold_mode, $server->{account}->{id});
1358 $status = $ils->Transaction::Hold;
1359 $status->screen_msg("System error. Please contact library status");
1362 $resp .= $status->ok;
1363 $resp .= sipbool($status->item && $status->item->available($patron_id));
1364 $resp .= Sip::timestamp;
1367 $resp .= add_field(FID_PATRON_ID, $status->patron->id);
1369 if ($status->expiration_date) {
1370 $resp .= maybe_add(FID_EXPIRATION,
1371 Sip::timestamp($status->expiration_date));
1373 $resp .= maybe_add(FID_QUEUE_POS, $status->queue_position);
1374 $resp .= maybe_add(FID_PICKUP_LOCN, $status->pickup_location);
1375 $resp .= maybe_add(FID_ITEM_ID, $status->item->id);
1376 $resp .= maybe_add(FID_TITLE_ID, $status->item->title_id);
1378 # Not ok. still need required fields
1379 $resp .= add_field(FID_PATRON_ID, $patron_id);
1382 $resp .= add_field(FID_INST_ID, $ils->institution);
1383 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1384 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1386 $self->write_msg($resp);
1392 my ($self, $server) = @_;
1393 my $ils = $server->{ils};
1394 my ($third_party, $no_block, $trans_date, $nb_due_date);
1395 my ($patron_id, $patron_pwd, $item_id, $title_id, $item_props, $fee_ack);
1396 my $fields = $self->{fields};
1398 my ($patron, $item);
1399 my $resp = RENEW_RESP;
1401 ($third_party, $no_block, $trans_date, $nb_due_date) =
1402 @{$self->{fixed_fields}};
1404 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_renew");
1406 if ($no_block eq 'Y') {
1407 syslog("LOG_WARNING",
1408 "handle_renew: recieved 'no block' renewal from terminal '%s'",
1409 $server->{account}->{id});
1412 $patron_id = $fields->{(FID_PATRON_ID)};
1413 $patron_pwd = $fields->{(FID_PATRON_PWD)};
1414 $item_id = $fields->{(FID_ITEM_ID)};
1415 $title_id = $fields->{(FID_TITLE_ID)};
1416 $item_props = $fields->{(FID_ITEM_PROPS)};
1417 $fee_ack = $fields->{(FID_FEE_ACK)};
1419 $status = $ils->renew($patron_id, $patron_pwd, $item_id, $title_id,
1420 $no_block, $nb_due_date, $third_party,
1421 $item_props, $fee_ack);
1423 $patron = $status->patron;
1424 $item = $status->item;
1428 $resp .= $status->renewal_ok ? 'Y' : 'N';
1429 if ($ils->supports('magnetic media')) {
1430 $resp .= sipbool($item->magnetic);
1434 $resp .= sipbool($status->desensitize);
1435 $resp .= Sip::timestamp;
1436 $resp .= add_field(FID_PATRON_ID, $patron->id);
1437 $resp .= add_field(FID_ITEM_ID, $item->id);
1438 $resp .= add_field(FID_TITLE_ID, $item->title_id);
1439 $resp .= add_field(FID_DUE_DATE, $item->due_date);
1440 if ($ils->supports('security inhibit')) {
1441 $resp .= add_field(FID_SECURITY_INHIBIT, $status->security_inhibit);
1443 $resp .= add_field(FID_MEDIA_TYPE, $item->sip_media_type);
1444 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
1446 # renew failed for some reason
1447 # not OK, renewal not OK, Unknown media type (why bother checking?)
1449 $resp .= Sip::timestamp;
1450 # If we found the patron or the item, the return the ILS
1451 # information, otherwise echo back the infomation we received
1453 $resp .= add_field(FID_PATRON_ID, $patron ? $patron->id : $patron_id);
1454 $resp .= add_field(FID_ITEM_ID, $item ? $item->id : $item_id );
1455 $resp .= add_field(FID_TITLE_ID, $item ? $item->title_id : $title_id );
1456 $resp .= add_field(FID_DUE_DATE, '');
1459 if ($status->fee_amount) {
1460 $resp .= add_field(FID_FEE_AMT, $status->fee_amount);
1461 $resp .= maybe_add(FID_CURRENCY, $status->sip_currency);
1462 $resp .= maybe_add(FID_FEE_TYPE, $status->sip_fee_type);
1463 $resp .= maybe_add(FID_TRANSACTION_ID, $status->transaction_id);
1466 $resp .= add_field(FID_INST_ID, $ils->institution);
1467 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1468 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1470 $self->write_msg($resp);
1475 sub handle_renew_all {
1476 my ($self, $server) = @_;
1477 my $ils = $server->{ils};
1478 my ($trans_date, $patron_id, $patron_pwd, $terminal_pwd, $fee_ack);
1479 my $fields = $self->{fields};
1480 my $resp = RENEW_ALL_RESP;
1482 my (@renewed, @unrenewed);
1484 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_renew_all");
1486 ($trans_date) = @{$self->{fixed_fields}};
1488 $patron_id = $fields->{(FID_PATRON_ID)};
1489 $patron_pwd = $fields->{(FID_PATRON_PWD)};
1490 $terminal_pwd = $fields->{(FID_TERMINAL_PWD)};
1491 $fee_ack = $fields->{(FID_FEE_ACK)};
1493 $status = $ils->renew_all($patron_id, $patron_pwd, $fee_ack);
1495 $resp .= $status->ok ? '1' : '0';
1498 $resp .= add_count("renew_all/renewed_count", 0);
1499 $resp .= add_count("renew_all/unrenewed_count", 0);
1503 @renewed = @{$status->renewed};
1504 @unrenewed = @{$status->unrenewed};
1505 $resp .= add_count("renew_all/renewed_count", scalar @renewed);
1506 $resp .= add_count("renew_all/unrenewed_count", scalar @unrenewed);
1509 $resp .= Sip::timestamp;
1510 $resp .= add_field(FID_INST_ID, $ils->institution);
1512 $resp .= join('', map(add_field(FID_RENEWED_ITEMS, $_), @renewed));
1513 $resp .= join('', map(add_field(FID_UNRENEWED_ITEMS, $_), @unrenewed));
1515 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1516 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1518 $self->write_msg($resp);
1524 # send_acs_status($self, $server)
1526 # Send an ACS Status message, which is contains lots of little fields
1527 # of information gleaned from all sorts of places.
1530 my @message_type_names = (
1531 "patron status request",
1536 "request sc/acs resend",
1538 "patron information",
1539 "end patron session",
1542 "item status update",
1549 sub send_acs_status {
1550 my ($self, $server, $screen_msg, $print_line) = @_;
1551 my $msg = ACS_STATUS;
1552 my $account = $server->{account};
1553 my $policy = $server->{policy};
1554 my $ils = $server->{ils};
1555 my ($online_status, $checkin_ok, $checkout_ok, $ACS_renewal_policy);
1556 my ($status_update_ok, $offline_ok, $timeout, $retries);
1558 $online_status = 'Y';
1559 $checkout_ok = sipbool($ils->checkout_ok);
1560 $checkin_ok = sipbool($ils->checkin_ok);
1561 $ACS_renewal_policy = sipbool($policy->{renewal});
1562 $status_update_ok = sipbool($ils->status_update_ok);
1563 $offline_ok = sipbool($ils->offline_ok);
1564 $timeout = sprintf("%03d", $policy->{timeout});
1565 $retries = sprintf("%03d", $policy->{retries});
1567 if (length($timeout) != 3) {
1568 syslog("LOG_ERR", "handle_acs_status: timeout field wrong size: '%s'", $timeout);
1572 if (length($retries) != 3) {
1573 syslog("LOG_ERR", "handle_acs_status: retries field wrong size: '%s'", $retries);
1577 $msg .= "$online_status$checkin_ok$checkout_ok$ACS_renewal_policy";
1578 $msg .= "$status_update_ok$offline_ok$timeout$retries";
1579 $msg .= Sip::timestamp();
1581 if ($protocol_version == 1) {
1583 } elsif ($protocol_version == 2) {
1586 syslog("LOG_ERR", 'Bad setting for $protocol_version, "%s" in send_acs_status', $protocol_version);
1591 $msg .= add_field(FID_INST_ID, $account->{institution});
1593 if ($protocol_version >= 2) {
1594 # Supported messages: we do it all
1595 my $supported_msgs = '';
1597 foreach my $msg_name (@message_type_names) {
1598 if ( $msg_name eq 'request sc/acs resend' ) {
1599 $supported_msgs .= Sip::sipbool(1);
1601 $supported_msgs .= Sip::sipbool( $ils->supports($msg_name) );
1604 if (length($supported_msgs) < 16) {
1605 syslog("LOG_ERR", 'send_acs_status: supported messages "%s" too short', $supported_msgs);
1607 $msg .= add_field(FID_SUPPORTED_MSGS, $supported_msgs);
1610 $msg .= maybe_add(FID_SCREEN_MSG, $screen_msg);
1612 if (defined($account->{print_width}) && defined($print_line)
1613 && $account->{print_width} < length( $print_line)) {
1614 syslog("LOG_WARNING", "send_acs_status: print line '%s' too long. Truncating", $print_line);
1615 $print_line = substr($print_line, 0, $account->{print_width});
1618 $msg .= maybe_add(FID_PRINT_LINE, $print_line);
1620 # Do we want to tell the terminal its location?
1622 $self->write_msg($msg);
1627 # patron_status_string: create the 14-char patron status
1628 # string for the Patron Status message
1630 sub patron_status_string {
1632 syslog("LOG_DEBUG", "patron_status_string for %s charge_ok: %s", $patron->id, $patron->charge_ok);
1633 my $patron_status = sprintf('%s%s%s%s%s%s%s%s%s%s%s%s%s%s',
1634 denied($patron->charge_ok),
1635 denied($patron->renew_ok),
1636 denied($patron->recall_ok),
1637 denied($patron->hold_ok),
1638 boolspace($patron->card_lost),
1639 boolspace($patron->too_many_charged),
1640 boolspace($patron->too_many_overdue),
1641 boolspace($patron->too_many_renewal),
1642 boolspace($patron->too_many_claim_return),
1643 boolspace($patron->too_many_lost),
1644 boolspace($patron->excessive_fines),
1645 boolspace($patron->excessive_fees),
1646 boolspace($patron->recall_overdue),
1647 boolspace($patron->too_many_billed)
1649 return $patron_status;