<?php
/**************************************************
* NZBirc v1
* Copyright (c) 2006 Harry Bragg
* tiberious.org
* Module: func
**************************************************
*
* Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
class Net_SmartIRC_module_func
{
// default module variable
var $name = 'func';
var $version = 'v0.1';
var $description = 'general functions for the irc bot';
var $author = 'Harry \'tiberious\' Bragg www.tiberious.org';
var $license = 'GPL';
var $actionids = array();
var $_def = array(
'regex' => array(
'dateDur' => array(
'timeSplit' => '/(?:from\s)?(.+)(\s+(-|to|by|for)\s+)(.+)/i',
'date' => '/(?:date:)?((?:\d+)(?:th|nd|st)?\s(?:\w+)\s(?:\d+)|(?:\d+)(\/|:|.|-)(?:\d+)(?:\2)(?:\d+)|(?:last|next)?\s*(?:monday|tuesday|wednesday|thursday|friday|saturday|sunday|yesterday|today|tomorrow))/i',
'period' => '/(-?\d+)\s?(w|d)/i'
)
)
);
function module_init( &$irc )
{
}
function module_exit( &$irc )
{
}
/*****************************************************
* Date/Time functions
*****************************************************/
/**
* Get the duration in days
*
* @param int $t - time span to get duration of
* @return string - duration in years, weeks and days
* @access public
*/
function dduration($t)
{
$r = $this->durs($t,31449600,'y ').$this->durs($t,604800,'w ').$this->durs($t,86400,'d ');
return (strlen($r) > 0)? $r:'0d ';
}
/**
* Get the duration
*
* @param int $t - time span to get duration of
* @return string - duration in years, weeks, days, hours and minutes
* @access public
*/
function duration($t,$s = false)
{
$r = $this->durs($t,31449600,'y ').$this->durs($t,604800,'w ').$this->durs($t,86400,'d ').
$this->durs($t,3600,'h ').$this->durs($t,60,'m ').(($s)? $this->durs($t,1,'s'):'');
return (strlen($r) > 0)? $r:'0m ';
}
/**
* Duration helper function
*/
function durs(&$t,$i,$n)
{
$x = floor($t / $i);
$t = $t % $i;
if ($x > 0) return $x.$n;
}
/**
* Creates a human readable date from a UNIX TIMESTAMP
*
* @param int $t - input UNIX TIMESTAMP
* @param int $n - time to reference $t from, default now()
* @return string - Nicely formatted date
* @access public
*/
function nicedate($t,$n = false)
{
if (!$n) { $n = time(); }
if ($t == 0)
return 'Unknown';
if ( $t > $n )
$r = ' ('.substr( $this->duration( $t - $n ), 0, -1).')';
else
$r = ' ('.substr( $this->duration( $n - $t ), 0, -1).' ago)';
if (date('d/m/y',$n) == date('d/m/y',$t))
return date('h:ia',$t).$r;
else if ((date('d',$t) == date('d',$n)+1) && ($t < $n+172800) && ($t > $n))
return 'Tommorow '.date('h:ia',$t).$r;
else if ((date('d',$t) == date('d',$n)-1) && ($t > $n-259200) && ($t < $n))
return 'Yesterday '.date('h:ia',$t).$r;
else if (($t < $n+518400) and ($t > $n))
return date('l h:ia',$t).$r;
else if (date('y',$n) == date('y',$t))
return date('M jS h:ia',$t).$r;
else
return date('M jS Y h:ia',$t).$r;
}
function nicedur($t,$n = false)
{
if (!$n) { $n = time(); }
if ( $t >= $n )
return substr( $this->duration( $t - $n ), 0, -1);
else
return substr( $this->duration( $n - $t ), 0, -1).' ago';
}
function snicedur($t,$n = false)
{
if (!$n) { $n = time(); }
$t = mktime(0,0,0,date('m',$t),date('d',$t),date('y',$t));
$n = mktime(0,0,0,date('m',$n),date('d',$n),date('y',$n));
if ( $t >= $n )
return substr( $this->dduration( $t - $n ), 0, -1);
else
return substr( $this->dduration( $n - $t ), 0, -1).' ago';
}
/**
* Creates a human readable date from a UNIX TIMESTAMP (without the hours/minutes)
*
* @param int $t - input UNIX TIMESTAMP
* @param int $n - time to reference $t from, default now()
* @return string - Nicely formatted date
* @access public
*/
function snicedate($t,$n = false)
{
if (!$n) { $n = mktime(0,0,0); }
if ($t == 0) {
return 'Unknown';
}
$t = mktime(0,0,0,date('m',$t),date('d',$t),date('y',$t));
if ( $t > $n )
$r = ' ('.substr( $this->dduration( $t - $n ), 0, -1).')';
else
$r = ' ('.substr( $this->dduration( $n - $t ), 0, -1).' ago)';
if (date('d/m/y',$n) == date('d/m/y',$t))
return 'Today';
else if ((date('d',$t) == date('d',$n)+1) && ($t < $n+172800) && ($t > $n))
return 'Tommorow';
else if ((date('d',$t) == date('d',$n)-1) && ($t > $n-259200) && ($t < $n))
return 'Yesterday';
else if (($t < $n+518400) and ($t > $n))
return date('l',$t).$r;
else if (date('y',$n) == date('y',$t))
return date('jS M',$t).$r;
else
return date('jS M Y',$t).$r;
}
/**
* Date to Day
*
* @var int $date - UNIX TIMESTAMP of the date
* @return int - the number of seconds from 1970 to $date 00:00
* @access public
*/
function dateToDay( $date )
{
$tdate = mktime(0,0,0,date('m',$date),date('d',$date),date('y',$date));
return $tdate;
}
/**
* Date & Duration parse
*
* @var string $text - Input message
* @return array - pair, or 1 date containing the information in the string, and the remaining string
* @access public
*/
function dateDurationParse( $text )
{
$string = $text;
$clean = $text;
$num = array( 'y' => 31449600, 'w' => 604800, 'd' => 86400, 'h' => 3600, 'm' => 60, 's' => 1 );
// check for a split
if ( preg_match( $this->_def['regex']['dateDur']['timeSplit'], $text, $sMatch ) )
{
$string = $sMatch[1];
$clean = str_replace( $sMatch[2], '', $clean );
}
if ( preg_match( $this->_def['regex']['dateDur']['date'], $string, $match ) )
{
$clean = str_replace( $match[0], '', $clean );
$date = strtotime( $match[1] );
}
else if ( preg_match_all( $this->_def['regex']['dateDur']['period'], $string, $matches ) > 0 )
{
if ( preg_match( '/ago$/i', $string ) ) {
$mul = -1;
} else {
$mul = 1;
}
for( $i=0; $i < count($matches[0]); $i++ ) {
$clean = str_replace( $matches[0][$i], '', $clean );
$time += $matches[1][$i] * $num[$matches[2][$i]] * $mul;
}
$date = time() + $time;
}
else
{
$date = 0;
}
$date2 = -1;
if ( isset( $sMatch[4] ) )
{
// check for a date
if ( preg_match( $this->_def['regex']['dateDur']['date'], $sMatch[4], $match ) ) {
$clean = str_replace( $match[0], '', $clean );
$date2 = strtotime( $match[1] );
// dur search
} else if ( preg_match_all( $this->_def['regex']['dateDur']['period'], $sMatch[4], $matches ) > 0 ) {
if ( preg_match( '/ago$/i', $sMatch[4] ) ) {
$mul = -1;
} else {
$mul = 1;
}
for( $i=0; $i < count($matches[0]); $i++ ) {
$clean = str_replace( $matches[0][$i], '', $clean );
$time2 += $matches[1][$i] * $num[$matches[2][$i]] * $mul;
}
if ( $sMatch[3] == 'for' )
$date2 = $date + $time2;
else
$date2 = time() + $time2;
}
}
$clean = trim( $clean );
return array( $date, $date2, $clean );
}
/*****************************************************
* File size functions
*****************************************************/
/**
* Returns a human readable size
*
* @var int size - Size to change the format of
* @var int iStart - Where to start counting from (ie the format of the input size)
* @return string - Formatted size with postfix
* @access public
*/
function humanSize( $size, $iStart = 0 )
{
$size = str_replace( ',', '', $size );
$format = array('b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb' );
for ( $j = $iStart; ( ( $size / 1024 ) > 1 ) && $j < 8; $j++ )
{
$size = $size / 1024;
}
$decimals = ( ( $size < 100 )? 2:
( ( $size < 1000 )? 1:0 ) );
return number_format( $size, $decimals ).' '.$format[$j];
}
/*****************************************************
* Bot functions
*****************************************************/
function getTData( $data )
{
return (object)array(
'type' => $data->type,
'nick' => $data->nick,
'channel' => $data->channel,
'notice' => $data->notice,
'hellaMod' => $data->hellaMod
);
}
/**
* reply to a message
*
* @response void
* @param object &$data - Contains the information retrieved
* @param array $msg - Message to send
* @param bool $notice - To notice or not
*/
function reply( &$irc, &$data, $msg, $notice = false, $priority = SMARTIRC_MEDIUM )
{
if ( ( !$notice ) && ( isset( $data->notice ) ) )
{
$notice = $data->notice;
}
$how = ( $notice )? SMARTIRC_TYPE_NOTICE:SMARTIRC_TYPE_CHANNEL;
$to = ( $data->type == SMARTIRC_TYPE_QUERY || $notice )? $data->nick:$data->channel;
$irc->message( $how, $to, $msg, $priority );
}
/**
* Check if a user is an admin
*
* @param resource $irc - Big scary object
* @param string $nick - User name
* @return bool
* @access public
*/
function isAdmin( &$irc, $nick )
{
if ( is_array( $irc->config->admin['nick'] ) ) {
if ( in_array( $nick, $irc->config->admin['nick'] ) ) {
return true;
} else {
return false;
}
} else {
if ( $nick == $irc->config->admin['nick'] ) {
return true;
} else {
return false;
}
}
}
/**
* Checks to see if the bot is in admin only mode
*
* @param resource $irc
* @param string $nick - Users name
* @return bool
* @access public
*/
function hasAccess( &$irc, $nick )
{
if ( ( $irc->config->admin['only'] == true ) &&
( !$this->isAdmin( $irc, $nick ) ) ) {
return false;
} else {
return true;
}
}
/*****************************************************
* Bot functions
*****************************************************/
function array_append( &$a1, $a2 )
{
for ( $i = 0; $i < count( $a2 ); $i++ ) {
$a1[] = $a2[$i];
}
}
function array_merge( &$a1, $a2 )
{
foreach( $a2 as $k => $v )
{
$a1[$k] = $v;
}
}
}
?>