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);
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 syslog);
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 syslog)]);
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 = '';
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
60 my ($field_id, $value, $server) = @_;
62 if ( my $hide_fields = $server->{account}->{hide_fields} ) {
63 my @fields = split( ',', $hide_fields );
64 return q{} if first
{ $_ eq $field_id } @fields;
69 if (!defined($value)) {
70 syslog
("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
74 $value=~s/\r/ /g; # CR terminates a sip message
75 # Protect against them in sip text fields
77 # Replace any occurrences of the field delimiter in the
78 # field value with the HTML character entity
79 $ent = sprintf("&#%d;", ord($field_delimiter));
81 while (($i = index($value, $field_delimiter)) != ($[-1)) {
82 substr($value, $i, 1) = $ent;
85 return $field_id . $value . $field_delimiter;
88 # maybe_add(field_id, value):
89 # If value is defined and non-empty, then return the
90 # constructed field value, otherwise return the empty string.
91 # NOTE: if zero is a valid value for your field, don't use maybe_add!
94 my ($fid, $value, $server) = @_;
96 if ( my $hide_fields = $server->{account
}->{hide_fields
} ) {
97 my @fields = split( ',', $hide_fields );
98 return q{} if first
{ $_ eq $fid } @fields;
101 if ( $fid eq FID_SCREEN_MSG
&& $server->{account
}->{screen_msg_regex
} ) {
103 ref $server->{account
}->{screen_msg_regex
} eq "ARRAY"
104 ? @
{ $server->{account
}->{screen_msg_regex
} }
105 : $server->{account
}->{screen_msg_regex
} )
107 $value =~ s/$regex->{find}/$regex->{replace}/g;
110 return (defined($value) && $value) ? add_field
($fid, $value) : '';
114 # add_count() produce fixed four-character count field,
115 # or a string of four spaces if the count is invalid for some
119 my ($label, $count) = @_;
121 # If the field is unsupported, it will be undef, return blanks
123 if (!defined($count)) {
127 $count = sprintf("%04d", $count);
128 if (length($count) != 4) {
129 syslog
("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
138 # if $bool is false, return true. This is because SIP statuses
139 # are inverted: we report that something has been denied, not that
140 # it's permitted. For example, 'renewal priv. denied' of 'Y' means
141 # that the user's not permitted to renew. I assume that the ILS has
142 # real positive tests.
146 return boolspace
(!$bool);
151 return $bool ?
'Y' : 'N';
155 # boolspace: ' ' is false, 'Y' is true. (don't ask)
159 return $bool ?
'Y' : ' ';
163 # write_msg($msg, $file)
165 # Send $msg to the SC. If error detection is active, then
166 # add the sequence number (if $seqno is non-zero) and checksum
167 # to the message, and save the whole thing as $last_response
169 # If $file is set, then it's a file handle: write to it, otherwise
170 # just write to the default destination.
174 my ($self, $msg, $file, $terminator, $encoding) = @_;
177 $terminator = ( $terminator eq 'CR' ) ?
$CR : $CRLF;
179 $msg = encode
($encoding, $msg) if ( $encoding );
183 # $msg = encode_utf8($msg);
184 if ($error_detection) {
185 if (defined($self->{seqno
})) {
186 $msg .= 'AY' . $self->{seqno
};
189 $cksum = checksum
($msg);
190 $msg .= sprintf('%04.4X', $cksum);
196 print $file $msg, $terminator;
198 STDOUT
->autoflush(1);
199 print $msg, $terminator;
200 syslog
("LOG_INFO", "OUTPUT MSG: '$msg'");
203 $last_response = $msg;
207 my ( $level, $mask, @args ) = @_;
210 $level eq 'LOG_ERR' ?
'error'
211 : $level eq 'LOG_DEBUG' ?
'debug'
212 : $level eq 'LOG_INFO' ?
'info'
213 : $level eq 'LOG_WARNING' ?
'warn'
216 my $message = @args ?
sprintf($mask, @args) : $mask;
218 C4
::SIP
::SIPServer
::get_logger
()->$method($message);