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();
51 return strftime(SIP_DATETIME, localtime($time));
55 # add_field(field_id, value)
56 # return constructed field value
59 my ($field_id, $value) = @_;
62 if (!defined($value)) {
63 syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
68 # Replace any occurences of the field delimiter in the
69 # field value with the HTML character entity
70 $ent = sprintf("&#%d;", ord($field_delimiter));
72 while (($i = index($value, $field_delimiter)) != ($[-1)) {
73 substr($value, $i, 1) = $ent;
76 return $field_id . $value . $field_delimiter;
79 # maybe_add(field_id, value):
80 # If value is defined and non-empty, then return the
81 # constructed field value, otherwise return the empty string
84 my ($fid, $value) = @_;
85 return (defined($value) && $value) ? add_field($fid, $value) : '';
89 # add_count() produce fixed four-character count field,
90 # or a string of four spaces if the count is invalid for some
94 my ($label, $count) = @_;
96 # If the field is unsupported, it will be undef, return blanks
98 if (!defined($count)) {
102 $count = sprintf("%04d", $count);
103 if (length($count) != 4) {
104 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
113 # if $bool is false, return true. This is because SIP statuses
114 # are inverted: we report that something has been denied, not that
115 # it's permitted. For example, 'renewal priv. denied' of 'Y' means
116 # that the user's not permitted to renew. I assume that the ILS has
117 # real positive tests.
121 return boolspace(!$bool);
126 return $bool ? 'Y' : 'N';
130 # boolspace: ' ' is false, 'Y' is true. (don't ask)
134 return $bool ? 'Y' : ' ';
138 # read_SIP_packet($file)
140 # Read a packet from $file, using the correct record separator
142 sub read_SIP_packet {
144 { # adapted from http://perldoc.perl.org/5.8.8/functions/readline.html
147 unless (defined($record = readline(shift))) {
149 syslog("LOG_ERR", "read_SIP_packet ERROR: $!");
150 die "read_SIP_packet ERROR: $!";
155 syslog("LOG_INFO", "read_SIP_packet, INPUT MSG: '$record'") if $record;
160 # write_msg($msg, $file)
162 # Send $msg to the SC. If error detection is active, then
163 # add the sequence number (if $seqno is non-zero) and checksum
164 # to the message, and save the whole thing as $last_response
166 # If $file is set, then it's a file handle: write to it, otherwise
167 # just write to the default destination.
171 my ($self, $msg, $file) = @_;
174 if ($error_detection) {
175 if (defined($self->{seqno})) {
176 $msg .= 'AY' . $self->{seqno};
179 $cksum = checksum($msg);
180 $msg .= sprintf('%04.4X', $cksum);
185 print $file "$msg\r";
188 syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
191 $last_response = $msg;