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 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
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 = '';
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) = @_;
63 if (!defined($value)) {
64 syslog("LOG_DEBUG", "add_field: Undefined value being added to '%s'",
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!
88 my ($fid, $value, $server) = @_;
90 if ( $fid eq FID_SCREEN_MSG && $server->{account}->{screen_msg_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
109 my ($label, $count) = @_;
111 # If the field is unsupported, it will be undef, return blanks
113 if (!defined($count)) {
117 $count = sprintf("%04d", $count);
118 if (length($count) != 4) {
119 syslog("LOG_WARNING", "handle_patron_info: %s wrong size: '%s'",
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.
136 return boolspace(!$bool);
141 return $bool ? 'Y' : 'N';
145 # boolspace: ' ' is false, 'Y' is true. (don't ask)
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.
164 my ($self, $msg, $file, $terminator, $encoding) = @_;
167 $terminator = ( $terminator eq 'CR' ) ?
$CR : $CRLF;
169 $msg = encode
($encoding, $msg) if ( $encoding );
173 # $msg = encode_utf8($msg);
174 if ($error_detection) {
175 if (defined($self->{seqno
})) {
176 $msg .= 'AY' . $self->{seqno
};
179 $cksum = checksum
($msg);
180 $msg .= sprintf('%04.4X', $cksum);
186 print $file $msg, $terminator;
188 STDOUT
->autoflush(1);
189 print $msg, $terminator;
190 syslog
("LOG_INFO", "OUTPUT MSG: '$msg'");
193 $last_response = $msg;