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

Source for file main.module.php

Documentation is available at main.module.php

  1. <?php
  2. /**
  3.  * phpIcqBot Main Module
  4.  * 
  5.  * Main (Admin) Module is used for controlling bot.
  6.  * Now available commands:
  7.  * - !mod - for controlling modules
  8.  * - !roles - for controlling users
  9.  * - !stop - the exit command for bot
  10.  * - !restart - restart command (tries to load new modules)
  11.  *
  12.  * @author Jector <no.one.on@gmail.com>
  13.  * @version 0.2
  14.  * @package phpIcqBot
  15.  * @todo a lot of things
  16.  */
  17.  
  18.  
  19. {
  20.  
  21.     /**
  22.      * Standart initialization function
  23.      */
  24.     public function init()
  25.     {
  26.         $this->commands->addCommand($this'about');
  27.         $this->commands->addCommand($this'restart');
  28.         $this->commands->addCommand($this'exit',   array('die','bye','quit','stop','выход'));
  29.         $this->commands->addCommand($this'help',   array('h'));
  30.  
  31.         $this->commands->addCommand($this'roles');
  32.         
  33.         $this->commands->addCommand($this'mod',    array('module','modules'));
  34.     }
  35.     
  36.     /**
  37.      * Standart deinitialization function
  38.      */
  39.     public function deinit()
  40.     {
  41.     }
  42.     
  43.     /**
  44.      * About Command
  45.      *
  46.      * Returns information about phpIcqBot like version, author, etc
  47.      *
  48.      * @param integer $uin 
  49.      */
  50.     public function Command_About($uin)
  51.     {
  52.         $this->commands->sendMsg($uinVER."\r\nAuthor: NoOne <jector@inbox.ru>\r\nSpecial thanks for hosting: schAlexey");
  53.     }
  54.     
  55.     /**
  56.      * Exit command.
  57.      * 
  58.      * Terminate working of phpIcqBot.
  59.      * It's not really stop it here, but just set WORKING flag to false.
  60.      *
  61.      * @see IcqBot::work()
  62.      * @param integer $uin 
  63.      */
  64.     public function Command_Exit($uin)
  65.     {
  66.         if (!IcqBot_Users::load($uin)->canRoles('exit'))
  67.         {
  68.             $this->commands->sendMsg($uin'Access denied.');
  69.             return;
  70.         }
  71.         $this->logs->write('EXIT command from '.$uin);
  72.         $this->icqbot->working false;
  73.     }
  74.     
  75.     /**
  76.      * Restart command.
  77.      * 
  78.      * Do restart of phpIcqBot.
  79.      * It's not really restart it here, but just set RESTART flag to true.
  80.      * @see IcqBot::work()
  81.      * @param integer $uin 
  82.      */
  83.     public function Command_Restart($uin)
  84.     {
  85.         if (!IcqBot_Users::load($uin)->canRoles('restart'))
  86.         {
  87.             $this->commands->sendMsg($uin'Access denied.');
  88.             return;
  89.         }
  90.         
  91.         $this->logs->write('RESTART command from '.$uin);
  92.         $this->commands->sendMsg($uin"Restarting...");
  93.         $this->icqbot->restart();
  94.     }
  95.     
  96.     /**
  97.      * Help command.
  98.      * 
  99.      * Supports help system of phpIcqBot.
  100.      *
  101.      * @todo A lot of changes will be here
  102.      * @param integer $uin 
  103.      * @param string $params 
  104.      * @return mixed 
  105.      */
  106.     public function Command_Help($uin$params)
  107.     {
  108.         if ($params == '')
  109.         {
  110.             /**
  111.              * @todo do some thing, if no params specified
  112.              */
  113.             return true;
  114.         }
  115.         
  116.         if (strpos($params,' ')>0)
  117.         {
  118.             list($cmd$paramsexplode(' '$params2);
  119.         }
  120.         else
  121.         {
  122.             $cmd $params;
  123.             $params '';
  124.         }
  125.         
  126.         
  127.         if (!isset($this->conf->commands[$cmd]))
  128.         {
  129.             return $this->commands->sendMsg($uin'Command "'.$cmd.'" doesn\'t exist');
  130.         }
  131.  
  132.         $command $this->conf->commands[$cmd];
  133.         $cmd ucfirst(strtolower($cmd));
  134.         if (!is_callable(array($command[0]'Help_'.$cmd)))
  135.         {
  136.             return $this->commands->sendMsg($uin'Command "'.$cmd.'" doesn\'t have help');
  137.         }
  138.         $help call_user_func_array(array($command[0]'Help_'.$cmd)array($params));
  139.         if ($help)
  140.         {
  141.             $this->commands->sendMsg($uin'Command "'.$cmd."\":\r\n".$help);
  142.         }
  143.         return true;
  144.     }
  145.     
  146.     /**
  147.      * Mod command.
  148.      * 
  149.      * Command, which controls phpIcqBot modules
  150.      * Available params:
  151.      *  - !mod list
  152.      *  - !mod cmdlist <modulename|modulenum>
  153.      *  - !mod info <modulename|modulenum>
  154.      *
  155.      * @todo params for load/unload modules
  156.      * @param integer $uin 
  157.      * @param string $params 
  158.      */
  159.     public function Command_Mod($uin$params)
  160.     {
  161.         $msg '';
  162.         
  163.         if ($params == '')
  164.         {
  165.             $params 'list';
  166.         }
  167.         
  168.         if (strpos($params,' ')>0)
  169.         {
  170.             list($cmd$paramsexplode(' '$params2);
  171.         }
  172.         else
  173.         {
  174.             $cmd $params;
  175.             $params '';
  176.         }
  177.         
  178.         
  179.         switch($cmd)
  180.         {
  181.             case 'info':
  182.                 if ($params=='')
  183.                 {
  184.                     $msg 'Please, specify module\'s name of number from !mod list';
  185.                     break;
  186.                 }
  187.                 $params=ucfirst(strtolower(trim($params)));
  188.                 $i=1;
  189.                 if (!is_numeric($params))
  190.                 {
  191.                     $params='IcqBot_Module_'.$params;
  192.                 }
  193.                 foreach ($this->conf->modules as $modname=>$mod)
  194.                 {
  195.                     if (($i++ == $params|| ($modname == $params))
  196.                     {
  197.                         $msg $modname."\r\n";
  198.                         $msg.= "Filename: modules/".str_replace("IcqBot_Module_","",$modname).".module.php\r\n";
  199.                         $msg.= "Commands: ".$mod['Commands']."\r\nAliases: ".$mod['Aliases'];
  200.                         break;
  201.                     }
  202.                 }
  203.                 if (!$msg)
  204.                 {
  205.                     $msg 'Module "'.$params.'" not found';
  206.                 }
  207.                 break;
  208.                 
  209.             case 'cmdlist':
  210.                 if ($params=='')
  211.                 {
  212.                     $msg 'Please, specify module\'s name of number from !mod list';
  213.                     break;
  214.                 }
  215.                 $params=ucfirst(strtolower(trim($params)));
  216.                 $i=1;
  217.                 if (!is_numeric($params))
  218.                 {
  219.                     $params='IcqBot_Module_'.$params;
  220.                 }
  221.                 $module null;
  222.                 foreach ($this->conf->modules as $modname=>$mod)
  223.                 {
  224.                     if (($i++ == $params|| ($modname == $params))
  225.                     {
  226.                         $module $mod;
  227.                         break;
  228.                     }
  229.                 }    
  230.                 if (!$module)
  231.                 {
  232.                     $msg 'Module "'.$params.'" not found';
  233.                     break;
  234.                 }
  235.                 $cmds array();
  236.                 $aliases array();
  237.                 $cls_name get_class($module['iface']);
  238.                 foreach($this->conf->commands as $cmdname=>$command)
  239.                 {
  240.                     if (get_class($command[0]== $cls_name)
  241.                     {
  242.                         if ('Command_'.ucfirst(strtolower($cmdname))==$command[1])
  243.                         {
  244.                             $cmds[]=$cmdname;
  245.                         }
  246.                         else
  247.                         {
  248.                             $aliases[str_replace('command_','',strtolower($command[1]))][]=$cmdname;
  249.                         }
  250.                     }
  251.                 }
  252.  
  253.                 $msg 'Commands of module "'.$modname.'":'."\r\n";
  254.                 sort($cmds,SORT_STRING);
  255.                 foreach ($cmds as $cmdname)
  256.                 {
  257.                     $msg.=CMD_PREF.$cmdname.' (help: '.
  258.                     (is_callable(array($module['iface'],'Help_'.ucfirst(strtolower($cmdname))))?'yes':'no').')';
  259.                     if (isset($aliases[$cmdname]))
  260.                     {
  261.                         sort($aliases[$cmdname],SORT_STRING);
  262.                         $msg.=' (aliases: '.implode(', ',$aliases[$cmdname]).')';
  263.                         unset($aliases[$cmdname]);
  264.                     }
  265.                     $msg.="\r\n";
  266.                 }
  267.  
  268.                 /**
  269.                  * @todo Parenless aliases in !mod cmslist 
  270.                  */
  271.                 
  272.                 
  273.                 break;
  274.  
  275.                 
  276.             case 'load':
  277.                 
  278.                 $modulesList glob('./modules/*.module.php');
  279.                 $included_files get_included_files();
  280.                 foreach ($included_files as $n=>$f)
  281.                 {
  282.                     $included_files[$n]=basename($f);
  283.                 }
  284.  
  285.                 if (($params == ''|| ($params == 'list'))
  286.                 {
  287.                     $m false;
  288.                     foreach ($modulesList as $moduleName)
  289.                     {
  290.                         if (!preg_match('/([a-z0-9_]+)\.module\.php$/i',$moduleName,$m))
  291.                         {
  292.                             continue;
  293.                         }
  294.                         $mod_name 'IcqBot_Module_'.ucfirst(strtolower($m[1]));
  295.                         if (key_exists($mod_name$this->conf->modules))
  296.                         {
  297.                             continue;
  298.                         }
  299.                         $msg .= $mod_name."\r\n";
  300.                     }
  301.                     
  302.                     if ($msg == '')
  303.                     {
  304.                         $msg 'There are no available modules to load. All of them are already loaded.';
  305.                     }
  306.                     else 
  307.                     {
  308.                         $msg 'Modules available for loading:'."\r\n" $msg;
  309.                     }
  310.                 }
  311.                 elseif ($params == 'all')
  312.                 {
  313.                     $cnt 0;
  314.                     foreach ($modulesList as $moduleName)
  315.                     {
  316.                         $result $this->icqbot->loadModule($moduleName$included_files);
  317.                         if ($result === true)
  318.                         {
  319.                             $msg .= $moduleName." loaded\r\n";
  320.                             $cnt++;
  321.                         }
  322.                         elseif ($result === false)
  323.                         {
  324.                             $msg .= "Wrong filename$moduleName\r\n";
  325.                         }
  326.                         else
  327.                         {
  328.                             $msg .= $moduleName.' - '.$result."\r\n";
  329.                         }
  330.                     }
  331.                     
  332.                     if ($cnt>0)
  333.                     {
  334.                         $msg .= 'Modules loaded: '.$cnt;
  335.                     }
  336.                     
  337.                     if ($msg == '')
  338.                     {
  339.                         $msg 'There are no available modules to load. All of them are already loaded.';
  340.                     }
  341.                 }
  342.                 else
  343.                 {
  344.                     $m '';
  345.                     if (strpos($params'.php')>0)
  346.                     {
  347.                         $moduleName $params;
  348.                     }
  349.                     elseif (preg_match("/^IcqBot_Module_([A-Za-z0-9_]+)$/"$params$m))
  350.                     {
  351.                         $moduleName strtolower($m[1]).'.module.php';
  352.                     }
  353.                     else
  354.                     {
  355.                         $moduleName strtolower($params).'.module.php';
  356.                     }
  357.                     
  358.                     $result $this->icqbot->loadModule($moduleName,$included_files);
  359.                     if ($result === true)
  360.                     {
  361.                         $msg $moduleName." loaded\r\n";
  362.                     }
  363.                     elseif ($result === false)
  364.                     {
  365.                         $msg "Wrong filename$moduleName\r\n";
  366.                     }
  367.                     else
  368.                     {
  369.                         $msg $moduleName.' - '.$result."\r\n";
  370.                     }
  371.                     break;
  372.                 }
  373.                 
  374.                 break;
  375.                 
  376.             case 'unloadforce':                
  377.             case 'unload':
  378.                 if (($params == ''|| ($params == 'list'))
  379.                 {
  380.                     foreach ($this->conf->modules as $mod_name=>$mod)
  381.                     {
  382.                         if (is_subclass_of($mod['iface']"IcqBot_ModuleAdmin"))
  383.                         {
  384.                             $mod_name.='*';
  385.                         }
  386.                         $msg .= $mod_name."\r\n";
  387.                     }
  388.                     
  389.                     if ($msg != '')
  390.                     {
  391.                         $msg 'Available modules to unload: '."\r\n".$msg;
  392.                     }
  393.                     else
  394.                     {
  395.                         $msg 'There are no available modules to unload.';
  396.                     }
  397.                 }
  398.                 elseif ($params == 'all')
  399.                 {
  400.                     $keys array_keys($this->conf->modules);
  401.                     foreach ($keys as $mod_name)
  402.                     {
  403.                         if (is_subclass_of($this->conf->modules[$mod_name]['iface']"IcqBot_ModuleAdmin"))
  404.                         {
  405.                             if ($cmd != 'unloadforce')
  406.                             {
  407.                                 $msg.=$mod_name.": cannot be unloaded. Use \"unloadforce\" instead.\r\n";
  408.                                 continue;
  409.                             }
  410.                         }
  411.                         $this->logs->write('Unloading '.$mod_name);
  412.                         $this->conf->modules[$mod_name]['iface']->deinit();
  413.                         $this->commands->removeAll($this->conf->modules[$mod_name]['iface']);
  414.                         unset($this->conf->modules[$mod_name]['iface']);
  415.                         unset($this->conf->modules[$mod_name]);
  416.                         $msg .= $mod_name.": unloaded\r\n";        
  417.                     }
  418.                     
  419.                     if ($msg == '')
  420.                     {
  421.                         $msg 'There are no available modules to unload.';
  422.                     }
  423.                     else 
  424.                     {
  425.                         $msg 'Unloaded modules:'."\r\n" $msg;
  426.                     }
  427.                 }
  428.                 else
  429.                 {
  430.                     $m '';
  431.                     if (preg_match('/([a-z0-9_]+)\.module\.php$/i',$params,$m))
  432.                     {
  433.                         $mod_name 'IcqBot_Module_'.ucfirst(strtolower($m[1]));
  434.                     }
  435.                     elseif (preg_match("/^IcqBot_Module_([A-Za-z0-9_]+)$/"$params$m))
  436.                     {
  437.                         $mod_name $params;
  438.                     }
  439.                     else
  440.                     {
  441.                         $mod_name 'IcqBot_Module_'.ucfirst(strtolower($params));
  442.                     }
  443.                     
  444.                     if (!class_exists($mod_name))
  445.                     {
  446.                         $msg "Module $mod_name doesn't exists.";
  447.                         break;
  448.                     }
  449.                     
  450.                     if (!key_exists($mod_name,$this->conf->modules))
  451.                     {
  452.                         $msg "Module $mod_name isn't loaded.";
  453.                         break;
  454.                     }
  455.                     
  456.                     if (is_subclass_of($this->conf->modules[$mod_name]['iface']"IcqBot_ModuleAdmin"))
  457.                     {
  458.                         if ($cmd != 'unloadforce')
  459.                         {
  460.                             $msg =$mod_name.": cannot be unloaded. Use \"unloadforce\" instead.\r\n";
  461.                             break;
  462.                         }
  463.                     }
  464.                     
  465.                     $this->logs->write('Unloading '.$mod_name);
  466.                     $this->conf->modules[$mod_name]['iface']->deinit();
  467.                     $this->commands->removeAll($this->conf->modules[$mod_name]['iface']);
  468.                     unset($this->conf->modules[$mod_name]['iface']);
  469.                     unset($this->conf->modules[$mod_name]);
  470.                     $msg $mod_name.": unloaded\r\n";
  471.                 }
  472.  
  473.                 break;
  474.             
  475.             case 'list':
  476.             default:
  477.                 $i=1;
  478.                 foreach ($this->conf->modules as $modname=>$mod)
  479.                 {
  480.                     $msg.=($i++).'. '.str_replace('IcqBot_Module_','',$modname)."\r\n";
  481.                 }
  482.                 break;
  483.                 
  484.         
  485.                 
  486.         }
  487.         
  488.         if ($msg)
  489.         {
  490.             $this->commands->sendMsg($uin$msg);
  491.         }
  492.     }
  493.     
  494.     /**
  495.      * Roles command
  496.      *
  497.      * Command, which rules users ACL.
  498.      * Available params:
  499.      *  - !roles list <icqnumber>
  500.      *  - !roles add <icqnumber> <role1> [role2 [role3 ... ] ]
  501.      *  - !roles remove <icqnumber> <role1> [role2 [role3 ... ] ]
  502.      * 
  503.      * @todo Roles and ACL system should be more powerfull
  504.      * @param integer $uin 
  505.      * @param string $params 
  506.      * @return mixed 
  507.      */
  508.     public function Command_Roles($uin$params)
  509.     {
  510.         /** @var $user IcqBot_Users **/
  511.         
  512.         $msg '';
  513.         
  514.         if ($params == '')
  515.         {
  516.             return $this->commands->sendMsg($uin'Please, specify params. See help for more info.');
  517.         }
  518.         
  519.         if (strpos($params,' ')>0)
  520.         {
  521.             list($cmd$paramsexplode(' '$params2);
  522.         }
  523.         else
  524.         {
  525.             $cmd $params;
  526.             $params '';
  527.         }
  528.  
  529.         switch($cmd)
  530.         {
  531.             case 'list':
  532.  
  533.                 if ($params == '')
  534.                 {
  535.                     $params $uin;
  536.                 }
  537.  
  538.                 if (!is_numeric($params))
  539.                 {
  540.                     $msg 'Not an icq number given';
  541.                     break;
  542.                 }
  543.                 $roles IcqBot_Users::load(trim($params))->getRoles();
  544.                 sort($roles);
  545.                 $msg 'Roles for '.$params.':'."\r\n".implode("\n",$roles);
  546.                 break;
  547.                 
  548.             case 'add':
  549.                 if (!IcqBot_Users::load($uin)->canRoles('roles'))
  550.                 {
  551.                     $msg 'Access denied.';
  552.                     break;
  553.                 }
  554.                 $params explode(' ',$params);
  555.                 if (is_numeric($params[0]))
  556.                 {
  557.                     $user IcqBot_Users::load(trim($params[0]));
  558.                     unset($params[0]);
  559.                 }
  560.                 else 
  561.                 {
  562.                     $user IcqBot_Users::load($uin);
  563.                 }
  564.                 
  565.                 /** @var $user IcqBot_Users **/
  566.                 $user->addRoles($params);
  567.                 $msg 'Roles added to uin '.$user->get($this'uin'ICQBOT_USER_GLOBALVAR);
  568.                 break;
  569.  
  570.             case 'remove':
  571.                 if (!IcqBot_Users::load($uin)->canRoles('roles'))
  572.                 {
  573.                     $msg 'Access denied.';
  574.                     break;
  575.                 }
  576.                 $params explode(' ',$params);
  577.                 if (is_numeric($params[0]))
  578.                 {
  579.                     $user IcqBot_Users::load(trim($params[0]));
  580.                     unset($params[0]);
  581.                 }
  582.                 else 
  583.                 {
  584.                     $user IcqBot_Users::load($uin);
  585.                 }
  586.                 
  587.                 /** @var $user IcqBot_Users **/
  588.                 $user->removeRoles($params);
  589.                 $msg 'Roles removed from uin '.$user->get($this'uin'ICQBOT_USER_GLOBALVAR);
  590.                 break;
  591.         }
  592.         if ($msg)
  593.         {
  594.             return $this->commands->sendMsg($uin$msg);
  595.         }
  596.         return false;
  597.     }
  598.     
  599.     /**
  600.      * Help for Mod command
  601.      *
  602.      * @deprecated will be deprecated
  603.      * @param string $params 
  604.      * @return string 
  605.      */
  606.     public function Help_Mod ($params)
  607.     {
  608.         if ($params == 'list')
  609.         {
  610.             return "Returns modules list.";
  611.         }
  612.         elseif ($params == 'info')
  613.         {
  614.             return "Returns information about module <module_name>. Also you can specify module's number from '".CMD_PREF."mod list' command\r\nFor example:\r\n".CMD_PREF."mod info main\r\n".CMD_PREF."mod info 1";
  615.         }
  616.         elseif ($params == 'cmdlist')
  617.         {
  618.             return "Returns all command of module <module_name>. Also you can specify module's number from '".CMD_PREF."mod list' command\r\nFor example:\r\n".CMD_PREF."mod cmdlist main\r\n".CMD_PREF."mod cmdlist 1";
  619.         }
  620.  
  621.         $hlp "Modules controlling command. Syntax:\r\n";
  622.         $hlp.= CMD_PREF."mod [list]\r\n".CMD_PREF."mod <info|cmdlist> <module_name|module_num>\r\n";
  623.         $hlp.= "For additional info, use ".CMD_PREF."help mod <command>";
  624.         return $hlp;
  625.     }
  626. }

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