Bug 20292: Filter/censor info sent via SIP
[koha-equinox.git] / C4 / SIP / Sip.pm
1 #
2 # Sip.pm: General Sip utility functions
3 #
4
5 package C4::SIP::Sip;
6
7 use strict;
8 use warnings;
9 use Exporter;
10 use Encode;
11 use Sys::Syslog qw(syslog);
12 use POSIX qw(strftime);
13 use Socket qw(:crlf);
14 use IO::Handle;
15 use List::Util qw(first);
16
17 use C4::SIP::Sip::Constants qw(SIP_DATETIME FID_SCREEN_MSG);
18 use C4::SIP::Sip::Checksum qw(checksum);
19
20 use base qw(Exporter);
21
22 our @EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
23     denied sipbool boolspace write_msg
24     $error_detection $protocol_version $field_delimiter
25     $last_response);
26
27 our %EXPORT_TAGS = (
28     all => [qw(y_or_n timestamp add_field maybe_add
29         add_count denied sipbool boolspace write_msg
30         $error_detection $protocol_version
31         $field_delimiter $last_response)]);
32
33 our $error_detection = 0;
34 our $protocol_version = 1;
35 our $field_delimiter = '|'; # Protocol Default
36
37 # We need to keep a copy of the last message we sent to the SC,
38 # in case there's a transmission error and the SC sends us a
39 # REQUEST_ACS_RESEND.  If we receive a REQUEST_ACS_RESEND before
40 # we've ever sent anything, then we are to respond with a
41 # REQUEST_SC_RESEND (p.16)
42
43 our $last_response = '';
44
45 sub timestamp {
46     my $time = $_[0] || time();
47     if ( ref $time eq 'DateTime') {
48         return $time->strftime(SIP_DATETIME);
49     } elsif ($time=~m/^(\d{4})\-(\d{2})\-(\d{2})/) {
50         # passing a db returned date as is + bogus time
51         return sprintf( '%04d%02d%02d    235900', $1, $2, $3);
52     }
53     return strftime(SIP_DATETIME, localtime($time));
54 }
55
56 #
57 # add_field(field_id, value)
58 #    return constructed field value
59 #
60 sub add_field {
61     my ($field_id, $value, $server) = @_;
62
63     if ( my $hide_fields = $server->{account}->{hide_fields} ) {
64         my @fields = split( ',', $hide_fields );
65         return q{} if first { $_ eq $field_id } @fields;
66     }
67
68     my ($i, $ent);
69
70     if (!defined($value)) {
71         syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
72                $field_id);
73                 $value = '';
74     }
75     $value=~s/\r/ /g; # CR terminates a sip message
76                       # Protect against them in sip text fields
77
78     # Replace any occurrences of the field delimiter in the
79     # field value with the HTML character entity
80     $ent = sprintf("&#%d;", ord($field_delimiter));
81
82     while (($i = index($value, $field_delimiter)) != ($[-1)) {
83                 substr($value, $i, 1) = $ent;
84     }
85
86     return $field_id . $value . $field_delimiter;
87 }
88 #
89 # maybe_add(field_id, value):
90 #    If value is defined and non-empty, then return the
91 #    constructed field value, otherwise return the empty string.
92 #    NOTE: if zero is a valid value for your field, don't use maybe_add!
93 #
94 sub maybe_add {
95     my ($fid, $value, $server) = @_;
96
97     if ( my $hide_fields = $server->{account}->{hide_fields} ) {
98         my @fields = split( ',', $hide_fields );
99         return q{} if first { $_ eq $fid } @fields;
100     }
101
102     if ( $fid eq FID_SCREEN_MSG && $server->{account}->{screen_msg_regex} ) {
103         foreach my $regex (
104             ref $server->{account}->{screen_msg_regex} eq "ARRAY"
105             ? @{ $server->{account}->{screen_msg_regex} }
106             : $server->{account}->{screen_msg_regex} )
107         {
108             $value =~ s/$regex->{find}/$regex->{replace}/g;
109         }
110     }
111
112     return (defined($value) && $value) ? add_field($fid, $value) : '';
113 }
114
115 #
116 # add_count()  produce fixed four-character count field,
117 # or a string of four spaces if the count is invalid for some
118 # reason
119 #
120 sub add_count {
121     my ($label, $count) = @_;
122
123     # If the field is unsupported, it will be undef, return blanks
124     # as per the spec.
125     if (!defined($count)) {
126                 return ' ' x 4;
127     }
128
129     $count = sprintf("%04d", $count);
130     if (length($count) != 4) {
131                 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
132                $label, $count);
133                 $count = ' ' x 4;
134     }
135     return $count;
136 }
137
138 #
139 # denied($bool)
140 # if $bool is false, return true.  This is because SIP statuses
141 # are inverted:  we report that something has been denied, not that
142 # it's permitted.  For example, 'renewal priv. denied' of 'Y' means
143 # that the user's not permitted to renew.  I assume that the ILS has
144 # real positive tests.
145 #
146 sub denied {
147     my $bool = shift;
148     return boolspace(!$bool);
149 }
150
151 sub sipbool {
152     my $bool = shift;
153     return $bool ? 'Y' : 'N';
154 }
155
156 #
157 # boolspace: ' ' is false, 'Y' is true. (don't ask)
158 #
159 sub boolspace {
160     my $bool = shift;
161     return $bool ? 'Y' : ' ';
162 }
163
164 #
165 # write_msg($msg, $file)
166 #
167 # Send $msg to the SC.  If error detection is active, then
168 # add the sequence number (if $seqno is non-zero) and checksum
169 # to the message, and save the whole thing as $last_response
170 #
171 # If $file is set, then it's a file handle: write to it, otherwise
172 # just write to the default destination.
173 #
174
175 sub write_msg {
176     my ($self, $msg, $file, $terminator, $encoding) = @_;
177
178     $terminator ||= q{};
179     $terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
180
181     $msg = encode($encoding, $msg) if ( $encoding );
182
183     my $cksum;
184
185     # $msg = encode_utf8($msg);
186     if ($error_detection) {
187         if (defined($self->{seqno})) {
188             $msg .= 'AY' . $self->{seqno};
189         }
190         $msg .= 'AZ';
191         $cksum = checksum($msg);
192         $msg .= sprintf('%04.4X', $cksum);
193     }
194
195
196     if ($file) {
197         $file->autoflush(1);
198         print $file $msg, $terminator;
199     } else {
200         STDOUT->autoflush(1);
201         print $msg, $terminator;
202         syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
203     }
204
205     $last_response = $msg;
206 }
207
208 1;