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 use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
24 @EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
25 denied sipbool boolspace write_msg read_SIP_packet
26 $error_detection $protocol_version $field_delimiter
30 all => [qw(y_or_n timestamp add_field maybe_add
31 add_count denied sipbool boolspace write_msg
33 $error_detection $protocol_version
34 $field_delimiter $last_response)]);
37 our $error_detection = 0;
38 our $protocol_version = 1;
39 our $field_delimiter = '|'; # Protocol Default
41 # We need to keep a copy of the last message we sent to the SC,
42 # in case there's a transmission error and the SC sends us a
43 # REQUEST_ACS_RESEND. If we receive a REQUEST_ACS_RESEND before
44 # we've ever sent anything, then we are to respond with a
45 # REQUEST_SC_RESEND (p.16)
47 our $last_response = '';
50 my $time = $_[0] || time();
52 return strftime(SIP_DATETIME, localtime($time));
56 # add_field(field_id, value)
57 # return constructed field value
60 my ($field_id, $value) = @_;
63 if (!defined($value)) {
64 syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
69 # Replace any occurences of the field delimiter in the
70 # field value with the HTML character entity
71 $ent = sprintf("&#%d;", ord($field_delimiter));
73 while (($i = index($value, $field_delimiter)) != ($[-1)) {
74 substr($value, $i, 1) = $ent;
77 return $field_id . $value . $field_delimiter;
80 # maybe_add(field_id, value):
81 # If value is defined and non-empty, then return the
82 # constructed field value, otherwise return the empty string
85 my ($fid, $value) = @_;
87 return (defined($value) && $value) ? add_field($fid, $value) : '';
91 # add_count() produce fixed four-character count field,
92 # or a string of four spaces if the count is invalid for some
96 my ($label, $count) = @_;
98 # If the field is unsupported, it will be undef, return blanks
100 if (!defined($count)) {
104 $count = sprintf("%04d", $count);
105 if (length($count) != 4) {
106 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
115 # if $bool is false, return true. This is because SIP statuses
116 # are inverted: we report that something has been denied, not that
117 # it's permitted. For example, 'renewal priv. denied' of 'Y' means
118 # that the user's not permitted to renew. I assume that the ILS has
119 # real positive tests.
124 return boolspace(!$bool);
130 return $bool ? 'Y' : 'N';
134 # boolspace: ' ' is false, 'Y' is true. (don't ask)
139 return $bool ? 'Y' : ' ';
143 # read_SIP_packet($file)
145 # Read a packet from $file, using the correct record separator
147 sub read_SIP_packet {
152 $record = readline($file);
153 syslog("LOG_INFO", "INPUT MSG: '$record'") if $record;
158 # write_msg($msg, $file)
160 # Send $msg to the SC. If error detection is active, then
161 # add the sequence number (if $seqno is non-zero) and checksum
162 # to the message, and save the whole thing as $last_response
164 # If $file is set, then it's a file handle: write to it, otherwise
165 # just write to the default destination.
169 my ($self, $msg, $file) = @_;
172 if ($error_detection) {
173 if (defined($self->{seqno})) {
174 $msg .= 'AY' . $self->{seqno};
177 $cksum = checksum($msg);
178 $msg .= sprintf('%04.4X', $cksum);
183 print $file "$msg\r";
186 syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
189 $last_response = $msg;