Bug 9288: (follow-up) remove function prototype
[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 = q{};
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 # Set perl to expect the same record terminator it is sending
53 $/ = $terminator;
54
55 my ( $sec, $min, $hour, $day, $month, $year ) = localtime(time);
56 $year += 1900;
57 my $transaction_date = "$year$month$day    $hour$min$sec";
58
59 my $institution_id    = $location_code;
60 my $terminal_password = $login_password;
61
62 my $socket = IO::Socket::INET->new("$host:$port")
63   or die "ERROR in Socket Creation host=$host port=$port : $!\n";
64
65 my $login_command = "9300CN$login_user_id|CO$login_password|CP$location_code|";
66
67 print "\nOUTBOUND: $login_command\n";
68 print $socket $login_command . $terminator;
69
70 my $data = <$socket>;
71
72 print "\nINBOUND: $data\n";
73
74 if ( $data =~ '^941' ) { ## we are logged in
75
76     ## Patron Status Request
77     print "\nTrying 'Patron Status Request'\n";
78     my $patron_status_request = "23001"
79       . $transaction_date
80       . "AO"  . $institution_id
81       . "|AA" . $patron_identifier
82       . "|AC" . $terminal_password
83       . "|AD" . $patron_password;
84
85     print "\nOUTBOUND: $patron_status_request\n";
86     print $socket $patron_status_request . $terminator;
87
88     $data = <$socket>;
89
90     print "\nINBOUND: $data\n";
91
92     ## Patron Information
93     print "\nTrying 'Patron Information'\n";
94     my $summary = "          ";
95     $patron_status_request = "63001"
96       . $transaction_date
97       . $summary
98       . "AO"  . $institution_id
99       . "|AA" . $patron_identifier
100       . "|AC" . $terminal_password
101       . "|AD" . $patron_password;
102
103     print "\nOUTBOUND: $patron_status_request\n";
104     print $socket $patron_status_request . $terminator;
105
106     $data = <$socket>;
107
108     print "\nINBOUND: $data\n";
109
110 }
111 else {
112     print "\nLogin Failed!\n";
113 }
114
115 sub help {
116     print
117 q/
118 sip_cli_emulator.pl - SIP command line emulator
119
120   Usage:
121     sip_cli_emulator.pl --address localhost -port 6001 --sip_user myuser --sip_pass mypass --location MYLOCATION --patron 70000003 --password Patr0nP@ssword
122
123   Options:
124     --help          brief help message
125
126     -a --address    SIP server ip address or host name
127     -p --port       SIP server port
128
129     -su --sip_user  SIP server login username
130     -sp --sip_pass  SIP server login password
131
132     -l --location   SIP location code
133
134     --patron        ILS patron cardnumber or username
135     --password      ILS patron password
136
137     -t --terminator    Specifies the SIP2 message terminator, either CR, or CRLF ( defaults to CRLF )
138
139 sip_cli_emulator.pl will make requests for information about the given user from the given server via SIP2.
140
141 /
142
143 }