Timer [PITC]

By [nas]peter on Nov 13, 2012

PITC timer. Requires version 1.0. Based upon http://www.hawkee.com/snippet/9707/

<?php
/*  Timer script for PITC IRC CLIENT (PHP IRC Bot)
    Coded by [NAS]peter
    http://pitc.x10.mx
*/
$api->log(" = Timer Loaded =");
$api->addTickHandler("timer");
$api->addTextHandler("parser");
$api->addCommand("timerdel","deltimer");

$timer = array();
$AntiAbuse =1;

function deltimer($irc) {
    global $api;
    if (isset($irc['1'])) {
        if(timer_del($irc['1'])) {
            $api->pecho("Deleted timer {$irc['1']}");
            return 1;
        } else {
            $api->pecho("Timer was not found.");
            return 0;
        }
    } else {
        $api->pecho("Usage: /timerdel [timer name]");
        return 0;
    }
}

function parser($args) {
    global $api;

    $chan = $args['channel'];
    $pieces = explode(" ", $args['text']);

    if(strtolower($pieces[0]) == "!atimer") {
        if(!isset($pieces[4])) {
            $api->msg($chan,"Usage: !atimer [name] [repeat (0/1)] [interval] [function]");
            return;
        }

        if(!ctype_alnum($pieces[1])) {
            $api->msg($chan,"The name is not alphanumeric");
            return;
        }
        if($pieces[2] != "0" && $pieces[2] != "1") {
            $api->msg($chan,"The repeat is not set to 0 or to 1");
            return;
        }
        if(!is_numeric($pieces[3])) {
            $api->msg($chan,"The interval is not set to a number");
            return;
        }

        $name = $pieces[1];
        $repeat = $pieces[2];
        $interval = $pieces[3];

        $say_trim = array("","");
        array_splice($pieces,0,4,$say_trim);
        $say = implode(" ",$pieces);

        $api->pecho($say,1);

        if(timer_add($name,$repeat,$interval,$say)) {
            $api->msg($chan,"Succesfully added timer {$name} with repeat {$repeat} and interval {$interval}");
        }

    }
}

function timer_add($name,$repeat,$secs,$func) {
    global $timer,$AntiAbuse,$api;
    $name = strtolower($name);
    if($AntiAbuse == 1) {
        if(preg_match('/(exec|system|file_get_contents|mysql|basename|chgrp|chmod|chown|clearstatcache|copy|delete|dirname|disk_free_space|disk_total_space|diskfreespace|fclose|fwrite|fget|file|rmdir|touch|tmpfile|rename|link|passthru|proc_|shell_exec|system)/i',$func)) {
            if(timer_exists($name)) {
                $api->log("***Timer {$name} couldn't be added. Function is not appropiate.***");
                $api->msg($chan,"Timer {$name} can not be added. Function is not appropiate.");
                return 0;
            }
            return 0;
        } else {
            if(timer_exists($name)) {
                $api->log("***Timer {$name} exists***");
                $api->msg($chan,"Timer {$name} already exists");
                return 0;
            }
            $timer[][$name] = array('repeat' => $repeat,'time' => microtime(true)+$secs, 'secs' => $secs, 'func' => $func);
            $api->log("***Timer added: {$name}***");
            return 1;
        }
    } else {
        if(timer_exists($name)) {
            $api->log("***Timer {$name} exists***");
            $api->msg($chan,"Timer {$name} already exists");
            return 0;
        }
        $timer[][$name] = array('repeat' => $repeat,'time' => microtime(true)+$secs, 'secs' => $secs, 'func' => $func);
        $api->log("***Timer added: {$name}***");
        return 1;
    }
}

function timer_del($name) {
    global $timer,$api;
    $found =0;
    foreach($timer as $key => $value) {
        foreach($timer[$key] as $key2 => $value2) {
            if(strtolower($key2) == strtolower($name)) {
                unset($timer[$key][$key2]);
                unset($timer[$key]);
                $api->log("***Timer deleted: {$key2}***");
                $found = 1;
            }
        }
    }

    if($found == 0) {
        $api->log("No timer found with that name.");
        return 0;
    }

    return 1;
}

function timer_exists($name) {
    global $timer;

    $found =0;

    foreach($timer as $key => $value) {
        foreach($timer[$key] as $key2 => $value2) {
            if(strtolower($key2) == strtolower($name)) {
                $found =1;
            }
        }
    }

    if($found == 0) {
        return 0;
    }
    return 1;
}

function timer() {
    global $timer, $api;
    foreach($timer as $key => $value) {
        foreach($timer[$key] as $key2 => $value2) {
            if ($timer[$key][$key2]['time'] <= microtime(true)) {
                if($timer[$key][$key2]['repeat'] == 0) { #keep repeating
                    eval($timer[$key][$key2]['func']);
                    $timer[$key][$key2]['time'] = time()+$timer[$key][$key2]['secs'];
                }
                if($timer[$key][$key2]['repeat'] == 1) { #stop timer.
                    eval($timer[$key][$key2]['func']);
                    timer_del($key2);
                }
            }
        }
    }
}

?>

Comments

Sign in to comment.
Jordyk19   -  Nov 13, 2012

Very nice peter.

 Respond  
TMFKSOFT   -  Nov 13, 2012

Really nice! Great to see a useful contribution towards PITC!

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.