X-Git-Url: http://cgit.babelmonkeys.de/?a=blobdiff_plain;f=src%2Fcore%2FJubChatClient.m;h=fa4bd9e0aa2309957810b6e85f711c7296a9f0ef;hb=HEAD;hp=3c563f5e9dd1a9f267b9b09ad63787a8be9de1ea;hpb=4d4a2b07fe52fa2d9e21b697dcf85c73bdd537a9;p=jubjub.git diff --git a/src/core/JubChatClient.m b/src/core/JubChatClient.m index 3c563f5..fa4bd9e 100644 --- a/src/core/JubChatClient.m +++ b/src/core/JubChatClient.m @@ -1,37 +1,57 @@ #import "JubChatClient.h" +#import "ObjXMPP/namespaces.h" + +#import "JubAvatarManager.h" + +#define JUB_CLIENT_URI @"http://babelmonkeys.de/jubjub" @implementation JubChatClient -@synthesize connection; -@synthesize roster; -@synthesize contactManager; -@synthesize presence; -@synthesize ui; +@synthesize connection = _connection; +@synthesize roster = _roster; +@synthesize avatarManager = _avatarManager; +@synthesize contactManager = _contactManager; +@synthesize discoEntity = _discoEntity; +@synthesize presence = _presence; +@synthesize ui = _ui; - initWithConfig: (JubConfig*)config { self = [super init]; @try { - chatMap = [[OFMutableDictionary alloc] init]; - - connection = [XMPPConnection new]; - connection.username = config.username; - connection.domain = config.domain; - connection.server = config.server; - connection.password = config.password; - [connection addDelegate: self]; - - roster = [[XMPPRoster alloc] initWithConnection: connection]; - [roster addDelegate: self]; - - contactManager = [[XMPPContactManager alloc] - initWithConnection: connection - roster: roster]; - - streamManagement = [[XMPPStreamManagement alloc] - initWithConnection: connection]; - - [connection asyncConnectAndHandle]; + _chatMap = [[OFMutableDictionary alloc] init]; + + _connection = [XMPPConnection new]; + _connection.username = config.username; + _connection.domain = config.domain; + _connection.server = config.server; + _connection.password = config.password; + [_connection addDelegate: self]; + + _roster = [[XMPPRoster alloc] initWithConnection: _connection]; + [_roster addDelegate: self]; + + _discoEntity = + [[XMPPDiscoEntity alloc] initWithConnection: _connection + capsNode: JUB_CLIENT_URI]; + + XMPPDiscoIdentity *identity = + [XMPPDiscoIdentity identityWithCategory: @"client" + type: @"pc" + name: @"JubJub"]; + [_discoEntity addIdentity: identity]; + [_discoEntity addFeature: XMPP_NS_CAPS]; + + _avatarManager = + [[JubAvatarManager alloc] initWithClient: self]; + + _contactManager = [[XMPPContactManager alloc] + initWithConnection: _connection + roster: _roster]; + [_contactManager addDelegate: self]; + + _streamManagement = [[XMPPStreamManagement alloc] + initWithConnection: _connection]; } @catch (id e) { [self release]; @throw e; @@ -42,12 +62,18 @@ - (void)dealloc { - [roster release]; - [contactManager release]; - [streamManagement release]; - [connection release]; - [presence release]; - [chatMap release]; + [_roster removeDelegate: self]; + [_contactManager removeDelegate: self]; + [_connection removeDelegate: self]; + + [_roster release]; + [_contactManager release]; + [_discoEntity release]; + [_streamManagement release]; + [_avatarManager release]; + [_connection release]; + [_presence release]; + [_chatMap release]; [super dealloc]; } @@ -57,27 +83,27 @@ OFAutoreleasePool *pool = [OFAutoreleasePool new]; OFString *bareJID = [contact.rosterItem.JID bareJID]; - id chat = [chatMap objectForKey: bareJID]; + id chat = [_chatMap objectForKey: bareJID]; if (chat == nil) { OFString * title = [@"Chat with " stringByAppendingString: bareJID]; - chat = [[[[ui chatUIClass] alloc] + chat = [[[[_ui chatUIClass] alloc] initWithTitle: title closeBlock: ^{ - [chatMap removeObjectForKey: bareJID]; + [_chatMap removeObjectForKey: bareJID]; } sendBlock: ^(OFString *text) { XMPPMessage *msg = [XMPPMessage messageWithType: @"chat"]; msg.body = text; [contact sendMessage: msg - connection: connection]; + connection: _connection]; } ] autorelease]; - [chatMap setObject: chat - forKey: bareJID]; + [_chatMap setObject: chat + forKey: bareJID]; } [pool release]; @@ -85,28 +111,73 @@ return chat; } -- (void)connection: (XMPPConnection*)connection_ - wasBoundToJID: (XMPPJID*)jid +- (void)sendPresenceWithStatus: (OFString*)status +{ + [self sendPresenceWithStatus: status + text: nil]; +} + +- (void)sendPresenceWithStatus: (OFString*)status + text: (OFString*)text { - of_log(@"Bound to JID: %@", [jid fullJID]); + XMPPPresence *presence; + + if ([status isEqual: @"unavailable"]) + presence = [XMPPPresence presenceWithType: @"unavailable"]; + else + presence = [XMPPPresence presence]; - [roster requestRoster]; + if (!([status isEqual: @"available"] || + [status isEqual: @"unavailable"])) + presence.show = status; + + if (text != nil) + presence.status = text; + + OFXMLElement *caps = [OFXMLElement elementWithName: @"c" + namespace: XMPP_NS_CAPS]; + [caps addAttributeWithName: @"hash" + stringValue: @"sha-1"]; + [caps addAttributeWithName: @"ver" + stringValue: [_discoEntity capsHash]]; + [caps addAttributeWithName: @"node" + stringValue: JUB_CLIENT_URI]; + + [presence addChild: caps]; + + [_connection sendStanza: presence]; +} + +- (void)connection: (XMPPConnection*)connection + wasBoundToJID: (XMPPJID*)jid +{ + [_roster requestRoster]; } -- (void)connection: (XMPPConnection*)connection_ - didReceivePresence: (XMPPPresence*)presence_ +- (void)connection: (XMPPConnection*)connection + didReceivePresence: (XMPPPresence*)presence { - if ([presence_.from isEqual: connection.JID]) { - [ui client: self - didChangePresence: presence_]; - OF_SETTER(presence, presence_, YES, 0); + if ([presence.from isEqual: connection.JID]) { + [_ui client: self + didChangePresence: presence]; + OF_SETTER(_presence, presence, true, 0); } } +- (void)contact: (XMPPContact*)contact + didSendMessage: (XMPPMessage*)message +{ + if (message.body == nil || ![message.type isEqual: @"chat"]) + return; + + id chat = [self chatForContact: contact]; + [chat addMessage: message.body + sender: [message.from bareJID]]; +} + - (void)rosterWasReceived: (XMPPRoster*)roster { - XMPPPresence *pres = [XMPPPresence presence]; - [pres setStatus: @"Hello from JubJub"]; - [connection sendStanza: pres]; + [self sendPresenceWithStatus: @"available" + text: @"Hello from JubJub"]; } @end