phpDocumentor phpIcqBot
[ class tree: phpIcqBot ] [ index: phpIcqBot ] [ all elements ]

Source for file time.module.php

Documentation is available at time.module.php

  1. <?php
  2. /**
  3.  * phpIcqBot Time & Uptime Module
  4.  * Usage:
  5.  * !uptime
  6.  * !time
  7.  * 
  8.  * @author Jector <no.one.on@gmail.com>
  9.  * @version 0.2
  10.  * @package phpIcqBot
  11.  * @todo number of connections, restarts etc in !uptime
  12.  */
  13.  
  14.  
  15. {
  16.     /**
  17.      * Time, when bot was started. (time()-$this->uptime) -- current uptime;
  18.      *
  19.      * @var integer 
  20.      */
  21.     private $uptime;
  22.     
  23.     /**
  24.      * Store last uptime update time
  25.      *
  26.      * @var integer 
  27.      */
  28.     private $lastuptime;
  29.     
  30.     /**
  31.      * Store uptime statistic
  32.      *
  33.      * @var array 
  34.      */
  35.     private $stats;
  36.     
  37.     /**
  38.      * Standart module's init function
  39.      * Adding commands, events, timers
  40.      */
  41.     public function init()
  42.     {
  43.         $this->uptime $this->lastuptime time();
  44.         $this->stats $this->commands->store->getData($this'stats');
  45.         if (!$this->stats)
  46.         {
  47.             $this->stats['restart']=$this->stats['connect']=$this->stats['disconnect']=$this->stats['commands']=$this->stats['uptime']=0;
  48.         }
  49.         
  50.         $this->commands->addCommand($this'uptime'array('u'));
  51.  
  52.         $this->commands->addCommand($this'time');
  53.         
  54.         $this->commands->addEvent($thisICQBOT_EVENT_RESTART);
  55.         $this->commands->addEvent($thisICQBOT_EVENT_CONNECT);
  56.         $this->commands->addEvent($thisICQBOT_EVENT_DISCONNECT);
  57.         $this->commands->addEvent($thisICQBOT_EVENT_COMMAND);
  58.         
  59.         $this->commands->addTimer($this"uptime",60,"Uptime");
  60.     }
  61.     
  62.     /**
  63.      * Standart module's deinit function
  64.      * Saving uptime stats
  65.      */
  66.     public function deinit()
  67.     {
  68.         $this->stats['uptime']+=(time()-$this->lastuptime);
  69.         $this->logs->write('UPTIME: '.$this->diff2time(time()-$this->uptime).' seconds.');
  70.         $this->commands->store->setData($this'stats'$this->stats);
  71.     }
  72.  
  73.     /**
  74.      * Replies current bot's uptime
  75.      *
  76.      * @param unknown_type $uin 
  77.      */
  78.     public function Command_Uptime($uin)
  79.     {
  80.         $this->commands->sendMsg($uin"Bot is working  ".$this->diff2time(time()-$this->uptime)." starting from ".date('r',$this->uptime)."\r\nOverall uptime is: ".$this->diff2time($this->stats['uptime']));
  81.         $this->stats['uptime']+=(time()-$this->lastuptime);
  82.         $this->lastuptime=time();
  83.     }
  84.     
  85.     /**
  86.      * Replies current time
  87.      *
  88.      * @param integer $uin 
  89.      */
  90.     public function Command_Time($uin)
  91.     {
  92.         $this->commands->sendMsg($uin,"Current time: ".date('r'));
  93.     }
  94.     
  95.     public function Help_Time()
  96.     {
  97.         return 'Shows current time';
  98.     }
  99.     
  100.     /**
  101.      * Counts Restart Events
  102.      *
  103.      */
  104.     public function Event_Restart()
  105.     {
  106.         $this->stats['restart']++;
  107.     }
  108.     
  109.     /**
  110.      * Counts Command Events
  111.      *
  112.      */
  113.     public function Event_Command()
  114.     {
  115.         $this->stats['commands']++;
  116.     }
  117.     
  118.     /**
  119.      * Counts Connect Events
  120.      *
  121.      */
  122.     public function Event_Connect()
  123.     {
  124.         $this->stats['connect']++;
  125.     }
  126.     
  127.     /**
  128.      * Counts Disconnect Events
  129.      *
  130.      */
  131.     public function Event_Disconnect()
  132.     {
  133.         $this->stats['disconnect']++;
  134.     }
  135.     
  136.     /**
  137.      * Update Uptime information every minute
  138.      *
  139.      */
  140.     public function Timer_Uptime()
  141.     {
  142.         $this->stats['uptime']+=(time()-$this->lastuptime);
  143.         $this->lastuptime=time();
  144.     }
  145.     
  146.     /**
  147.      * Counts diff time in seconds to diff time in days, hours, minutes, seconds
  148.      *
  149.      * @param integer $timestamp 
  150.      * @return string 
  151.      */
  152.     private function diff2time($timestamp)
  153.     {
  154.         $sec=array("sec"=>60,"min"=>60,"hrs"=>24,"days"=>30);
  155.         $r '';
  156.         foreach ($sec as $k=>$v)
  157.         {
  158.             $r ($timestamp $v).' '.$k.'. '.$r;
  159.                 $timestamp floor($timestamp $v);
  160.             if ($timestamp <= 0break;
  161.         }
  162.         return $r;
  163.     }
  164. }

Documentation generated on Mon, 24 Dec 2007 09:34:35 +0500 by phpDocumentor 1.4.0