* @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons Attribution-Share Alike 3.0 Germany * @homepage http://oss.tiggerswelt.net/twMuc * @copyright Copyright © 2010 tiggersWelt.net **/ /** * twMUC_User * ------------- * Abstract storage for users in a chat-room * * @class twMUC_User * @package twMUC * @author Bernd Holzmueller **/ class twMUC_User { const AFFILIATION_NONE = 'none'; const AFFILIATION_MEMBER = 'member'; const AFFILIATION_ADMIN = 'admin'; const AFFILIATION_OWNER = 'owner'; const AFFILIATION_OUTCAST = 'outcast'; const ROLE_NONE = 'none'; const ROLE_VISITOR = 'visitor'; const ROLE_PARTICIPANT = 'participant'; const ROLE_MODERATOR = 'moderator'; private $Nickname = ''; private $lNickname = ''; private $JID = ''; private $plainJID = ''; private $Room = null; private $Affiliation = twMuc_User::AFFILIATION_NONE; private $Role = twMuc_User::ROLE_NONE; // {{{ __construct /** * Setup a new user * * @param string $JID * @param string $Nickname * * @access friendly * @return void **/ function __construct ($JID, $Nickname, $Room) { $this->Room = $Room; $this->setNickname ($Nickname); $this->setJID ($JID); } // }}} // {{{ getJID /** * Retrive our JID * * @access public * @return string **/ public function getJID () { return $this->JID; } // }}} // {{{ matchJID /** * Check if this user matches a given JID * * @param string $JID * @remark Make sure that the JID is passed in lower case * * @access public * @return bool **/ public function matchJID ($JID, $Exact = false) { return ($JID == $this->JID) || (!$Exact && ($JID == $this->plainJID)); } // }}} // {{{ setJID /** * Set JID for this user * * @param string $JID * * @access private * @return void **/ private function setJID ($JID) { $JID = $this->Room->normalizeUsername ($JID); $this->JID = $JID; $this->plainJID = $JID; if (($p = strpos ($JID, '/')) !== false) $this->plainJID = substr ($JID, 0, $p); } // }}} // {{{ getNickname /** * Retrive our current Nickname * * @access public * @return string **/ public function getNickname () { return $this->Nickname; } // }}} // {{{ setNickname /** * Set a new nickname for this user * * @param string $Nickname * * @access public * @return void **/ public function setNickname ($Nickname) { $this->Nickname = $Nickname; $this->lNickname = strtolower ($Nickname); } // }}} // {{{ matchNickname /** * Compare our nickname with another * * @param string $Nickname * @remark Make sure that the given nickname is lower-case * * @access public * @return bool **/ public function matchNickname ($Nickname) { return ($Nickname == $this->lNickname); } // }}} // {{{ getRoom /** * Retrive handle of the room we are in * * @access public * @return object **/ public function getRoom () { return $this->Room; } // }}} // {{{ getHandler /** * Retrive the MUC-Handler * * @access public * @return object **/ public function getHandler () { if (!is_object ($R = $this->getRoom ())) return false; return $R->getHandler (); } // }}} // {{{ setAffiliation /** * Set a new affiliation for this user * * @param enum $Affiliation * * @access public * @return bool **/ public function setAffiliation ($Affiliation) { // Check the given affiliation if (!in_array ($Affiliation, array (self::AFFILIATION_OUTCAST, self::AFFILIATION_MEMBER, self::AFFILIATION_ADMIN, self::AFFILIATION_OWNER, self::AFFILIATION_NONE))) { trigger_error ('Invalid affiliation given'); return false; } // Check for changes if ($this->Affiliation == $Affiliation) return true; // Set the affiliation $this->Affiliation = $Affiliation; if (!$this->isOnline ()) return true; // Retrive handle of our room $R = $this->getRoom (); // Check the role switch ($this->Affiliation) { case self::AFFILIATION_OUTCAST: $this->setRole (self::ROLE_NONE); break; case self::AFFILIATION_MEMBER: if ($this->getRole () == self::ROLE_MODERATOR) { if (is_object ($R) && $R->isModerated ()) $this->setRole (self::ROLE_VISITOR); else $this->setRole (self::ROLE_PARTICIPANT); } break; case self::AFFILIATION_ADMIN: case self::AFFILIATION_OWNER: $this->setRole (self::ROLE_MODERATOR); break; } // Tell the handler that our affiliation was changed if (is_object ($H = $this->getHandler ())) $H->roleChanged ($this->getRoom (), $this); return true; } // }}} // {{{ getAffiliation /** * Retrive the assigned affiliation of this user * * @access public * @return enum **/ public function getAffiliation () { return $this->Affiliation; } // }}} // {{{ setRole /** * Assign a new role to this user * * @param enum $Role * * @access public * @return bool **/ public function setRole ($Role) { // Validate the role if (!in_array ($Role, array (self::ROLE_NONE, self::ROLE_VISITOR, self::ROLE_PARTICIPANT, self::ROLE_MODERATOR))) return false; // Set the role $this->Role = $Role; return true; } // }}} // {{{ getRole /** * Retrive the assigned role for this user * * @access public * @return enum **/ public function getRole () { return $this->Role; } // }}} // {{{ isOnline /** * Check if this user is marked as online * * @access public * @return bool **/ public function isOnline () { return ($this->getRole () != self::ROLE_NONE); } // }}} } ?>