View | Details | Raw Unified | Return to bug 13159
Collapse All | Expand All

(-)a/C4/SIP/sip_cli_emulator.pl (+350 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright (C) 2012-2013 ByWater Solutions
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Socket qw(:crlf);
23
use IO::Socket::INET;
24
use Getopt::Long;
25
26
use Sip::Constants qw(:all);
27
28
use constant { LANGUAGE => '001' };
29
30
my $help = 0;
31
32
my $host;
33
my $port = '6001';
34
35
my $login_user_id;
36
my $login_password;
37
my $location_code;
38
39
my $patron_identifier;
40
my $patron_password;
41
42
my $item_identifier;
43
44
my $terminator = q{};
45
46
my @messages;
47
48
GetOptions(
49
    "a|address|host|hostaddress=s" => \$host,              # sip server ip
50
    "p|port=s"                     => \$port,              # sip server port
51
    "su|sip_user=s"                => \$login_user_id,     # sip user
52
    "sp|sip_pass=s"                => \$login_password,    # sip password
53
    "l|location|location_code=s"   => \$location_code,     # sip location code
54
55
    "patron=s"   => \$patron_identifier,    # patron cardnumber or login
56
    "password=s" => \$patron_password,      # patron's password
57
58
    "i|item=s" => \$item_identifier,
59
60
    "t|terminator=s" => \$terminator,
61
62
    "m|message=s" => \@messages,
63
64
    'h|help|?' => \$help
65
);
66
67
if (   $help
68
    || !$host
69
    || !$login_user_id
70
    || !$login_password
71
    || !$location_code )
72
{
73
    say &help();
74
    exit();
75
}
76
77
$terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
78
79
# Set perl to expect the same record terminator it is sending
80
$/ = $terminator;
81
82
my ( $sec, $min, $hour, $day, $month, $year ) = localtime(time);
83
$year += 1900;
84
my $transaction_date = "$year$month$day    $hour$min$sec";
85
86
my $terminal_password = $login_password;
87
88
$| = 1;
89
print "Attempting socket connection to $host:$port...";
90
91
my $socket = IO::Socket::INET->new("$host:$port")
92
  or die "failed! : $!\n";
93
say "connected!";
94
95
my $handlers = {
96
    login => {
97
        name       => 'Login',
98
        subroutine => \&build_login_command_message,
99
        parameters => {
100
            login_user_id  => $login_user_id,
101
            login_password => $login_password,
102
            location_code  => $location_code,
103
        },
104
    },
105
    patron_status_request => {
106
        name       => 'Patron Status Request',
107
        subroutine => \&build_patron_status_request_command_message,
108
        parameters => {
109
            transaction_date  => $transaction_date,
110
            institution_id    => $location_code,
111
            patron_identifier => $patron_identifier,
112
            terminal_password => $terminal_password,
113
            patron_password   => $patron_password,
114
        },
115
    },
116
    patron_information => {
117
        name       => 'Patron Information',
118
        subroutine => \&build_patron_information_command_message,
119
        parameters => {
120
            transaction_date  => $transaction_date,
121
            institution_id    => $location_code,
122
            patron_identifier => $patron_identifier,
123
            terminal_password => $terminal_password,
124
            patron_password   => $patron_password,
125
        },
126
    },
127
    checkout => {
128
        name       => 'Checkout',
129
        subroutine => \&build_checkout_command_message,
130
        parameters => {
131
            SC_renewal_policy => 'Y',
132
            no_block          => 'N',
133
            transaction_date  => $transaction_date,
134
            nb_due_date       => undef,
135
            institution_id    => $location_code,
136
            patron_identifier => $patron_identifier,
137
            item_identifier   => $item_identifier,
138
            terminal_password => $terminal_password,
139
            item_properties   => undef,
140
            patron_password   => $patron_password,
141
            fee_acknowledged  => undef,
142
            cancel            => undef,
143
        },
144
        optional => [
145
            'nb_due_date',    # defaults to transaction date
146
            'item_properties',
147
            'patron_password',
148
            'fee_acknowledged',
149
            'cancel',
150
        ],
151
    },
152
};
153
154
my $data = run_command_message('login');
155
156
if ( $data =~ '^941' ) {      ## we are logged in
157
    foreach my $m (@messages) {
158
        say "Trying '$m'";
159
160
        my $data = run_command_message($m);
161
162
    }
163
}
164
else {
165
    say "Login Failed!";
166
}
167
168
sub build_command_message {
169
    my ($message) = @_;
170
171
    ##FIXME It would be much better to use exception handling so we aren't priting from subs
172
    unless ( $handlers->{$message} ) {
173
        say "$message is an unsupported command!";
174
        return;
175
    }
176
177
    my $subroutine = $handlers->{$message}->{subroutine};
178
    my $parameters = $handlers->{$message}->{parameters};
179
    my %optional   = map { $_ => 1 } @{ $handlers->{$message}->{optional} };
180
181
    foreach my $key ( keys %$parameters ) {
182
        unless ( $parameters->{$key} ) {
183
            unless ( $optional{$key} ) {
184
                say "$key is required for $message";
185
                return;
186
            }
187
        }
188
    }
189
190
    return &$subroutine($parameters);
191
}
192
193
sub run_command_message {
194
    my ($message) = @_;
195
196
    my $command_message = build_command_message($message);
197
198
    return unless $command_message;
199
200
    say "SEND: $command_message";
201
    print $socket $command_message . $terminator;
202
203
    my $data = <$socket>;
204
205
    say "READ: $data";
206
207
    return $data;
208
}
209
210
sub build_login_command_message {
211
    my ($params) = @_;
212
213
    my $login_user_id  = $params->{login_user_id};
214
    my $login_password = $params->{login_password};
215
    my $location_code  = $params->{location_code};
216
217
    return
218
        LOGIN . "00"
219
      . build_field( FID_LOGIN_UID,     $login_user_id )
220
      . build_field( FID_LOGIN_PWD,     $login_password )
221
      . build_field( FID_LOCATION_CODE, $location_code );
222
}
223
224
sub build_patron_status_request_command_message {
225
    my ($params) = @_;
226
227
    my $transaction_date  = $params->{transaction_date};
228
    my $institution_id    = $params->{institution_id};
229
    my $patron_identifier = $params->{patron_identifier};
230
    my $terminal_password = $params->{terminal_password};
231
    my $patron_password   = $params->{patron_password};
232
233
    return
234
        PATRON_STATUS_REQ
235
      . LANGUAGE
236
      . $transaction_date
237
      . build_field( FID_INST_ID,      $institution_id )
238
      . build_field( FID_PATRON_ID,    $patron_identifier )
239
      . build_field( FID_TERMINAL_PWD, $terminal_password )
240
      . build_field( FID_PATRON_PWD,   $patron_password );
241
}
242
243
sub build_patron_information_command_message {
244
    my ($params) = @_;
245
246
    my $transaction_date  = $params->{transaction_date};
247
    my $institution_id    = $params->{institution_id};
248
    my $patron_identifier = $params->{patron_identifier};
249
    my $terminal_password = $params->{terminal_password};
250
    my $patron_password   = $params->{patron_password};
251
252
    my $summary = "          ";
253
254
    return
255
        PATRON_INFO
256
      . LANGUAGE
257
      . $transaction_date
258
      . $summary
259
      . build_field( FID_INST_ID,      $institution_id )
260
      . build_field( FID_PATRON_ID,    $patron_identifier )
261
      . build_field( FID_TERMINAL_PWD, $terminal_password )
262
      . build_field( FID_PATRON_PWD,   $patron_password );
263
}
264
265
sub build_checkout_command_message {
266
    my ($params) = @_;
267
268
    my $SC_renewal_policy = $params->{SC_renewal_policy};
269
    my $no_block          = $params->{no_block};
270
    my $transaction_date  = $params->{transaction_date};
271
    my $nb_due_date       = $params->{nb_due_date};
272
    my $institution_id    = $params->{institution_id};
273
    my $patron_identifier = $params->{patron_identifier};
274
    my $item_identifier   = $params->{item_identifier};
275
    my $terminal_password = $params->{terminal_password};
276
    my $item_properties   = $params->{item_properties};
277
    my $patron_password   = $params->{patron_password};
278
    my $fee_acknowledged  = $params->{fee_acknowledged};
279
    my $cancel            = $params->{cancel};
280
281
    $SC_renewal_policy = $SC_renewal_policy ? 'Y' : 'N';
282
    $no_block          = $no_block          ? 'Y' : 'N';
283
    $fee_acknowledged  = $fee_acknowledged  ? 'Y' : 'N';
284
    $cancel            = $cancel            ? 'Y' : 'N';
285
286
    $nb_due_date ||= $transaction_date;
287
288
    return
289
        CHECKOUT
290
      . $SC_renewal_policy
291
      . $no_block
292
      . $transaction_date
293
      . $nb_due_date
294
      . build_field( FID_INST_ID,      $institution_id )
295
      . build_field( FID_PATRON_ID,    $patron_identifier )
296
      . build_field( FID_ITEM_ID,      $item_identifier )
297
      . build_field( FID_TERMINAL_PWD, $terminal_password )
298
      . build_field( FID_ITEM_PROPS,   $item_properties, { optional => 1 } )
299
      . build_field( FID_PATRON_PWD,   $patron_password, { optional => 1 } )
300
      . build_field( FID_FEE_ACK,      $fee_acknowledged, { optional => 1 } )
301
      . build_field( FID_CANCEL,       $cancel, { optional => 1 } );
302
}
303
304
sub build_field {
305
    my ( $field_identifier, $value, $params ) = @_;
306
307
    $params //= {};
308
309
    return q{} if ( $params->{optional} && !$value );
310
311
    return $field_identifier . $value . '|';
312
}
313
314
sub help {
315
    say q/sip_cli_emulator.pl - SIP command line emulator
316
317
Test a SIP2 service by sending patron status and patron
318
information requests.
319
320
Usage:
321
  sip_cli_emulator.pl [OPTIONS]
322
323
Options:
324
  --help           display help message
325
326
  -a --address     SIP server ip address or host name
327
  -p --port        SIP server port
328
329
  -su --sip_user   SIP server login username
330
  -sp --sip_pass   SIP server login password
331
332
  -l --location    SIP location code
333
334
  --patron         ILS patron cardnumber or username
335
  --password       ILS patron password
336
337
  --item           ILS item identifier ( item barcode )
338
339
  -t --terminator  SIP2 message terminator, either CR, or CRLF
340
                   (defaults to CRLF)
341
342
  -m --message     SIP2 message to execut
343
344
  Implemented Messages:
345
    patron_status_request
346
    patron_information
347
    checkout
348
349
/
350
}
(-)a/misc/sip_cli_emulator.pl (-162 lines)
Lines 1-161 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright (C) 2012-2013 ByWater Solutions
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Socket qw(:crlf);
23
use IO::Socket::INET;
24
use Getopt::Long;
25
26
my $help = 0;
27
28
my $host;
29
my $port = '6001';
30
31
my $login_user_id;
32
my $login_password;
33
my $location_code;
34
35
my $patron_identifier;
36
my $patron_password;
37
38
my $terminator = q{};
39
40
GetOptions(
41
    "a|address|host|hostaddress=s" => \$host,              # sip server ip
42
    "p|port=s"                     => \$port,              # sip server port
43
    "su|sip_user=s"                => \$login_user_id,     # sip user
44
    "sp|sip_pass=s"                => \$login_password,    # sip password
45
    "l|location|location_code=s"   => \$location_code,     # sip location code
46
47
    "patron=s"   => \$patron_identifier,    # patron cardnumber or login
48
    "password=s" => \$patron_password,      # patron's password
49
50
    "t|terminator=s" => \$terminator,
51
52
    'h|help|?' => \$help
53
);
54
55
if (   $help
56
    || !$host
57
    || !$login_user_id
58
    || !$login_password
59
    || !$location_code
60
    || !$patron_identifier
61
    || !$patron_password )
62
{
63
    print &help();
64
    exit();
65
}
66
67
$terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
68
69
# Set perl to expect the same record terminator it is sending
70
$/ = $terminator;
71
72
my ( $sec, $min, $hour, $day, $month, $year ) = localtime(time);
73
$year += 1900;
74
my $transaction_date = "$year$month$day    $hour$min$sec";
75
76
my $institution_id    = $location_code;
77
my $terminal_password = $login_password;
78
79
my $socket = IO::Socket::INET->new("$host:$port")
80
  or die "ERROR in Socket Creation host=$host port=$port : $!\n";
81
82
my $login_command = "9300CN$login_user_id|CO$login_password|CP$location_code|";
83
84
print "\nOUTBOUND: $login_command\n";
85
print $socket $login_command . $terminator;
86
87
my $data = <$socket>;
88
89
print "\nINBOUND: $data\n";
90
91
if ( $data =~ '^941' ) { ## we are logged in
92
93
    ## Patron Status Request
94
    print "\nTrying 'Patron Status Request'\n";
95
    my $patron_status_request = "23001"
96
      . $transaction_date
97
      . "AO"  . $institution_id
98
      . "|AA" . $patron_identifier
99
      . "|AC" . $terminal_password
100
      . "|AD" . $patron_password;
101
102
    print "\nOUTBOUND: $patron_status_request\n";
103
    print $socket $patron_status_request . $terminator;
104
105
    $data = <$socket>;
106
107
    print "\nINBOUND: $data\n";
108
109
    ## Patron Information
110
    print "\nTrying 'Patron Information'\n";
111
    my $summary = "          ";
112
    $patron_status_request = "63001"
113
      . $transaction_date
114
      . $summary
115
      . "AO"  . $institution_id
116
      . "|AA" . $patron_identifier
117
      . "|AC" . $terminal_password
118
      . "|AD" . $patron_password;
119
120
    print "\nOUTBOUND: $patron_status_request\n";
121
    print $socket $patron_status_request . $terminator;
122
123
    $data = <$socket>;
124
125
    print "\nINBOUND: $data\n";
126
127
}
128
else {
129
    print "\nLogin Failed!\n";
130
}
131
132
sub help {
133
    print
134
q/sip_cli_emulator.pl - SIP command line emulator
135
136
Test a SIP2 service by sending patron status and patron
137
information requests.
138
139
Usage:
140
  sip_cli_emulator.pl [OPTIONS]
141
142
Options:
143
  --help           display help message
144
145
  -a --address     SIP server ip address or host name
146
  -p --port        SIP server port
147
148
  -su --sip_user   SIP server login username
149
  -sp --sip_pass   SIP server login password
150
151
  -l --location    SIP location code
152
 
153
  --patron         ILS patron cardnumber or username
154
  --password       ILS patron password
155
156
  -t --terminator  SIP2 message terminator, either CR, or CRLF
157
                   (defaults to CRLF)
158
159
/
160
161
}
162
- 

Return to bug 13159