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 $terminator = q{}; |
43 |
|
44 |
my $patron_status_request; |
45 |
my $patron_information; |
46 |
my $checkout; |
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 |
"t|terminator=s" => \$terminator, |
59 |
|
60 |
"psr|patron-status-request" => \$patron_status_request, |
61 |
"pi|patron-information" => \$patron_information, |
62 |
"c|checkout=s" => \$checkout, |
63 |
|
64 |
'h|help|?' => \$help |
65 |
); |
66 |
|
67 |
if ( $help |
68 |
|| !$host |
69 |
|| !$login_user_id |
70 |
|| !$login_password |
71 |
|| !$location_code |
72 |
|| !$patron_identifier |
73 |
|| !$patron_password ) |
74 |
{ |
75 |
say &help(); |
76 |
exit(); |
77 |
} |
78 |
|
79 |
$terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF; |
80 |
|
81 |
# Set perl to expect the same record terminator it is sending |
82 |
$/ = $terminator; |
83 |
|
84 |
my ( $sec, $min, $hour, $day, $month, $year ) = localtime(time); |
85 |
$year += 1900; |
86 |
my $transaction_date = "$year$month$day $hour$min$sec"; |
87 |
|
88 |
my $terminal_password = $login_password; |
89 |
|
90 |
$| = 1; |
91 |
print "Attempting socket connection to $host:$port..."; |
92 |
|
93 |
my $socket = IO::Socket::INET->new("$host:$port") |
94 |
or die "failed! : $!\n"; |
95 |
say "connected!"; |
96 |
|
97 |
my $login_command = get_login_command_message( |
98 |
{ |
99 |
login_user_id => $login_user_id, |
100 |
login_password => $login_password, |
101 |
location_code => $location_code, |
102 |
} |
103 |
); |
104 |
|
105 |
say "OUTBOUND: $login_command"; |
106 |
print $socket $login_command . $terminator; |
107 |
|
108 |
my $data = <$socket>; |
109 |
|
110 |
say " INBOUND: $data"; |
111 |
|
112 |
if ( $data =~ '^941' ) { ## we are logged in |
113 |
|
114 |
if ($patron_status_request) { |
115 |
|
116 |
say "Trying 'Patron Status Request'"; |
117 |
my $patron_status_request = get_patron_status_request_command_message( |
118 |
{ |
119 |
transaction_date => $transaction_date, |
120 |
institution_id => $location_code, |
121 |
patron_identifier => $patron_identifier, |
122 |
terminal_password => $terminal_password, |
123 |
patron_password => $patron_password, |
124 |
} |
125 |
); |
126 |
|
127 |
say "OUTBOUND: $patron_status_request"; |
128 |
print $socket $patron_status_request . $terminator; |
129 |
|
130 |
$data = <$socket>; |
131 |
|
132 |
say " INBOUND: $data"; |
133 |
} |
134 |
|
135 |
if ($patron_information) { |
136 |
|
137 |
say "Trying 'Patron Information'"; |
138 |
my $parton_status_request = get_patron_information_command_message( |
139 |
{ |
140 |
transaction_date => $transaction_date, |
141 |
institution_id => $location_code, |
142 |
patron_identifier => $patron_identifier, |
143 |
terminal_password => $terminal_password, |
144 |
patron_password => $patron_password, |
145 |
} |
146 |
); |
147 |
|
148 |
say "OUTBOUND: $patron_status_request"; |
149 |
print $socket $patron_status_request . $terminator; |
150 |
|
151 |
$data = <$socket>; |
152 |
|
153 |
say " INBOUND: $data"; |
154 |
} |
155 |
|
156 |
if ($checkout) { |
157 |
|
158 |
say "Trying 'Checkout'"; |
159 |
my $checkout_message = get_checkout_command_message( |
160 |
{ |
161 |
SC_renewal_policy => 'Y', |
162 |
no_block => 'N', |
163 |
transaction_date => $transaction_date, |
164 |
nb_due_date => undef, |
165 |
institution_id => $location_code, |
166 |
patron_identifier => $patron_identifier, |
167 |
item_identifier => $checkout, |
168 |
terminal_password => $terminal_password, |
169 |
item_properties => undef, |
170 |
patron_password => $patron_password, |
171 |
fee_acknowledged => undef, |
172 |
cancel => undef, |
173 |
} |
174 |
); |
175 |
|
176 |
say "OUTBOUND: $checkout_message"; |
177 |
print $socket $checkout_message . $terminator; |
178 |
|
179 |
$data = <$socket>; |
180 |
|
181 |
say " INBOUND: $data"; |
182 |
} |
183 |
|
184 |
} |
185 |
else { |
186 |
say "Login Failed!"; |
187 |
} |
188 |
|
189 |
sub get_login_command_message { |
190 |
my ($params) = @_; |
191 |
|
192 |
my $login_user_id = $params->{login_user_id}; |
193 |
my $login_password = $params->{login_password}; |
194 |
my $location_code = $params->{location_code}; |
195 |
|
196 |
return |
197 |
LOGIN . "00" |
198 |
. build_field( FID_LOGIN_UID, $login_user_id ) |
199 |
. build_field( FID_LOGIN_PWD, $login_password ) |
200 |
. build_field( FID_LOCATION_CODE, $location_code ); |
201 |
} |
202 |
|
203 |
sub get_patron_status_request_command_message { |
204 |
my ($params) = @_; |
205 |
|
206 |
my $transaction_date = $params->{transaction_date}; |
207 |
my $institution_id = $params->{institution_id}; |
208 |
my $patron_identifier = $params->{patron_identifier}; |
209 |
my $terminal_password = $params->{terminal_password}; |
210 |
my $patron_password = $params->{patron_password}; |
211 |
|
212 |
return |
213 |
PATRON_STATUS_REQ |
214 |
. LANGUAGE |
215 |
. $transaction_date |
216 |
. build_field( FID_INST_ID, $institution_id ) |
217 |
. build_field( FID_PATRON_ID, $patron_identifier ) |
218 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
219 |
. build_field( FID_PATRON_PWD, $patron_password ); |
220 |
} |
221 |
|
222 |
sub get_patron_information_command_message { |
223 |
my ($params) = @_; |
224 |
|
225 |
my $transaction_date = $params->{transaction_date}; |
226 |
my $institution_id = $params->{institution_id}; |
227 |
my $patron_identifier = $params->{patron_identifier}; |
228 |
my $terminal_password = $params->{terminal_password}; |
229 |
my $patron_password = $params->{patron_password}; |
230 |
|
231 |
my $summary = " "; |
232 |
|
233 |
return |
234 |
PATRON_INFO |
235 |
. LANGUAGE |
236 |
. $transaction_date |
237 |
. $summary |
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 get_checkout_command_message { |
245 |
my ($params) = @_; |
246 |
|
247 |
my $SC_renewal_policy = $params->{SC_renewal_policy}; |
248 |
my $no_block = $params->{no_block}; |
249 |
my $transaction_date = $params->{transaction_date}; |
250 |
my $nb_due_date = $params->{nb_due_date}; |
251 |
my $institution_id = $params->{institution_id}; |
252 |
my $patron_identifier = $params->{patron_identifier}; |
253 |
my $item_identifier = $params->{item_identifier}; |
254 |
my $terminal_password = $params->{terminal_password}; |
255 |
my $item_properties = $params->{item_properties}; |
256 |
my $patron_password = $params->{patron_password}; |
257 |
my $fee_acknowledged = $params->{fee_acknowledged}; |
258 |
my $cancel = $params->{cancel}; |
259 |
|
260 |
$SC_renewal_policy = $SC_renewal_policy ? 'Y' : 'N'; |
261 |
$no_block = $no_block ? 'Y' : 'N'; |
262 |
$fee_acknowledged = $fee_acknowledged ? 'Y' : 'N'; |
263 |
$cancel = $cancel ? 'Y' : 'N'; |
264 |
|
265 |
$nb_due_date ||= $transaction_date; |
266 |
|
267 |
return |
268 |
CHECKOUT |
269 |
. $SC_renewal_policy |
270 |
. $no_block |
271 |
. $transaction_date |
272 |
. $nb_due_date |
273 |
. build_field( FID_INST_ID, $institution_id ) |
274 |
. build_field( FID_PATRON_ID, $patron_identifier ) |
275 |
. build_field( FID_ITEM_ID, $item_identifier ) |
276 |
. build_field( FID_TERMINAL_PWD, $terminal_password ) |
277 |
. build_field( FID_ITEM_PROPS, $item_properties, { optional => 1 } ) |
278 |
. build_field( FID_PATRON_PWD, $patron_password, { optional => 1 } ) |
279 |
. build_field( FID_FEE_ACK, $fee_acknowledged, { optional => 1 } ) |
280 |
. build_field( FID_CANCEL, $cancel, { optional => 1 } ); |
281 |
} |
282 |
|
283 |
sub build_field { |
284 |
my ( $field_identifier, $value, $params ) = @_; |
285 |
|
286 |
$params //= {}; |
287 |
|
288 |
return q{} if ( $params->{optional} && !$value ); |
289 |
|
290 |
return $field_identifier . $value . '|'; |
291 |
} |
292 |
|
293 |
sub help { |
294 |
say q/sip_cli_emulator.pl - SIP command line emulator |
295 |
|
296 |
Test a SIP2 service by sending patron status and patron |
297 |
information requests. |
298 |
|
299 |
Usage: |
300 |
sip_cli_emulator.pl [OPTIONS] |
301 |
|
302 |
Options: |
303 |
--help display help message |
304 |
|
305 |
-a --address SIP server ip address or host name |
306 |
-p --port SIP server port |
307 |
|
308 |
-su --sip_user SIP server login username |
309 |
-sp --sip_pass SIP server login password |
310 |
|
311 |
-l --location SIP location code |
312 |
|
313 |
--patron ILS patron cardnumber or username |
314 |
--password ILS patron password |
315 |
|
316 |
-t --terminator SIP2 message terminator, either CR, or CRLF |
317 |
(defaults to CRLF) |
318 |
|
319 |
Implemented Messages: |
320 |
-psr --patron-status-request |
321 |
-pi --patron-information |
322 |
-c --checkout <item_identifier> |
323 |
|
324 |
/ |
325 |
} |