]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/core/JubChatClient.m
Move presence changing to JubChatClient
[jubjub.git] / src / core / JubChatClient.m
1 #import "JubChatClient.h"
2
3 @implementation JubChatClient
4 @synthesize connection = _connection;
5 @synthesize roster = _roster;
6 @synthesize contactManager = _contactManager;
7 @synthesize presence = _presence;
8 @synthesize ui = _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                 [_contactManager addDelegate: self];
31
32                 _streamManagement = [[XMPPStreamManagement alloc]
33                     initWithConnection: _connection];
34         } @catch (id e) {
35                 [self release];
36                 @throw e;
37         }
38
39         return self;
40 }
41
42 - (void)dealloc
43 {
44         [_roster removeDelegate: self];
45         [_contactManager removeDelegate: self];
46         [_connection removeDelegate: self];
47
48         [_roster release];
49         [_contactManager release];
50         [_streamManagement release];
51         [_connection release];
52         [_presence release];
53         [_chatMap release];
54
55         [super dealloc];
56 }
57
58 - (id<JubChatUI>)chatForContact: (XMPPContact*)contact
59 {
60         OFAutoreleasePool *pool = [OFAutoreleasePool new];
61         OFString *bareJID = [contact.rosterItem.JID bareJID];
62
63         id<JubChatUI> chat = [_chatMap objectForKey: bareJID];
64         if (chat == nil) {
65                 OFString * title =
66                     [@"Chat with " stringByAppendingString: bareJID];
67
68                 chat = [[[[_ui chatUIClass] alloc]
69                     initWithTitle: title
70                        closeBlock: ^{
71                                 [_chatMap removeObjectForKey: bareJID];
72                         }
73                         sendBlock: ^(OFString *text) {
74                                 XMPPMessage *msg =
75                                     [XMPPMessage messageWithType: @"chat"];
76                                 msg.body = text;
77                                 [contact sendMessage: msg
78                                           connection: _connection];
79                         }
80                 ] autorelease];
81
82                 [_chatMap setObject: chat
83                              forKey: bareJID];
84         }
85
86         [pool release];
87
88         return chat;
89 }
90
91 - (void)sendPresenceWithStatus: (OFString*)status
92 {
93         [self sendPresenceWithStatus: status
94                                 text: nil];
95 }
96
97 - (void)sendPresenceWithStatus: (OFString*)status
98                           text: (OFString*)text
99 {
100         XMPPPresence *presence;
101
102         if ([status isEqual: @"unavailable"])
103                 presence = [XMPPPresence presenceWithType: @"unavailable"];
104         else
105                 presence = [XMPPPresence presence];
106
107         if (!([status isEqual: @"available"] ||
108               [status isEqual: @"unavailable"]))
109                 presence.show = status;
110
111         if (text != nil)
112                 presence.status = text;
113
114         [_connection sendStanza: presence];
115 }
116
117 - (void)connection: (XMPPConnection*)connection
118      wasBoundToJID: (XMPPJID*)jid
119 {
120         of_log(@"Bound to JID: %@", [jid fullJID]);
121
122         [_roster requestRoster];
123 }
124
125 -   (void)connection: (XMPPConnection*)connection
126   didReceivePresence: (XMPPPresence*)presence
127 {
128         if ([presence.from isEqual: connection.JID]) {
129                 [_ui         client: self
130                   didChangePresence: presence];
131                 OF_SETTER(_presence, presence, YES, 0);
132         }
133 }
134
135 -  (void)contact: (XMPPContact*)contact
136   didSendMessage: (XMPPMessage*)message
137 {
138         if (message.body == nil || ![message.type isEqual: @"chat"])
139                 return;
140
141         id<JubChatUI> chat = [self chatForContact: contact];
142         [chat addMessage: message.body
143                   sender: [message.from bareJID]];
144 }
145
146 - (void)rosterWasReceived: (XMPPRoster*)roster
147 {
148         XMPPPresence *pres = [XMPPPresence presence];
149         [pres setStatus: @"Hello from JubJub"];
150         [_connection sendStanza: pres];
151 }
152 @end