Add debugging around readline.
[koha.git] / C4 / SIP / Sip.pm
1 #
2 # Sip.pm: General Sip utility functions
3 #
4
5 package Sip;
6
7 use strict;
8 use warnings;
9 use English;
10 use Exporter;
11
12 use Sys::Syslog qw(syslog);
13 use POSIX qw(strftime);
14
15 use Sip::Constants qw(SIP_DATETIME);
16 use Sip::Checksum qw(checksum);
17
18 use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
19
20 BEGIN {
21         $VERSION = 1.00;
22         @ISA = qw(Exporter);
23
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
27                     $last_response);
28
29         %EXPORT_TAGS = (
30                     all => [qw(y_or_n timestamp add_field maybe_add
31                                add_count denied sipbool boolspace write_msg
32                                read_SIP_packet
33                                $error_detection $protocol_version
34                                $field_delimiter $last_response)]);
35 }
36
37 our $error_detection = 0;
38 our $protocol_version = 1;
39 our $field_delimiter = '|';     # Protocol Default
40
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)
46
47 our $last_response = '';
48
49 sub timestamp {
50     my $time = $_[0] || time();
51     return strftime(SIP_DATETIME, localtime($time));
52 }
53
54 #
55 # add_field(field_id, value)
56 #    return constructed field value
57 #
58 sub add_field {
59     my ($field_id, $value) = @_;
60     my ($i, $ent);
61
62     if (!defined($value)) {
63         syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
64                $field_id);
65                 $value = '';
66     }
67
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));
71
72     while (($i = index($value, $field_delimiter)) != ($[-1)) {
73                 substr($value, $i, 1) = $ent;
74     }
75
76     return $field_id . $value . $field_delimiter;
77 }
78 #
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
82 #
83 sub maybe_add {
84     my ($fid, $value) = @_;
85     return (defined($value) && $value) ? add_field($fid, $value) : '';
86 }
87
88 #
89 # add_count()  produce fixed four-character count field,
90 # or a string of four spaces if the count is invalid for some
91 # reason
92 #
93 sub add_count {
94     my ($label, $count) = @_;
95
96     # If the field is unsupported, it will be undef, return blanks
97     # as per the spec.
98     if (!defined($count)) {
99                 return ' ' x 4;
100     }
101
102     $count = sprintf("%04d", $count);
103     if (length($count) != 4) {
104                 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
105                $label, $count);
106                 $count = ' ' x 4;
107     }
108     return $count;
109 }
110
111 #
112 # denied($bool)
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.
118 #
119 sub denied {
120     my $bool = shift;
121     return boolspace(!$bool);
122 }
123
124 sub sipbool {
125     my $bool = shift;
126     return $bool ? 'Y' : 'N';
127 }
128
129 #
130 # boolspace: ' ' is false, 'Y' is true. (don't ask)
131 #
132 sub boolspace {
133     my $bool = shift;
134     return $bool ? 'Y' : ' ';
135 }
136
137
138 # read_SIP_packet($file)
139 #
140 # Read a packet from $file, using the correct record separator
141 #
142 sub read_SIP_packet {
143     my $record;
144         {               # adapted from http://perldoc.perl.org/5.8.8/functions/readline.html
145                 undef $!;
146         local $/ = "\r";
147                 unless (defined($record = readline(shift))) {
148                         if ($!) {
149                         syslog("LOG_ERR", "read_SIP_packet ERROR: $!");
150                                 die "read_SIP_packet ERROR: $!";
151                         }
152                         # else reached EOF
153                 }
154         }
155     syslog("LOG_INFO", "read_SIP_packet, INPUT MSG: '$record'") if $record;
156     return $record;
157 }
158
159 #
160 # write_msg($msg, $file)
161 #
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
165 #
166 # If $file is set, then it's a file handle: write to it, otherwise
167 # just write to the default destination.
168 #
169
170 sub write_msg {
171     my ($self, $msg, $file) = @_;
172     my $cksum;
173
174     if ($error_detection) {
175                 if (defined($self->{seqno})) {
176                     $msg .= 'AY' . $self->{seqno};
177                 }
178                 $msg .= 'AZ';
179                 $cksum = checksum($msg);
180                 $msg .= sprintf('%04.4X', $cksum);
181     }
182
183
184     if ($file) {
185                 print $file "$msg\r";
186     } else {
187                 print "$msg\r";
188                 syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
189     }
190
191     $last_response = $msg;
192 }
193
194 1;