Bug 9288: (follow-up) add --terminator option; use Modern::Perl
[koha.git] / misc / sip_cli_emulator.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Socket qw(:crlf);
6 use IO::Socket::INET;
7 use Getopt::Long;
8
9 my $help = 0;
10
11 my $host;
12 my $port = '6001';
13
14 my $login_user_id;
15 my $login_password;
16 my $location_code;
17
18 my $patron_identifier;
19 my $patron_password;
20
21 my $terminator;
22
23 GetOptions(
24     "a|address|host|hostaddress=s" => \$host,              # sip server ip
25     "p|port=s"                     => \$port,              # sip server port
26     "su|sip_user=s"                => \$login_user_id,     # sip user
27     "sp|sip_pass=s"                => \$login_password,    # sip password
28     "l|location|location_code=s"   => \$location_code,     # sip location code
29
30     "patron=s"   => \$patron_identifier,    # patron cardnumber or login
31     "password=s" => \$patron_password,      # patron's password
32
33     "t|terminator=s" => \$terminator,
34
35     'h|help|?' => \$help
36 );
37
38 if (   $help
39     || !$host
40     || !$login_user_id
41     || !$login_password
42     || !$location_code
43     || !$patron_identifier
44     || !$patron_password )
45 {
46     print help();
47     exit();
48 }
49
50 $terminator = ( $terminator eq 'CR' ) ? $CR : $CRLF;
51
52 my ( $sec, $min, $hour, $day, $month, $year ) = localtime(time);
53 $year += 1900;
54 my $transaction_date = "$year$month$day    $hour$min$sec";
55
56 my $institution_id    = $location_code;
57 my $terminal_password = $login_password;
58
59 my $socket = IO::Socket::INET->new("$host:$port")
60   or die "ERROR in Socket Creation host=$host port=$port : $!\n";
61
62 my $login_command = "9300CN$login_user_id|CO$login_password|CP$location_code|";
63
64 print "\nOUTBOUND: $login_command\n";
65 print $socket $login_command . $terminator;
66
67 my $data = <$socket>;
68
69 print "\nINBOUND: $data\n";
70
71 if ( $data =~ '^941' ) { ## we are logged in
72
73     ## Patron Status Request
74     print "\nTrying 'Patron Status Request'\n";
75     my $patron_status_request = "23001"
76       . $transaction_date
77       . "AO"  . $institution_id
78       . "|AA" . $patron_identifier
79       . "|AC" . $terminal_password
80       . "|AD" . $patron_password;
81
82     print "\nOUTBOUND: $patron_status_request\n";
83     print $socket $patron_status_request . $terminator;
84
85     $data = <$socket>;
86
87     print "\nINBOUND: $data\n";
88
89     ## Patron Information
90     print "\nTrying 'Patron Information'\n";
91     my $summary = "          ";
92     $patron_status_request = "63001"
93       . $transaction_date
94       . $summary
95       . "AO"  . $institution_id
96       . "|AA" . $patron_identifier
97       . "|AC" . $terminal_password
98       . "|AD" . $patron_password;
99
100     print "\nOUTBOUND: $patron_status_request\n";
101     print $socket $patron_status_request . $terminator;
102
103     $data = <$socket>;
104
105     print "\nINBOUND: $data\n";
106
107 }
108 else {
109     print "\nLogin Failed!\n";
110 }
111
112 sub help() {
113     print
114 q/
115 sip_cli_emulator.pl - SIP command line emulator
116
117   Usage:
118     sip_cli_emulator.pl --address localhost -port 6001 --sip_user myuser --sip_pass mypass --location MYLOCATION --patron 70000003 --password Patr0nP@ssword
119
120   Options:
121     --help          brief help message
122
123     -a --address    SIP server ip address or host name
124     -p --port       SIP server port
125
126     -su --sip_user  SIP server login username
127     -sp --sip_pass  SIP server login password
128
129     -l --location   SIP location code
130
131     --patron        ILS patron cardnumber or username
132     --password      ILS patron password
133
134     -t --terminator    Specifies the SIP2 message terminator, either CR, or CRLF ( defaults to CRLF )
135
136 sip_cli_emulator.pl will make requests for information about the given user from the given server via SIP2.
137
138 /
139
140 }