IRC, Software

IRC Bot in PHP 5.4 Part 4

Started work on the modules system. I know I said I was going to talk about other things, but my head just wasn’t in it. I did code in a couple ctcp responses, as you can see in the repository. I am undecided as to how I want to handle the modules system fully. Anyway, here’s what I have as a rough idea so far.

<?php
/**
 * Module Library
 *
 * Loads and registers modules and capabilities.
 *
 * @author    Mark LaDoux <mark.ladoux@gmail.com>
 * @copyright Copyright © 2012, Mark LaDoux
 * @version   0.1.0
 */

namespace IRC;

class Module
{
	/**
	 * Loaded Modules
	 *
	 * @access protected
	 * @var    array
	 */
	protected $_loaded = array();

	/**
	 * Map of triggers to module functions
	 *
	 * @access protected
	 * @var    array
	 */
	protected $_funcs = array();

	/**
	 * Map of permissions required for triggers
	 *
	 * @access protected
	 * @var    array
	 */
	protected $_perms = array();

	/**
	 * load
	 *
	 * Loads modules, and tells them that this is for IRC use so that the
	 * modules will return the proper function and permission information
	 *
	 * @access public
	 * @param  string
	 * @return void
	 */
	public function load($module)
	{
		$module_name   = "\Module\{$module}";
		$this->$module = new $module_name($access_mode = 'IRC');

		// add module to loaded modules list
		$this->_loaded[$module] = $this->$module->version();

		// get module properties
		$this->_funcs[$module] = $this->$module->get_functions();
		$this->_perms[$module] = $this->$module->get_perms();
	}

	/**
	 * loaded
	 *
	 * Lists loaded modules and versions, or reports whether or not a specific
	 * module is loaded.
	 *
	 * @access public
	 * @param  string            $module  Module name
	 * @param  string            $version Module version
	 * @return array|string|bool
	 */
	public function loaded($module = NULL, $version = NULL)
	{
		// if we aren't looking for a specific module, return a list
		if($module === NULL)
		{
			return $this->_loaded;
		}
		// if we aren't looking for a specific version,
		// return it's version ( can also be used for version compares )
		elseif($version === NULL && $module !== NULL)
		{
			if(isset($this->_loaded[$module]))
			{
				return $this->_loaded[$module];
			}
		}
		// if we require a specific version, return if that version of the
		// module is loaded.
		else
		{
			if(
				isset($this->_loaded[$module]) &&
				$this->_loaded[$module] == $version)
			{
				return TRUE;
			}
		}

		// Module is not loaded, return FALSE
		return FALSE;
	}

	/**
	 * Get module permissions
	 *
	 * @access public
	 * @param  string
	 * @return array|bool
	 */
	public function get_perms($module)
	{
		// if module is loaded, share it's permissions
		if(isset($this->_perms[$module])) return $this->_perms[$module];

		// other wise return FALSE.
		return FALSE;
	}

	/**
	 * Get triggers
	 *
	 * @access public
	 * @param  string
	 * @return array|bool
	 */
	public function get_triggers($module)
	{
		// if module is loaded, return it's triggers
		if(isset($this->_funcs[$module])) return $this->_funcs[$module];

		// otherwise return FALSE
		return FALSE;
	}
}

At this stage it’s not yet linked into the bot. I’ve uploaded it to the repository of course, but I’ve yet to decide if this is how I’m wanting to go about it. I’ll run some tests later. I think I’m already starting to see some major pitfalls to the layout I have so far. Nothing I can’t work my way through, but we’ll see how this all works in the end.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.