Bug 2094: cleanup of lost items report
[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
52     return strftime(SIP_DATETIME, localtime($time));
53 }
54
55 #
56 # add_field(field_id, value)
57 #    return constructed field value
58 #
59 sub add_field {
60     my ($field_id, $value) = @_;
61     my ($i, $ent);
62
63     if (!defined($value)) {
64         syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
65                $field_id);
66         $value = '';
67     }
68
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));
72
73     while (($i = index($value, $field_delimiter)) != ($[-1)) {
74         substr($value, $i, 1) = $ent;
75     }
76
77     return $field_id . $value . $field_delimiter;
78 }
79 #
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
83 #
84 sub maybe_add {
85     my ($fid, $value) = @_;
86
87     return (defined($value) && $value) ? add_field($fid, $value) : '';
88 }
89
90 #
91 # add_count()  produce fixed four-character count field,
92 # or a string of four spaces if the count is invalid for some
93 # reason
94 #
95 sub add_count {
96     my ($label, $count) = @_;
97
98     # If the field is unsupported, it will be undef, return blanks
99     # as per the spec.
100     if (!defined($count)) {
101         return ' ' x 4;
102     }
103
104     $count = sprintf("%04d", $count);
105     if (length($count) != 4) {
106         syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
107                $label, $count);
108         $count = ' ' x 4;
109     }
110     return $count;
111 }
112
113 #
114 # denied($bool)
115 # if $bool is false, return true.  This is because SIP statuses
116 # are inverted:  we report that something has been denied, not that
117 # it's permitted.  For example, 'renewal priv. denied' of 'Y' means
118 # that the user's not permitted to renew.  I assume that the ILS has
119 # real positive tests.
120 #
121 sub denied {
122     my $bool = shift;
123
124     return boolspace(!$bool);
125 }
126
127 sub sipbool {
128     my $bool = shift;
129
130     return $bool ? 'Y' : 'N';
131 }
132
133 #
134 # boolspace: ' ' is false, 'Y' is true. (don't ask)
135 #
136 sub boolspace {
137     my $bool = shift;
138
139     return $bool ? 'Y' : ' ';
140 }
141
142
143 # read_SIP_packet($file)
144 #
145 # Read a packet from $file, using the correct record separator
146 #
147 sub read_SIP_packet {
148     my $file = shift;
149     my $record;
150     local $/ = "\r";
151
152     $record = readline($file);
153     syslog("LOG_INFO", "INPUT MSG: '$record'") if $record;
154     return $record;
155 }
156
157 #
158 # write_msg($msg, $file)
159 #
160 # Send $msg to the SC.  If error detection is active, then
161 # add the sequence number (if $seqno is non-zero) and checksum
162 # to the message, and save the whole thing as $last_response
163 #
164 # If $file is set, then it's a file handle: write to it, otherwise
165 # just write to the default destination.
166 #
167
168 sub write_msg {
169     my ($self, $msg, $file) = @_;
170     my $cksum;
171
172     if ($error_detection) {
173         if (defined($self->{seqno})) {
174             $msg .= 'AY' . $self->{seqno};
175         }
176         $msg .= 'AZ';
177         $cksum = checksum($msg);
178         $msg .= sprintf('%04.4X', $cksum);
179     }
180
181
182     if ($file) {
183         print $file "$msg\r";
184     } else {
185         print "$msg\r";
186         syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
187     }
188
189     $last_response = $msg;
190 }
191
192 1;