<?php
class BotCommand {
private $name,
$helpstr,
$access,
$case_sens = false,
$hidden = false,
$callback_list = array(),
$aliases_list = array(),
$enabled = true;
public $plugin = null;
const HELP_SHORT = 0;
const HELP_FULL = 1;
public function __construct($command, $func, $access = 1, $helpstr = false, $plugin) {
$this->name = $command;
$this->callback_list[] = $func;
$this->access = $access;
$this->helpstr = $helpstr;
$this->plugin = $plugin;
}
public function SetHide($v) {
$this->hidden = $v;
return true;
}
public function IsHidden() {
return $this->hidden;
}
public function GetName() {
return $this->name;
}
public function GetCallbackList() {
return $this->callback_list;
}
public function AddCallbackFunc($func) {
if (!in_array($func, $this->callback_list)) {
$this->callback_list[] = $func;
return true;
} else {
return false;
}
}
public function DelCallbackFunc($func) {
foreach ($this->callback_list as $k=>$c) {
if ($c==$func) {
unset($this->callback_list[$k]);
return true;
}
}
return false;
}
public function CaseSensitive($value = false) {
$this->case_sens = $value;
return true;
}
public function GetHelp($inlist=0) {
if ($this->hidden || !$this->enabled || is_null($this->helpstr) ) {
return null;
}
if ($inlist == self::HELP_SHORT) {
$msg = str_replace(
array('%c', '%s'),
array( $this->name, $this->helpstr ),
SteelBot::$cfg['help.format']
);
} else {
$msg = str_replace(
array('%c', '%s'),
array( $this->name, $this->helpstr ),
SteelBot::$cfg['help.format_full']
);
}
return $msg;
}
public function GetPlugin() {
return $this->plugin;
}
public function SetHelp($text) {
$this->helpstr = $text;
return true;
}
public function GetAccess() {
return $this->access;
}
public function SetAccess($level) {
$this->access = $level;
return true;
}
public function Enable() {
$this->enabled = true;
return true;
}
public function Disable() {
$this->enabled = false;
return true;
}
public function IsEnabled() {
return $this->enabled;
}
public function AddAlias($alias) {
$i = array_search($alias, $this->aliases_list);
if ($i === false) {
$this->aliases_list[] = $alias;
return true;
} else {
return false;
}
}
public function RemoveAlias($alias) {
$i = array_search($alias, $this->aliases_list);
if ($i !== false) {
unset( $this->aliases_list[$i] );
return true;
} else {
return false;
}
}
public function GetAliases($single) {
if ($single) {
if (!empty($this->aliases_list)) {
return $this->aliases_list[0];
} else {
return false;
}
} else {
return $this->aliases_list;
}
}
public function HaveAlias($alias) {
return in_array($alias, $this->aliases_list);
}
public function Execute($params) {
if (!$this->enabled) return;
//access check
if ( $ac=SteelBot::GetUserAccess() < $this->access) {
throw new BotException("{$this->name}: acces denied (user: $ac, cmd: {$this->access})", ERR_CMD_ACCESS);
}
//running handlers
foreach ($this->callback_list as $callback) {
if ( is_callable($callback) ) {
call_user_func($callback, $params);
} else {
throw new BotException("Function ".func2str($callback)." does not exists", ERR_FUNC);
}
}
}
}