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

(-)a/C4/SIP/sip_cli_emulator.pl (+351 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
## Please see file perltidy.ERR
96
my $handlers = {
97
    login => {
98
        name       => 'Login',
99
        subroutine => \&build_login_command_message,
100
        parameters => {
101
            login_user_id  => $login_user_id,
102
            login_password => $login_password,
103
            location_code  => $location_code,
104
        },
105
    },
106
    patron_status_request => {
107
        name       => 'Patron Status Request',
108
        subroutine => \&build_patron_status_request_command_message,
109
        parameters => {
110
            transaction_date  => $transaction_date,
111
            institution_id    => $location_code,
112
            patron_identifier => $patron_identifier,
113
            terminal_password => $terminal_password,
114
            patron_password   => $patron_password,
115
        },
116
    },
117
    patron_information => {
118
        name       => 'Patron Information',
119
        subroutine => \&build_patron_information_command_message,
120
        parameters => {
121
            transaction_date  => $transaction_date,
122
            institution_id    => $location_code,
123
            patron_identifier => $patron_identifier,
124
            terminal_password => $terminal_password,
125
            patron_password   => $patron_password,
126
        },
127
    },
128
    checkout => {
129
        name       => 'Checkout',
130
        subroutine => \&build_checkout_command_message,
131
        parameters => {
132
            SC_renewal_policy => 'Y',
133
            no_block          => 'N',
134
            transaction_date  => $transaction_date,
135
            nb_due_date       => undef,
136
            institution_id    => $location_code,
137
            patron_identifier => $patron_identifier,
138
            item_identifier   => $item_identifier,
139
            terminal_password => $terminal_password,
140
            item_properties   => undef,
141
            patron_password   => $patron_password,
142
            fee_acknowledged  => undef,
143
            cancel            => undef,
144
        },
145
        optional => [
146
            'nb_due_date',    # defaults to transaction date
147
            'item_properties',
148
            'patron_password',
149
            'fee_acknowledged',
150
            'cancel',
151
        ],
152
    },
153
};
154
155
my $data = run_command_message('login');
156
157
if ( $data =~ '^941' ) {      ## we are logged in
158
    foreach my $m (@messages) {
159
        say "Trying '$m'";
160
161
        my $data = run_command_message($m);
162
163
    }
164
}
165
else {
166
    say "Login Failed!";
167
}
168
169
sub build_command_message {
170
    my ($message) = @_;
171
172
    ##FIXME It would be much better to use exception handling so we aren't priting from subs
173
    unless ( $handlers->{$message} ) {
174
        say "$message is an unsupported command!";
175
        return;
176
    }
177
178
    my $subroutine = $handlers->{$message}->{subroutine};
179
    my $parameters = $handlers->{$message}->{parameters};
180
    my %optional   = map { $_ => 1 } @{ $handlers->{$message}->{optional} };
181
182
    foreach my $key ( keys %$parameters ) {
183
        unless ( $parameters->{$key} ) {
184
            unless ( $optional{$key} ) {
185
                say "$key is required for $message";
186
                return;
187
            }
188
        }
189
    }
190
191
    return &$subroutine($parameters);
192
}
193
194
sub run_command_message {
195
    my ($message) = @_;
196
197
    my $command_message = build_command_message($message);
198
199
    return unless $command_message;
200
201
    say "OUTBOUND: $command_message";
202
    print $socket $command_message . $terminator;
203
204
    my $data = <$socket>;
205
206
    say " INBOUND: $data";
207
208
    return $data;
209
}
210
211
sub build_login_command_message {
212
    my ($params) = @_;
213
214
    my $login_user_id  = $params->{login_user_id};
215
    my $login_password = $params->{login_password};
216
    my $location_code  = $params->{location_code};
217
218
    return
219
        LOGIN . "00"
220
      . build_field( FID_LOGIN_UID,     $login_user_id )
221
      . build_field( FID_LOGIN_PWD,     $login_password )
222
      . build_field( FID_LOCATION_CODE, $location_code );
223
}
224
225
sub build_patron_status_request_command_message {
226
    my ($params) = @_;
227
228
    my $transaction_date  = $params->{transaction_date};
229
    my $institution_id    = $params->{institution_id};
230
    my $patron_identifier = $params->{patron_identifier};
231
    my $terminal_password = $params->{terminal_password};
232
    my $patron_password   = $params->{patron_password};
233
234
    return
235
        PATRON_STATUS_REQ
236
      . LANGUAGE
237
      . $transaction_date
238
      . build_field( FID_INST_ID,      $institution_id )
239
      . build_field( FID_PATRON_ID,    $patron_identifier )
240
      . build_field( FID_TERMINAL_PWD, $terminal_password )
241
      . build_field( FID_PATRON_PWD,   $patron_password );
242
}
243
244
sub build_patron_information_command_message {
245
    my ($params) = @_;
246
247
    my $transaction_date  = $params->{transaction_date};
248
    my $institution_id    = $params->{institution_id};
249
    my $patron_identifier = $params->{patron_identifier};
250
    my $terminal_password = $params->{terminal_password};
251
    my $patron_password   = $params->{patron_password};
252
253
    my $summary = "          ";
254
255
    return
256
        PATRON_INFO
257
      . LANGUAGE
258
      . $transaction_date
259
      . $summary
260
      . build_field( FID_INST_ID,      $institution_id )
261
      . build_field( FID_PATRON_ID,    $patron_identifier )
262
      . build_field( FID_TERMINAL_PWD, $terminal_password )
263
      . build_field( FID_PATRON_PWD,   $patron_password );
264
}
265
266
sub build_checkout_command_message {
267
    my ($params) = @_;
268
269
    my $SC_renewal_policy = $params->{SC_renewal_policy};
270
    my $no_block          = $params->{no_block};
271
    my $transaction_date  = $params->{transaction_date};
272
    my $nb_due_date       = $params->{nb_due_date};
273
    my $institution_id    = $params->{institution_id};
274
    my $patron_identifier = $params->{patron_identifier};
275
    my $item_identifier   = $params->{item_identifier};
276
    my $terminal_password = $params->{terminal_password};
277
    my $item_properties   = $params->{item_properties};
278
    my $patron_password   = $params->{patron_password};
279
    my $fee_acknowledged  = $params->{fee_acknowledged};
280
    my $cancel            = $params->{cancel};
281
282
    $SC_renewal_policy = $SC_renewal_policy ? 'Y' : 'N';
283
    $no_block          = $no_block          ? 'Y' : 'N';
284
    $fee_acknowledged  = $fee_acknowledged  ? 'Y' : 'N';
285
    $cancel            = $cancel            ? 'Y' : 'N';
286
287
    $nb_due_date ||= $transaction_date;
288
289
    return
290
        CHECKOUT
291
      . $SC_renewal_policy
292
      . $no_block
293
      . $transaction_date
294
      . $nb_due_date
295
      . build_field( FID_INST_ID,      $institution_id )
296
      . build_field( FID_PATRON_ID,    $patron_identifier )
297
      . build_field( FID_ITEM_ID,      $item_identifier )
298
      . build_field( FID_TERMINAL_PWD, $terminal_password )
299
      . build_field( FID_ITEM_PROPS,   $item_properties, { optional => 1 } )
300
      . build_field( FID_PATRON_PWD,   $patron_password, { optional => 1 } )
301
      . build_field( FID_FEE_ACK,      $fee_acknowledged, { optional => 1 } )
302
      . build_field( FID_CANCEL,       $cancel, { optional => 1 } );
303
}
304
305
sub build_field {
306
    my ( $field_identifier, $value, $params ) = @_;
307
308
    $params //= {};
309
310
    return q{} if ( $params->{optional} && !$value );
311
312
    return $field_identifier . $value . '|';
313
}
314
315
sub help {
316
    say q/sip_cli_emulator.pl - SIP command line emulator
317
318
Test a SIP2 service by sending patron status and patron
319
information requests.
320
321
Usage:
322
  sip_cli_emulator.pl [OPTIONS]
323
324
Options:
325
  --help           display help message
326
327
  -a --address     SIP server ip address or host name
328
  -p --port        SIP server port
329
330
  -su --sip_user   SIP server login username
331
  -sp --sip_pass   SIP server login password
332
333
  -l --location    SIP location code
334
335
  --patron         ILS patron cardnumber or username
336
  --password       ILS patron password
337
338
  --item           ILS item identifier ( item barcode )
339
340
  -t --terminator  SIP2 message terminator, either CR, or CRLF
341
                   (defaults to CRLF)
342
343
  -m --message     SIP2 message to execut
344
345
  Implemented Messages:
346
    patron_status_request
347
    patron_informatio
348
    checkout
349
350
/
351
}
(-)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