fix the private shelf deletion in opac
[koha.git] / C4 / SIP / Sip.pm
blob558a1d40008da018beef73153765b8f9fb2b6a93
2 # Sip.pm: General Sip utility functions
5 package Sip;
7 use strict;
8 use warnings;
9 use English;
10 use Exporter;
12 use Sys::Syslog qw(syslog);
13 use POSIX qw(strftime);
14 use Socket qw(:crlf);
16 use Sip::Constants qw(SIP_DATETIME);
17 use Sip::Checksum qw(checksum);
19 use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
21 BEGIN {
22 $VERSION = 1.00;
23 @ISA = qw(Exporter);
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
28 $last_response);
30 %EXPORT_TAGS = (
31 all => [qw(y_or_n timestamp add_field maybe_add
32 add_count denied sipbool boolspace write_msg
33 read_SIP_packet
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 = '';
50 sub timestamp {
51 my $time = $_[0] || time();
52 return strftime(SIP_DATETIME, localtime($time));
56 # add_field(field_id, value)
57 # return constructed field value
59 sub add_field {
60 my ($field_id, $value) = @_;
61 my ($i, $ent);
63 if (!defined($value)) {
64 syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
65 $field_id);
66 $value = '';
69 # Replace any occurences of the field delimiter in the
70 # field value with the HTML character entity
71 $ent = sprintf("&#%d;", ord($field_delimiter));
73 while (($i = index($value, $field_delimiter)) != ($[-1)) {
74 substr($value, $i, 1) = $ent;
77 return $field_id . $value . $field_delimiter;
80 # maybe_add(field_id, value):
81 # If value is defined and non-empty, then return the
82 # constructed field value, otherwise return the empty string
84 sub maybe_add {
85 my ($fid, $value) = @_;
86 return (defined($value) && $value) ? add_field($fid, $value) : '';
90 # add_count() produce fixed four-character count field,
91 # or a string of four spaces if the count is invalid for some
92 # reason
94 sub add_count {
95 my ($label, $count) = @_;
97 # If the field is unsupported, it will be undef, return blanks
98 # as per the spec.
99 if (!defined($count)) {
100 return ' ' x 4;
103 $count = sprintf("%04d", $count);
104 if (length($count) != 4) {
105 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
106 $label, $count);
107 $count = ' ' x 4;
109 return $count;
113 # denied($bool)
114 # if $bool is false, return true. This is because SIP statuses
115 # are inverted: we report that something has been denied, not that
116 # it's permitted. For example, 'renewal priv. denied' of 'Y' means
117 # that the user's not permitted to renew. I assume that the ILS has
118 # real positive tests.
120 sub denied {
121 my $bool = shift;
122 return boolspace(!$bool);
125 sub sipbool {
126 my $bool = shift;
127 return $bool ? 'Y' : 'N';
131 # boolspace: ' ' is false, 'Y' is true. (don't ask)
133 sub boolspace {
134 my $bool = shift;
135 return $bool ? 'Y' : ' ';
139 # Read a packet from $file, using the correct record separator
141 sub read_SIP_packet {
142 my $record;
143 my $fh = shift or syslog("LOG_ERR", "read_SIP_packet: no filehandle argument!");
144 my $len1 = 999;
145 # local $/ = "\012"; # Internet Record Separator (lax version)
146 { # adapted from http://perldoc.perl.org/5.8.8/functions/readline.html
147 for (my $tries=1; $tries<=3; $tries++) {
148 undef $!;
149 $record = readline($fh);
150 if (defined($record)) {
151 while(chomp($record)){1;}
152 $len1 = length($record);
153 syslog("LOG_DEBUG", "read_SIP_packet, INPUT MSG: '$record'");
154 $record =~ s/^\s*[^A-z0-9]+//s;
155 $record =~ s/[^A-z0-9]+$//s;
156 $record =~ s/\015?\012//g;
157 $record =~ s/\015?\012//s;
158 $record =~ s/\015*\012*$//s; # treat as one line to include the extra linebreaks we are trying to remove!
159 while(chomp($record)){1;}
160 if ($record) {
161 last; # success
163 } else {
164 if ($!) {
165 syslog("LOG_DEBUG", "read_SIP_packet (try #$tries) ERROR: $!");
166 # die "read_SIP_packet ERROR: $!";
167 warn "read_SIP_packet ERROR: $!";
172 if ($record) {
173 my $len2 = length($record);
174 syslog("LOG_INFO", "read_SIP_packet, INPUT MSG: '$record'") if $record;
175 ($len1 != $len2) and syslog("LOG_DEBUG", "read_SIP_packet, trimmed %s character(s) (after chomps).", $len1-$len2);
176 } else {
177 syslog("LOG_WARNING", "read_SIP_packet input %s, end of input.", (defined($record)? "empty ($record)" : 'undefined'));
179 return $record;
183 # write_msg($msg, $file)
185 # Send $msg to the SC. If error detection is active, then
186 # add the sequence number (if $seqno is non-zero) and checksum
187 # to the message, and save the whole thing as $last_response
189 # If $file is set, then it's a file handle: write to it, otherwise
190 # just write to the default destination.
193 sub write_msg {
194 my ($self, $msg, $file) = @_;
195 my $cksum;
197 if ($error_detection) {
198 if (defined($self->{seqno})) {
199 $msg .= 'AY' . $self->{seqno};
201 $msg .= 'AZ';
202 $cksum = checksum($msg);
203 $msg .= sprintf('%04.4X', $cksum);
206 if ($file) {
207 print $file "$msg$CRLF";
208 syslog("LOG_DEBUG", "write_msg outputting to $file");
209 } else {
210 print "$msg$CRLF";
212 syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
214 $last_response = $msg;