]> git.evergreen-ils.org Git - working/SIPServer.git/blob - Sip.pm
Fix missing parens in hash dereference
[working/SIPServer.git] / Sip.pm
1 #
2 # Copyright (C) 2006-2008  Georgia Public Library Service
3
4 # Author: David J. Fiander
5
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.
9
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.
14
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,
18 # MA 02111-1307 USA
19 #
20 # Sip.pm: General Sip utility functions
21 #
22
23 package Sip;
24
25 use strict;
26 use warnings;
27 use English;
28 use Exporter;
29
30 use Sys::Syslog qw(syslog);
31 use POSIX qw(strftime);
32
33 use Sip::Constants qw(SIP_DATETIME);
34 use Sip::Checksum qw(checksum);
35
36 our @ISA = qw(Exporter);
37
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
41                     $last_response);
42
43 our %EXPORT_TAGS = (
44                     all => [qw(y_or_n timestamp add_field maybe_add
45                                add_count denied sipbool boolspace write_msg
46                                read_SIP_packet
47                                $error_detection $protocol_version
48                                $field_delimiter $last_response)]);
49
50
51 our $error_detection = 0;
52 our $protocol_version = 1;
53 our $field_delimiter = '|';     # Protocol Default
54
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)
60
61 our $last_response = '';
62
63 sub timestamp {
64     my $time = $_[0] || time();
65
66     return strftime(SIP_DATETIME, localtime($time));
67 }
68
69 #
70 # add_field(field_id, value)
71 #    return constructed field value
72 #
73 sub add_field {
74     my ($field_id, $value) = @_;
75     my ($i, $ent);
76
77     if (!defined($value)) {
78         syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
79                $field_id);
80         $value = '';
81     }
82
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));
86
87     while (($i = index($value, $field_delimiter)) != ($[-1)) {
88         substr($value, $i, 1) = $ent;
89     }
90
91     return $field_id . $value . $field_delimiter;
92 }
93 #
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
97 #
98 sub maybe_add {
99     my ($fid, $value) = @_;
100
101     return (defined($value) && $value) ? add_field($fid, $value) : '';
102 }
103
104 #
105 # add_count()  produce fixed four-character count field,
106 # or a string of four spaces if the count is invalid for some
107 # reason
108 #
109 sub add_count {
110     my ($label, $count) = @_;
111
112     # If the field is unsupported, it will be undef, return blanks
113     # as per the spec.
114     if (!defined($count)) {
115         return ' ' x 4;
116     }
117
118     $count = sprintf("%04d", $count);
119     if (length($count) != 4) {
120         syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
121                $label, $count);
122         $count = ' ' x 4;
123     }
124     return $count;
125 }
126
127 #
128 # denied($bool)
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.
134 #
135 sub denied {
136     my $bool = shift;
137
138     return boolspace(!$bool);
139 }
140
141 sub sipbool {
142     my $bool = shift;
143
144     return $bool ? 'Y' : 'N';
145 }
146
147 #
148 # boolspace: ' ' is false, 'Y' is true. (don't ask)
149 #
150 sub boolspace {
151     my $bool = shift;
152
153     return $bool ? 'Y' : ' ';
154 }
155
156
157 # read_SIP_packet($file)
158 #
159 # Read a packet from $file, using the correct record separator
160 #
161 sub read_SIP_packet {
162     my $file = shift;
163     my $record;
164     local $/ = "\r";
165
166     $record = readline($file);
167
168     #
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
172     # right.
173     # 
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
177     # on the input.
178     # 
179     $record =~ s/^\012// if $record;
180     syslog("LOG_INFO", "INPUT MSG: '$record'") if $record;
181     return $record;
182 }
183
184 #
185 # write_msg($msg, $file)
186 #
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
190 #
191 # If $file is set, then it's a file handle: write to it, otherwise
192 # just write to the default destination.
193 #
194
195 sub write_msg {
196     my ($self, $msg, $file) = @_;
197     my $cksum;
198
199     if ($error_detection) {
200         if (defined($self->{seqno})) {
201             $msg .= 'AY' . $self->{seqno};
202         }
203         $msg .= 'AZ';
204         $cksum = checksum($msg);
205         $msg .= sprintf('%04.4X', $cksum);
206     }
207
208
209     if ($file) {
210         print $file "$msg\r";
211     } else {
212         print "$msg\r";
213         syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
214     }
215
216     $last_response = $msg;
217 }
218
219 1;