* @revision 05 * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons Attribution-Share Alike 3.0 Germany * @homepage http://oss.tiggerswelt.net/xmpp * @copyright Copyright © 2008 tiggersWelt.net */ require_once ("tiggerXMPP/packet.php"); /** * XMPP Message Paket * * @package tiggerXMPP * @class tiggerXMPP_Message */ class tiggerXMPP_Message extends tiggerXMPP_Packet { const TAG_NAME = "message"; const TYPE_NORMAL = "normal"; const TYPE_CHAT = "chat"; const TYPE_GROUPCHAT = "groupchat"; const TYPE_HEADLINE = "headline"; const TYPE_ERROR = "error"; // Basic properties of this packet public $Type = tiggerXMPP_Message::TYPE_NORMAL; public $ID = ''; function __construct ($Name = '', $Parent = null, $Value = null, $To = '', $From = null) { parent::__construct ('message', $Parent, $Value); $this->setNamespace ('jabber:client'); $this->setOriginator ($From); $this->setDestination ($To); } public static function newTag ($To = '', $From = null) { return new tiggerXMPP_Message (null, null, null, $To, $From); } // {{{ getAttributes /** * Retrive a list of attributes for this tag * * @access public * @return array **/ public function getAttributes () { // Set default attributes $Attribs = array ( 'to' => $this->getDestination (), ); // Set message-type $Types = array ( self::TYPE_NORMAL, self::TYPE_CHAT, self::TYPE_GROUPCHAT, self::TYPE_HEADLINE, self::TYPE_ERROR, ); if (in_array ($this->Type, $Types)) $Attribs ['type'] = $this->Type; else trigger_error ('Unknown Message-Type ' . $this->Type); // Do some operations with our parent if (is_object ($p = $this->getStream ())) { // Generate a clean receiver $Attribs ['from'] = $p->getJID (true, true, true, $this->getOriginator ()); // Auto-generate ID if ($this->ID == '') $this->ID = $p->getUniqueID (); // Just append our From-Value } elseif (strlen ($this->getOriginator ()) > 0) $Attribs ['from'] = $this->getOriginator (); // Append our ID if there is one if ($this->ID != '') $Attribs ['id'] = $this->ID; return $Attribs; } // }}} // {{{ getMessage /** * Retrive the message-body from this tag * * @access public * @return array **/ public function getMessage ($Language = null, $Fallback = true) { // Retrive the messages if (!is_array ($Messages = $this->getSubtagsByName ('body')) || (count ($Messages) == 0)) return false; $out = array (); foreach ($Messages as $Msg) $out [$Msg->getLanguage ()] = $Msg->getValue (); if ($Language !== null) { if (!$Fallback || isset ($out [$Language])) return $out [$Language]; return array_shift ($out); } return $out; } // }}} public function setMessage ($Message, $Language = null) { require_once ('tiggerXMPP/message/body.php'); // Remove current messages $this->removeSubtagsByName ('body'); // Convert parameters if (!is_array ($Message)) $Message = array ($Language => $Message); // Append messages foreach ($Message as $Lang=>$Text) new tiggerXMPP_Message_Body (null, $this, $Text, $Lang); } public function unsetMessage () { return $this->removeSubtagsByName ('body'); } public function setSubject ($Subject, $Language = null) { require_once ('tiggerXMPP/message/subject.php'); // Remove current subjects $this->removeSubtagsByName ('subject'); // Convert parameters if (!is_array ($Subject)) $Subject = array ($Language => $Subject); // Append subjects foreach ($Subject as $Lang=>$Text) new tiggerXMPP_Message_Subject (null, $this, $Text, $Lang); } public function setThread ($Thread) { require_once ('tiggerXMPP/message/thread.php'); new tiggerXMPP_Message_Thread (null, $this, $Thread); } public function setType ($Type) { $this->Type = $Type; } // {{{ setTimestamp /** * Set an XEP-0203-compilant timestamp for delayed delivery * * @param int $TS Timestamp to set in unix-time * @param string $From (optional) Originator of this message * @param string $Value (optional) Reason for the delay * * @access public * @return void **/ public function setTimestamp ($TS, $From = null, $Value = null) { $this->removeSubtagsByName ('delay'); $Delay = new tiggerXMPP_Packet ('delay', $this, $Value); $Delay->setNamespace ('urn:xmpp:delay'); $Delay->setAttribute ('stamp', date ('c', $TS)); if ($From !== null) $Delay->setAttribute ('from', $From); } // }}} // {{{ appendError /** * Set an error-status for this packet * * @access public * @return void **/ public function appendError ($Error, $Text = '') { // Force an error-type on this packet $this->Type = self::TYPE_ERROR; // Inherit to our parent parent::appendError ($Error, $Text = ''); } // }}} } ?>