StropheCappuccino API 1.0.0
/tmp/tempDoc.doc/MUC/TNStropheMUCRoom.j
Go to the documentation of this file.
00001 /*
00002  * TNStropheMUCRoom.j
00003  *
00004  * Copyright (C) 2010 Ben Langfeld <ben@langfeld.me>
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 
00023 
00024 TNStropheMUCConversationWasUpdatedNotification      = @"TNStropheMUCConversationWasUpdatedNotification";
00025 TNStropheMUCDataReceivedNotification                = @"TNStropheMUCDataReceivedNotification";
00026 TNStropheMUCPrivateMessageWasReceivedNotification   = @"TNStropheMUCPrivateMessageWasReceivedNotification";
00027 TNStropheMUCRosterWasUpdatedNotification            = @"TNStropheMUCRosterWasUpdatedNotification";
00028 TNStropheMUCSubjectWasUpdatedNotification           = @"TNStropheMUCSubjectWasUpdatedNotification";
00029 
00030 
00034 @implementation TNStropheMUCRoom : CPObject
00035 {
00036     CPArray                 _messages;
00037     CPString                _subject;
00038     id                      _delegate;
00039     TNStropheJID            _roomJID;
00040     TNStropheMUCRoster      _roster;
00041 
00042     CPArray                 _handlerIDs;
00043     TNStropheConnection     _connection;
00044 }
00045 
00046 #pragma mark -
00047 #pragma mark Initialization
00048 
00056 + (TNStropheMUCRoom)joinRoom:(CPString)aRoom onService:(CPString)aService usingConnection:(TNStropheConnection)aConnection withNick:(CPString)aNick
00057 {
00058     return [[TNStropheMUCRoom alloc] initWithRoom:aRoom
00059                                  onService:aService
00060                            usingConnection:aConnection
00061                                   withNick:aNick];
00062 }
00063 
00071 - (id)initWithRoom:(CPString)aRoom onService:(CPString)aService usingConnection:(TNStropheConnection)aConnection withNick:(CPString)aNick
00072 {
00073     if (self = [super init])
00074     {
00075         _connection     = aConnection;
00076         _roomJID        = [TNStropheJID stropheJIDWithNode:aRoom domain:aService resource:aNick];
00077         _handlerIDs     = [CPArray array];
00078         _messages       = [CPArray array];
00079         _roster         = [TNStropheMUCRoster rosterWithConnection:_connection forRoom:self];
00080     }
00081     return self;
00082 }
00083 
00084 
00085 #pragma mark -
00086 #pragma mark Membership
00087 
00088 - (TNStropheStanza)directedPresence
00089 {
00090     return [TNStropheStanza presenceTo:_roomJID];
00091 }
00092 
00093 - (void)join
00094 {
00095     // Handle messages sent to room
00096     var messageParams   = [CPDictionary dictionaryWithObjectsAndKeys:@"message", @"name",
00097                                                                      [_roomJID full], @"from",
00098                                                                      @"groupchat", @"type",
00099                                                                      {matchBare: true}, @"options"],
00100         messageHandler  = [_connection registerSelector:@selector(receiveMessage:) ofObject:self withDict:messageParams];
00101     [_handlerIDs addObject:messageHandler];
00102 
00103     // Handle private messages from room roster
00104     var pmParams    = [CPDictionary dictionaryWithObjectsAndKeys:@"message", @"name",
00105                                                                 [_roomJID full], @"from",
00106                                                                  @"chat", @"type",
00107                                                                  {matchBare: true}, @"options"],
00108         pmHandler   = [_connection registerSelector:@selector(receivePrivateMessage:) ofObject:self withDict:pmParams];
00109     [_handlerIDs addObject:pmHandler];
00110 
00111     [_connection send:[self directedPresence]];
00112 }
00113 
00114 - (void)leave
00115 {
00116     // Send directed unavailable presence
00117     var leavePresence = [self directedPresence];
00118     [leavePresence setType:@"unavailable"];
00119     [_connection send:leavePresence];
00120 
00121     // Remove room handlers
00122     for (var i = 0; i < [_handlerIDs count]; i++)
00123         [_connection deleteRegisteredSelector:[_handlerIDs objectAtIndex:i]];
00124 }
00125 
00126 
00127 #pragma mark -
00128 #pragma mark Content
00129 
00130 - (void)setSubject:(CPString)aSubject
00131 {
00132     var message = [TNStropheStanza message];
00133     [message setType:@"groupchat"];
00134     [message addChildWithName:@"subject"];
00135     [message addTextNode:aSubject];
00136 
00137     [self sendStanzaToRoom:message];
00138 }
00139 
00140 - (void)sayToRoom:(CPString)aMessage
00141 {
00142     var message = [TNStropheStanza message];
00143     [message setType:@"groupchat"];
00144     [message addChildWithName:@"body"];
00145     [message addTextNode:aMessage];
00146 
00147     [self sendStanzaToRoom:message];
00148 }
00149 
00150 - (void)sendStanzaToRoom:(TNStropheStanza)aStanza
00151 {
00152     [aStanza setTo:[_roomJID bare]];
00153     [_connection send:aStanza];
00154 }
00155 
00156 - (BOOL)receiveMessage:(TNStropheStanza)aStanza
00157 {
00158     if ([aStanza containsChildrenWithName:@"subject"])
00159     {
00160         _subject = [[aStanza firstChildWithName:@"subject"] text];
00161 
00162         if (_delegate && [_delegate respondsToSelector:@selector(mucRoom:receivedNewSubject:)])
00163             [_delegate mucRoom:self receivedNewSubject:_subject];
00164 
00165         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheMUCSubjectWasUpdatedNotification object:self userInfo:aStanza];
00166     }
00167 
00168     if ([aStanza containsChildrenWithName:@"body"])
00169     {
00170         var body    = [[aStanza firstChildWithName:@"body"] text],
00171             contact = [_roster contactWithJID:[aStanza from]];
00172 
00173         if (!contact)
00174         {
00175             [_roster addContact:[aStanza from] withName:[[aStanza from] resource] inGroup:[_roster visitors]];
00176             contact = [_roster contactWithJID:[aStanza from]];
00177         }
00178 
00179         var message = [CPDictionary dictionaryWithObjectsAndKeys:body, @"body",
00180                                                                  contact, @"from",
00181                                                                  [aStanza delayTime], @"time"];
00182         [_messages addObject:message];
00183 
00184         if (_delegate && [_delegate respondsToSelector:@selector(mucRoom:receivedMessage:)])
00185             [_delegate mucRoom:self receivedMessage:message];
00186 
00187         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheMUCConversationWasUpdatedNotification object:self userInfo:aStanza];
00188     }
00189 
00190     var otherChildren = [aStanza children];
00191     [otherChildren removeObjectsInArray:[aStanza childrenWithName:@"body"]];
00192     [otherChildren removeObjectsInArray:[aStanza childrenWithName:@"subject"]];
00193 
00194     if ([otherChildren count] > 0)
00195     {
00196         if (_delegate && [_delegate respondsToSelector:@selector(mucRoom:receivedData:)])
00197             [_delegate mucRoom:self receivedData:aStanza];
00198 
00199         [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheMUCDataReceivedNotification object:self userInfo:aStanza];
00200     }
00201 
00202     return YES;
00203 }
00204 
00205 - (BOOL)receivePrivateMessage:(TNStropheStanza)aMessage
00206 {
00207     // TODO: Handle receiving private messages
00208 
00209     return YES;
00210 }
00211 
00212 @end
00213 
00214 @implementation TNStropheMUCRoom (CPSynthesizedAccessors)
00215 
00219 - (CPArray)messages
00220 {
00221     return _messages;
00222 }
00223 
00227 - (CPString)subject
00228 {
00229     return _subject;
00230 }
00231 
00235 - (id)delegate
00236 {
00237     return _delegate;
00238 }
00239 
00243 - (void)setDelegate:(id)aValue
00244 {
00245     _delegate = aValue;
00246 }
00247 
00251 - (TNStropheJID)roomJID
00252 {
00253     return _roomJID;
00254 }
00255 
00259 - (TNStropheMUCRoster)roster
00260 {
00261     return _roster;
00262 }
00263 
00264 @end
 All Classes Namespaces Files Functions Variables