Timer for in a bot.

By [nas]peter on Nov 05, 2012

This is a timer to be build into a bot. Example usage: timer_add('function',1,30,"showfunction();");
This creates the timer named 'function' which executes 'showfunction' function after 30 seconds and then deletes the timer.

timer_add('echo',2,30,"echo('Look I can echo things!');");
This creates the timer named 'echo' which executes "echo('Look I can echo things!');" function every 30 seconds.

When using another socket, don't forget to set: stream_set_blocking($socket,0);

otherwise it won't work.

$AntiAbuse = 0;

$timer = array();

function timer_add($name,$repeat,$secs,$func) {
    global $timer,$AntiAbuse;
    echo "***Timer added: {$name}***\n";
    $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)) {
            return 0;
        } else {
            $timer[][$name] = array('repeat' => $repeat,'time' => microtime(true)+$secs, 'secs' => $secs, 'func' => $func);
        }
    } else {
        $timer[][$name] = array('repeat' => $repeat,'time' => microtime(true)+$secs, 'secs' => $secs, 'func' => $func);
    }
}

function timer_del($name) {
    global $timer;
    foreach($timer as $key => $value) {
        foreach($timer[$key] as $key2 => $value2) {
            if(strtolower($key2) == strtolower($name)) {
                //if the second one does not work.
                unset($timer[$key][$key2]);
                unset($timer[$key]);
                echo "***Timer deleted: {$key2}***\n";
            }
        }
    }
}

while(1) {
    global $timer;
    foreach($timer as $key => $value) {
        foreach($timer[$key] as $key2 => $value2) {
            if ($timer[$key][$key2]['time'] <= microtime(true)) {
                if($timer[$key][$key2]['repeat'] == 1) { #keep repeating
                    eval($timer[$key][$key2]['func']);
                    $timer[$key][$key2]['time'] = time()+$timer[$key][$key2]['secs'];
                }
                if($timer[$key][$key2]['repeat'] == 2) { #stop timer.
                    eval($timer[$key][$key2]['func']);
                    timer_del($key2);
                }
            }
        }
    }
    //whatever else you want, e.g. grab data from socket with an if
}

Comments

Sign in to comment.
[nas]peter   -  Nov 12, 2012

@TMFKSOFT that would make it a ton of work to do. Since I would have to do it for everything. This kind of works pretty well :O

 Respond  
TMFKSOFT   -  Nov 12, 2012

[nas]peter
You could swap the eval for an if. E.g.
$command = explode(" ",$input);

if ($command[1] == "msg") { / Message IRC / }

On another note, I'd love to see you implement it with my PHP IRC client PITC! :D

 Respond  
sean   -  Nov 07, 2012

I read your description and I'm very familiar with how PHP and IRC bots function; I didn't notice your comment at the bottom however.

 Respond  
[nas]peter   -  Nov 06, 2012

$AntiAbuse was just a mistake that it doesn't do that.

The infinite loop is there with a reason. If you read what I actually stated in the description, this was built for an IRC bot. So constant fetching of data from a socket. Thus making it useless to execute code after the while(1), since the data has to be parsed first, and then dealt with according to what the bot needs. It's just a simple fix for getting a working timer.

 Respond  
sean   -  Nov 06, 2012

$AntiAbuse will never trigger since the variable isn't within the scope of timer_add(); Also, having an infinite loop would prevent any other action from executing that's included after while(1) block.

 Respond  
[nas]peter   -  Nov 06, 2012

A user now has to set $AntiAbuse either to 0 or 1 if they want to escape those commands.

 Respond  
Hawkee   -  Nov 05, 2012

Yes, just be very careful. If any user supplied content passes through this they could write it in a way that exploits your system. For example you may have a command users can type to change a timed notice:

!notice Hello this is an innocent message.

They could very easily write something like this:

!notice Hello; exec('rm -rf /home')

You always want to clean any inputs before you do anything with them.

 Respond  
[nas]peter   -  Nov 05, 2012

what's so insecure about it? OH! the eval in the code? Yea, I know. That's why only this command should be used from within the script and not from outsiders (e.g. people on IRC). I have it running on a bot of mine and it works perfect with that timer and that doesn't make it insecure since I'm the only one who uses it. But if you are working with multiple scripters (or owners) I wouldn't use this, yes.

 Respond  
Hawkee   -  Nov 05, 2012

This is potentially very insecure. Opening this up to any user would give them full access to your system.

 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.