StropheCappuccino API 1.0.0
/tmp/tempDoc.doc/TNStropheStanza.j
Go to the documentation of this file.
00001 /*
00002  * TNStropheStanza.j
00003  *
00004  * Copyright (C) 2010  Antoine Mercadal <antoine.mercadal@inframonde.eu>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 3.0 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 
00022 
00026 @implementation TNStropheStanza: TNXMLNode
00027 {
00028 }
00029 
00030 #pragma mark -
00031 #pragma mark Class methods
00032 
00037 + (TNStropheStanza)stanzaWithStanza:(id)aStanza
00038 {
00039     return [[TNStropheStanza alloc] initWithNode:aStanza];
00040 }
00041 
00047 + (TNStropheStanza)stanzaWithName:(CPString)aName andAttributes:(CPDictionary)attributes
00048 {
00049     return [[TNStropheStanza alloc] initWithName:aName andAttributes:attributes];
00050 }
00051 
00058 + (TNStropheStanza)stanzaWithName:(CPString)aName to:(TNStropheJID)aJID  attributes:(CPDictionary)attributes
00059 {
00060     return [[TNStropheStanza alloc] initWithName:aName to:aJID attributes:attributes bare:NO];
00061 }
00062 
00070 + (TNStropheStanza)stanzaWithName:(CPString)aName to:(TNStropheJID)aJID  attributes:(CPDictionary)attributes bare:(BOOL)sendToBareJID
00071 {
00072     return [[TNStropheStanza alloc] initWithName:aName to:aJID attributes:attributes bare:sendToBareJID];
00073 }
00074 
00075 
00080 + (TNStropheStanza)iqWithAttributes:(CPDictionary)attributes
00081 {
00082     return [TNStropheStanza stanzaWithName:@"iq" andAttributes:attributes];
00083 }
00084 
00088 + (TNStropheStanza)iq
00089 {
00090     return [TNStropheStanza iqWithAttributes:nil];
00091 }
00092 
00097 + (TNStropheStanza)iqTo:(TNStropheJID)aJID
00098 {
00099     return [TNStropheStanza iqTo:aJID withAttributes:nil];
00100 }
00101 
00106 + (TNStropheStanza)iqWithType:(CPString)aType
00107 {
00108     return [TNStropheStanza iqWithAttributes:{"type": aType}];
00109 }
00110 
00116 + (TNStropheStanza)iqTo:(TNStropheJID)aJID withType:(CPString)aType
00117 {
00118     return [TNStropheStanza iqTo:aJID withAttributes:{"type": aType}];
00119 }
00120 
00126 + (TNStropheStanza)iqTo:(TNStropheJID)aJID withAttributes:(CPDictionary)attributes
00127 {
00128     return [TNStropheStanza stanzaWithName:@"iq" to:aJID attributes:attributes bare:NO];
00129 }
00130 
00137 + (TNStropheStanza)iqTo:(TNStropheJID)aJID withAttributes:(CPDictionary)attributes bare:(BOOL)sendToBareJID
00138 {
00139     return [TNStropheStanza stanzaWithName:@"iq" to:aJID attributes:attributes bare:sendToBareJID];
00140 }
00141 
00146 + (TNStropheStanza)presenceWithAttributes:(CPDictionary)attributes
00147 {
00148     return [TNStropheStanza stanzaWithName:@"presence" andAttributes:attributes];
00149 }
00150 
00154 + (TNStropheStanza)presence
00155 {
00156     return [TNStropheStanza presenceWithAttributes:nil];
00157 }
00158 
00163 + (TNStropheStanza)presenceTo:(TNStropheJID)aJID
00164 {
00165     return [TNStropheStanza presenceTo:aJID withAttributes:nil];
00166 }
00167 
00173 + (TNStropheStanza)presenceTo:(TNStropheJID)aJID withAttributes:(CPDictionary)attributes
00174 {
00175     return [TNStropheStanza stanzaWithName:@"presence" to:aJID attributes:attributes bare:NO];
00176 }
00177 
00184 + (TNStropheStanza)presenceTo:(TNStropheJID)aJID withAttributes:(CPDictionary)attributes bare:(BOOL)sendToBareJID
00185 {
00186     return [TNStropheStanza stanzaWithName:@"presence" to:aJID attributes:attributes bare:sendToBareJID];
00187 }
00188 
00193 + (TNStropheStanza)messageWithAttributes:(CPDictionary)attributes
00194 {
00195     return [TNStropheStanza stanzaWithName:@"message" andAttributes:attributes];
00196 }
00197 
00201 + (TNStropheStanza)message
00202 {
00203     return [TNStropheStanza messageWithAttributes:nil];
00204 }
00205 
00210 + (TNStropheStanza)messageTo:(TNStropheJID)aJID
00211 {
00212     return [TNStropheStanza messageTo:aJID withAttributes:nil];
00213 }
00214 
00220 + (TNStropheStanza)messageTo:(TNStropheJID)aJID withAttributes:(CPDictionary)attributes
00221 {
00222     return [TNStropheStanza stanzaWithName:@"message" to:aJID attributes:attributes];
00223 }
00224 
00231 + (TNStropheStanza)messageTo:(TNStropheJID)aJID withAttributes:(CPDictionary)attributes bare:(BOOL)sendToBareJID
00232 {
00233     return [TNStropheStanza stanzaWithName:@"message" to:aJID attributes:attributes bare:sendToBareJID];
00234 }
00235 
00236 
00237 #pragma mark -
00238 #pragma mark Initialization
00239 
00240 - (TNStropheStanza)initWithName:(CPString)aName to:(TNStropheJID)aJID attributes:(CPDictionary)someAttributes bare:(BOOL)sendToBareJID
00241 {
00242     if (aJID && !someAttributes)
00243         someAttributes = {};
00244 
00245     if (someAttributes)
00246     {
00247         if (someAttributes.isa)
00248             [someAttributes setValue:((sendToBareJID) ? [aJID bare] : aJID) forKey:"to"];
00249         else
00250             someAttributes.to = ((sendToBareJID) ? [aJID bare] : [aJID full]);
00251     }
00252 
00253     return [super initWithName:aName andAttributes:someAttributes];
00254 }
00255 
00256 
00257 #pragma mark -
00258 #pragma mark Attributes
00259 
00263 - (CPString)from
00264 {
00265     while ([self up]);
00266     return [TNStropheJID stropheJIDWithString:[self valueForAttribute:@"from"]];
00267 }
00268 
00272 - (void)setFrom:(id)aFrom
00273 {
00274     if ([aFrom isKindOfClass:CPString])
00275         aFrom = [TNStropheJID stropheJIDWithString:aFrom];
00276 
00277     while ([self up]);
00278     [self setValue:[aFrom full] forAttribute:@"from"];
00279 }
00280 
00284 - (CPString)fromBare
00285 {
00286     return [[self from] bare];
00287 }
00288 
00292 - (CPString)fromUser
00293 {
00294     return [[self from] node];
00295 }
00296 
00300 - (CPString)fromDomain
00301 {
00302     return [[self from] domain];
00303 }
00304 
00308 - (CPString)fromResource
00309 {
00310     return [[self from] resource];
00311 }
00312 
00316 - (CPString)to
00317 {
00318     while ([self up]);
00319     return [TNStropheJID stropheJIDWithString:[self valueForAttribute:@"to"]];
00320 }
00321 
00325 - (void)setTo:(id)aTo
00326 {
00327     if ([aTo isKindOfClass:CPString])
00328         aTo = [TNStropheJID stropheJIDWithString:aTo];
00329 
00330     while ([self up]);
00331     [self setValue:[aTo full] forAttribute:@"to"];
00332 }
00333 
00337 - (CPString)type
00338 {
00339     return [self valueForAttribute:@"type"];
00340 }
00341 
00345 - (void)setType:(CPString)aType
00346 {
00347     [self setValue:aType forAttribute:@"type"];
00348 }
00349 
00353 - (CPString)ID
00354 {
00355     return [self valueForAttribute:@"id"];
00356 }
00357 
00361 - (void)setID:(CPString)anID
00362 {
00363     while ([self up]);
00364     [self setValue:anID forAttribute:@"id"];
00365 }
00366 
00370 - (CPDate)delayTime
00371 {
00372     if ([self containsChildrenWithName:@"delay"] && [[self firstChildWithName:@"delay"] namespace] == Strophe.NS.DELAY)
00373     {
00374         // TODO: Fix this to match non-UTC
00375         var messageDelay    = [[self firstChildWithName:@"delay"] valueForAttribute:@"stamp"],
00376             match           = messageDelay.match(new RegExp(/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})Z/));
00377 
00378         if (!match || match.length != 3)
00379             [CPException raise:CPInvalidArgumentException
00380                         reason:"delayTime: the string must be of YYYY-MM-DDTHH:MM:SSZ format"];
00381 
00382         return [[CPDate alloc] initWithString:(match[1] + @" " + match[2] + @" +0000")];
00383     }
00384 
00385     return [CPDate dateWithTimeIntervalSinceNow:0];
00386 }
00387 
00388 @end
 All Classes Namespaces Files Functions Variables