Preliminary (beta) release notes for 3.18
[koha.git] / misc / sip_cli_emulator.pl
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 }