]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/core/JubChatClient.m
Add a zxcv-like CLI frontend
[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)connection: (XMPPConnection*)connection
92      wasBoundToJID: (XMPPJID*)jid
93 {
94         of_log(@"Bound to JID: %@", [jid fullJID]);
95
96         [_roster requestRoster];
97 }
98
99 -   (void)connection: (XMPPConnection*)connection
100   didReceivePresence: (XMPPPresence*)presence
101 {
102         if ([presence.from isEqual: connection.JID]) {
103                 [_ui         client: self
104                   didChangePresence: presence];
105                 OF_SETTER(_presence, presence, YES, 0);
106         }
107 }
108
109 -  (void)contact: (XMPPContact*)contact
110   didSendMessage: (XMPPMessage*)message
111 {
112         if (message.body == nil || ![message.type isEqual: @"chat"])
113                 return;
114
115         id<JubChatUI> chat = [self chatForContact: contact];
116         [chat addMessage: message.body
117                   sender: [message.from bareJID]];
118 }
119
120 - (void)rosterWasReceived: (XMPPRoster*)roster
121 {
122         XMPPPresence *pres = [XMPPPresence presence];
123         [pres setStatus: @"Hello from JubJub"];
124         [_connection sendStanza: pres];
125 }
126 @end