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

(-)a/C4/SIP/sip_cli_emulator.pl (+409 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 $fee_acknowledged = 0;
45
46
my $terminator = q{};
47
48
my @messages;
49
50
GetOptions(
51
    "a|address|host|hostaddress=s" => \$host,              # sip server ip
52
    "p|port=s"                     => \$port,              # sip server port
53
    "su|sip_user=s"                => \$login_user_id,     # sip user
54
    "sp|sip_pass=s"                => \$login_password,    # sip password
55
    "l|location|location_code=s"   => \$location_code,     # sip location code
56
57
    "patron=s"   => \$patron_identifier,    # patron cardnumber or login
58
    "password=s" => \$patron_password,      # patron's password
59
60
    "i|item=s" => \$item_identifier,
61
62
    "fa|fee-acknowledged" => \$fee_acknowledged,
63
64
    "t|terminator=s" => \$terminator,
65
66
    "m|message=s" => \@messages,
67
68
    'h|help|?' => \$help
69
);
70
71
if (   $help
72
    || !$host
73
    || !$login_user_id
74
    || !$login_password
75
    || !$location_code )
76
{
77
    say &help();
78
    exit();
79
}
80
81
$terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
82
83
# Set perl to expect the same record terminator it is sending
84
$/ = $terminator;
85
86
my ( $sec, $min, $hour, $day, $month, $year ) = localtime(time);
87
$year += 1900;
88
my $transaction_date = "$year$month$day    $hour$min$sec";
89
90
my $terminal_password = $login_password;
91
92
$| = 1;
93
print "Attempting socket connection to $host:$port...";
94
95
my $socket = IO::Socket::INET->new("$host:$port")
96
  or die "failed! : $!\n";
97
say "connected!";
98
99
my $handlers = {
100
    login => {
101
        name       => 'Login',
102
        subroutine => \&build_login_command_message,
103
        parameters => {
104
            login_user_id  => $login_user_id,
105
            login_password => $login_password,
106
            location_code  => $location_code,
107
        },
108
    },
109
    patron_status_request => {
110
        name       => 'Patron Status Request',
111
        subroutine => \&build_patron_status_request_command_message,
112
        parameters => {
113
            transaction_date  => $transaction_date,
114
            institution_id    => $location_code,
115
            patron_identifier => $patron_identifier,
116
            terminal_password => $terminal_password,
117
            patron_password   => $patron_password,
118
        },
119
    },
120
    patron_information => {
121
        name       => 'Patron Information',
122
        subroutine => \&build_patron_information_command_message,
123
        parameters => {
124
            transaction_date  => $transaction_date,
125
            institution_id    => $location_code,
126
            patron_identifier => $patron_identifier,
127
            terminal_password => $terminal_password,
128
            patron_password   => $patron_password,
129
        },
130
    },
131
    checkout => {
132
        name       => 'Checkout',
133
        subroutine => \&build_checkout_command_message,
134
        parameters => {
135
            SC_renewal_policy => 'Y',
136
            no_block          => 'N',
137
            transaction_date  => $transaction_date,
138
            nb_due_date       => undef,
139
            institution_id    => $location_code,
140
            patron_identifier => $patron_identifier,
141
            item_identifier   => $item_identifier,
142
            terminal_password => $terminal_password,
143
            item_properties   => undef,
144
            patron_password   => $patron_password,
145
            fee_acknowledged  => $fee_acknowledged,
146
            cancel            => undef,
147
        },
148
        optional => [
149
            'nb_due_date',    # defaults to transaction date
150
            'item_properties',
151
            'patron_password',
152
            'fee_acknowledged',
153
            'cancel',
154
        ],
155
    },
156
    checkin => {
157
        name       => 'Checkin',
158
        subroutine => \&build_checkin_command_message,
159
        parameters => {
160
            no_block          => 'N',
161
            transaction_date  => $transaction_date,
162
            return_date       => $transaction_date,
163
            current_location  => $location_code,
164
            institution_id    => $location_code,
165
            item_identifier   => $item_identifier,
166
            terminal_password => $terminal_password,
167
            item_properties   => undef,
168
            cancel            => undef,
169
        },
170
        optional => [
171
            'return_date',    # defaults to transaction date
172
            'item_properties',
173
            'patron_password',
174
            'cancel',
175
        ],
176
    },
177
};
178
179
my $data = run_command_message('login');
180
181
if ( $data =~ '^941' ) {      ## we are logged in
182
    foreach my $m (@messages) {
183
        say "Trying '$m'";
184
185
        my $data = run_command_message($m);
186
187
    }
188
}
189
else {
190
    say "Login Failed!";
191
}
192
193
sub build_command_message {
194
    my ($message) = @_;
195
196
    ##FIXME It would be much better to use exception handling so we aren't priting from subs
197
    unless ( $handlers->{$message} ) {
198
        say "$message is an unsupported command!";
199
        return;
200
    }
201
202
    my $subroutine = $handlers->{$message}->{subroutine};
203
    my $parameters = $handlers->{$message}->{parameters};
204
    my %optional   = map { $_ => 1 } @{ $handlers->{$message}->{optional} };
205
206
    foreach my $key ( keys %$parameters ) {
207
        unless ( $parameters->{$key} ) {
208
            unless ( $optional{$key} ) {
209
                say "$key is required for $message";
210
                return;
211
            }
212
        }
213
    }
214
215
    return &$subroutine($parameters);
216
}
217
218
sub run_command_message {
219
    my ($message) = @_;
220
221
    my $command_message = build_command_message($message);
222
223
    return unless $command_message;
224
225
    say "SEND: $command_message";
226
    print $socket $command_message . $terminator;
227
228
    my $data = <$socket>;
229
230
    say "READ: $data";
231
232
    return $data;
233
}
234
235
sub build_login_command_message {
236
    my ($params) = @_;
237
238
    my $login_user_id  = $params->{login_user_id};
239
    my $login_password = $params->{login_password};
240
    my $location_code  = $params->{location_code};
241
242
    return
243
        LOGIN . "00"
244
      . build_field( FID_LOGIN_UID,     $login_user_id )
245
      . build_field( FID_LOGIN_PWD,     $login_password )
246
      . build_field( FID_LOCATION_CODE, $location_code );
247
}
248
249
sub build_patron_status_request_command_message {
250
    my ($params) = @_;
251
252
    my $transaction_date  = $params->{transaction_date};
253
    my $institution_id    = $params->{institution_id};
254
    my $patron_identifier = $params->{patron_identifier};
255
    my $terminal_password = $params->{terminal_password};
256
    my $patron_password   = $params->{patron_password};
257
258
    return
259
        PATRON_STATUS_REQ
260
      . LANGUAGE
261
      . $transaction_date
262
      . build_field( FID_INST_ID,      $institution_id )
263
      . build_field( FID_PATRON_ID,    $patron_identifier )
264
      . build_field( FID_TERMINAL_PWD, $terminal_password )
265
      . build_field( FID_PATRON_PWD,   $patron_password );
266
}
267
268
sub build_patron_information_command_message {
269
    my ($params) = @_;
270
271
    my $transaction_date  = $params->{transaction_date};
272
    my $institution_id    = $params->{institution_id};
273
    my $patron_identifier = $params->{patron_identifier};
274
    my $terminal_password = $params->{terminal_password};
275
    my $patron_password   = $params->{patron_password};
276
277
    my $summary = "          ";
278
279
    return
280
        PATRON_INFO
281
      . LANGUAGE
282
      . $transaction_date
283
      . $summary
284
      . build_field( FID_INST_ID,      $institution_id )
285
      . build_field( FID_PATRON_ID,    $patron_identifier )
286
      . build_field( FID_TERMINAL_PWD, $terminal_password )
287
      . build_field( FID_PATRON_PWD,   $patron_password );
288
}
289
290
sub build_checkout_command_message {
291
    my ($params) = @_;
292
293
    my $SC_renewal_policy = $params->{SC_renewal_policy};
294
    my $no_block          = $params->{no_block};
295
    my $transaction_date  = $params->{transaction_date};
296
    my $nb_due_date       = $params->{nb_due_date};
297
    my $institution_id    = $params->{institution_id};
298
    my $patron_identifier = $params->{patron_identifier};
299
    my $item_identifier   = $params->{item_identifier};
300
    my $terminal_password = $params->{terminal_password};
301
    my $item_properties   = $params->{item_properties};
302
    my $patron_password   = $params->{patron_password};
303
    my $fee_acknowledged  = $params->{fee_acknowledged};
304
    my $cancel            = $params->{cancel};
305
306
    $SC_renewal_policy = $SC_renewal_policy ? 'Y' : 'N';
307
    $no_block          = $no_block          ? 'Y' : 'N';
308
    $fee_acknowledged  = $fee_acknowledged  ? 'Y' : 'N';
309
    $cancel            = $cancel            ? 'Y' : 'N';
310
311
    $nb_due_date ||= $transaction_date;
312
313
    return
314
        CHECKOUT
315
      . $SC_renewal_policy
316
      . $no_block
317
      . $transaction_date
318
      . $nb_due_date
319
      . build_field( FID_INST_ID,      $institution_id )
320
      . build_field( FID_PATRON_ID,    $patron_identifier )
321
      . build_field( FID_ITEM_ID,      $item_identifier )
322
      . build_field( FID_TERMINAL_PWD, $terminal_password )
323
      . build_field( FID_ITEM_PROPS,   $item_properties, { optional => 1 } )
324
      . build_field( FID_PATRON_PWD,   $patron_password, { optional => 1 } )
325
      . build_field( FID_FEE_ACK,      $fee_acknowledged, { optional => 1 } )
326
      . build_field( FID_CANCEL,       $cancel, { optional => 1 } );
327
}
328
329
sub build_checkin_command_message {
330
    my ($params) = @_;
331
332
    my $no_block          = $params->{no_block};
333
    my $transaction_date  = $params->{transaction_date};
334
    my $return_date       = $params->{return_date};
335
    my $current_location  = $params->{current_location};
336
    my $institution_id    = $params->{institution_id};
337
    my $item_identifier   = $params->{item_identifier};
338
    my $terminal_password = $params->{terminal_password};
339
    my $item_properties   = $params->{item_properties};
340
    my $cancel            = $params->{cancel};
341
342
    $no_block          = $no_block          ? 'Y' : 'N';
343
    $cancel            = $cancel            ? 'Y' : 'N';
344
345
    $return_date ||= $transaction_date;
346
347
    return
348
        CHECKIN
349
      . $no_block
350
      . $transaction_date
351
      . $return_date
352
      . build_field( FID_CURRENT_LOCN, $current_location )
353
      . build_field( FID_INST_ID,      $institution_id )
354
      . build_field( FID_ITEM_ID,      $item_identifier )
355
      . build_field( FID_TERMINAL_PWD, $terminal_password )
356
      . build_field( FID_ITEM_PROPS,   $item_properties, { optional => 1 } )
357
      . build_field( FID_CANCEL,       $cancel, { optional => 1 } );
358
}
359
360
sub build_field {
361
    my ( $field_identifier, $value, $params ) = @_;
362
363
    $params //= {};
364
365
    return q{} if ( $params->{optional} && !$value );
366
367
    return $field_identifier . $value . '|';
368
}
369
370
sub help {
371
    say q/sip_cli_emulator.pl - SIP command line emulator
372
373
Test a SIP2 service by sending patron status and patron
374
information requests.
375
376
Usage:
377
  sip_cli_emulator.pl [OPTIONS]
378
379
Options:
380
  --help           display help message
381
382
  -a --address     SIP server ip address or host name
383
  -p --port        SIP server port
384
385
  -su --sip_user   SIP server login username
386
  -sp --sip_pass   SIP server login password
387
388
  -l --location    SIP location code
389
390
  --patron         ILS patron cardnumber or username
391
  --password       ILS patron password
392
393
  --item           ILS item identifier ( item barcode )
394
395
  -t --terminator  SIP2 message terminator, either CR, or CRLF
396
                   (defaults to CRLF)
397
398
  -fa --fee-acknowledged Sends a confirmation of checkout fee
399
400
  -m --message     SIP2 message to execute
401
402
  Implemented Messages:
403
    patron_status_request
404
    patron_information
405
    checkout
406
    checkin
407
408
/
409
}
(-)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