2 # Sip.pm: General Sip utility functions
12 use Sys::Syslog qw(syslog);
13 use POSIX qw(strftime);
16 use Sip::Constants qw(SIP_DATETIME);
17 use Sip::Checksum qw(checksum);
19 use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
25 @EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
26 denied sipbool boolspace write_msg read_SIP_packet
27 $error_detection $protocol_version $field_delimiter
31 all => [qw(y_or_n timestamp add_field maybe_add
32 add_count denied sipbool boolspace write_msg
34 $error_detection $protocol_version
35 $field_delimiter $last_response)]);
38 our $error_detection = 0;
39 our $protocol_version = 1;
40 our $field_delimiter = '|'; # Protocol Default
42 # We need to keep a copy of the last message we sent to the SC,
43 # in case there's a transmission error and the SC sends us a
44 # REQUEST_ACS_RESEND. If we receive a REQUEST_ACS_RESEND before
45 # we've ever sent anything, then we are to respond with a
46 # REQUEST_SC_RESEND (p.16)
48 our $last_response = '';
51 my $time = $_[0] || time();
52 if ($time=~m/^(\d{4})\-(\d{2})\-(\d{2})/) {
53 # passing a db returned date as is + bogus time
54 return sprintf( '%04d%02d%02d 235900', $1, $2, $3);
56 return strftime(SIP_DATETIME, localtime($time));
60 # add_field(field_id, value)
61 # return constructed field value
64 my ($field_id, $value) = @_;
67 if (!defined($value)) {
68 syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
72 $value=~s/\r/ /g; # CR terminates a sip message
73 # Protect against them in sip text fields
75 # Replace any occurences of the field delimiter in the
76 # field value with the HTML character entity
77 $ent = sprintf("&#%d;", ord($field_delimiter));
79 while (($i = index($value, $field_delimiter)) != ($[-1)) {
80 substr($value, $i, 1) = $ent;
83 return $field_id . $value . $field_delimiter;
86 # maybe_add(field_id, value):
87 # If value is defined and non-empty, then return the
88 # constructed field value, otherwise return the empty string
91 my ($fid, $value) = @_;
92 return (defined($value) && $value) ? add_field($fid, $value) : '';
96 # add_count() produce fixed four-character count field,
97 # or a string of four spaces if the count is invalid for some
101 my ($label, $count) = @_;
103 # If the field is unsupported, it will be undef, return blanks
105 if (!defined($count)) {
109 $count = sprintf("%04d", $count);
110 if (length($count) != 4) {
111 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
120 # if $bool is false, return true. This is because SIP statuses
121 # are inverted: we report that something has been denied, not that
122 # it's permitted. For example, 'renewal priv. denied' of 'Y' means
123 # that the user's not permitted to renew. I assume that the ILS has
124 # real positive tests.
128 return boolspace(!$bool);
133 return $bool ? 'Y' : 'N';
137 # boolspace: ' ' is false, 'Y' is true. (don't ask)
141 return $bool ? 'Y' : ' ';
145 # Read a packet from $file, using the correct record separator
147 sub read_SIP_packet {
149 my $fh = shift or syslog("LOG_ERR", "read_SIP_packet: no filehandle argument!");
151 # local $/ = "\012"; # Internet Record Separator (lax version)
152 { # adapted from http://perldoc.perl.org/5.8.8/functions/readline.html
153 for (my $tries=1; $tries<=3; $tries++) {
155 $record = readline($fh);
156 if (defined($record)) {
157 while(chomp($record)){1;}
158 $len1 = length($record);
159 syslog("LOG_DEBUG", "read_SIP_packet, INPUT MSG: '$record'");
160 $record =~ s/^\s*[^A-z0-9]+//s;
161 $record =~ s/[^A-z0-9]+$//s;
162 $record =~ s/\015?\012//g;
163 $record =~ s/\015?\012//s;
164 $record =~ s/\015*\012*$//s; # treat as one line to include the extra linebreaks we are trying to remove!
165 while(chomp($record)){1;}
171 syslog("LOG_DEBUG", "read_SIP_packet (try #$tries) ERROR: $!");
172 # die "read_SIP_packet ERROR: $!";
173 warn "read_SIP_packet ERROR: $!";
179 my $len2 = length($record);
180 syslog("LOG_INFO", "read_SIP_packet, INPUT MSG: '$record'") if $record;
181 ($len1 != $len2) and syslog("LOG_DEBUG", "read_SIP_packet, trimmed %s character(s) (after chomps).", $len1-$len2);
183 syslog("LOG_WARNING", "read_SIP_packet input %s, end of input.", (defined($record)? "empty ($record)" : 'undefined'));
189 # write_msg($msg, $file)
191 # Send $msg to the SC. If error detection is active, then
192 # add the sequence number (if $seqno is non-zero) and checksum
193 # to the message, and save the whole thing as $last_response
195 # If $file is set, then it's a file handle: write to it, otherwise
196 # just write to the default destination.
200 my ($self, $msg, $file) = @_;
203 if ($error_detection) {
204 if (defined($self->{seqno})) {
205 $msg .= 'AY' . $self->{seqno};
208 $cksum = checksum($msg);
209 $msg .= sprintf('%04.4X', $cksum);
213 print $file "$msg$CRLF";
214 syslog("LOG_DEBUG", "write_msg outputting to $file");
218 syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
220 $last_response = $msg;