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