Bug 15253: Add Koha::Logger based logging for SIP2
[koha-equinox.git] / C4 / SIP / Sip.pm
index b6f1916..9146ebc 100644 (file)
@@ -2,38 +2,31 @@
 # Sip.pm: General Sip utility functions
 #
 
-package Sip;
+package C4::SIP::Sip;
 
 use strict;
 use warnings;
 use Exporter;
 use Encode;
-use Sys::Syslog qw(syslog);
 use POSIX qw(strftime);
 use Socket qw(:crlf);
 use IO::Handle;
 
-use Sip::Constants qw(SIP_DATETIME);
-use Sip::Checksum qw(checksum);
+use C4::SIP::Sip::Constants qw(SIP_DATETIME FID_SCREEN_MSG);
+use C4::SIP::Sip::Checksum qw(checksum);
 
-use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
+use base qw(Exporter);
 
-BEGIN {
-    $VERSION = 3.07.00.049;
-       @ISA = qw(Exporter);
+our @EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
+    denied sipbool boolspace write_msg
+    $error_detection $protocol_version $field_delimiter
+    $last_response syslog);
 
-       @EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
-                   denied sipbool boolspace write_msg read_SIP_packet
-                   $error_detection $protocol_version $field_delimiter
-                   $last_response);
-
-       %EXPORT_TAGS = (
-                   all => [qw(y_or_n timestamp add_field maybe_add
-                              add_count denied sipbool boolspace write_msg
-                              read_SIP_packet
-                              $error_detection $protocol_version
-                              $field_delimiter $last_response)]);
-}
+our %EXPORT_TAGS = (
+    all => [qw(y_or_n timestamp add_field maybe_add
+        add_count denied sipbool boolspace write_msg
+        $error_detection $protocol_version
+        $field_delimiter $last_response syslog)]);
 
 our $error_detection = 0;
 our $protocol_version = 1;
@@ -74,7 +67,7 @@ sub add_field {
     $value=~s/\r/ /g; # CR terminates a sip message
                       # Protect against them in sip text fields
 
-    # Replace any occurences of the field delimiter in the
+    # Replace any occurrences of the field delimiter in the
     # field value with the HTML character entity
     $ent = sprintf("&#%d;", ord($field_delimiter));
 
@@ -91,7 +84,18 @@ sub add_field {
 #    NOTE: if zero is a valid value for your field, don't use maybe_add!
 #
 sub maybe_add {
-    my ($fid, $value) = @_;
+    my ($fid, $value, $server) = @_;
+
+    if ( $fid eq FID_SCREEN_MSG && $server->{account}->{screen_msg_regex} ) {
+        foreach my $regex (
+            ref $server->{account}->{screen_msg_regex} eq "ARRAY"
+            ? @{ $server->{account}->{screen_msg_regex} }
+            : $server->{account}->{screen_msg_regex} )
+        {
+            $value =~ s/$regex->{find}/$regex->{replace}/g;
+        }
+    }
+
     return (defined($value) && $value) ? add_field($fid, $value) : '';
 }
 
@@ -144,59 +148,6 @@ sub boolspace {
     return $bool ? 'Y' : ' ';
 }
 
-
-# read_SIP_packet($file)
-#
-# Read a packet from $file, using the correct record separator
-#
-sub read_SIP_packet {
-    my $record;
-    my $fh = shift or syslog("LOG_ERR", "read_SIP_packet: no filehandle argument!");
-    my $len1 = 999;
-
-    # local $/ = "\r";      # don't need any of these here.  use whatever the prevailing $/ is.
-    local $/ = "\015";    # proper SPEC: (octal) \015 = (hex) x0D = (dec) 13 = (ascii) carriage return
-    {    # adapted from http://perldoc.perl.org/5.8.8/functions/readline.html
-            undef $!;
-            $record = readline($fh);
-            if ( defined($record) ) {
-                while ( chomp($record) ) { 1; }
-                $len1 = length($record);
-                syslog( "LOG_DEBUG", "read_SIP_packet, INPUT MSG: '$record'" );
-                $record =~ s/^\s*[^A-z0-9]+//s; # Every line must start with a "real" character.  Not whitespace, control chars, etc. 
-                $record =~ s/[^A-z0-9]+$//s;    # Same for the end.  Note this catches the problem some clients have sending empty fields at the end, like |||
-                $record =~ s/\015?\012//g;      # Extra line breaks must die
-                $record =~ s/\015?\012//s;      # Extra line breaks must die
-                $record =~ s/\015*\012*$//s;    # treat as one line to include the extra linebreaks we are trying to remove!
-                while ( chomp($record) ) { 1; }
-
-                $record and last;    # success
-            }
-    }
-    if ($record) {
-        my $len2 = length($record);
-        syslog("LOG_INFO", "read_SIP_packet, INPUT MSG: '$record'") if $record;
-        ($len1 != $len2) and syslog("LOG_DEBUG", "read_SIP_packet, trimmed %s character(s) (after chomps).", $len1-$len2);
-    } else {
-        syslog("LOG_WARNING", "read_SIP_packet input %s, end of input.", (defined($record) ? "empty ($record)" : 'undefined'));
-    }
-    #
-    # Cen-Tec self-check terminals transmit '\r\n' line terminators.
-    # This is actually very hard to deal with in perl in a reasonable
-    # since every OTHER piece of hardware out there gets the protocol
-    # right.
-    # 
-    # The incorrect line terminator presents as a \r at the end of the
-    # first record, and then a \n at the BEGINNING of the next record.
-    # So, the simplest thing to do is just throw away a leading newline
-    # on the input.
-    #  
-    # This is now handled by the vigorous cleansing above.
-    # syslog("LOG_INFO", encode_utf8("INPUT MSG: '$record'")) if $record;
-    syslog("LOG_INFO", "INPUT MSG: '$record'") if $record;
-    return $record;
-}
-
 #
 # write_msg($msg, $file)
 #
@@ -241,4 +192,19 @@ sub write_msg {
     $last_response = $msg;
 }
 
+sub syslog {
+    my ( $level, $mask, @args ) = @_;
+
+    my $method =
+        $level eq 'LOG_ERR'     ? 'error'
+      : $level eq 'LOG_DEBUG'   ? 'debug'
+      : $level eq 'LOG_INFO'    ? 'info'
+      : $level eq 'LOG_WARNING' ? 'warn'
+      :                           'error';
+
+    my $message = @args ? sprintf($mask, @args) : $mask;
+
+    C4::SIP::SIPServer::get_logger()->$method($message);
+}
+
 1;