4 # A Class for handing SIP messages
12 use Sys::Syslog qw(syslog);
13 use UNIVERSAL qw(can);
16 use Sip::Constants qw(:all);
17 use Sip::Checksum qw(verify_cksum);
21 use C4::Auth qw(&check_api_auth);
23 use vars qw(@ISA $VERSION @EXPORT_OK);
28 @EXPORT_OK = qw(handle);
31 # Predeclare handler subroutines
32 use subs qw(handle_patron_status handle_checkout handle_checkin
33 handle_block_patron handle_sc_status handle_request_acs_resend
34 handle_login handle_patron_info handle_end_patron_session
35 handle_fee_paid handle_item_information handle_item_status_update
36 handle_patron_enable handle_hold handle_renew handle_renew_all);
39 # For the most part, Version 2.00 of the protocol just adds new
40 # variable fields, but sometimes it changes the fixed header.
42 # In general, if there's no '2.00' protocol entry for a handler, that's
43 # because 2.00 didn't extend the 1.00 version of the protocol. This will
44 # be handled by the module initialization code following the declaration,
45 # which goes through the handlers table and creates a '2.00' entry that
46 # points to the same place as the '1.00' entry. If there's a 2.00 entry
47 # but no 1.00 entry, then that means that it's a completely new service
48 # in 2.00, so 1.00 shouldn't recognize it.
51 (PATRON_STATUS_REQ) => {
52 name => "Patron Status Request",
53 handler => \&handle_patron_status,
58 fields => [(FID_INST_ID), (FID_PATRON_ID),
59 (FID_TERMINAL_PWD), (FID_PATRON_PWD)],
65 handler => \&handle_checkout,
68 template => "CCA18A18",
70 fields => [(FID_INST_ID), (FID_PATRON_ID),
71 (FID_ITEM_ID), (FID_TERMINAL_PWD)],
74 template => "CCA18A18",
76 fields => [(FID_INST_ID), (FID_PATRON_ID),
77 (FID_ITEM_ID), (FID_TERMINAL_PWD),
78 (FID_ITEM_PROPS), (FID_PATRON_PWD),
79 (FID_FEE_ACK), (FID_CANCEL)],
85 handler => \&handle_checkin,
88 template => "CA18A18",
90 fields => [(FID_CURRENT_LOCN), (FID_INST_ID),
91 (FID_ITEM_ID), (FID_TERMINAL_PWD)],
94 template => "CA18A18",
96 fields => [(FID_CURRENT_LOCN), (FID_INST_ID),
97 (FID_ITEM_ID), (FID_TERMINAL_PWD),
98 (FID_ITEM_PROPS), (FID_CANCEL)],
103 name => "Block Patron",
104 handler => \&handle_block_patron,
109 fields => [(FID_INST_ID), (FID_BLOCKED_CARD_MSG),
110 (FID_PATRON_ID), (FID_TERMINAL_PWD)],
116 handler => \&handle_sc_status,
125 (REQUEST_ACS_RESEND) => {
126 name => "Request ACS Resend",
127 handler => \&handle_request_acs_resend,
138 handler => \&handle_login,
143 fields => [(FID_LOGIN_UID), (FID_LOGIN_PWD),
144 (FID_LOCATION_CODE)],
149 name => "Patron Info",
150 handler => \&handle_patron_info,
153 template => "A3A18A10",
155 fields => [(FID_INST_ID), (FID_PATRON_ID),
156 (FID_TERMINAL_PWD), (FID_PATRON_PWD),
157 (FID_START_ITEM), (FID_END_ITEM)],
161 (END_PATRON_SESSION) => {
162 name => "End Patron Session",
163 handler => \&handle_end_patron_session,
168 fields => [(FID_INST_ID), (FID_PATRON_ID),
169 (FID_TERMINAL_PWD), (FID_PATRON_PWD)],
175 handler => \&handle_fee_paid,
178 template => "A18A2A3",
180 fields => [(FID_FEE_AMT), (FID_INST_ID),
181 (FID_PATRON_ID), (FID_TERMINAL_PWD),
182 (FID_PATRON_PWD), (FID_FEE_ID),
183 (FID_TRANSACTION_ID)],
187 (ITEM_INFORMATION) => {
188 name => "Item Information",
189 handler => \&handle_item_information,
194 fields => [(FID_INST_ID), (FID_ITEM_ID),
199 (ITEM_STATUS_UPDATE) => {
200 name => "Item Status Update",
201 handler => \&handle_item_status_update,
206 fields => [(FID_INST_ID), (FID_PATRON_ID),
207 (FID_ITEM_ID), (FID_TERMINAL_PWD),
213 name => "Patron Enable",
214 handler => \&handle_patron_enable,
219 fields => [(FID_INST_ID), (FID_PATRON_ID),
220 (FID_TERMINAL_PWD), (FID_PATRON_PWD)],
226 handler => \&handle_hold,
231 fields => [(FID_EXPIRATION), (FID_PICKUP_LOCN),
232 (FID_HOLD_TYPE), (FID_INST_ID),
233 (FID_PATRON_ID), (FID_PATRON_PWD),
234 (FID_ITEM_ID), (FID_TITLE_ID),
235 (FID_TERMINAL_PWD), (FID_FEE_ACK)],
241 handler => \&handle_renew,
244 template => "CCA18A18",
246 fields => [(FID_INST_ID), (FID_PATRON_ID),
247 (FID_PATRON_PWD), (FID_ITEM_ID),
248 (FID_TITLE_ID), (FID_TERMINAL_PWD),
249 (FID_ITEM_PROPS), (FID_FEE_ACK)],
255 handler => \&handle_renew_all,
260 fields => [(FID_INST_ID), (FID_PATRON_ID),
261 (FID_PATRON_PWD), (FID_TERMINAL_PWD),
269 # Now, initialize some of the missing bits of %handlers
271 foreach my $i (keys(%handlers)) {
272 if (!exists($handlers{$i}->{protocol}->{2})) {
273 $handlers{$i}->{protocol}->{2} = $handlers{$i}->{protocol}->{1};
278 my ($class, $msg, $seqno) = @_;
280 my $msgtag = substr($msg, 0, 2);
282 if ($msgtag eq LOGIN) {
283 # If the client is using the 2.00-style "Login" message
284 # to authenticate to the server, then we get the Login message
285 # _before_ the client has indicated that it supports 2.00, but
286 # it's using the 2.00 login process, so it must support 2.00.
287 $protocol_version = 2;
289 syslog("LOG_DEBUG", "Sip::MsgType::new('%s', '%s...', '%s'): msgtag '%s', protocol %s",
290 $class, substr($msg, 0, 10), $msgtag, $seqno, $protocol_version);
291 # warn "SIP PROTOCOL: $protocol_version";
292 if (!exists($handlers{$msgtag})) {
293 syslog("LOG_WARNING", "new Sip::MsgType: Skipping message of unknown type '%s' in '%s'",
296 } elsif (!exists($handlers{$msgtag}->{protocol}->{$protocol_version})) {
297 syslog("LOG_WARNING", "new Sip::MsgType: Skipping message '%s' unsupported by protocol rev. '%d'",
298 $msgtag, $protocol_version);
304 $self->{seqno} = $seqno;
305 $self->_initialize(substr($msg,2), $handlers{$msgtag});
311 my ($self, $msg, $control_block) = @_;
313 my $proto = $control_block->{protocol}->{$protocol_version};
315 $self->{name} = $control_block->{name};
316 $self->{handler} = $control_block->{handler};
318 $self->{fields} = {};
319 $self->{fixed_fields} = [];
326 # syslog("LOG_DEBUG", "Sip::MsgType::_initialize('%s', '%s...')", $self->{name}, substr($msg, 0, 20));
328 foreach my $field (@{$proto->{fields}}) {
329 $self->{fields}->{$field} = undef;
332 syslog("LOG_DEBUG", "Sip::MsgType::_initialize('%s', '%s', '%s', '%s', ...)",
333 $self->{name}, $msg, $proto->{template}, $proto->{template_len});
335 $self->{fixed_fields} = [ unpack($proto->{template}, $msg) ];
337 # Skip over the fixed fields and the split the rest of
338 # the message into fields based on the delimiter and parse them
339 foreach my $field (split(quotemeta($field_delimiter), substr($msg, $proto->{template_len}))) {
340 $fn = substr($field, 0, 2);
342 if (!exists($self->{fields}->{$fn})) {
343 syslog("LOG_WARNING", "Unsupported field '%s' in %s message '%s'",
344 $fn, $self->{name}, $msg);
345 } elsif (defined($self->{fields}->{$fn})) {
346 syslog("LOG_WARNING", "Duplicate field '%s' (previous value '%s') in %s message '%s'",
347 $fn, $self->{fields}->{$fn}, $self->{name}, $msg);
349 $self->{fields}->{$fn} = substr($field, 2);
357 my ($msg, $server, $req) = @_;
358 my $config = $server->{config};
362 # What's the field delimiter for variable length fields?
363 # This can't be based on the account, since we need to know
364 # the field delimiter to parse a SIP login message
366 if (defined($server->{config}->{delimiter})) {
367 $field_delimiter = $server->{config}->{delimiter};
370 # error detection is active if this is a REQUEST_ACS_RESEND
371 # message with a checksum, or if the message is long enough
372 # and the last nine characters begin with a sequence number
374 if ($msg eq REQUEST_ACS_RESEND_CKSUM) {
376 $error_detection = 1;
377 $self = new Sip::MsgType ((REQUEST_ACS_RESEND), 0);
378 } elsif((length($msg) > 11) && (substr($msg, -9, 2) eq "AY")) {
379 $error_detection = 1;
381 if (!verify_cksum($msg)) {
382 syslog("LOG_WARNING", "Checksum failed on message '%s'", $msg);
383 # REQUEST_SC_RESEND with error detection
384 $last_response = REQUEST_SC_RESEND_CKSUM;
385 print("$last_response\r");
386 return REQUEST_ACS_RESEND;
388 # Save the sequence number, then strip off the
389 # error detection data to process the message
390 $self = new Sip::MsgType (substr($msg, 0, -9), substr($msg, -7, 1));
392 } elsif ($error_detection) {
393 # We received a non-ED message when ED is supposed to be active.
394 # Warn about this problem, then process the message anyway.
395 syslog("LOG_WARNING",
396 "Received message without error detection: '%s'", $msg);
397 $error_detection = 0;
398 $self = new Sip::MsgType ($msg, 0);
400 $self = new Sip::MsgType ($msg, 0);
403 if ((substr($msg, 0, 2) ne REQUEST_ACS_RESEND) &&
404 $req && (substr($msg, 0, 2) ne $req)) {
405 return substr($msg, 0, 2);
407 unless ($self->{handler}) {
408 syslog("LOG_WARNING", "No handler defined for '%s'", $msg);
411 return($self->{handler}->($self, $server)); # FIXME
412 # FIXME: Use of uninitialized value in subroutine entry
413 # Can't use string ("") as a subroutine ref while "strict refs" in use
421 # Patron status messages are produced in response to both
422 # "Request Patron Status" and "Block Patron"
424 # Request Patron Status requires a patron password, but
425 # Block Patron doesn't (since the patron may never have
426 # provided one before attempting some illegal action).
428 # ASSUMPTION: If the patron password field is present in the
429 # message, then it must match, otherwise incomplete patron status
430 # information will be returned to the terminal.
432 sub build_patron_status {
433 my ($patron, $lang, $fields)= @_;
434 my $patron_pwd = $fields->{(FID_PATRON_PWD)};
435 my $resp = (PATRON_STATUS_RESP);
438 $resp .= patron_status_string($patron);
439 $resp .= $lang . Sip::timestamp();
440 $resp .= add_field(FID_PERSONAL_NAME, $patron->name);
442 # while the patron ID we got from the SC is valid, let's
443 # use the one returned from the ILS, just in case...
444 $resp .= add_field(FID_PATRON_ID, $patron->id);
445 if ($protocol_version >= 2) {
446 $resp .= add_field(FID_VALID_PATRON, 'Y');
447 # If the patron password field doesn't exist, then
448 # we can't report that the password was valid, now can
449 # we? But if it does exist, then we know it's valid.
450 if (defined($patron_pwd)) {
451 $resp .= add_field(FID_VALID_PATRON_PWD,
452 sipbool($patron->check_password($patron_pwd)));
454 $resp .= maybe_add(FID_CURRENCY, $patron->currency);
455 $resp .= maybe_add(FID_FEE_AMT, $patron->fee_amount);
458 $resp .= maybe_add(FID_SCREEN_MSG, $patron->screen_msg);
459 $resp .= maybe_add(FID_PRINT_LINE, $patron->print_line);
461 # Invalid patron id. Report that the user has no privs.,
462 # no personal name, and is invalid (if we're using 2.00)
463 $resp .= 'YYYY' . (' ' x 10) . $lang . Sip::timestamp();
464 $resp .= add_field(FID_PERSONAL_NAME, '');
466 # the patron ID is invalid, but it's a required field, so
468 $resp .= add_field(FID_PATRON_ID, $fields->{(FID_PATRON_ID)});
470 ($protocol_version >= 2) and
471 $resp .= add_field(FID_VALID_PATRON, 'N');
474 $resp .= add_field(FID_INST_ID, $fields->{(FID_INST_ID)});
478 sub handle_patron_status {
479 my ($self, $server) = @_;
480 #warn Dumper($server);
481 my $ils = $server->{ils};
483 my $resp = (PATRON_STATUS_RESP);
484 my $account = $server->{account};
485 my ($lang, $date) = @{$self->{fixed_fields}};
486 my $fields = $self->{fields};
487 #warn Dumper($fields);
489 #warn $fields->{(FID_INST_ID)};
490 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_patron_status");
491 $patron = $ils->find_patron($fields->{(FID_PATRON_ID)});
492 $resp = build_patron_status($patron, $lang, $fields);
493 $self->write_msg($resp);
494 return (PATRON_STATUS_REQ);
497 sub handle_checkout {
498 my ($self, $server) = @_;
499 my $account = $server->{account};
500 my $ils = $server->{ils};
501 my $inst = $ils->institution;
502 my ($sc_renewal_policy, $no_block, $trans_date, $nb_due_date);
504 my ($patron_id, $item_id, $status);
508 ($sc_renewal_policy, $no_block, $trans_date, $nb_due_date) =
509 @{$self->{fixed_fields}};
510 $fields = $self->{fields};
512 $patron_id = $fields->{(FID_PATRON_ID)};
513 $item_id = $fields->{(FID_ITEM_ID)};
516 if ($no_block eq 'Y') {
517 # Off-line transactions need to be recorded, but there's
518 # not a lot we can do about it
519 syslog("LOG_WARNING", "received no-block checkout from terminal '%s'",
522 $status = $ils->checkout_no_block($patron_id, $item_id,
524 $trans_date, $nb_due_date);
526 # Does the transaction date really matter for items that are
527 # checkout out while the terminal is online? I'm guessing 'no'
528 $status = $ils->checkout($patron_id, $item_id, $sc_renewal_policy);
531 $item = $status->item;
532 $patron = $status->patron;
535 # Item successfully checked out
537 $resp = CHECKOUT_RESP . '1';
538 $resp .= sipbool($status->renew_ok);
539 if ($ils->supports('magnetic media')) {
540 $resp .= sipbool($item->magnetic);
544 # We never return the obsolete 'U' value for 'desensitize'
545 $resp .= sipbool($status->desensitize);
546 $resp .= Sip::timestamp;
548 # Now for the variable fields
549 $resp .= add_field(FID_INST_ID, $inst);
550 $resp .= add_field(FID_PATRON_ID, $patron_id);
551 $resp .= add_field(FID_ITEM_ID, $item_id);
552 $resp .= add_field(FID_TITLE_ID, $item->title_id);
553 $resp .= add_field(FID_DUE_DATE, $item->due_date);
555 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
556 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
558 if ($protocol_version >= 2) {
559 if ($ils->supports('security inhibit')) {
560 $resp .= add_field(FID_SECURITY_INHIBIT,
561 $status->security_inhibit);
563 $resp .= maybe_add(FID_MEDIA_TYPE, $item->sip_media_type);
564 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
567 if ($status->fee_amount) {
568 $resp .= add_field(FID_FEE_AMT, $status->fee_amount);
569 $resp .= maybe_add(FID_CURRENCY, $status->sip_currency);
570 $resp .= maybe_add(FID_FEE_TYPE, $status->sip_fee_type);
571 $resp .= maybe_add(FID_TRANSACTION_ID,
572 $status->transaction_id);
578 # Checkout Response: not ok, no renewal, don't know mag. media,
580 $resp = sprintf("120NUN%s", Sip::timestamp);
581 $resp .= add_field(FID_INST_ID, $inst);
582 $resp .= add_field(FID_PATRON_ID, $patron_id);
583 $resp .= add_field(FID_ITEM_ID, $item_id);
585 # If the item is valid, provide the title, otherwise
587 $resp .= add_field(FID_TITLE_ID, $item ? $item->title_id : '');
588 # Due date is required. Since it didn't get checked out,
589 # it's not due, so leave the date blank
590 $resp .= add_field(FID_DUE_DATE, '');
592 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
593 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
595 if ($protocol_version >= 2) {
596 # Is the patron ID valid?
597 $resp .= add_field(FID_VALID_PATRON, sipbool($patron));
599 if ($patron && exists($fields->{FID_PATRON_PWD})) {
600 # Password provided, so we can tell if it was valid or not
601 $resp .= add_field(FID_VALID_PATRON_PWD,
602 sipbool($patron->check_password($fields->{(FID_PATRON_PWD)})));
607 $self->write_msg($resp);
612 my ($self, $server) = @_;
613 my $account = $server->{account};
614 my $ils = $server->{ils};
615 my ($current_loc, $inst_id, $item_id, $terminal_pwd, $item_props, $cancel);
616 my ($patron, $item, $status);
617 my $resp = CHECKIN_RESP;
618 my ($no_block, $trans_date, $return_date) = @{$self->{fixed_fields}};
619 my $fields = $self->{fields};
621 $current_loc = $fields->{(FID_CURRENT_LOCN)};
622 $inst_id = $fields->{(FID_INST_ID)};
623 $item_id = $fields->{(FID_ITEM_ID)};
624 $item_props = $fields->{(FID_ITEM_PROPS)};
625 $cancel = $fields->{(FID_CANCEL)};
627 $ils->check_inst_id($inst_id, "handle_checkin");
629 if ($no_block eq 'Y') {
630 # Off-line transactions, ick.
631 syslog("LOG_WARNING", "received no-block checkin from terminal '%s'",
633 $status = $ils->checkin_no_block($item_id, $trans_date,
634 $return_date, $item_props, $cancel);
636 $status = $ils->checkin($item_id, $trans_date, $return_date,
637 $current_loc, $item_props, $cancel);
640 $patron = $status->patron;
641 $item = $status->item;
643 $resp .= $status->ok ? '1' : '0';
644 $resp .= $status->resensitize ? 'Y' : 'N';
645 if ($item && $ils->supports('magnetic media')) {
646 $resp .= sipbool($item->magnetic);
648 # The item barcode was invalid or the system doesn't support
649 # the 'magnetic media' indicator
652 $resp .= $status->alert ? 'Y' : 'N';
653 $resp .= Sip::timestamp;
654 $resp .= add_field(FID_INST_ID, $inst_id);
655 $resp .= add_field(FID_ITEM_ID, $item_id);
658 $resp .= add_field(FID_PERM_LOCN, $item->permanent_location);
659 $resp .= maybe_add(FID_TITLE_ID, $item->title_id);
662 if ($protocol_version >= 2) {
663 $resp .= maybe_add(FID_SORT_BIN, $status->sort_bin);
665 $resp .= add_field(FID_PATRON_ID, $patron->id);
668 $resp .= maybe_add(FID_MEDIA_TYPE, $item->sip_media_type);
669 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
673 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
674 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
676 $self->write_msg($resp);
681 sub handle_block_patron {
682 my ($self, $server) = @_;
683 my $account = $server->{account};
684 my $ils = $server->{ils};
685 my ($card_retained, $trans_date);
686 my ($inst_id, $blocked_card_msg, $patron_id, $terminal_pwd);
687 my ($fields,$resp,$patron);
689 ($card_retained, $trans_date) = @{$self->{fixed_fields}};
690 $fields = $self->{fields};
691 $inst_id = $fields->{(FID_INST_ID)};
692 $blocked_card_msg = $fields->{(FID_BLOCKED_CARD_MSG)};
693 $patron_id = $fields->{(FID_PATRON_ID)};
694 $terminal_pwd = $fields->{(FID_TERMINAL_PWD)};
696 # Terminal passwords are different from account login
697 # passwords, but I have no idea what to do with them. So,
698 # I'll just ignore them for now.
702 $ils->check_inst_id($inst_id, "block_patron");
703 $patron = $ils->find_patron($patron_id);
705 # The correct response for a "Block Patron" message is a
706 # "Patron Status Response", so use that handler to generate
707 # the message, but then return the correct code from here.
709 # Normally, the language is provided by the "Patron Status"
710 # fixed field, but since we're not responding to one of those
711 # we'll just say, "Unspecified", as per the spec. Let the
712 # terminal default to something that, one hopes, will be
716 $patron->block($card_retained, $blocked_card_msg);
719 $resp = build_patron_status($patron, $patron->language, $fields);
720 $self->write_msg($resp);
721 return(BLOCK_PATRON);
724 sub handle_sc_status {
725 my ($self, $server) = @_;
726 ($server) or warn "handle_sc_status error: no \$server argument received.";
727 my ($status, $print_width, $sc_protocol_version) = @{$self->{fixed_fields}};
730 if ($sc_protocol_version =~ /^1\./) {
732 } elsif ($sc_protocol_version =~ /^2\./) {
735 syslog("LOG_WARNING", "Unrecognized protocol revision '%s', falling back to '1'", $sc_protocol_version);
739 if ($new_proto != $protocol_version) {
740 syslog("LOG_INFO", "Setting protocol level to $new_proto");
741 $protocol_version = $new_proto;
744 if ($status == SC_STATUS_PAPER) {
745 syslog("LOG_WARNING", "Self-Check unit '%s@%s' out of paper",
746 $self->{account}->{id}, $self->{account}->{institution});
747 } elsif ($status == SC_STATUS_SHUTDOWN) {
748 syslog("LOG_WARNING", "Self-Check unit '%s@%s' shutting down",
749 $self->{account}->{id}, $self->{account}->{institution});
752 $self->{account}->{print_width} = $print_width;
753 return (send_acs_status($self, $server) ? SC_STATUS : '');
756 sub handle_request_acs_resend {
757 my ($self, $server) = @_;
759 if (!$last_response) {
760 # We haven't sent anything yet, so respond with a
761 # REQUEST_SC_RESEND msg (p. 16)
762 $self->write_msg(REQUEST_SC_RESEND);
763 } elsif ((length($last_response) < 9)
764 || substr($last_response, -9, 2) ne 'AY') {
765 # When resending a message, we aren't supposed to include
766 # a sequence number, even if the original had one (p. 4).
767 # If the last message didn't have a sequence number, then
768 # we can just send it.
769 print("$last_response\r"); # not write_msg?
771 # Cut out the sequence number and checksum, since the old
772 # checksum is wrong for the resent message.
773 my $rebuilt = substr($last_response, 0, -9);
774 $self->write_msg($rebuilt);
777 return REQUEST_ACS_RESEND;
781 my ($self, $server) = @_;
782 my ($uid_algorithm, $pwd_algorithm);
786 my $status = 1; # Assume it all works
788 $fields = $self->{fields};
789 ($uid_algorithm, $pwd_algorithm) = @{$self->{fixed_fields}};
791 $uid = $fields->{(FID_LOGIN_UID)};
792 $pwd = $fields->{(FID_LOGIN_PWD)};
794 if ($uid_algorithm || $pwd_algorithm) {
795 syslog("LOG_ERR", "LOGIN: Unsupported non-zero encryption method(s): uid = $uid_algorithm, pwd = $pwd_algorithm");
799 if (!exists($server->{config}->{accounts}->{$uid})) {
800 syslog("LOG_WARNING", "MsgType::handle_login: Unknown login '$uid'");
802 } elsif ($server->{config}->{accounts}->{$uid}->{password} ne $pwd) {
803 syslog("LOG_WARNING", "MsgType::handle_login: Invalid password for login '$uid'");
806 # Store the active account someplace handy for everybody else to find.
807 $server->{account} = $server->{config}->{accounts}->{$uid};
808 $inst = $server->{account}->{institution};
809 $server->{institution} = $server->{config}->{institutions}->{$inst};
810 $server->{policy} = $server->{institution}->{policy};
811 $server->{sip_username} = $uid;
812 $server->{sip_password} = $pwd;
814 my $auth_status = api_auth($uid,$pwd);
815 if (!$auth_status or $auth_status !~ /^ok$/i) {
816 syslog("LOG_WARNING", "api_auth failed for SIP terminal '%s' of '%s': %s",
817 $uid, $inst, ($auth_status||'unknown'));
820 syslog("LOG_INFO", "Successful login/auth for '%s' of '%s'", $server->{account}->{id}, $inst);
822 # initialize connection to ILS
824 my $module = $server->{config}->{institutions}->{$inst}->{implementation};
825 syslog("LOG_DEBUG", 'handle_login: ' . Dumper($module));
828 syslog("LOG_ERR", "%s: Loading ILS implementation '%s' for institution '%s' failed",
829 $server->{service}, $module, $inst);
830 die("Failed to load ILS implementation '$module' for $inst");
833 # like ILS->new(), I think.
834 $server->{ils} = $module->new($server->{institution}, $server->{account});
835 if (!$server->{ils}) {
836 syslog("LOG_ERR", "%s: ILS connection to '%s' failed", $server->{service}, $inst);
837 die("Unable to connect to ILS '$inst'");
841 $self->write_msg(LOGIN_RESP . $status);
842 return $status ? LOGIN : '';
846 # Build the detailed summary information for the Patron
847 # Information Response message based on the first 'Y' that appears
848 # in the 'summary' field of the Patron Information reqest. The
849 # specification says that only one 'Y' can appear in that field,
850 # and we're going to believe it.
853 my ($ils, $patron, $summary, $start, $end) = @_;
859 # Map from offsets in the "summary" field of the Patron Information
860 # message to the corresponding field and handler
863 { func => $patron->can("hold_items"),
864 fid => FID_HOLD_ITEMS },
865 { func => $patron->can("overdue_items"),
866 fid => FID_OVERDUE_ITEMS },
867 { func => $patron->can("charged_items"),
868 fid => FID_CHARGED_ITEMS },
869 { func => $patron->can("fine_items"),
870 fid => FID_FINE_ITEMS },
871 { func => $patron->can("recall_items"),
872 fid => FID_RECALL_ITEMS },
873 { func => $patron->can("unavail_holds"),
874 fid => FID_UNAVAILABLE_HOLD_ITEMS },
878 if (($summary_type = index($summary, 'Y')) == -1) {
879 # No detailed information required
883 syslog("LOG_DEBUG", "Summary_info: index == '%d', field '%s'",
884 $summary_type, $summary_map[$summary_type]->{fid});
886 $func = $summary_map[$summary_type]->{func};
887 $fid = $summary_map[$summary_type]->{fid};
888 $itemlist = &$func($patron, $start, $end);
890 syslog("LOG_DEBUG", "summary_info: list = (%s)", join(", ", @{$itemlist}));
891 foreach my $i (@{$itemlist}) {
892 $resp .= add_field($fid, $i);
898 sub handle_patron_info {
899 my ($self, $server) = @_;
900 my $ils = $server->{ils};
901 my ($lang, $trans_date, $summary) = @{$self->{fixed_fields}};
902 my $fields = $self->{fields};
903 my ($inst_id, $patron_id, $terminal_pwd, $patron_pwd, $start, $end);
904 my ($resp, $patron, $count);
906 $inst_id = $fields->{(FID_INST_ID)};
907 $patron_id = $fields->{(FID_PATRON_ID)};
908 $terminal_pwd = $fields->{(FID_TERMINAL_PWD)};
909 $patron_pwd = $fields->{(FID_PATRON_PWD)};
910 $start = $fields->{(FID_START_ITEM)};
911 $end = $fields->{(FID_END_ITEM)};
913 $patron = $ils->find_patron($patron_id);
915 $resp = (PATRON_INFO_RESP);
917 $resp .= patron_status_string($patron);
918 $resp .= $lang . Sip::timestamp();
920 $resp .= add_count('patron_info/hold_items',
921 scalar @{$patron->hold_items});
922 $resp .= add_count('patron_info/overdue_items',
923 scalar @{$patron->overdue_items});
924 $resp .= add_count('patron_info/charged_items',
925 scalar @{$patron->charged_items});
926 $resp .= add_count('patron_info/fine_items',
927 scalar @{$patron->fine_items});
928 $resp .= add_count('patron_info/recall_items',
929 scalar @{$patron->recall_items});
930 $resp .= add_count('patron_info/unavail_holds',
931 scalar @{$patron->unavail_holds});
933 # while the patron ID we got from the SC is valid, let's
934 # use the one returned from the ILS, just in case...
935 $resp .= add_field(FID_PATRON_ID, $patron->id);
937 $resp .= add_field(FID_PERSONAL_NAME, $patron->name);
939 # TODO: add code for the fields
941 # overdue items limit
942 # charged items limit
945 $resp .= maybe_add(FID_CURRENCY, $patron->currency);
946 $resp .= maybe_add(FID_FEE_AMT, $patron->fee_amount);
948 $resp .= maybe_add(FID_HOME_ADDR,$patron->address);
949 $resp .= maybe_add(FID_EMAIL, $patron->email_addr);
950 $resp .= maybe_add(FID_HOME_PHONE, $patron->home_phone);
952 $resp .= summary_info($ils, $patron, $summary, $start, $end);
954 $resp .= add_field(FID_VALID_PATRON, 'Y');
955 if (defined($patron_pwd)) {
956 # If the patron password was provided, report on if
958 $resp .= add_field(FID_VALID_PATRON_PWD,
959 sipbool($patron->check_password($patron_pwd)));
962 # SIP 2.0 extensions used by Envisionware
963 # Other types of terminals will ignore the fields, if
964 # they don't recognize the codes
965 $resp .= maybe_add(FID_PATRON_BIRTHDATE, $patron->sip_birthdate);
966 $resp .= maybe_add(FID_PATRON_CLASS, $patron->ptype);
968 # Custom protocol extension to report patron internet privileges
969 $resp .= maybe_add(FID_INET_PROFILE, $patron->inet_privileges);
971 $resp .= maybe_add(FID_SCREEN_MSG, $patron->screen_msg);
972 $resp .= maybe_add(FID_PRINT_LINE, $patron->print_line);
975 # He has no privileges, no items associated with him,
976 # no personal name, and is invalid (if we're using 2.00)
977 $resp .= 'YYYY' . (' ' x 10) . $lang . Sip::timestamp();
979 $resp .= add_field(FID_PERSONAL_NAME, '');
981 # the patron ID is invalid, but it's a required field, so
983 $resp .= add_field(FID_PATRON_ID, $fields->{(FID_PATRON_ID)});
985 if ($protocol_version >= 2) {
986 $resp .= add_field(FID_VALID_PATRON, 'N');
990 $resp .= add_field(FID_INST_ID, $server->{ils}->institution);
992 $self->write_msg($resp);
997 sub handle_end_patron_session {
998 my ($self, $server) = @_;
999 my $ils = $server->{ils};
1001 my $fields = $self->{fields};
1002 my $resp = END_SESSION_RESP;
1003 my ($status, $screen_msg, $print_line);
1005 ($trans_date) = @{$self->{fixed_fields}};
1007 $ils->check_inst_id($fields->{FID_INST_ID}, "handle_end_patron_session");
1009 ($status, $screen_msg, $print_line) = $ils->end_patron_session($fields->{(FID_PATRON_ID)});
1011 $resp .= $status ? 'Y' : 'N';
1012 $resp .= Sip::timestamp();
1014 $resp .= add_field(FID_INST_ID, $server->{ils}->institution);
1015 $resp .= add_field(FID_PATRON_ID, $fields->{(FID_PATRON_ID)});
1017 $resp .= maybe_add(FID_SCREEN_MSG, $screen_msg);
1018 $resp .= maybe_add(FID_PRINT_LINE, $print_line);
1020 $self->write_msg($resp);
1022 return(END_PATRON_SESSION);
1025 sub handle_fee_paid {
1026 my ($self, $server) = @_;
1027 my $ils = $server->{ils};
1028 my ($trans_date, $fee_type, $pay_type, $currency) = $self->{fixed_fields};
1029 my $fields = $self->{fields};
1030 my ($fee_amt, $inst_id, $patron_id, $terminal_pwd, $patron_pwd);
1031 my ($fee_id, $trans_id);
1033 my $resp = FEE_PAID_RESP;
1035 $fee_amt = $fields->{(FID_FEE_AMT)};
1036 $inst_id = $fields->{(FID_INST_ID)};
1037 $patron_id = $fields->{(FID_PATRON_ID)};
1038 $patron_pwd = $fields->{(FID_PATRON_PWD)};
1039 $fee_id = $fields->{(FID_FEE_ID)};
1040 $trans_id = $fields->{(FID_TRANSACTION_ID)};
1042 $ils->check_inst_id($inst_id, "handle_fee_paid");
1044 $status = $ils->pay_fee($patron_id, $patron_pwd, $fee_amt, $fee_type,
1045 $pay_type, $fee_id, $trans_id, $currency);
1047 $resp .= ($status->ok ? 'Y' : 'N') . Sip::timestamp;
1048 $resp .= add_field(FID_INST_ID, $inst_id);
1049 $resp .= add_field(FID_PATRON_ID, $patron_id);
1050 $resp .= maybe_add(FID_TRANSACTION_ID, $status->transaction_id);
1051 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1052 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1054 $self->write_msg($resp);
1059 sub handle_item_information {
1060 my ($self, $server) = @_;
1061 my $ils = $server->{ils};
1063 my $fields = $self->{fields};
1064 my $resp = ITEM_INFO_RESP;
1068 ($trans_date) = @{$self->{fixed_fields}};
1070 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_item_information");
1072 $item = $ils->find_item($fields->{(FID_ITEM_ID)});
1074 if (!defined($item)) {
1076 # "Other" circ stat, "Other" security marker, "Unknown" fee type
1078 $resp .= Sip::timestamp;
1079 # Just echo back the invalid item id
1080 $resp .= add_field(FID_ITEM_ID, $fields->{(FID_ITEM_ID)});
1081 # title id is required, but we don't have one
1082 $resp .= add_field(FID_TITLE_ID, '');
1084 # Valid Item ID, send the good stuff
1085 $resp .= $item->sip_circulation_status;
1086 $resp .= $item->sip_security_marker;
1087 $resp .= $item->sip_fee_type;
1088 $resp .= Sip::timestamp;
1090 $resp .= add_field(FID_ITEM_ID, $item->id);
1091 $resp .= add_field(FID_TITLE_ID, $item->title_id);
1093 $resp .= maybe_add(FID_MEDIA_TYPE, $item->sip_media_type);
1094 $resp .= maybe_add(FID_PERM_LOCN, $item->permanent_location);
1095 $resp .= maybe_add(FID_CURRENT_LOCN, $item->current_location);
1096 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
1098 if (($i = $item->fee) != 0) {
1099 $resp .= add_field(FID_CURRENCY, $item->fee_currency);
1100 $resp .= add_field(FID_FEE_AMT, $i);
1102 $resp .= maybe_add(FID_OWNER, $item->owner);
1104 if (($i = scalar @{$item->hold_queue}) > 0) {
1105 $resp .= add_field(FID_HOLD_QUEUE_LEN, $i);
1107 if (($i = $item->due_date) != 0) {
1108 $resp .= add_field(FID_DUE_DATE, Sip::timestamp($i));
1110 if (($i = $item->recall_date) != 0) {
1111 $resp .= add_field(FID_RECALL_DATE, Sip::timestamp($i));
1113 if (($i = $item->hold_pickup_date) != 0) {
1114 $resp .= add_field(FID_HOLD_PICKUP_DATE, Sip::timestamp($i));
1117 $resp .= maybe_add(FID_SCREEN_MSG, $item->screen_msg);
1118 $resp .= maybe_add(FID_PRINT_LINE, $item->print_line);
1121 $self->write_msg($resp);
1123 return(ITEM_INFORMATION);
1126 sub handle_item_status_update {
1127 my ($self, $server) = @_;
1128 my $ils = $server->{ils};
1129 my ($trans_date, $item_id, $terminal_pwd, $item_props);
1130 my $fields = $self->{fields};
1133 my $resp = ITEM_STATUS_UPDATE_RESP;
1135 ($trans_date) = @{$self->{fixed_fields}};
1137 $ils->check_inst_id($fields->{(FID_INST_ID)});
1139 $item_id = $fields->{(FID_ITEM_ID)};
1140 $item_props = $fields->{(FID_ITEM_PROPS)};
1142 if (!defined($item_id)) {
1143 syslog("LOG_WARNING",
1144 "handle_item_status: received message without Item ID field");
1146 $item = $ils->find_item($item_id);
1152 $resp .= Sip::timestamp;
1153 $resp .= add_field(FID_ITEM_ID, $item_id);
1157 $status = $item->status_update($item_props);
1159 $resp .= $status->ok ? '1' : '0';
1160 $resp .= Sip::timestamp;
1162 $resp .= add_field(FID_ITEM_ID, $item->id);
1163 $resp .= add_field(FID_TITLE_ID, $item->title_id);
1164 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
1167 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1168 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1170 $self->write_msg($resp);
1172 return(ITEM_STATUS_UPDATE);
1175 sub handle_patron_enable {
1176 my ($self, $server) = @_;
1177 my $ils = $server->{ils};
1178 my $fields = $self->{fields};
1179 my ($trans_date, $patron_id, $terminal_pwd, $patron_pwd);
1180 my ($status, $patron);
1181 my $resp = PATRON_ENABLE_RESP;
1183 ($trans_date) = @{$self->{fixed_fields}};
1184 $patron_id = $fields->{(FID_PATRON_ID)};
1185 $patron_pwd = $fields->{(FID_PATRON_PWD)};
1187 syslog("LOG_DEBUG", "handle_patron_enable: patron_id: '%s', patron_pwd: '%s'",
1188 $patron_id, $patron_pwd);
1190 $patron = $ils->find_patron($patron_id);
1192 if (!defined($patron)) {
1194 $resp .= 'YYYY' . (' ' x 10) . '000' . Sip::timestamp();
1195 $resp .= add_field(FID_PATRON_ID, $patron_id);
1196 $resp .= add_field(FID_PERSONAL_NAME, '');
1197 $resp .= add_field(FID_VALID_PATRON, 'N');
1198 $resp .= add_field(FID_VALID_PATRON_PWD, 'N');
1201 if (!defined($patron_pwd) || $patron->check_password($patron_pwd)) {
1202 # Don't enable the patron if there was an invalid password
1203 $status = $patron->enable;
1205 $resp .= patron_status_string($patron);
1206 $resp .= $patron->language . Sip::timestamp();
1208 $resp .= add_field(FID_PATRON_ID, $patron->id);
1209 $resp .= add_field(FID_PERSONAL_NAME, $patron->name);
1210 if (defined($patron_pwd)) {
1211 $resp .= add_field(FID_VALID_PATRON_PWD,
1212 sipbool($patron->check_password($patron_pwd)));
1214 $resp .= add_field(FID_VALID_PATRON, 'Y');
1215 $resp .= maybe_add(FID_SCREEN_MSG, $patron->screen_msg);
1216 $resp .= maybe_add(FID_PRINT_LINE, $patron->print_line);
1219 $resp .= add_field(FID_INST_ID, $ils->institution);
1221 $self->write_msg($resp);
1223 return(PATRON_ENABLE);
1227 my ($self, $server) = @_;
1228 my $ils = $server->{ils};
1229 my ($hold_mode, $trans_date);
1230 my ($expiry_date, $pickup_locn, $hold_type, $patron_id, $patron_pwd);
1231 my ($item_id, $title_id, $fee_ack);
1232 my $fields = $self->{fields};
1234 my $resp = HOLD_RESP;
1236 ($hold_mode, $trans_date) = @{$self->{fixed_fields}};
1238 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_hold");
1240 $patron_id = $fields->{(FID_PATRON_ID) };
1241 $expiry_date = $fields->{(FID_EXPIRATION) } || '';
1242 $pickup_locn = $fields->{(FID_PICKUP_LOCN)} || '';
1243 $hold_type = $fields->{(FID_HOLD_TYPE) } || '2'; # Any copy of title
1244 $patron_pwd = $fields->{(FID_PATRON_PWD) };
1245 $item_id = $fields->{(FID_ITEM_ID) } || '';
1246 $title_id = $fields->{(FID_TITLE_ID) } || '';
1247 $fee_ack = $fields->{(FID_FEE_ACK) } || 'N';
1249 if ($hold_mode eq '+') {
1250 $status = $ils->add_hold($patron_id, $patron_pwd, $item_id, $title_id,
1251 $expiry_date, $pickup_locn, $hold_type, $fee_ack);
1252 } elsif ($hold_mode eq '-') {
1253 $status = $ils->cancel_hold($patron_id, $patron_pwd, $item_id, $title_id);
1254 } elsif ($hold_mode eq '*') {
1255 $status = $ils->alter_hold($patron_id, $patron_pwd, $item_id, $title_id,
1256 $expiry_date, $pickup_locn, $hold_type, $fee_ack);
1258 syslog("LOG_WARNING", "handle_hold: Unrecognized hold mode '%s' from terminal '%s'",
1259 $hold_mode, $server->{account}->{id});
1260 $status = $ils->Transaction::Hold; # new?
1261 $status->screen_msg("System error. Please contact library staff.");
1264 $resp .= $status->ok;
1265 $resp .= sipbool($status->item && $status->item->available($patron_id));
1266 $resp .= Sip::timestamp;
1269 $resp .= add_field(FID_PATRON_ID, $status->patron->id);
1271 ($status->expiration_date) and
1272 $resp .= maybe_add(FID_EXPIRATION,
1273 Sip::timestamp($status->expiration_date));
1274 $resp .= maybe_add(FID_QUEUE_POS, $status->queue_position);
1275 $resp .= maybe_add(FID_PICKUP_LOCN, $status->pickup_location);
1276 $resp .= maybe_add(FID_ITEM_ID, $status->item->id);
1277 $resp .= maybe_add(FID_TITLE_ID, $status->item->title_id);
1279 # Not ok. still need required fields
1280 $resp .= add_field(FID_PATRON_ID, $patron_id);
1283 $resp .= add_field(FID_INST_ID, $ils->institution);
1284 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1285 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1287 $self->write_msg($resp);
1293 my ($self, $server) = @_;
1294 my $ils = $server->{ils};
1295 my ($third_party, $no_block, $trans_date, $nb_due_date);
1296 my ($patron_id, $patron_pwd, $item_id, $title_id, $item_props, $fee_ack);
1297 my $fields = $self->{fields};
1299 my ($patron, $item);
1300 my $resp = RENEW_RESP;
1302 ($third_party, $no_block, $trans_date, $nb_due_date) =
1303 @{$self->{fixed_fields}};
1305 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_renew");
1307 if ($no_block eq 'Y') {
1308 syslog("LOG_WARNING",
1309 "handle_renew: recieved 'no block' renewal from terminal '%s'",
1310 $server->{account}->{id});
1313 $patron_id = $fields->{(FID_PATRON_ID)};
1314 $patron_pwd = $fields->{(FID_PATRON_PWD)};
1315 $item_id = $fields->{(FID_ITEM_ID)};
1316 $title_id = $fields->{(FID_TITLE_ID)};
1317 $item_props = $fields->{(FID_ITEM_PROPS)};
1318 $fee_ack = $fields->{(FID_FEE_ACK)};
1320 $status = $ils->renew($patron_id, $patron_pwd, $item_id, $title_id,
1321 $no_block, $nb_due_date, $third_party,
1322 $item_props, $fee_ack);
1324 $patron = $status->patron;
1325 $item = $status->item;
1329 $resp .= $status->renewal_ok ? 'Y' : 'N';
1330 if ($ils->supports('magnetic media')) {
1331 $resp .= sipbool($item->magnetic);
1335 $resp .= sipbool($status->desensitize);
1336 $resp .= Sip::timestamp;
1337 $resp .= add_field(FID_PATRON_ID, $patron->id);
1338 $resp .= add_field(FID_ITEM_ID, $item->id);
1339 $resp .= add_field(FID_TITLE_ID, $item->title_id);
1340 $resp .= add_field(FID_DUE_DATE, Sip::timestamp($item->due_date));
1341 if ($ils->supports('security inhibit')) {
1342 $resp .= add_field(FID_SECURITY_INHIBIT,
1343 $status->security_inhibit);
1345 $resp .= add_field(FID_MEDIA_TYPE, $item->sip_media_type);
1346 $resp .= maybe_add(FID_ITEM_PROPS, $item->sip_item_properties);
1348 # renew failed for some reason
1349 # not OK, renewal not OK, Unknown media type (why bother checking?)
1351 $resp .= Sip::timestamp;
1352 # If we found the patron or the item, the return the ILS
1353 # information, otherwise echo back the infomation we received
1355 $resp .= add_field(FID_PATRON_ID, $patron ? $patron->id : $patron_id);
1356 $resp .= add_field(FID_ITEM_ID, $item ? $item->id : $item_id );
1357 $resp .= add_field(FID_TITLE_ID, $item ? $item->title_id : $title_id );
1358 $resp .= add_field(FID_DUE_DATE, '');
1361 if ($status->fee_amount) {
1362 $resp .= add_field(FID_FEE_AMT, $status->fee_amount);
1363 $resp .= maybe_add(FID_CURRENCY, $status->sip_currency);
1364 $resp .= maybe_add(FID_FEE_TYPE, $status->sip_fee_type);
1365 $resp .= maybe_add(FID_TRANSACTION_ID, $status->transaction_id);
1368 $resp .= add_field(FID_INST_ID, $ils->institution);
1369 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1370 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1372 $self->write_msg($resp);
1377 sub handle_renew_all {
1378 # my ($third_party, $no_block, $nb_due_date, $fee_ack, $patron);
1380 my ($self, $server) = @_;
1381 my $ils = $server->{ils};
1382 my ($trans_date, $patron_id, $patron_pwd, $terminal_pwd, $fee_ack);
1383 my $fields = $self->{fields};
1384 my $resp = RENEW_ALL_RESP;
1386 my (@renewed, @unrenewed);
1388 $ils->check_inst_id($fields->{(FID_INST_ID)}, "handle_renew_all");
1390 ($trans_date) = @{$self->{fixed_fields}};
1392 $patron_id = $fields->{(FID_PATRON_ID)};
1393 $patron_pwd = $fields->{(FID_PATRON_PWD)};
1394 $terminal_pwd = $fields->{(FID_TERMINAL_PWD)};
1395 $fee_ack = $fields->{(FID_FEE_ACK)};
1397 $status = $ils->renew_all($patron_id, $patron_pwd, $fee_ack);
1399 $resp .= $status->ok ? '1' : '0';
1402 $resp .= add_count("renew_all/renewed_count" , 0);
1403 $resp .= add_count("renew_all/unrenewed_count", 0);
1407 @renewed = (@{$status->renewed});
1408 @unrenewed = (@{$status->unrenewed});
1409 $resp .= add_count("renew_all/renewed_count" , scalar @renewed );
1410 $resp .= add_count("renew_all/unrenewed_count", scalar @unrenewed);
1413 $resp .= Sip::timestamp;
1414 $resp .= add_field(FID_INST_ID, $ils->institution);
1416 $resp .= join('', map(add_field(FID_RENEWED_ITEMS , $_), @renewed ));
1417 $resp .= join('', map(add_field(FID_UNRENEWED_ITEMS, $_), @unrenewed));
1419 $resp .= maybe_add(FID_SCREEN_MSG, $status->screen_msg);
1420 $resp .= maybe_add(FID_PRINT_LINE, $status->print_line);
1422 $self->write_msg($resp);
1428 # send_acs_status($self, $server)
1430 # Send an ACS Status message, which is contains lots of little fields
1431 # of information gleaned from all sorts of places.
1434 my @message_type_names = (
1435 "patron status request",
1440 "request sc/acs resend",
1442 "patron information",
1443 "end patron session",
1446 "item status update",
1453 sub send_acs_status {
1454 my ($self, $server, $screen_msg, $print_line) = @_;
1455 my $msg = ACS_STATUS;
1456 ($server) or die "send_acs_status error: no \$server argument received";
1457 my $account = $server->{account} or die "send_acs_status error: no 'account' in \$server object:\n" . Dumper($server);
1458 my $policy = $server->{policy} or die "send_acs_status error: no 'policy' in \$server object:\n" . Dumper($server);
1459 my $ils = $server->{ils} or die "send_acs_status error: no 'ils' in \$server object:\n" . Dumper($server);
1460 my ($online_status, $checkin_ok, $checkout_ok, $ACS_renewal_policy);
1461 my ($status_update_ok, $offline_ok, $timeout, $retries);
1463 $online_status = 'Y';
1464 $checkout_ok = sipbool($ils->checkout_ok);
1465 $checkin_ok = sipbool($ils->checkin_ok);
1466 $ACS_renewal_policy = sipbool($policy->{renewal});
1467 $status_update_ok = sipbool($ils->status_update_ok);
1468 $offline_ok = sipbool($ils->offline_ok);
1469 $timeout = sprintf("%03d", $policy->{timeout});
1470 $retries = sprintf("%03d", $policy->{retries});
1472 if (length($timeout) != 3) {
1473 syslog("LOG_ERR", "handle_acs_status: timeout field wrong size: '%s'",
1478 if (length($retries) != 3) {
1479 syslog("LOG_ERR", "handle_acs_status: retries field wrong size: '%s'",
1484 $msg .= "$online_status$checkin_ok$checkout_ok$ACS_renewal_policy";
1485 $msg .= "$status_update_ok$offline_ok$timeout$retries";
1486 $msg .= Sip::timestamp();
1488 if ($protocol_version == 1) {
1490 } elsif ($protocol_version == 2) {
1494 'Bad setting for $protocol_version, "%s" in send_acs_status',
1500 $msg .= add_field(FID_INST_ID, $account->{institution});
1502 if ($protocol_version >= 2) {
1503 # Supported messages: we do it all
1504 my $supported_msgs = '';
1506 foreach my $msg_name (@message_type_names) {
1507 if ($msg_name eq 'request sc/acs resend') {
1508 $supported_msgs .= Sip::sipbool(1);
1510 $supported_msgs .= Sip::sipbool($ils->supports($msg_name));
1513 if (length($supported_msgs) < 16) {
1514 syslog("LOG_ERR", 'send_acs_status: supported messages "%s" too short', $supported_msgs);
1516 $msg .= add_field(FID_SUPPORTED_MSGS, $supported_msgs);
1519 $msg .= maybe_add(FID_SCREEN_MSG, $screen_msg);
1521 if (defined($account->{print_width}) && defined($print_line)
1522 && $account->{print_width} < length($print_line)) {
1523 syslog("LOG_WARNING", "send_acs_status: print line '%s' too long. Truncating",
1525 $print_line = substr($print_line, 0, $account->{print_width});
1528 $msg .= maybe_add(FID_PRINT_LINE, $print_line);
1530 # Do we want to tell the terminal its location?
1532 $self->write_msg($msg);
1537 # build_patron_status: create the 14-char patron status
1538 # string for the Patron Status message
1540 sub patron_status_string {
1544 syslog("LOG_DEBUG", "patron_status_string: %s charge_ok: %s", $patron->id,
1545 $patron->charge_ok);
1546 $patron_status = sprintf('%s%s%s%s%s%s%s%s%s%s%s%s%s%s',
1547 denied($patron->charge_ok),
1548 denied($patron->renew_ok),
1549 denied($patron->recall_ok),
1550 denied($patron->hold_ok),
1551 boolspace($patron->card_lost),
1552 boolspace($patron->too_many_charged),
1553 boolspace($patron->too_many_overdue),
1554 boolspace($patron->too_many_renewal),
1555 boolspace($patron->too_many_claim_return),
1556 boolspace($patron->too_many_lost),
1557 boolspace($patron->excessive_fines),
1558 boolspace($patron->excessive_fees),
1559 boolspace($patron->recall_overdue),
1560 boolspace($patron->too_many_billed));
1561 return $patron_status;
1566 my ($username,$password) = (shift,shift);
1567 $ENV{REMOTE_USER} = $username;
1568 my $query = CGI->new();
1569 $query->param(userid => $username);
1570 $query->param(password => $password);
1571 my ($status, $cookie, $sessionID) = check_api_auth($query, {circulate=>1}, "intranet");
1572 print STDERR "check_api_auth returns " . ($status || 'undef') . "\n";
1573 # print "api_auth userenv = " . &dump_userenv;