Bug 18578: Use subdirectory in /tmp for session storage during installation
[koha.git] / C4 / SIP / Sip.pm
blob3f41226352df9f5e680fb52dd92a460eb5aa4cce
2 # Sip.pm: General Sip utility functions
5 package C4::SIP::Sip;
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;
16 use C4::SIP::Sip::Constants qw(SIP_DATETIME FID_SCREEN_MSG);
17 use C4::SIP::Sip::Checksum qw(checksum);
19 use base qw(Exporter);
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);
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)]);
32 our $error_detection = 0;
33 our $protocol_version = 1;
34 our $field_delimiter = '|'; # Protocol Default
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)
42 our $last_response = '';
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);
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 = '';
68 $value=~s/\r/ /g; # CR terminates a sip message
69 # Protect against them in sip text fields
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));
75 while (($i = index($value, $field_delimiter)) != ($[-1)) {
76 substr($value, $i, 1) = $ent;
79 return $field_id . $value . $field_delimiter;
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!
87 sub maybe_add {
88 my ($fid, $value, $server) = @_;
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} )
96 $value =~ s/$regex->{find}/$regex->{replace}/g;
100 return (defined($value) && $value) ? add_field($fid, $value) : '';
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
108 sub add_count {
109 my ($label, $count) = @_;
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;
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;
123 return $count;
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.
134 sub denied {
135 my $bool = shift;
136 return boolspace(!$bool);
139 sub sipbool {
140 my $bool = shift;
141 return $bool ? 'Y' : 'N';
145 # boolspace: ' ' is false, 'Y' is true. (don't ask)
147 sub boolspace {
148 my $bool = shift;
149 return $bool ? 'Y' : ' ';
153 # write_msg($msg, $file)
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
159 # If $file is set, then it's a file handle: write to it, otherwise
160 # just write to the default destination.
163 sub write_msg {
164 my ($self, $msg, $file, $terminator, $encoding) = @_;
166 $terminator ||= q{};
167 $terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
169 $msg = encode($encoding, $msg) if ( $encoding );
171 my $cksum;
173 # $msg = encode_utf8($msg);
174 if ($error_detection) {
175 if (defined($self->{seqno})) {
176 $msg .= 'AY' . $self->{seqno};
178 $msg .= 'AZ';
179 $cksum = checksum($msg);
180 $msg .= sprintf('%04.4X', $cksum);
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'");
193 $last_response = $msg;