2 # Sip.pm: General Sip utility functions
11 use POSIX qw(strftime);
14 use List::Util qw(first);
16 use C4::SIP::Sip::Constants qw(SIP_DATETIME FID_SCREEN_MSG);
17 use C4::SIP::Sip::Checksum qw(checksum);
18 use C4::SIP::Logger qw( get_logger );
20 use base qw(Exporter);
22 our @EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
23 denied sipbool boolspace write_msg
24 $error_detection $protocol_version $field_delimiter
25 $last_response siplog);
28 all => [qw(y_or_n timestamp add_field maybe_add
29 add_count denied sipbool boolspace write_msg
30 $error_detection $protocol_version
31 $field_delimiter $last_response siplog)]);
33 our $error_detection = 0;
34 our $protocol_version = 1;
35 our $field_delimiter = '|'; # Protocol Default
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)
43 our $last_response = '';
46 my $time = $_[0] || time();
47 if ( ref $time eq 'DateTime') {
48 return $time->strftime(SIP_DATETIME);
49 } elsif ($time=~m/^(\d{4})\-(\d{2})\-(\d{2})/) {
50 # passing a db returned date as is + bogus time
51 return sprintf( '%04d%02d%02d 235900', $1, $2, $3);
53 return strftime(SIP_DATETIME, localtime($time));
57 # add_field(field_id, value)
58 # return constructed field value
61 my ($field_id, $value, $server) = @_;
63 if ( my $hide_fields = $server->{account}->{hide_fields} ) {
64 my @fields = split( ',', $hide_fields );
65 return q{} if first { $_ eq $field_id } @fields;
70 if (!defined($value)) {
71 siplog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
75 $value=~s/\r/ /g; # CR terminates a sip message
76 # Protect against them in sip text fields
78 # Replace any occurrences of the field delimiter in the
79 # field value with the HTML character entity
80 $ent = sprintf("&#%d;", ord($field_delimiter));
82 while (($i = index($value, $field_delimiter)) != ($[-1)) {
83 substr($value, $i, 1) = $ent;
86 return $field_id . $value . $field_delimiter;
89 # maybe_add(field_id, value):
90 # If value is defined and non-empty, then return the
91 # constructed field value, otherwise return the empty string.
92 # NOTE: if zero is a valid value for your field, don't use maybe_add!
95 my ($fid, $value, $server) = @_;
97 if ( my $hide_fields = $server->{account}->{hide_fields} ) {
98 my @fields = split( ',', $hide_fields );
99 return q{} if first { $_ eq $fid } @fields;
102 if ( $fid eq FID_SCREEN_MSG && $server->{account}->{screen_msg_regex} && defined($value)) {
104 ref $server->{account}->{screen_msg_regex} eq "ARRAY"
105 ? @{ $server->{account}->{screen_msg_regex} }
106 : $server->{account}->{screen_msg_regex} )
108 $value =~ s/$regex->{find}/$regex->{replace}/g;
112 return ( defined($value) && length($value) )
113 ? add_field( $fid, $value )
118 # add_count() produce fixed four-character count field,
119 # or a string of four spaces if the count is invalid for some
123 my ($label, $count) = @_;
125 # If the field is unsupported, it will be undef, return blanks
127 if (!defined($count)) {
131 $count = sprintf("%04d", $count);
132 if (length($count) != 4) {
133 siplog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
142 # if $bool is false, return true. This is because SIP statuses
143 # are inverted: we report that something has been denied, not that
144 # it's permitted. For example, 'renewal priv. denied' of 'Y' means
145 # that the user's not permitted to renew. I assume that the ILS has
146 # real positive tests.
150 return boolspace(!$bool);
155 return $bool ? 'Y' : 'N';
159 # boolspace: ' ' is false, 'Y' is true. (don't ask)
163 return $bool ? 'Y' : ' ';
167 # write_msg($msg, $file)
169 # Send $msg to the SC. If error detection is active, then
170 # add the sequence number (if $seqno is non-zero) and checksum
171 # to the message, and save the whole thing as $last_response
173 # If $file is set, then it's a file handle: write to it, otherwise
174 # just write to the default destination.
178 my ($self, $msg, $file, $terminator, $encoding) = @_;
181 $terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
183 $msg = encode($encoding, $msg) if ( $encoding );
187 # $msg = encode_utf8($msg);
188 if ($error_detection) {
189 if (defined($self->{seqno})) {
190 $msg .= 'AY' . $self->{seqno};
193 $cksum = checksum($msg);
194 $msg .= sprintf('%04.4X', $cksum);
200 print $file $msg, $terminator;
202 STDOUT->autoflush(1);
203 print $msg, $terminator;
204 siplog("LOG_INFO", "OUTPUT MSG: '$msg'");
207 $last_response = $msg;
211 my ( $level, $mask, @args ) = @_;
214 $level eq 'LOG_ERR' ? 'error'
215 : $level eq 'LOG_DEBUG' ? 'debug'
216 : $level eq 'LOG_INFO' ? 'info'
217 : $level eq 'LOG_WARNING' ? 'warn'
220 my $message = @args ? sprintf($mask, @args) : $mask;
222 my $logger = C4::SIP::Logger::get_logger();
223 $logger->$method($message) if $logger;