2 # Sip.pm: General Sip utility functions
11 use Sys::Syslog qw(syslog);
12 use POSIX qw(strftime);
16 use C4::SIP::Sip::Constants qw(SIP_DATETIME FID_SCREEN_MSG);
17 use C4::SIP::Sip::Checksum qw(checksum);
19 use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
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
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)]);
36 our $error_detection = 0;
37 our $protocol_version = 1;
38 our $field_delimiter = '|'; # Protocol Default
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)
46 our $last_response = '';
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);
56 return strftime(SIP_DATETIME, localtime($time));
60 # add_field(field_id, value)
61 # return constructed field value
64 my ($field_id, $value) = @_;
67 if (!defined($value)) {
68 syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
72 $value=~s/\r/ /g; # CR terminates a sip message
73 # Protect against them in sip text fields
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));
79 while (($i = index($value, $field_delimiter)) != ($[-1)) {
80 substr($value, $i, 1) = $ent;
83 return $field_id . $value . $field_delimiter;
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!
92 my ($fid, $value, $server) = @_;
94 if ( $fid eq FID_SCREEN_MSG && $server->{account}->{screen_msg_regex} ) {
96 ref $server->{account}->{screen_msg_regex} eq "ARRAY"
97 ? @{ $server->{account}->{screen_msg_regex} }
98 : $server->{account}->{screen_msg_regex} )
100 $value =~ s/$regex->{find}/$regex->{replace}/g;
104 return (defined($value) && $value) ? add_field($fid, $value) : '';
108 # add_count() produce fixed four-character count field,
109 # or a string of four spaces if the count is invalid for some
113 my ($label, $count) = @_;
115 # If the field is unsupported, it will be undef, return blanks
117 if (!defined($count)) {
121 $count = sprintf("%04d", $count);
122 if (length($count) != 4) {
123 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
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.
140 return boolspace(!$bool);
145 return $bool ? 'Y' : 'N';
149 # boolspace: ' ' is false, 'Y' is true. (don't ask)
153 return $bool ? 'Y' : ' ';
157 # write_msg($msg, $file)
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
163 # If $file is set, then it's a file handle: write to it, otherwise
164 # just write to the default destination.
168 my ($self, $msg, $file, $terminator, $encoding) = @_;
171 $terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
173 $msg = encode($encoding, $msg) if ( $encoding );
177 # $msg = encode_utf8($msg);
178 if ($error_detection) {
179 if (defined($self->{seqno})) {
180 $msg .= 'AY' . $self->{seqno};
183 $cksum = checksum($msg);
184 $msg .= sprintf('%04.4X', $cksum);
190 print $file $msg, $terminator;
192 STDOUT->autoflush(1);
193 print $msg, $terminator;
194 syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
197 $last_response = $msg;