1  //!-- UTF8
     2  /*
     3  Oregano Multiuser Server - Version 1.2.0 - January 4th, 2005
     4   
     5  	Web:  www.oregano-server.org
     6  	Mail: info@oregano-server.org
     7   
     8  	Copyright 2003 - 2004 - 2004 Jens Halm / Cologne, Germany
     9   
    10   This library is free software; you can redistribute it and/or
    11   modify it under the terms of the GNU Lesser General Public
    12   License as published by the Free Software Foundation; either
    13   version 2.1 of the License, or (at your option) any later version.
    14   
    15   This library is distributed in the hope that it will be useful,
    16   but WITHOUT ANY WARRANTY; without even the implied warranty of
    17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    18   Lesser General Public License for more details.
    19   
    20   You should have received a copy of the GNU Lesser General Public
    21   License along with this library; if not, write to the Free Software
    22   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23   */
    24  
    25  /*
    26  -------------------------------------------
    27  	Classe Envelope
    28  
    29  	@description :
    30  	Gestion de la totalité d'un message.
    31  
    32  
    33  	@author Jens Halm copyright http://www.spicefactory.org/
    34  	@author erixtekila copyleft http://www.v-i-a.net  
    35  -------------------------------------------
    36  	version history :
    37  	1.2.0 : 29/12/04
    38  			- Portage en actionscript 2 pour le
    39  			compile time type checking
    40  -------------------------------------------
    41  */
    42  
    43  import org.omus.msg.Message;
    44  
    45  /**
    46   *	Conteneur de toutes les informations soumises sous forme de message en tre le client et le serveur.
    47   *	Toutes les communications se font en faiosant parvenir des Messages dans des Envelope avec d'autre informations utiles.
    48   *
    49   *	@author Jens Halm copyright http://www.spicefactory.org/
    50   *	@author erixtekila copyleft http://www.v-i-a.net 
    51   *	@version 1.2.0
    52   */
    53  class org.omus.msg.Envelope
    54  {
    55  	//--------------------
    56  	// PROPRIETES
    57  	//--------------------
    58  	/**
    59  	 *	Le message proprement dit.
    60  	 */
    61  	private var message:Message;
    62  	
    63  	/**
    64  	 *	Type d'envoi soumis avec l'enveloppe
    65  	 */
    66  	private var type:String;
    67  	
    68  	/**
    69  	 *	Identifiant unique de l'enveloppe.
    70  	 */
    71  	private var msgID:Number;
    72  	
    73  	/**
    74  	 *	Destinataire de l'enveloppe.
    75  	 */
    76  	private var recipient:String;
    77  	
    78  	//--------------------
    79  	// CONSTRUCTEUR
    80  	//--------------------
    81  	/**
    82  	 *	
    83  	 *
    84  	 *	@param msg		Le message à envelopper.
    85  	 *	@param tp		Le type d'envoi.
    86  	 *	@param recp		Optionnel. Une valeur à modifier sur le serveur (cf Messenger et changement de nom, de groupe).
    87  	 *	@param id		Identifiant unique du message.
    88  	 */
    89  	public function Envelope(msg:Message, tp:String, recp:String, id:Number)
    90  	{
    91  		// Propriétés
    92  		message = msg;
    93  		type = tp;
    94  		recipient = (recp == undefined) ? "" : recp;
    95  		msgID = id;
    96  		
    97  		// trace(this+ " installé.");
    98  	}
    99  	
   100  	//--------------------
   101  	// METHODES PUBLIQUES
   102  	//--------------------
   103  	/**
   104  	 *	Utilisé dans un contexte littéral
   105  	 *	@return	Une chaine définissant l'objet
   106  	 */
   107  	public function toString():String
   108  	{
   109  		var s= "[Object Envelope]";
   110  		s += "\norg.omus.Envelope:\n==============\nid = " + msgID;
   111  		s += "\ntype = " + type;
   112  		s += "\nrecipient = " + recipient;
   113  		s += "\n" + message.toString();
   114  		return s;
   115  	}
   116  	
   117  	/**
   118  	 *	Renvoie l'identiofiant d'une enveloppe.
   119  	 *
   120  	 *	@return		Un identifiant unique.
   121  	 */
   122  	public function getID ():Number
   123  	{
   124  		return msgID;
   125  	}
   126  
   127  	/**
   128  	 *	Renvoie le type d'envoi.
   129  	 *
   130  	 *	@return		Une chaine spécifiant le type de l'enveloppe.
   131  	 */
   132  	public function getType ():String
   133  	{
   134  		return type;
   135  	}
   136  	
   137  	/**
   138  	 *	Renvoie le message joint à l'enveloppe.
   139  	 *
   140  	 *	@return		Un object Message (conteneur).
   141  	 */
   142  	public function getMessage ():Message
   143  	{
   144  		return message;
   145  	}
   146  	
   147  	/**
   148  	 *	Renvoie une chaine encodée de l'enveloppe totale
   149  	 *	correspondant aux contraintes de format de l'encodage "Marshall".
   150  	 *
   151  	 *	Exemple : entête
   152  	 *	[#msg][type=12][msgID=4][LongueurRecipient=3][LongueurSujet=3][LongueurDonnées=6]
   153  	 *	messageMarshallisé = "#msggroup       123411    45 999   "+username+Je change de nom+données
   154  	 */
   155  	public function getMarshalledString ():String
   156  	{
   157  		var subj = message.getSubject();
   158  		var data = message.getMarshalledAttachment();
   159  		var header = "#msg";
   160  		header += fill(type, 12);
   161  		header += fill(String(msgID), 4);
   162  		header += fill(String(recipient.length), 3);
   163  		header += fill(String(subj.length), 3);
   164  		header += fill(String(data.length), 6);
   165  		return header + recipient + subj + data;
   166  	}
   167  	
   168  	//--------------------
   169  	// METHODES PRIVEES
   170  	//--------------------
   171  	/**
   172  	 *	Formatte les chaines Marshall pour qu'elles correspondent à des longueurs précises.
   173  	 *	Encodage "Marshall" inclus au framework Oregano.
   174  	 *
   175  	 *	@param str		Une chaine à formatter.
   176  	 *	@param len		La longueur de la chaîne à ne pas dépasser.
   177  	 *	@return			La chaine originale si elle correspond à la bonne longueur, 
   178  	 *					sinon la chaine suivie d'autant d'espace pour compléter.
   179  	 */
   180  	private function fill (str:String, len:Number):String
   181  	{
   182  		var dif = len - str.length;
   183  		if (dif == 0) return str;
   184  		for (var i = 0; i < dif; i++) {
   185  			str += " ";
   186  		}
   187  		return str;
   188  	}
   189  	
   190  	//--------------------
   191  	// METHODES STATIQUES
   192  	//--------------------
   193  	/**
   194  	 *	Utilisé dans un contexte littéral
   195  	 *	@return	Une chaine définissant l'objet
   196  	 */
   197  	public static function toLog():String
   198  	{
   199  		return "[Object Envelope]";
   200  	}
   201  }
   202