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
7 # modify it under the terms of version 2 of the GNU General Public
8 # License as published by the Free Software Foundation.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public
16 # License along with this program; if not, write to the Free
17 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 # Sip.pm: General Sip utility functions
30 use Sys::Syslog qw(syslog);
31 use POSIX qw(strftime);
33 use Sip::Constants qw(SIP_DATETIME);
34 use Sip::Checksum qw(checksum);
36 our @ISA = qw(Exporter);
38 our @EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
39 denied sipbool boolspace write_msg read_SIP_packet
40 $error_detection $protocol_version $field_delimiter
44 all => [qw(y_or_n timestamp add_field maybe_add
45 add_count denied sipbool boolspace write_msg
47 $error_detection $protocol_version
48 $field_delimiter $last_response)]);
51 our $error_detection = 0;
52 our $protocol_version = 1;
53 our $field_delimiter = '|'; # Protocol Default
55 # We need to keep a copy of the last message we sent to the SC,
56 # in case there's a transmission error and the SC sends us a
57 # REQUEST_ACS_RESEND. If we receive a REQUEST_ACS_RESEND before
58 # we've ever sent anything, then we are to respond with a
59 # REQUEST_SC_RESEND (p.16)
61 our $last_response = '';
64 my $time = $_[0] || time();
66 return strftime(SIP_DATETIME, localtime($time));
70 # add_field(field_id, value)
71 # return constructed field value
74 my ($field_id, $value) = @_;
77 if (!defined($value)) {
78 syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
83 # Replace any occurences of the field delimiter in the
84 # field value with the HTML character entity
85 $ent = sprintf("&#%d;", ord($field_delimiter));
87 while (($i = index($value, $field_delimiter)) != ($[-1)) {
88 substr($value, $i, 1) = $ent;
91 return $field_id . $value . $field_delimiter;
94 # maybe_add(field_id, value):
95 # If value is defined and non-empty, then return the
96 # constructed field value, otherwise return the empty string
99 my ($fid, $value) = @_;
101 return (defined($value) && $value) ? add_field($fid, $value) : '';
105 # add_count() produce fixed four-character count field,
106 # or a string of four spaces if the count is invalid for some
110 my ($label, $count) = @_;
112 # If the field is unsupported, it will be undef, return blanks
114 if (!defined($count)) {
118 $count = sprintf("%04d", $count);
119 if (length($count) != 4) {
120 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
129 # if $bool is false, return true. This is because SIP statuses
130 # are inverted: we report that something has been denied, not that
131 # it's permitted. For example, 'renewal priv. denied' of 'Y' means
132 # that the user's not permitted to renew. I assume that the ILS has
133 # real positive tests.
138 return boolspace(!$bool);
144 return $bool ? 'Y' : 'N';
148 # boolspace: ' ' is false, 'Y' is true. (don't ask)
153 return $bool ? 'Y' : ' ';
157 # read_SIP_packet($file)
159 # Read a packet from $file, using the correct record separator
161 sub read_SIP_packet {
166 $record = readline($file);
169 # Cen-Tec self-check terminals transmit '\r\n' line terminators.
170 # This is actually very hard to deal with in perl in a reasonable
171 # since every OTHER piece of hardware out there gets the protocol
174 # The incorrect line terminator presents as a \r at the end of the
175 # first record, and then a \n at the BEGINNING of the next record.
176 # So, the simplest thing to do is just throw away a leading newline
179 $record =~ s/^\012// if $record;
180 syslog("LOG_INFO", "INPUT MSG: '$record'") if $record;
185 # write_msg($msg, $file)
187 # Send $msg to the SC. If error detection is active, then
188 # add the sequence number (if $seqno is non-zero) and checksum
189 # to the message, and save the whole thing as $last_response
191 # If $file is set, then it's a file handle: write to it, otherwise
192 # just write to the default destination.
196 my ($self, $msg, $file) = @_;
199 if ($error_detection) {
200 if (defined($self->{seqno})) {
201 $msg .= 'AY' . $self->{seqno};
204 $cksum = checksum($msg);
205 $msg .= sprintf('%04.4X', $cksum);
210 print $file "$msg\r";
213 syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
216 $last_response = $msg;