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