Created
July 4, 2010 17:08
-
-
Save kekssw/463590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Juick.h | |
// iJuick | |
// | |
// Created by Vladislav Szajniak on 26.08.09. | |
// Copyright 2009 keks.dev. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "XMPP.h" | |
@class MulticastDelegate; | |
typedef enum { | |
MOD_PM, | |
MOD_POST, | |
MOD_REPLY | |
} ModalMessageType; | |
typedef enum { | |
MSG_NIL, | |
MSG_LIST_FRIENDS, | |
MSG_RESTORE_MESSAGE, | |
MSG_RESTORE_REPLIES, | |
MSG_RETRIEVE_IDENTIFIERS, | |
MSG_DETAILED_SUBSCRITOR, | |
MSG_SUBSCRITOR_MESSAGES, | |
MSG_TIMELINE | |
} JuickMessageType; | |
@interface Juick : NSObject { | |
XMPPClient *client; | |
XMPPUser *juick; | |
JuickMessageType expectedMessage; | |
int myUID; | |
NSString *myNickname; | |
NSString *myActiveJID; | |
int nextLastAfterMID; | |
NSMutableArray *friends; | |
NSMutableDictionary *lastMessages; | |
NSMutableArray *lastMessagesIndex; | |
NSMutableDictionary *friendMessages; | |
NSMutableDictionary *privateMessages; | |
MulticastDelegate *updateDelegate; | |
} | |
- (int)getUIDForName:(NSString *)name; | |
- (NSString *)getAvatarURLForUID:(NSString *)uid; | |
- (NSXMLElement *)sendMessage:(NSString *)bare_text; | |
- (void)applySubscribtionAction:(NSString *)action forMessage:(int)mid; | |
- (BOOL)loginToDomain:(NSString *)domain atPort:(int)port withLogin:(NSString *)login andPassword:(NSString *)password; | |
- (void)postPrivateMessage:(NSString *)text to:(NSString *)username; | |
- (void)postMessage:(NSString *)text withTags:(NSArray *)tags; | |
- (void)postReply:(NSString *)text toMessage:(NSString *)mrid; | |
- (void)subscribeToUser:(NSString *)username; | |
- (void)unsubscribeFromUser:(NSString *)username; | |
- (void)subscribeToMessage:(int)mid; | |
- (void)unsubscribeFromMessage:(int)mid; | |
- (void)requestLastMessagesAfter:(int)aftermid; | |
- (void)requestFriendMessagesForUser:(int)uid after:(int)aftermid; | |
- (void)requestMessage:(int)mid; | |
- (void)requestRepliesForMessage:(int)mid; | |
- (void)requestFriendListForUser:(int)uid; | |
- (void)requestSubscriberListForUser:(int)uid; | |
@property (assign) int myUID; | |
@property (retain) NSString *myNickname; | |
@property (retain) NSString *myActiveJID; | |
@property (assign) MulticastDelegate *updateDelegate; | |
@property (retain) NSMutableArray *friends; | |
@property (retain) NSMutableDictionary *lastMessages; | |
@property (retain) NSMutableArray *lastMessagesIndex; | |
@property (retain) NSMutableDictionary *friendMessages; | |
@property (retain) NSMutableDictionary *privateMessages; | |
@end | |
@interface NSObject (JuickUpdateDelegate) | |
- (void)friendListUpdated; | |
- (void)friendListUpdatingDidBegin; | |
- (void)friendListUpdatingDidEnd; | |
- (void)privateMessageRecieved:(NSXMLElement*)message; | |
- (void)friendMessageRecieved:(NSXMLElement*)message; | |
- (void)lastMessageRecieved:(NSXMLElement*)message; | |
- (void)lastMessagesUpdatingDidBegin; | |
- (void)lastMessagesUpdatingDidEnd:(int)count; | |
- (void)replyRecieved:(NSXMLElement*)replyMessage forMessage:(NSString *)mid; | |
@end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Juick.m | |
// iJuick | |
// | |
// Created by Vladislav Szajniak on 25.08.09. | |
// Copyright 2009 keks.dev. All rights reserved. | |
// | |
#import "Juick.h" | |
#import "MulticastDelegate.h" | |
@implementation Juick | |
@synthesize myUID; | |
@synthesize myActiveJID; | |
@synthesize myNickname; | |
@synthesize updateDelegate; | |
@synthesize friends; | |
@synthesize lastMessages; | |
@synthesize lastMessagesIndex; | |
@synthesize friendMessages; | |
@synthesize privateMessages; | |
- (id)init | |
{ | |
if((self = [super init])) | |
{ | |
updateDelegate = [[MulticastDelegate alloc] init]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[updateDelegate release]; | |
[super dealloc]; | |
} | |
- (void)retrieveMyIdentifiers { | |
// <iq to='[email protected]' id='id345' type='get'> | |
// <query xmlns='http://juick.com/query#users'/> | |
// </iq> | |
NSXMLElement *query = [NSXMLElement elementWithName:@"query"]; | |
[query addAttributeWithName:@"xmlns" stringValue:@"http://juick.com/query#users"]; | |
NSXMLElement *element = [NSXMLElement elementWithName:@"iq"]; | |
[element addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"id%d", myUID]]; | |
[element addAttributeWithName:@"type" stringValue:@"get"]; | |
[element addAttributeWithName:@"to" stringValue:@"[email protected]"]; | |
[element addChild:query]; | |
NSLog(@"Retrieving information about current identifiers..."); | |
[client sendElement:element]; | |
} | |
- (int)getUIDForName:(NSString *)name { | |
return 6189; | |
} | |
- (NSString *)getAvatarURLForUID:(NSString *)uid { | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", uid]]]; | |
[request setTimeoutInterval:3.0]; | |
[request setHTTPMethod:@"HEAD"]; | |
NSHTTPURLResponse *response; | |
NSError *error; | |
//NSLog(@"Temporary connection: Loading image started (%@.png)", uid_str); | |
// Here the data could be used directly to build ImageView (HTTPMethod should be leaved as default GET value) | |
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; | |
(void) data; | |
int status_code = [response statusCode]; | |
//NSLog(@"Temporary connection: Loading image done [Data: (%@); Response code: (%d); Error: (%@)]", data, status_code, error); | |
NSString *default_url = [NSString stringWithFormat:@"file://%@", [[NSBundle mainBundle] pathForResource:@"no_avatar_small" ofType:@"png"]]; | |
return [NSString stringWithString: status_code < 400 ? | |
[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", uid] : default_url]; | |
} | |
- (BOOL)loginToDomain:(NSString *)domain atPort:(int)port withLogin:(NSString *)login andPassword:(NSString *)password { | |
//NSString *domain = @"jabber.ru"; | |
//int port = 5222; | |
juick = nil; | |
friends = nil; | |
nextLastAfterMID = 0; | |
lastMessages = [[NSMutableDictionary alloc] initWithCapacity:0]; | |
lastMessagesIndex = [[NSMutableArray alloc] initWithCapacity:0]; | |
friendMessages = [[NSMutableDictionary alloc] initWithCapacity:0]; | |
privateMessages = [[NSMutableDictionary alloc] initWithCapacity:0]; | |
client = [[XMPPClient alloc] init]; | |
[client addDelegate:self]; | |
[client setDomain:domain]; | |
[client setPort:port]; | |
XMPPJID *jid = [XMPPJID jidWithUser:login domain:domain resource:@"iJuick"]; | |
[client setMyJID:jid]; | |
[client setPassword:password]; | |
NSLog(@"isConnected:%d isAuthenticated:%d", [client isConnected], [client isAuthenticated]); | |
[client connect]; | |
return TRUE; | |
} | |
- (NSXMLElement *)sendMessage:(NSString *)rawText { | |
// <message to="[email protected]/Juick"> | |
// <body>*first *second some text</body> | |
// <juick xmlns="http://juick.com/message" /> | |
// </message> | |
NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; | |
[body setStringValue:rawText]; | |
NSXMLElement *juick_tag = [NSXMLElement elementWithName:@"juick"]; | |
[juick_tag addAttributeWithName:@"xmlns" stringValue:@"http://juick.com/message"]; | |
NSXMLElement *element = [NSXMLElement elementWithName:@"message"]; | |
[element addAttributeWithName:@"to" stringValue:@"[email protected]"]; | |
[element addChild:body]; | |
[element addChild:juick_tag]; | |
[client sendElement:element]; | |
return element; | |
} | |
- (void)postPrivateMessage:(NSString *)text to:(NSString *)username { | |
NSXMLElement *message = [self sendMessage:[NSString stringWithFormat:@"PM @%@ %@", username, text]]; | |
//Tweak the message object | |
NSXMLElement *body = [[message elementsForName:@"body"] objectAtIndex:0]; | |
[body detach]; | |
NSXMLElement *juick_tag = [[message elementsForName:@"juick"] objectAtIndex:0]; | |
[juick_tag detach]; | |
[body setStringValue:text]; | |
[juick_tag addAttributeWithName:@"uname" stringValue:@"me"]; | |
[juick_tag addAttributeWithName:@"uid" stringValue:[NSString stringWithFormat:@"%d", myUID]]; | |
[juick_tag addAttributeWithName:@"ts" stringValue:[[NSDate date] description]]; | |
[juick_tag addChild:body]; | |
// Store private message in stack | |
NSMutableArray *msg_set = [privateMessages objectForKey:username]; | |
if (!msg_set) | |
{ | |
msg_set = [[NSMutableArray alloc] initWithCapacity:1]; | |
[privateMessages setObject:msg_set forKey:username]; | |
} | |
[msg_set addObject:juick_tag]; | |
NSLog(@"Storing private message: %@", [juick_tag description]); | |
[updateDelegate privateMessageRecieved:message]; | |
} | |
- (void)postMessage:(NSString *)text withTags:(NSArray *)tags { | |
NSMutableString *msg = [[NSMutableString alloc] initWithString:@""]; | |
for (NSString *tag in tags) { | |
[msg appendFormat:@"*%@ ", tag]; | |
} | |
[msg appendString:text]; | |
NSLog(@"NEW_MESSAGE: %@", msg); | |
//NSXMLElement *message = | |
[self sendMessage:msg]; | |
} | |
- (void)postReply:(NSString *)text toMessage:(NSString *)mrid { | |
//NSXMLElement *message = | |
[self sendMessage:[NSString stringWithFormat:@"#%@ %@", mrid, text]]; | |
} | |
- (void)subscribeToUser:(NSString *)username { | |
[self sendMessage:[NSString stringWithFormat:@"S %@", username]]; | |
} | |
- (void)unsubscribeFromUser:(NSString *)username { | |
[self sendMessage:[NSString stringWithFormat:@"U %@", username]]; | |
} | |
- (void)applySubscribtionAction:(NSString *)action forMessage:(int)mid { | |
// <iq to='[email protected]' id='id123' type='set'> | |
// <subscriptions xmlns='http://juick.com/subscriptions#messages' | |
// action='subscribe' mid='123456'/> | |
// </iq> | |
NSXMLElement *query = [NSXMLElement elementWithName:@"subscriptions"]; | |
[query addAttributeWithName:@"xmlns" stringValue:@"http://juick.com/subscriptions#messages"]; | |
[query addAttributeWithName:@"mid" stringValue:[NSString stringWithFormat:@"%d", mid]]; | |
[query addAttributeWithName:@"action" stringValue:action]; | |
NSXMLElement *element = [NSXMLElement elementWithName:@"iq"]; | |
[element addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"id%d", myUID]]; | |
[element addAttributeWithName:@"type" stringValue:@"set"]; | |
[element addAttributeWithName:@"to" stringValue:@"[email protected]"]; | |
[element addChild:query]; | |
[client sendElement:element]; | |
} | |
- (void)subscribeToMessage:(int)mid { | |
[self applySubscribtionAction:@"subscribe" forMessage:mid]; | |
} | |
- (void)unsubscribeFromMessage:(int)mid { | |
[self applySubscribtionAction:@"unsubscribe" forMessage:mid]; | |
} | |
- (void)requestLastMessagesAfter:(int)aftermid { | |
// <iq to='[email protected]' id='id123' type='get'> | |
// <query xmlns='http://juick.com/query#messages' aftermid='123456'/> | |
// </iq> | |
NSXMLElement *query = [NSXMLElement elementWithName:@"query"]; | |
[query addAttributeWithName:@"xmlns" stringValue:@"http://juick.com/query#messages"]; | |
if (aftermid) | |
{ | |
[query addAttributeWithName:@"aftermid" stringValue:[NSString stringWithFormat:@"%d", aftermid]]; | |
} | |
NSXMLElement *element = [NSXMLElement elementWithName:@"iq"]; | |
[element addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"id%d", myUID]]; | |
[element addAttributeWithName:@"type" stringValue:@"get"]; | |
[element addAttributeWithName:@"to" stringValue:@"[email protected]"]; | |
[element addChild:query]; | |
[client sendElement:element]; | |
} | |
- (void)requestFriendMessagesForUser:(int)uid after:(int)aftermid { | |
// <iq to='[email protected]' id='id123' type='get'> | |
// <query xmlns='http://juick.com/query#messages' | |
// uid='123' aftermid='123456'/> | |
// </iq> | |
NSXMLElement *query = [NSXMLElement elementWithName:@"query"]; | |
[query addAttributeWithName:@"xmlns" stringValue:@"http://juick.com/query#messages"]; | |
[query addAttributeWithName:@"uid" stringValue:[NSString stringWithFormat:@"%d", uid]]; | |
if (aftermid) | |
{ | |
[query addAttributeWithName:@"aftermid" stringValue:[NSString stringWithFormat:@"%d", aftermid]]; | |
} | |
NSXMLElement *element = [NSXMLElement elementWithName:@"iq"]; | |
[element addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"id%d", myUID]]; | |
[element addAttributeWithName:@"type" stringValue:@"get"]; | |
[element addAttributeWithName:@"to" stringValue:@"[email protected]"]; | |
[element addChild:query]; | |
[client sendElement:element]; | |
} | |
- (void)requestMessage:(int)mid { | |
// <iq to='[email protected]' id='id123' type='get'> | |
// <query xmlns='http://juick.com/query#messages' mid='123456'/> | |
// </iq> | |
NSXMLElement *query = [NSXMLElement elementWithName:@"query"]; | |
[query addAttributeWithName:@"xmlns" stringValue:@"http://juick.com/query#messages"]; | |
[query addAttributeWithName:@"mid" stringValue:[NSString stringWithFormat:@"%d", mid]]; | |
NSXMLElement *element = [NSXMLElement elementWithName:@"iq"]; | |
[element addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"id%d", myUID]]; | |
[element addAttributeWithName:@"type" stringValue:@"get"]; | |
[element addAttributeWithName:@"to" stringValue:@"[email protected]"]; | |
[element addChild:query]; | |
[client sendElement:element]; | |
} | |
- (void)requestRepliesForMessage:(int)mid { | |
// <iq to='[email protected]' id='id123' type='get'> | |
// <query xmlns='http://juick.com/query#messages' mid='123456' rid='*'/> | |
// </iq> | |
NSXMLElement *query = [NSXMLElement elementWithName:@"query"]; | |
[query addAttributeWithName:@"xmlns" stringValue:@"http://juick.com/query#messages"]; | |
[query addAttributeWithName:@"mid" stringValue:[NSString stringWithFormat:@"%d", mid]]; | |
[query addAttributeWithName:@"rid" stringValue:[NSString stringWithString:@"*"]]; | |
NSXMLElement *element = [NSXMLElement elementWithName:@"iq"]; | |
[element addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"id%d", myUID]]; | |
[element addAttributeWithName:@"type" stringValue:@"get"]; | |
[element addAttributeWithName:@"to" stringValue:@"[email protected]"]; | |
[element addChild:query]; | |
[client sendElement:element]; | |
} | |
- (void)requestFriendListForUser:(int)uid { | |
// <iq to='[email protected]' id='id123' type='get'> | |
// <query xmlns='http://juick.com/query#users' friends='123'/> | |
// </iq> | |
NSXMLElement *query = [NSXMLElement elementWithName:@"query"]; | |
[query addAttributeWithName:@"xmlns" stringValue:@"http://juick.com/query#users"]; | |
[query addAttributeWithName:@"friends" stringValue:[NSString stringWithFormat:@"%d", uid]]; | |
NSXMLElement *element = [NSXMLElement elementWithName:@"iq"]; | |
[element addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"id%d", uid]]; | |
[element addAttributeWithName:@"type" stringValue:@"get"]; | |
[element addAttributeWithName:@"to" stringValue:@"[email protected]"]; | |
[element addChild:query]; | |
[client sendElement:element]; | |
} | |
- (void)requestSubscriberListForUser:(int) uid { | |
// <iq to='[email protected]' id='id123' type='get'> | |
// <query xmlns='http://juick.com/query#users' subscribers='123'/> | |
// </iq> | |
NSXMLElement *query = [NSXMLElement elementWithName:@"query"]; | |
[query addAttributeWithName:@"xmlns" stringValue:@"http://juick.com/query#users"]; | |
[query addAttributeWithName:@"subscribers" stringValue:[NSString stringWithFormat:@"%d", uid]]; | |
NSXMLElement *element = [NSXMLElement elementWithName:@"iq"]; | |
[element addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"id%d", uid]]; | |
[element addAttributeWithName:@"type" stringValue:@"get"]; | |
[element addAttributeWithName:@"to" stringValue:@"[email protected]"]; | |
[element addChild:query]; | |
[client sendElement:element]; | |
} | |
- (void)pollForLatestMessages:(NSTimer*)theTimer { | |
NSLog(@"PollForLatestMessages Timer Fired! (Latest MID: %d)", nextLastAfterMID); | |
[updateDelegate lastMessagesUpdatingDidBegin]; | |
[self requestLastMessagesAfter:nextLastAfterMID]; | |
} | |
- (void)xmppClientDidConnect:(XMPPClient *)sender { | |
NSLog(@"+++ xmppClientDidConnect"); | |
NSLog(@"isConnected:%d isAuthenticated:%d", [client isConnected], [client isAuthenticated]); | |
[client authenticateUser]; | |
} | |
- (void)xmppClientDidDisconnect:(XMPPClient *)sender { | |
NSLog(@"+++ xmppClientDidDisconnect"); | |
NSLog(@"isConnected:%d isAuthenticated:%d", [client isConnected], [client isAuthenticated]); | |
[juick release]; | |
juick = nil; | |
[friends release]; | |
friends = nil; | |
[client release]; | |
client = nil; | |
[lastMessages release]; | |
[friendMessages release]; | |
[privateMessages release]; | |
} | |
- (void)xmppClientDidAuthenticate:(XMPPClient *)sender { | |
NSLog(@"+++ xmppClientDidAuthenticate"); | |
NSLog(@"isConnected:%d isAuthenticated:%d", [client isConnected], [client isAuthenticated]); | |
} | |
- (void)xmppClientDidUpdateRoster:(XMPPClient *)sender { | |
NSLog(@"+++ xmppClientDidUpdateRoster"); | |
if (juick) | |
return; | |
NSArray *contacts = [sender sortedUsersByName]; | |
NSLog(@"Contacts: <%@>", contacts); | |
for (XMPPUser *contact in contacts) | |
{ | |
if ([[[contact jid] bare] isEqualToString:@"[email protected]"]) | |
{ | |
NSLog(@"JUICK detected!!!!"); | |
juick = [contact retain]; | |
// myUID = [self getUIDForName:@"iJuick"]; | |
expectedMessage = MSG_RETRIEVE_IDENTIFIERS; | |
[self retrieveMyIdentifiers]; | |
} | |
} | |
} | |
- (void)xmppClient:(XMPPClient *)sender didReceiveIQ:(XMPPIQ *)iq { | |
NSLog(@"+++ didReceiveIQ"); | |
NSLog(@"IQ: <%@>", iq); | |
if (expectedMessage == MSG_LIST_FRIENDS) | |
{ | |
NSLog(@"MSG_LIST_FRIENDS"); | |
if ([[[iq attributeForName:@"type"] stringValue] isEqualToString:@"error"]) | |
{ | |
friends = nil; | |
NSLog(@"JUICK ERROR: Couldn't get friendlist"); | |
// TODO: Generate an error | |
} | |
else | |
{ | |
NSXMLElement *query = [[iq elementsForName:@"query"] objectAtIndex:0]; | |
NSMutableArray *users = [[NSMutableArray alloc] initWithArray:[query elementsForName:@"user"]]; | |
friends = [users retain]; | |
NSLog(@"Friends: <%@>", friends); | |
} | |
[updateDelegate friendListUpdated]; | |
[updateDelegate friendListUpdatingDidEnd]; | |
expectedMessage = MSG_NIL; | |
} | |
else if (expectedMessage == MSG_RESTORE_MESSAGE) | |
{ | |
NSLog(@"MSG_RESTORE_MESSAGE"); | |
expectedMessage = MSG_RESTORE_REPLIES; | |
NSXMLElement *query = [[iq elementsForName:@"query"] objectAtIndex:0]; | |
[query detach]; | |
[self xmppClient:sender didReceiveMessage:[XMPPMessage messageFromElement:query]]; | |
} | |
else if (expectedMessage == MSG_RESTORE_REPLIES) | |
{ | |
NSLog(@"MSG_RESTORE_REPLIES"); | |
expectedMessage = MSG_NIL; | |
NSXMLElement *query = [[iq elementsForName:@"query"] objectAtIndex:0]; | |
for (NSXMLElement *juick_tag in [query elementsForName:@"juick"]) { | |
[juick_tag detach]; | |
NSXMLElement *msg = [NSXMLElement elementWithName:@"message"]; | |
[msg addChild:juick_tag]; | |
[self xmppClient:sender didReceiveMessage:[XMPPMessage messageFromElement:msg]]; | |
} | |
} | |
else if (expectedMessage == MSG_RETRIEVE_IDENTIFIERS) | |
{ | |
NSLog(@"MSG_RETRIEVE_IDENTIFIERS"); | |
expectedMessage = MSG_NIL; | |
NSXMLElement *query = [[iq elementsForName:@"query"] objectAtIndex:0]; | |
NSXMLElement *user = [[query elementsForName:@"user"] objectAtIndex:0]; | |
myUID = [[[user attributeForName:@"uid"] stringValue] integerValue]; | |
myNickname = [[user attributeForName:@"uname"] stringValue]; | |
myActiveJID = [[user attributeForName:@"jid"] stringValue]; | |
NSLog(@"We're logged in as: %@ (#%d) from JID: %@", myNickname, myUID, myActiveJID); | |
[updateDelegate friendListUpdatingDidBegin]; | |
[self requestFriendListForUser:myUID]; | |
expectedMessage = MSG_LIST_FRIENDS; | |
NSTimer *pollingLatestTimer = [NSTimer scheduledTimerWithTimeInterval:33 target:self selector:@selector(pollForLatestMessages:) userInfo:nil repeats:YES]; | |
[pollingLatestTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]]; | |
// Test messages on startup - 2 B REMOVED: | |
//[self postMessage:@"Тестирование тэгов (и geo-а)..." withTags:[NSArray arrayWithObjects:@"geo", @"test", nil]]; | |
//[self postPrivateMessage:@"Напишите мне письмо..." to:@"kekssw"]; | |
//[self requestRepliesForMessage:256789]; | |
//[self requestSubscriberListForUser:myUID]; | |
// [updateDelegate identifiersUpdated]; | |
} | |
else | |
{ | |
// No message expected. Let's assume them to be a response of polling for Last Messages | |
NSLog(@"MSG_NIL === MSG_LATEST_MESSAGE_POLLING"); | |
int msgCount = 0; | |
if ([[[iq attributeForName:@"type"] stringValue] isEqualToString:@"error"]) | |
{ | |
NSLog(@"404 ERROR = No new messages"); | |
} | |
else | |
{ | |
NSXMLElement *query = [[iq elementsForName:@"query"] objectAtIndex:0]; | |
msgCount = [[query elementsForName:@"juick"] count]; | |
for (NSXMLElement *juick_tag in [query elementsForName:@"juick"]) { | |
[juick_tag detach]; | |
[juick_tag addAttributeWithName:@"unread" stringValue:@"1"]; | |
NSString *mid = [[juick_tag attributeForName:@"mid"] stringValue]; | |
[lastMessages setObject:juick_tag forKey:mid]; | |
[lastMessagesIndex addObject:mid]; | |
[updateDelegate lastMessageRecieved:juick_tag]; | |
int currentMID = [mid intValue]; | |
if (currentMID > nextLastAfterMID) nextLastAfterMID = currentMID; | |
} | |
//NSLog(@"lastMessagesIndex now is: %@", lastMessagesIndex); | |
} | |
[updateDelegate lastMessagesUpdatingDidEnd:msgCount]; | |
} | |
NSLog(@"--- didReceiveIQ"); | |
} | |
- (void)xmppClient:(XMPPClient *)sender didReceiveMessage:(XMPPMessage *)message { | |
NSLog(@"+++ didReceiveMessage"); | |
NSLog(@"Message: <%@>", message); | |
NSArray *obj = [message elementsForName:@"juick"]; | |
if (![obj count]) | |
{ | |
NSLog(@"Juick stanza missed - suspected to be a text response on the previously sent message"); | |
} | |
else | |
{ | |
NSXMLElement *juick_tag = [obj objectAtIndex:0]; | |
[juick_tag detach]; | |
NSString *user_name = [[juick_tag attributeForName:@"uname"] stringValue]; | |
NSString *mid = [[juick_tag attributeForName:@"mid"] stringValue]; | |
if (!mid) | |
{ | |
NSLog(@"MSG: Private message recieved"); | |
[juick_tag addAttributeWithName:@"ts" stringValue:[[NSDate date] description]]; | |
NSMutableArray *msg_set = [privateMessages objectForKey:user_name]; | |
if (!msg_set) | |
{ | |
msg_set = [[NSMutableArray alloc] initWithCapacity:1]; | |
[privateMessages setObject:msg_set forKey:user_name]; | |
} | |
[msg_set addObject:juick_tag]; | |
[updateDelegate privateMessageRecieved:juick_tag]; | |
} | |
else | |
{ | |
NSString *rid = [[juick_tag attributeForName:@"rid"] stringValue]; | |
if (!rid) | |
{ | |
NSLog(@"MSG: Friend message recieved"); | |
[friendMessages setObject:juick_tag forKey:mid]; | |
[updateDelegate friendMessageRecieved:juick_tag]; | |
} | |
else | |
{ | |
NSLog(@"MSG: Reply message recieved"); | |
NSXMLElement *original_post = [friendMessages objectForKey:mid]; | |
if (!original_post) | |
{ | |
expectedMessage = MSG_RESTORE_MESSAGE; | |
[self requestMessage:[mid intValue]]; | |
[self requestRepliesForMessage:[mid intValue]]; | |
// TODO: Restore original post with its comments (including current one) | |
} | |
else | |
{ | |
[original_post addChild:juick_tag]; | |
[updateDelegate replyRecieved:juick_tag forMessage:mid]; | |
} | |
} | |
} | |
NSMutableString *msg = [[NSMutableString alloc] initWithString: user_name]; | |
[msg appendFormat:@": %@", [[[juick_tag elementsForName:@"body"] objectAtIndex:0] stringValue]]; | |
NSLog(@" >> %@ ", msg); | |
} | |
NSLog(@"--- didReceiveMessage"); | |
} | |
/* | |
- (void)xmppClient:(XMPPClient *)sender didReceiveMessage:(XMPPMessage *)message { | |
NSLog(@"+++ didReceiveMessage"); | |
NSString *msg = [[message elementsForName:@"body"] description]; | |
NSMutableArray *users = [[NSMutableArray alloc] initWithArray:[msg componentsSeparatedByString:@"\n"]]; | |
[users removeObjectAtIndex:0]; | |
[users removeLastObject]; | |
NSString *end = [[users objectAtIndex:0] description]; | |
[users removeObjectAtIndex:0]; | |
[users insertObject:[end substringFromIndex:10] atIndex:0]; //Trim " <body>" prefix | |
end = [[users lastObject] description]; | |
[users removeLastObject]; | |
[users addObject:[end substringToIndex:[end length]-7]]; //Trim "</body>" suffix | |
if (expectedMessage == MSG_LIST_FRIENDS) | |
{ | |
NSLog(@"MSG_LIST_FRIENDS"); | |
[users removeObjectAtIndex:0]; | |
friends = [users retain]; | |
NSLog([friends componentsJoinedByString:@" == "]); | |
[updateDelegate friendListUpdated]; | |
} | |
if (expectedMessage == MSG_DETAILED_SUBSCRITOR) | |
{ | |
NSLog(@"MSG_DETAILED_SUBSCRITOR"); | |
NSMutableDictionary *details = [[NSMutableDictionary alloc] init]; | |
for (NSObject *line in users) | |
{ | |
NSString *prop = [line description]; | |
NSArray *pair = [prop componentsSeparatedByString:@": "]; | |
[details setValue:[[pair objectAtIndex:1] description] forKey:[[pair objectAtIndex:0] description]]; | |
} | |
//anotherViewController.details = details; | |
NSLog([users description]); | |
} | |
if (expectedMessage == MSG_SUBSCRITOR_MESSAGES) | |
{ | |
NSLog(@"MSG_SUBSCRITOR_MESSAGES"); | |
//[(MessageViewController *) self.navigationController.topViewController setMessages:users]; | |
NSLog(@"Data renewal"); | |
//[[(MessageViewController *) self.navigationController.topViewController tableView] reloadData]; | |
} | |
expectedMessage = MSG_NIL; | |
[users release]; | |
} | |
*/ | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Objective-C Juick library.