]> git.evergreen-ils.org Git - working/SIPServer.git/blob - Sip.pm
Update to current default EG values.
[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 use Encode;
30
31 use Sys::Syslog qw(syslog);
32 use POSIX qw(strftime);
33 use Socket qw(:crlf);
34
35 use Sip::Constants qw(SIP_DATETIME);
36 use Sip::Checksum qw(checksum);
37
38 our @ISA = qw(Exporter);
39
40 our @EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
41                     denied sipbool boolspace write_msg read_SIP_packet
42                     $error_detection $protocol_version $field_delimiter
43                     $last_response);
44
45 our %EXPORT_TAGS = (
46                     all => [qw(y_or_n timestamp add_field maybe_add
47                                add_count denied sipbool boolspace write_msg
48                                read_SIP_packet
49                                $error_detection $protocol_version
50                                $field_delimiter $last_response)]);
51
52
53 our $error_detection = 0;
54 our $protocol_version = 1;
55 our $field_delimiter = '|';     # Protocol Default
56
57 # We need to keep a copy of the last message we sent to the SC,
58 # in case there's a transmission error and the SC sends us a
59 # REQUEST_ACS_RESEND.  If we receive a REQUEST_ACS_RESEND before
60 # we've ever sent anything, then we are to respond with a
61 # REQUEST_SC_RESEND (p.16)
62
63 our $last_response = '';
64
65 sub timestamp {
66     my $time = $_[0] || time();
67     return strftime(SIP_DATETIME, localtime($time));
68 }
69
70 #
71 # add_field(field_id, value)
72 #    return constructed field value
73 #
74 sub add_field {
75     my ($field_id, $value) = @_;
76     my ($i, $ent);
77
78     if (!defined($value)) {
79         syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
80                $field_id);
81         $value = '';
82     }
83
84     # Replace any occurences of the field delimiter in the
85     # field value with the HTML character entity
86     $ent = sprintf("&#%d;", ord($field_delimiter));
87
88     while (($i = index($value, $field_delimiter)) != ($[-1)) {
89         substr($value, $i, 1) = $ent;
90     }
91
92     return $field_id . $value . $field_delimiter;
93 }
94 #
95 # maybe_add(field_id, value):
96 #    If value is defined and non-empty, then return the
97 #    constructed field value, otherwise return the empty string.
98 #    NOTE: if zero is a valid value for your field, don't use maybe_add!
99 #
100 sub maybe_add {
101     my ($fid, $value) = @_;
102     return (defined($value) && $value) ? add_field($fid, $value) : '';
103 }
104
105 #
106 # add_count()  produce fixed four-character count field,
107 # or a string of four spaces if the count is invalid for some
108 # reason
109 #
110 sub add_count {
111     my ($label, $count) = @_;
112
113     # If the field is unsupported, it will be undef, return blanks
114     # as per the spec.
115     if (!defined($count)) {
116         return ' ' x 4;
117     }
118
119     $count = sprintf("%04d", $count);
120     if (length($count) != 4) {
121         syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
122                $label, $count);
123         $count = ' ' x 4;
124     }
125     return $count;
126 }
127
128 #
129 # denied($bool)
130 # if $bool is false, return true.  This is because SIP statuses
131 # are inverted:  we report that something has been denied, not that
132 # it's permitted.  For example, 'renewal priv. denied' of 'Y' means
133 # that the user's not permitted to renew.  I assume that the ILS has
134 # real positive tests.
135 #
136 sub denied {
137     my $bool = shift;
138     return boolspace(!$bool);
139 }
140
141 sub sipbool {
142     my $bool = shift;
143     return $bool ? 'Y' : 'N';
144 }
145
146 #
147 # boolspace: ' ' is false, 'Y' is true. (don't ask)
148 #
149 sub boolspace {
150     my $bool = shift;
151     return $bool ? 'Y' : ' ';
152 }
153
154
155 # read_SIP_packet($file)
156 #
157 # Read a packet from $file, using the correct record separator
158 #
159 sub read_SIP_packet {
160     my $record;
161     my $fh = shift or syslog("LOG_ERR", "read_SIP_packet: no filehandle argument!");
162     my $len1 = 999;
163
164     # local $/ = "\r";      # don't need any of these here.  use whatever the prevailing $/ is.
165     # local $/ = "\012";    # Internet Record Separator (lax version)
166     {    # adapted from http://perldoc.perl.org/5.8.8/functions/readline.html
167         for ( my $tries = 1 ; $tries <= 3 ; $tries++ ) {
168             undef $!;
169             $record = readline($fh);
170             if ( defined($record) ) {
171                 while ( chomp($record) ) { 1; }
172                 $len1 = length($record);
173                 syslog( "LOG_DEBUG", "read_SIP_packet, INPUT MSG: '$record'" );
174                 $record =~ s/^\s*[^A-z0-9]+//s; # Every line must start with a "real" character.  Not whitespace, control chars, etc. 
175                 $record =~ s/[^A-z0-9]+$//s;    # Same for the end.  Note this catches the problem some clients have sending empty fields at the end, like |||
176                 $record =~ s/\015?\012//g;      # Extra line breaks must die
177                 $record =~ s/\015?\012//s;      # Extra line breaks must die
178                 $record =~ s/\015*\012*$//s;    # treat as one line to include the extra linebreaks we are trying to remove!
179                 while ( chomp($record) ) { 1; }
180
181                 $record and last;    # success
182             } else {
183                 if ($!) {
184                     syslog( "LOG_DEBUG", "read_SIP_packet (try #$tries) ERROR: $! $@" );
185                     # die "read_SIP_packet ERROR: $!";
186                     warn "read_SIP_packet ERROR: $! $@";
187                 }
188             }
189         }
190     }
191     if ($record) {
192         my $len2 = length($record);
193         syslog("LOG_INFO", "read_SIP_packet, INPUT MSG: '$record'") if $record;
194         ($len1 != $len2) and syslog("LOG_DEBUG", "read_SIP_packet, trimmed %s character(s) (after chomps).", $len1-$len2);
195     } else {
196         syslog("LOG_WARNING", "read_SIP_packet input %s, end of input.", (defined($record) ? "empty ($record)" : 'undefined'));
197     }
198     #
199     # Cen-Tec self-check terminals transmit '\r\n' line terminators.
200     # This is actually very hard to deal with in perl in a reasonable
201     # since every OTHER piece of hardware out there gets the protocol
202     # right.
203     # 
204     # The incorrect line terminator presents as a \r at the end of the
205     # first record, and then a \n at the BEGINNING of the next record.
206     # So, the simplest thing to do is just throw away a leading newline
207     # on the input.
208     #  
209     # This is now handled by the vigorous cleansing above.
210     syslog("LOG_INFO", encode_utf8("INPUT MSG: '$record'")) if $record;
211     return $record;
212 }
213
214 #
215 # write_msg($msg, $file)
216 #
217 # Send $msg to the SC.  If error detection is active, then
218 # add the sequence number (if $seqno is non-zero) and checksum
219 # to the message, and save the whole thing as $last_response
220 #
221 # If $file is set, then it's a file handle: write to it, otherwise
222 # just write to the default destination.
223 #
224
225 sub write_msg {
226     my ($self, $msg, $file) = @_;
227     my $cksum;
228
229     $msg = encode_utf8($msg);
230     if ($error_detection) {
231         if (defined($self->{seqno})) {
232             $msg .= 'AY' . $self->{seqno};
233         }
234         $msg .= 'AZ';
235         $cksum = checksum($msg);
236         $msg .= sprintf('%04.4X', $cksum);
237     }
238
239
240     if ($file) {
241         print $file "$msg$CRLF";
242     } else {
243         print "$msg$CRLF";
244         syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
245     }
246
247     $last_response = $msg;
248 }
249
250 1;