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