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