Commits

Tony Manco  committed 9713f9b

Improved module support: Dynamic loading and unloading of modules without the need to restart;
Misc fixes

  • Participants
  • Parent commits d48b58e

Comments (0)

Files changed (1)

File Class.phIRCe.php

  */
 
 class phirce {
-    private $socket, $conn, $line, $cmd, $version, $allowed=array(), $badwords=array(), $modules=array(),
+    private $socket, $conn, $line, $cmd, $version, $allowed = array(), $badwords = array(), $modules = array(),
             $message, $tmppath, $useragent, $logpath, $log, $arcpath, $archive, $modpath;
     public $nick, $ident, $pass, $realname, $host, $port, $chan;
     public function __construct($nick, $pass, $ident, $realname, $host, $port,
                     $mod = "$this->modpath"."/$module".'.php';
                 if (file_exists($mod)) {
                     // Load it if file exists
-                    $this->SendCommand("Module $module sucefully loaded!\r\n");
+                    //$this->SendCommand("Module $module sucefully loaded!\r\n");
                     include "$mod";
                 }
-                else
+                else {
+                    $key = array_search($module, $this->modules);
+                    unset ($this->modules["$key"]);
                     $this->SendCommand("Failed to load module $module. Reason: Not found!\r\n");
+                    $this->SendCommand("Module $module has been removed until restarted!\r\n");
+                }
 
             }
             if ($this->log)
         $this->SendCommand("NOTICE $to :".$this->version."\r\n");
     }
 
+    private function DisplayModLoaded($user) {
+        if (!empty($this->modules)) {
+            foreach ($this->modules as $module) {
+                $this->SendCommand("PRIVMSG $user :Module \x02$module\x02 is loaded!\r\n");
+            }
+        }
+        else {
+            $this->SendCommand("PRIVMSG $user :No modules are loaded!\r\n");
+        }
+    }
+
+    private function UnloadMod ($user, $mod) {
+        // @var $user is needed to send message status messages back to user
+        // @var $mod is the name of the module
+        $key = array_search($mod, $this->modules);
+        if ($key === FALSE) {
+            $this->SendCommand("PRIVMSG $user :Unable to unload \x02$mod\x02. Module not loaded!\r\n");
+        }
+        else {
+            unset ($this->modules["$key"]);
+            $this->SendCommand("PRIVMSG $user :Module \x02$mod\x02 has been unloaded!\r\n");
+            if (empty($this->modules))
+                $this->SendCommand("PRIVMSG $user :You have no more modules in memory!\r\n")        ;
+        }
+    }
+
+    private function LoadMod ($user, $mod) {
+        // @var $user is needed to send message status messages back to user
+        // @var $mod is the name of the module
+        $load = $mod;
+        if ($_SERVER['OS'] == 'Windows_NT')
+            $mod = "$this->modpath"."\\$load".'.php';
+        else
+            $mod = "$this->modpath"."/$load".'.php';
+
+        if (in_array($load, $this->modules)){
+           $this->SendCommand("PRIVMSG $user :Module \x02$load\x02 is already loaded!\r\n");
+        }
+        else {
+            if (file_exists($mod)) {
+                include "$mod";
+                $this->modules[] = $load;
+                $this->SendCommand("PRIVMSG $user :Module \x02$load\x02 has been loaded!\r\n");
+            }
+            else {
+                $this->SendCommand("PRIVMSG $user :Failed to load module \x02$load\x02. Reason: Not found!\r\n");
+            }
+        }
+    }
+    
     private function MsgSplit($line){
         $array = explode(':', $line);
 
             case "!version":
                 $this->DisplayVersion($quser);
                 break;
+            case "!mods":
+                $this->DisplayModLoaded($quser);
+                break;
+            case "!loadmod":
+                // @param $user is module name in this case
+                $this->LoadMod($quser, $user);
+                break;
+            case "!unloadmod":
+                // @param $user is module name in this case
+                $this->UnloadMod($quser, $user);
+                break;
             default:
                 $this->SendCommand("PRIVMSG $quser :Not a valid command!\r\n");
                 break;
         $this->SendCommand("/msg NickServ identify $pass\r\n");
     }
 
-    public function allowed($allowed=array()) {
+    public function allowed($allowed = array()) {
         $this->allowed = $allowed;
     }
 
-    public function badwords($badwords=array()) {
+    public function badwords($badwords = array()) {
         $this->badwords = $badwords;
     }
 
-    public function modules($modules=array()) {
+    public function modules($modules = array()) {
         $this->modules = $modules;
     }
 
                         13 => "\x02[Clearlog]\x02 - Clears the normal logfile (!clearlog phIRCe)",
                         14 => "\x02[Clear-rawlog]\x02 - Clears the raw logfile (!clear-rawlog phIRCe)",
                         15 => "\x02[Version]\x02 - Shows the version of the bot (!version phIRCe)",
-                        16 => "---END---");
+                        16 => "\x02[Memory]\x02 - Shows current memory used by emalloc() (!memory phIRCe)",
+                        17 => "\x02[Mods]\x02 - Lists loaded modules (!mods phIRCe)",
+                        18 => "\x02[Loadmod]\x02 - Loads a module into memory (!loadmod {module_name})",
+                        19 => "\x02[Unloadmod]\x02 - Unloads a module from memory (!unloadmod {module_name})",
+                        20 => "---END---");
         foreach ($helpers as $help) {
             $this->SendCommand("PRIVMSG $user :$help\r\n");
+            // Don't flood the servers
+            sleep (1);
         }
     }