5<?php15                                                                           
12namespace15 knivey5\15lolbot5\15scripts5\15remindme5;15                                       
5/*15                                                                              
5 * script for our .tell nick blah blah and .ask nick blah blah commands15         
5 */15                                                                             
12use15 knivey5\15cmdr5\15attributes5\15CallWrap5;15                                            
12use15 knivey5\15cmdr5\15attributes5\15Cmd5;15                                                 
12use15 knivey5\15cmdr5\15attributes5\15Syntax5;15                                              
12use15 Khill5\15Duration5\15Duration5;15                                                    
12use15 5\15RedBeanPHP5\15R 12as15 R5;15                                                         
12global15 3$config5;15                                                                 
12const15 REMINDERDB 5=15 5"reminderdb";15                                                
3$dbfile15 5=15 3$config5[15REMINDERDB5]15 5??15 5"reminder.db";15                                 
R5::14addDatabase5(15REMINDERDB5,15 5"sqlite:{$dbfile}");15                                 
5#[Cmd("in", "remindme")]15                                                        
5#[Syntax("<time> <msg>...")]15                                                    
12function15 14in5(3$args5,15 5\15Irc5\15Client 3$bot5,15 5\15knivey5\15cmdr5\15Request 3$req5)15 5{15               
    5\15Amp5\14asyncCall5(12function15 5()15 12use15 5(3$args5,15 3$bot5,15 3$req5)15 5{15                        
        3$duration15 5=15 12new15 14Duration5(3$req5->15args5['time']);15                           
        5//Because the Duration lib is PECULIAR it resets its value to 0 after do
5ing outputs15                                                                     
        5//And also lingers data in the output, really i need to find another lib
        3$in15 5=15 3$duration5->14toSeconds5();15                                           
        12if5(3$in15 5<15 6155)15 5{15                                                          
            3$bot5->14pm5(3$args5->15chan5,15 5"Give me a proper duration of at least 15 seco
5nds with no spaces (Ex: 1h10m15s or 1:10:15)");15                                 
            12return5;15                                                             
        5}15                                                                       
        R5::14selectDatabase5(15REMINDERDB5);15                                          
        3$r15 5=15 R5::14dispense5("reminder");15                                           
        3$r5->15nick 5=15 3$args5->15nick5;15                                                 
        3$r5->15chan 5=15 3$args5->15chan5;15                                                 
        3$r5->15at 5=15 14time5()15 5+15 3$in5;15                                                  
        3$r5->15sent 5=15 605;15                                                           
        3$r5->15msg 5=15 3$req5->15args5['msg'];15                                            
        R5::14store5(3$r5);15                                                           
        3$bot5->14pm5(3$args5->15chan5,15 5"Ok, I'll remind you in "15 5.15 5(12new15 14Duration5(3$in5))->14h
14umanize5());15                                                                     
        yield from 14sendDelayed5(3$bot5,15 3$r5,15 3$in5);15                                  
    5});15                                                                         
5}15                                                                               
5/* 0TODO5, this actually needs to parse more than one word(arg) for the timestamp 
5so its a bit complicated to do15                                                  
5#[Cmd("at")]15                                                                    
5#[Syntax("<time> <msg>...")]15                                                    
5function at($args, \Irc\Client $bot, \knivey\cmdr\Request $req) {15               
5    try {15                                                                       
5        $dt = new \DateTime($req->args['time']);15                                
5    } catch (\Exception $e) {15                                                   
5        $bot->pm($args->chan, "That date time doesnt seem valid");15              
5        return;15                                                                 
5    }15                                                                           
5    if($dt->getTimestamp() <= time() + 15) {15                                    
5        $bot->pm($args->chan, "Give me a time at least 15 seconds from now");15   
5        return;15                                                                 
5    }15                                                                           
5    $in = $dt->getTimestamp() - time();15                                         
5    $duration = new Duration($in);15                                              
5    R::selectDatabase(REMINDERDB);15                                              
5    $r = R::dispense("reminder");15                                               
5    $r->nick = $args->nick;15                                                     
5    $r->chan = $args->chan;15                                                     
5    $r->at = $dt->getTimestamp();15                                               
5    $r->sent = 0;15                                                               
5    $r->msg = $req->args['msg'];15                                                
5    R::store($r);15                                                               
5    $bot->pm($args->chan, "Ok, I'll remind you in " . $duration->humanize());15   
5    sendDelayed($bot, $r, $in);15                                                 
5}15                                                                               
5*/15                                                                              
12function15 14sendDelayed5(\15Irc5\15Client 3$bot5,15 3$r5,15 3$seconds5)15 5{15                          
    yield 5\15Amp5\14delay5(3$seconds15 5*15 610005);15                                          
    5//if bot somehow isnt connected keep retrying15                               
    12while15 5(!3$bot5->14isEstablished5())15 5{15                                            
        yield 5\15Amp5\14delay5(6100005);15                                                
    5}15                                                                           
    3$bot5->14pm5(3$r5->15chan5,15 5"[REMINDER: {$r->nick}] {$r->msg}");15                     
    3$r5->15sent 5=15 615;15                                                               
    R5::14selectDatabase5(15REMINDERDB5);15                                              
    R5::14store5(3$r5);15                                                               
5}15                                                                               
12function15 14initRemindme5(3$bot5)15 5{15                                                   
    12static15 3$inited15 5=15 12false5;15                                                     
    12if5(3$inited5)15                                                                 
        12return5;15                                                                 
    3$inited15 5=15 12true5;15                                                             
    5\15Amp5\14asyncCall5(12function15 5()15 12use15 5(3$bot5)15 5{15                                     
        5//A bit of a hack here so we give the bot time to join channels etc15     
        yield 5\15Amp5\14delay5(650005);15                                                 
        R5::14selectDatabase5(15REMINDERDB5);15                                          
        5//load our reminders from db and call sendDelayed on all15                
        3$rs15 5=15 R5::14findAll5("reminder",15 5" `sent` = 0 ");15                           
        12foreach15 5(3$rs15 12as15 3$r5)15 5{15                                                   
            5//whoops already passed while bot was down15                          
            12if15 5(3$r5->15at 5<=15 14time5())15 5{15                                             
                12while15 5(!3$bot5->14isEstablished5())15 5{15                                
                    yield 5\15Amp5\14delay5(6100005);15                                    
                5}15                                                               
                3$ago15 5=15 5(12new15 14Duration5(14time5()15 5-15 3$r5->15at5))->14humanize5();15             
                3$bot5->14pm5(3$r5->15chan5,15 5"[REMINDER: {$r->nick} (late by $ago)] {$r->m
5sg}");15                                                                          
                3$r5->15sent 5=15 615;15                                                   
                R5::14store5(3$r5);15                                                   
            5}15 12else15 5{15                                                            
                yield from 14sendDelayed5(3$bot5,15 3$r5,15 3$r5->15at 5-15 14time5());15              
            5}15                                                                   
        5}15                                                                       
    5});15                                                                         
5}15                                                                               

