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 |
checkin => { |
153 |
name => 'Checkin', |
154 |
subroutine => \&build_checkin_command_message, |
155 |
parameters => { |
156 |
no_block => 'N', |
157 |
transaction_date => $transaction_date, |
158 |
return_date => $transaction_date, |
159 |
current_location => $location_code, |
160 |
institution_id => $location_code, |
161 |
item_identifier => $item_identifier, |
162 |
terminal_password => $terminal_password, |
163 |
item_properties => undef, |
164 |
cancel => undef, |
165 |
}, |
166 |
optional => [ |
167 |
'return_date', # defaults to transaction date |
168 |
'item_properties', |
169 |
'patron_password', |
170 |
'cancel', |
171 |
], |
172 |
}, |
173 |
}; |
174 |
|
175 |
my $data = run_command_message('login'); |
176 |
|
177 |
if ( $data =~ '^941' ) { ## we are logged in |
178 |
foreach my $m (@messages) { |
179 |
say "Trying '$m'"; |
180 |
|
181 |
my $data = run_command_message($m); |
182 |
|
183 |
} |
184 |
} |
185 |
else { |
186 |
say "Login Failed!"; |
187 |
} |
188 |
|
189 |
sub build_command_message { |
190 |
my ($message) = @_; |
191 |
|
192 |
##FIXME It would be much better to use exception handling so we aren't priting from subs |
193 |
unless ( $handlers->{$message} ) { |
194 |
say "$message is an unsupported command!"; |
195 |
return; |
196 |
} |
197 |
|
198 |
my $subroutine = $handlers->{$message}->{subroutine}; |
199 |
my $parameters = $handlers->{$message}->{parameters}; |
200 |
my %optional = map { $_ => 1 } @{ $handlers->{$message}->{optional} }; |
201 |
|
202 |
foreach my $key ( keys %$parameters ) { |
203 |
unless ( $parameters->{$key} ) { |
204 |
unless ( $optional{$key} ) { |
205 |
say "$key is required for $message"; |
206 |
return; |
207 |
} |
208 |
} |
209 |
} |
210 |
|
211 |
return &$subroutine($parameters); |
212 |
} |
213 |
|
214 |
sub run_command_message { |
215 |
my ($message) = @_; |
216 |
|
217 |
my $command_message = build_command_message($message); |
218 |
|
219 |
return unless $command_message; |
220 |
|
221 |
say "SEND: $command_message"; |
222 |
print $socket $command_message . $terminator; |
223 |
|
224 |
my $data = <$socket>; |
225 |
|
226 |
say "READ: $data"; |
227 |
|
228 |
return $data; |
229 |
} |
230 |
|
231 |
sub build_login_command_message { |
232 |
my ($params) = @_; |
233 |
|
234 |
my $login_user_id = $params->{login_user_id}; |
235 |
my $login_password = $params->{login_password}; |
236 |
my $location_code = $params->{location_code}; |
237 |
|
238 |
return |
239 |
LOGIN . "00" |
240 |
. build_field( FID_LOGIN_UID, $login_user_id ) |
241 |
. build_field( FID_LOGIN_PWD, $login_password ) |
242 |
. build_field( FID_LOCATION_CODE, $location_code ); |
243 |
} |
244 |
|
245 |
sub build_patron_status_request_command_message { |
246 |
my ($params) = @_; |
247 |
|
248 |
my $transaction_date = $params->{transaction_date}; |
249 |
my $institution_id = $params->{institution_id}; |
250 |
my $patron_identifier = $params->{patron_identifier}; |
251 |
my $terminal_password = $params->{terminal_password}; |
252 |
my $patron_password = $params->{patron_password}; |
253 |
|
254 |
return |
255 |
PATRON_STATUS_REQ |
256 |
. LANGUAGE |
257 |
. $transaction_date |
258 |
. build_field( FID_INST_ID, $institution_id ) |
259 |
. build_field( FID_PATRON_ID, $patron_identifier ) |
260 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
261 |
. build_field( FID_PATRON_PWD, $patron_password ); |
262 |
} |
263 |
|
264 |
sub build_patron_information_command_message { |
265 |
my ($params) = @_; |
266 |
|
267 |
my $transaction_date = $params->{transaction_date}; |
268 |
my $institution_id = $params->{institution_id}; |
269 |
my $patron_identifier = $params->{patron_identifier}; |
270 |
my $terminal_password = $params->{terminal_password}; |
271 |
my $patron_password = $params->{patron_password}; |
272 |
|
273 |
my $summary = " "; |
274 |
|
275 |
return |
276 |
PATRON_INFO |
277 |
. LANGUAGE |
278 |
. $transaction_date |
279 |
. $summary |
280 |
. build_field( FID_INST_ID, $institution_id ) |
281 |
. build_field( FID_PATRON_ID, $patron_identifier ) |
282 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
283 |
. build_field( FID_PATRON_PWD, $patron_password ); |
284 |
} |
285 |
|
286 |
sub build_checkout_command_message { |
287 |
my ($params) = @_; |
288 |
|
289 |
my $SC_renewal_policy = $params->{SC_renewal_policy}; |
290 |
my $no_block = $params->{no_block}; |
291 |
my $transaction_date = $params->{transaction_date}; |
292 |
my $nb_due_date = $params->{nb_due_date}; |
293 |
my $institution_id = $params->{institution_id}; |
294 |
my $patron_identifier = $params->{patron_identifier}; |
295 |
my $item_identifier = $params->{item_identifier}; |
296 |
my $terminal_password = $params->{terminal_password}; |
297 |
my $item_properties = $params->{item_properties}; |
298 |
my $patron_password = $params->{patron_password}; |
299 |
my $fee_acknowledged = $params->{fee_acknowledged}; |
300 |
my $cancel = $params->{cancel}; |
301 |
|
302 |
$SC_renewal_policy = $SC_renewal_policy ? 'Y' : 'N'; |
303 |
$no_block = $no_block ? 'Y' : 'N'; |
304 |
$fee_acknowledged = $fee_acknowledged ? 'Y' : 'N'; |
305 |
$cancel = $cancel ? 'Y' : 'N'; |
306 |
|
307 |
$nb_due_date ||= $transaction_date; |
308 |
|
309 |
return |
310 |
CHECKOUT |
311 |
. $SC_renewal_policy |
312 |
. $no_block |
313 |
. $transaction_date |
314 |
. $nb_due_date |
315 |
. build_field( FID_INST_ID, $institution_id ) |
316 |
. build_field( FID_PATRON_ID, $patron_identifier ) |
317 |
. build_field( FID_ITEM_ID, $item_identifier ) |
318 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
319 |
. build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } ) |
320 |
. build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } ) |
321 |
. build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } ) |
322 |
. build_field( FID_CANCEL, $cancel, { optional => 1 } ); |
323 |
} |
324 |
|
325 |
sub build_checkin_command_message { |
326 |
my ($params) = @_; |
327 |
|
328 |
my $no_block = $params->{no_block}; |
329 |
my $transaction_date = $params->{transaction_date}; |
330 |
my $return_date = $params->{return_date}; |
331 |
my $current_location = $params->{current_location}; |
332 |
my $institution_id = $params->{institution_id}; |
333 |
my $item_identifier = $params->{item_identifier}; |
334 |
my $terminal_password = $params->{terminal_password}; |
335 |
my $item_properties = $params->{item_properties}; |
336 |
my $cancel = $params->{cancel}; |
337 |
|
338 |
$no_block = $no_block ? 'Y' : 'N'; |
339 |
$cancel = $cancel ? 'Y' : 'N'; |
340 |
|
341 |
$return_date ||= $transaction_date; |
342 |
|
343 |
return |
344 |
CHECKIN |
345 |
. $no_block |
346 |
. $transaction_date |
347 |
. $return_date |
348 |
. build_field( FID_CURRENT_LOCN, $current_location ) |
349 |
. build_field( FID_INST_ID, $institution_id ) |
350 |
. build_field( FID_ITEM_ID, $item_identifier ) |
351 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
352 |
. build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } ) |
353 |
. build_field( FID_CANCEL, $cancel, { optional => 1 } ); |
354 |
} |
355 |
|
356 |
sub build_field { |
357 |
my ( $field_identifier, $value, $params ) = @_; |
358 |
|
359 |
$params //= {}; |
360 |
|
361 |
return q{} if ( $params->{optional} && !$value ); |
362 |
|
363 |
return $field_identifier . $value . '|'; |
364 |
} |
365 |
|
366 |
sub help { |
367 |
say q/sip_cli_emulator.pl - SIP command line emulator |
368 |
|
369 |
Test a SIP2 service by sending patron status and patron |
370 |
information requests. |
371 |
|
372 |
Usage: |
373 |
sip_cli_emulator.pl [OPTIONS] |
374 |
|
375 |
Options: |
376 |
--help display help message |
377 |
|
378 |
-a --address SIP server ip address or host name |
379 |
-p --port SIP server port |
380 |
|
381 |
-su --sip_user SIP server login username |
382 |
-sp --sip_pass SIP server login password |
383 |
|
384 |
-l --location SIP location code |
385 |
|
386 |
--patron ILS patron cardnumber or username |
387 |
--password ILS patron password |
388 |
|
389 |
--item ILS item identifier ( item barcode ) |
390 |
|
391 |
-t --terminator SIP2 message terminator, either CR, or CRLF |
392 |
(defaults to CRLF) |
393 |
|
394 |
-m --message SIP2 message to execut |
395 |
|
396 |
Implemented Messages: |
397 |
patron_status_request |
398 |
patron_information |
399 |
checkout |
400 |
checkin |
401 |
|
402 |
/ |
403 |
} |