Bug 6875 de-nesting C4::Items
[koha.git] / sms / sms_listen.pl
blobb36b25da46baa3e49b9c0ae77dbf40fa8f3715c8
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use C4::SMS;
7 use C4::Auth;
8 use C4::Context;
9 use C4::Members;
10 use C4::Circulation;
12 my ($res,$ua);
13 my %commands;
14 my $message;
15 my $phone;
16 my $result;
17 my $errorcode;
18 my $smsid;
19 my $wait=600;## 10 mn. wait between sms checking
20 my $dbh=C4::Context->dbh;
22 STARTAGAIN:
23 ($res,$ua)=get_sms_auth();
24 AGAIN:
25 $errorcode=0;
26 if ($res->{pRetCode}==200){
27 $result=read_sms($ua,$res->{pSessionId});
28 $errorcode=$result->{pErrCode};
29 print "connected\n";
30 } else {
31 kill_sms($ua,$res->{pSessionId});
32 warn (error_codes($res->{pErrCode}),$res->{pErrcode}) ;
33 # sleep $wait;
34 goto FINISH;
36 if ($errorcode && $errorcode !=-9005){
37 kill_sms($ua,$res->{pSessionId});
38 warn error_codes($errorcode) ;
39 # sleep $wait;
40 goto FINISH;
41 } elsif ($errorcode ==-9005){
42 print "no more messages to read\n";
43 goto WAITING;
47 #Parse the message to a useful hash
48 my @action=parse_message( $result->{pContent});
49 ## Log the request in our database;
50 $smsid=write_sms($action[1], $result->{pContent},$result->{pMsisdn});
51 print "message logged\n";
52 ##Now do the service required
53 if (uc($action[0]) eq "RN"){
54 print "dealing request\n";
55 my ($ok,$cardnumber)=C4::Auth::checkpw($dbh,$action[1],$action[2]);
56 unless ($ok) {
57 ##wrong user/pass
58 $message="Yanlis kullanici/sifre! :Wrong username/password!";
59 my $send=send_message($result,$message,$smsid);
60 goto AGAIN;
62 my $item=getiteminformation(undef,0,$action[3]);
63 if ($item){
64 my $borrower=getmember($cardnumber);
65 my $status=renewstatus(undef,$borrower->{borrowernumber},$item->{itemnumber});
66 if ($status==1) {
67 my $date=renewbook(undef,$borrower->{borrowernumber},$item->{itemnumber});
68 $message="Uzatildi :Renewed ".$item->{barcode}." : ".$date;
69 } elsif($status==2) {
70 $message="Cok erken- yenilenmedi! :Too early-not renewed:".$item->{barcode};
71 } elsif($status==3) {
72 $message="Uzatamazsiniz GERI getiriniz! :No more renewals RETURN the item:".$item->{barcode};
73 } elsif($status==4) {
74 $message="Ayirtildi GERI getiriniz! :Reserved RETURN the item:".$item->{barcode};
75 } elsif($status==0) {
76 $message="Uzatilamaz! :Can not renew:".$item->{barcode};
78 } else {
79 $message="Yanlis barkot! :Wrong barcode!";
81 } else {
82 ## reply about error
83 $message="Yanlis mesaj formati! :Wrong message! :
84 RN usercardno password barcode";
85 } ### wrong service
86 send_message($result,$message,$smsid);
88 goto AGAIN;
91 WAITING:
92 ##Now send the messages waiting in queue
93 my $smssth=$dbh->prepare("SELECT smsid,user_phone,message from sms_messages where date_replied like '0000-00-00%' ");
94 $smssth->execute();
95 my @phones;
96 while (my $data=$smssth->fetchrow_hashref){
97 push @phones,$data;
99 $smssth->finish;
101 foreach my $user(@phones){
102 print "replying $user->{user_phone}";
103 my $send=send_sms($ua,$user->{user_phone},$user->{message},$res->{pSessionId});
104 my $reply="--failed\n";
105 if ($send->{pRetCode}==200){
106 $reply= "--replied\n";
107 mod_sms($user->{smsid},"Sent");
109 print $reply;
111 $dbh->disconnect;
113 sub send_message {
114 my ($mes,$message,$smsid)=@_;
115 my $send=send_sms($ua,$mes->{pMsisdn},$message,$res->{pSessionId});
116 if ($send->{pRetCode}==200){
117 mod_sms($smsid,$message);
118 } else {
119 my $error=error_codes($send->{pErrCode});
120 mod_sms($smsid,"Not replied error:".$error);
122 return $send;
124 FINISH:
126 __END__