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 |
renew => { |
178 |
name => 'Renew', |
179 |
subroutine => \&build_renew_command_message, |
180 |
parameters => { |
181 |
third_party_allowed => 'N', |
182 |
no_block => 'N', |
183 |
transaction_date => $transaction_date, |
184 |
nb_due_date => undef, |
185 |
institution_id => $location_code, |
186 |
patron_identifier => $patron_identifier, |
187 |
patron_password => $patron_password, |
188 |
item_identifier => $item_identifier, |
189 |
title_identifier => undef, |
190 |
terminal_password => $terminal_password, |
191 |
item_properties => undef, |
192 |
fee_acknowledged => $fee_acknowledged, |
193 |
}, |
194 |
optional => [ |
195 |
'nb_due_date', # defaults to transaction date |
196 |
'patron_password', |
197 |
'item_identifier', |
198 |
'title_identifier', |
199 |
'terminal_password', |
200 |
'item_properties', |
201 |
'fee_acknowledged', |
202 |
], |
203 |
}, |
204 |
}; |
205 |
|
206 |
my $data = run_command_message('login'); |
207 |
|
208 |
if ( $data =~ '^941' ) { ## we are logged in |
209 |
foreach my $m (@messages) { |
210 |
say "Trying '$m'"; |
211 |
|
212 |
my $data = run_command_message($m); |
213 |
|
214 |
} |
215 |
} |
216 |
else { |
217 |
say "Login Failed!"; |
218 |
} |
219 |
|
220 |
sub build_command_message { |
221 |
my ($message) = @_; |
222 |
|
223 |
##FIXME It would be much better to use exception handling so we aren't priting from subs |
224 |
unless ( $handlers->{$message} ) { |
225 |
say "$message is an unsupported command!"; |
226 |
return; |
227 |
} |
228 |
|
229 |
my $subroutine = $handlers->{$message}->{subroutine}; |
230 |
my $parameters = $handlers->{$message}->{parameters}; |
231 |
my %optional = map { $_ => 1 } @{ $handlers->{$message}->{optional} }; |
232 |
|
233 |
foreach my $key ( keys %$parameters ) { |
234 |
unless ( $parameters->{$key} ) { |
235 |
unless ( $optional{$key} ) { |
236 |
say "$key is required for $message"; |
237 |
return; |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
return &$subroutine($parameters); |
243 |
} |
244 |
|
245 |
sub run_command_message { |
246 |
my ($message) = @_; |
247 |
|
248 |
my $command_message = build_command_message($message); |
249 |
|
250 |
return unless $command_message; |
251 |
|
252 |
say "SEND: $command_message"; |
253 |
print $socket $command_message . $terminator; |
254 |
|
255 |
my $data = <$socket>; |
256 |
|
257 |
say "READ: $data"; |
258 |
|
259 |
return $data; |
260 |
} |
261 |
|
262 |
sub build_login_command_message { |
263 |
my ($params) = @_; |
264 |
|
265 |
my $login_user_id = $params->{login_user_id}; |
266 |
my $login_password = $params->{login_password}; |
267 |
my $location_code = $params->{location_code}; |
268 |
|
269 |
return |
270 |
LOGIN . "00" |
271 |
. build_field( FID_LOGIN_UID, $login_user_id ) |
272 |
. build_field( FID_LOGIN_PWD, $login_password ) |
273 |
. build_field( FID_LOCATION_CODE, $location_code ); |
274 |
} |
275 |
|
276 |
sub build_patron_status_request_command_message { |
277 |
my ($params) = @_; |
278 |
|
279 |
my $transaction_date = $params->{transaction_date}; |
280 |
my $institution_id = $params->{institution_id}; |
281 |
my $patron_identifier = $params->{patron_identifier}; |
282 |
my $terminal_password = $params->{terminal_password}; |
283 |
my $patron_password = $params->{patron_password}; |
284 |
|
285 |
return |
286 |
PATRON_STATUS_REQ |
287 |
. LANGUAGE |
288 |
. $transaction_date |
289 |
. build_field( FID_INST_ID, $institution_id ) |
290 |
. build_field( FID_PATRON_ID, $patron_identifier ) |
291 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
292 |
. build_field( FID_PATRON_PWD, $patron_password ); |
293 |
} |
294 |
|
295 |
sub build_patron_information_command_message { |
296 |
my ($params) = @_; |
297 |
|
298 |
my $transaction_date = $params->{transaction_date}; |
299 |
my $institution_id = $params->{institution_id}; |
300 |
my $patron_identifier = $params->{patron_identifier}; |
301 |
my $terminal_password = $params->{terminal_password}; |
302 |
my $patron_password = $params->{patron_password}; |
303 |
|
304 |
my $summary = " "; |
305 |
|
306 |
return |
307 |
PATRON_INFO |
308 |
. LANGUAGE |
309 |
. $transaction_date |
310 |
. $summary |
311 |
. build_field( FID_INST_ID, $institution_id ) |
312 |
. build_field( FID_PATRON_ID, $patron_identifier ) |
313 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
314 |
. build_field( FID_PATRON_PWD, $patron_password ); |
315 |
} |
316 |
|
317 |
sub build_checkout_command_message { |
318 |
my ($params) = @_; |
319 |
|
320 |
my $SC_renewal_policy = $params->{SC_renewal_policy}; |
321 |
my $no_block = $params->{no_block}; |
322 |
my $transaction_date = $params->{transaction_date}; |
323 |
my $nb_due_date = $params->{nb_due_date}; |
324 |
my $institution_id = $params->{institution_id}; |
325 |
my $patron_identifier = $params->{patron_identifier}; |
326 |
my $item_identifier = $params->{item_identifier}; |
327 |
my $terminal_password = $params->{terminal_password}; |
328 |
my $item_properties = $params->{item_properties}; |
329 |
my $patron_password = $params->{patron_password}; |
330 |
my $fee_acknowledged = $params->{fee_acknowledged}; |
331 |
my $cancel = $params->{cancel}; |
332 |
|
333 |
$SC_renewal_policy = $SC_renewal_policy ? 'Y' : 'N'; |
334 |
$no_block = $no_block ? 'Y' : 'N'; |
335 |
$fee_acknowledged = $fee_acknowledged ? 'Y' : 'N'; |
336 |
$cancel = $cancel ? 'Y' : 'N'; |
337 |
|
338 |
$nb_due_date ||= $transaction_date; |
339 |
|
340 |
return |
341 |
CHECKOUT |
342 |
. $SC_renewal_policy |
343 |
. $no_block |
344 |
. $transaction_date |
345 |
. $nb_due_date |
346 |
. build_field( FID_INST_ID, $institution_id ) |
347 |
. build_field( FID_PATRON_ID, $patron_identifier ) |
348 |
. build_field( FID_ITEM_ID, $item_identifier ) |
349 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
350 |
. build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } ) |
351 |
. build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } ) |
352 |
. build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } ) |
353 |
. build_field( FID_CANCEL, $cancel, { optional => 1 } ); |
354 |
} |
355 |
|
356 |
sub build_checkin_command_message { |
357 |
my ($params) = @_; |
358 |
|
359 |
my $no_block = $params->{no_block}; |
360 |
my $transaction_date = $params->{transaction_date}; |
361 |
my $return_date = $params->{return_date}; |
362 |
my $current_location = $params->{current_location}; |
363 |
my $institution_id = $params->{institution_id}; |
364 |
my $item_identifier = $params->{item_identifier}; |
365 |
my $terminal_password = $params->{terminal_password}; |
366 |
my $item_properties = $params->{item_properties}; |
367 |
my $cancel = $params->{cancel}; |
368 |
|
369 |
$no_block = $no_block ? 'Y' : 'N'; |
370 |
$cancel = $cancel ? 'Y' : 'N'; |
371 |
|
372 |
$return_date ||= $transaction_date; |
373 |
|
374 |
return |
375 |
CHECKIN |
376 |
. $no_block |
377 |
. $transaction_date |
378 |
. $return_date |
379 |
. build_field( FID_CURRENT_LOCN, $current_location ) |
380 |
. build_field( FID_INST_ID, $institution_id ) |
381 |
. build_field( FID_ITEM_ID, $item_identifier ) |
382 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
383 |
. build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } ) |
384 |
. build_field( FID_CANCEL, $cancel, { optional => 1 } ); |
385 |
} |
386 |
|
387 |
sub build_renew_command_message { |
388 |
my ($params) = @_; |
389 |
|
390 |
my $third_party_allowed = $params->{third_party_allowed}; |
391 |
my $no_block = $params->{no_block}; |
392 |
my $transaction_date = $params->{transaction_date}; |
393 |
my $nb_due_date = $params->{nb_due_date}; |
394 |
my $institution_id = $params->{institution_id}; |
395 |
my $patron_identifier = $params->{patron_identifier}; |
396 |
my $patron_password = $params->{patron_password}; |
397 |
my $item_identifier = $params->{item_identifier}; |
398 |
my $title_identifier = $params->{title_identifier}; |
399 |
my $terminal_password = $params->{terminal_password}; |
400 |
my $item_properties = $params->{item_properties}; |
401 |
my $fee_acknowledged = $params->{fee_acknowledged}; |
402 |
|
403 |
$third_party_allowed = $third_party_allowed ? 'Y' : 'N'; |
404 |
$no_block = $no_block ? 'Y' : 'N'; |
405 |
$fee_acknowledged = $fee_acknowledged ? 'Y' : 'N'; |
406 |
|
407 |
$nb_due_date ||= $transaction_date; |
408 |
|
409 |
return |
410 |
RENEW |
411 |
. $third_party_allowed |
412 |
. $no_block |
413 |
. $transaction_date |
414 |
. $nb_due_date |
415 |
. build_field( FID_INST_ID, $institution_id ) |
416 |
. build_field( FID_PATRON_ID, $patron_identifier ) |
417 |
. build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } ) |
418 |
. build_field( FID_ITEM_ID, $item_identifier ) |
419 |
. build_field( FID_TITLE_ID, $title_identifier ) |
420 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
421 |
. build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } ) |
422 |
. build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } ); |
423 |
} |
424 |
|
425 |
sub build_field { |
426 |
my ( $field_identifier, $value, $params ) = @_; |
427 |
|
428 |
$params //= {}; |
429 |
|
430 |
return q{} if ( $params->{optional} && !$value ); |
431 |
|
432 |
return $field_identifier . $value . '|'; |
433 |
} |
434 |
|
435 |
sub help { |
436 |
say q/sip_cli_emulator.pl - SIP command line emulator |
437 |
|
438 |
Test a SIP2 service by sending patron status and patron |
439 |
information requests. |
440 |
|
441 |
Usage: |
442 |
sip_cli_emulator.pl [OPTIONS] |
443 |
|
444 |
Options: |
445 |
--help display help message |
446 |
|
447 |
-a --address SIP server ip address or host name |
448 |
-p --port SIP server port |
449 |
|
450 |
-su --sip_user SIP server login username |
451 |
-sp --sip_pass SIP server login password |
452 |
|
453 |
-l --location SIP location code |
454 |
|
455 |
--patron ILS patron cardnumber or username |
456 |
--password ILS patron password |
457 |
|
458 |
--item ILS item identifier ( item barcode ) |
459 |
|
460 |
-t --terminator SIP2 message terminator, either CR, or CRLF |
461 |
(defaults to CRLF) |
462 |
|
463 |
-fa --fee-acknowledged Sends a confirmation of checkout fee |
464 |
|
465 |
-m --message SIP2 message to execute |
466 |
|
467 |
Implemented Messages: |
468 |
patron_status_request |
469 |
patron_information |
470 |
checkout |
471 |
checkin |
472 |
renew |
473 |
|
474 |
/ |
475 |
} |