]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/core/JubChatClient.m
3c563f5e9dd1a9f267b9b09ad63787a8be9de1ea
[jubjub.git] / src / core / JubChatClient.m
1 #import "JubChatClient.h"
2
3 @implementation JubChatClient
4 @synthesize connection;
5 @synthesize roster;
6 @synthesize contactManager;
7 @synthesize presence;
8 @synthesize ui;
9
10 - initWithConfig: (JubConfig*)config
11 {
12         self = [super init];
13
14         @try {
15                 chatMap = [[OFMutableDictionary alloc] init];
16
17                 connection = [XMPPConnection new];
18                 connection.username = config.username;
19                 connection.domain = config.domain;
20                 connection.server = config.server;
21                 connection.password = config.password;
22                 [connection addDelegate: self];
23
24                 roster = [[XMPPRoster alloc] initWithConnection: connection];
25                 [roster addDelegate: self];
26
27                 contactManager = [[XMPPContactManager alloc]
28                                       initWithConnection: connection
29                                                   roster: roster];
30
31                 streamManagement = [[XMPPStreamManagement alloc]
32                     initWithConnection: connection];
33
34                 [connection asyncConnectAndHandle];
35         } @catch (id e) {
36                 [self release];
37                 @throw e;
38         }
39
40         return self;
41 }
42
43 - (void)dealloc
44 {
45         [roster release];
46         [contactManager release];
47         [streamManagement release];
48         [connection release];
49         [presence release];
50         [chatMap release];
51
52         [super dealloc];
53 }
54
55 - (id<JubChatUI>)chatForContact: (XMPPContact*)contact
56 {
57         OFAutoreleasePool *pool = [OFAutoreleasePool new];
58         OFString *bareJID = [contact.rosterItem.JID bareJID];
59
60         id<JubChatUI> chat = [chatMap objectForKey: bareJID];
61         if (chat == nil) {
62                 OFString * title =
63                     [@"Chat with " stringByAppendingString: bareJID];
64
65                 chat = [[[[ui chatUIClass] alloc]
66                     initWithTitle: title
67                        closeBlock: ^{
68                                 [chatMap removeObjectForKey: bareJID];
69                         }
70                         sendBlock: ^(OFString *text) {
71                                 XMPPMessage *msg =
72                                     [XMPPMessage messageWithType: @"chat"];
73                                 msg.body = text;
74                                 [contact sendMessage: msg
75                                           connection: connection];
76                         }
77                 ] autorelease];
78
79                 [chatMap setObject: chat
80                             forKey: bareJID];
81         }
82
83         [pool release];
84
85         return chat;
86 }
87
88 - (void)connection: (XMPPConnection*)connection_
89      wasBoundToJID: (XMPPJID*)jid
90 {
91         of_log(@"Bound to JID: %@", [jid fullJID]);
92
93         [roster requestRoster];
94 }
95
96 -   (void)connection: (XMPPConnection*)connection_
97   didReceivePresence: (XMPPPresence*)presence_
98 {
99         if ([presence_.from isEqual: connection.JID]) {
100                 [ui          client: self
101                   didChangePresence: presence_];
102                 OF_SETTER(presence, presence_, YES, 0);
103         }
104 }
105
106 - (void)rosterWasReceived: (XMPPRoster*)roster
107 {
108         XMPPPresence *pres = [XMPPPresence presence];
109         [pres setStatus: @"Hello from JubJub"];
110         [connection sendStanza: pres];
111 }
112 @end