2 # Sip.pm: General Sip utility functions
12 use Sys::Syslog qw(syslog);
13 use POSIX qw(strftime);
15 use Sip::Constants qw(SIP_DATETIME);
16 use Sip::Checksum qw(checksum);
18 our @ISA = qw(Exporter);
20 our @EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
21 denied sipbool boolspace write_msg read_SIP_packet
22 $error_detection $protocol_version $field_delimiter
26 all => [qw(y_or_n timestamp add_field maybe_add
27 add_count denied sipbool boolspace write_msg
29 $error_detection $protocol_version
30 $field_delimiter $last_response)]);
33 our $error_detection = 0;
34 our $protocol_version = 1;
35 our $field_delimiter = '|'; # Protocol Default
37 # We need to keep a copy of the last message we sent to the SC,
38 # in case there's a transmission error and the SC sends us a
39 # REQUEST_ACS_RESEND. If we receive a REQUEST_ACS_RESEND before
40 # we've ever sent anything, then we are to respond with a
41 # REQUEST_SC_RESEND (p.16)
43 our $last_response = '';
46 my $time = $_[0] || time();
48 return strftime(SIP_DATETIME, localtime($time));
52 # add_field(field_id, value)
53 # return constructed field value
56 my ($field_id, $value) = @_;
59 if (!defined($value)) {
60 syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
65 # Replace any occurences of the field delimiter in the
66 # field value with the HTML character entity
67 $ent = sprintf("&#%d;", ord($field_delimiter));
69 while (($i = index($value, $field_delimiter)) != ($[-1)) {
70 substr($value, $i, 1) = $ent;
73 return $field_id . $value . $field_delimiter;
76 # maybe_add(field_id, value):
77 # If value is defined and non-empty, then return the
78 # constructed field value, otherwise return the empty string
81 my ($fid, $value) = @_;
83 return (defined($value) && $value) ? add_field($fid, $value) : '';
87 # add_count() produce fixed four-character count field,
88 # or a string of four spaces if the count is invalid for some
92 my ($label, $count) = @_;
94 # If the field is unsupported, it will be undef, return blanks
96 if (!defined($count)) {
100 $count = sprintf("%04d", $count);
101 if (length($count) != 4) {
102 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
111 # if $bool is false, return true. This is because SIP statuses
112 # are inverted: we report that something has been denied, not that
113 # it's permitted. For example, 'renewal priv. denied' of 'Y' means
114 # that the user's not permitted to renew. I assume that the ILS has
115 # real positive tests.
120 return boolspace(!$bool);
126 return $bool ? 'Y' : 'N';
130 # boolspace: ' ' is false, 'Y' is true. (don't ask)
135 return $bool ? 'Y' : ' ';
139 # read_SIP_packet($file)
141 # Read a packet from $file, using the correct record separator
143 sub read_SIP_packet {
148 $record = readline($file);
149 syslog("LOG_INFO", "INPUT MSG: '$record'") if $record;
154 # write_msg($msg, $file)
156 # Send $msg to the SC. If error detection is active, then
157 # add the sequence number (if $seqno is non-zero) and checksum
158 # to the message, and save the whole thing as $last_response
160 # If $file is set, then it's a file handle: write to it, otherwise
161 # just write to the default destination.
165 my ($self, $msg, $file) = @_;
168 if ($error_detection) {
169 if (defined($self->{seqno})) {
170 $msg .= 'AY' . $self->{seqno};
173 $cksum = checksum($msg);
174 $msg .= sprintf('%04.4X', $cksum);
179 print $file "$msg\r";
182 syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
185 $last_response = $msg;