Example #1
0
void TalkManager::ProcessMessageData(XMLEntity *entity)
{
	ChatWindow::talk_type type;
	string                thread_id;
	string                sender;
	string				  receiver;

	string                group_room;
	string                group_server;
	string                group_identity;
	string                group_username;
	
	// must be sender to continue
	if (!entity->Attribute("from"))
	{
		fprintf(stderr, "From is unspecified. Return.\n");
		return;
	}

	// determine message type
	if (!entity->Attribute("type"))
	{
		if (entity->Child("x", "xmlns", "jabber:x:oob"))
			type = ChatWindow::GROUP;
		else
		{
			fprintf(stderr, "Type is unspecified. Return.\n");
			return;
		}
	}
	else if (!strcasecmp(entity->Attribute("type"), "normal"))
	{
		type = ChatWindow::CHAT;
	}
	else if (!strcasecmp(entity->Attribute("type"), "chat"))
	{
		type = ChatWindow::CHAT;
	}
	else if (!strcasecmp(entity->Attribute("type"), "groupchat"))
	{
		type = ChatWindow::GROUP;
	}
	else if (!strcasecmp(entity->Attribute("type"), "error"))
	{
		char buffer[2048];
		
		if (entity->Child("error")) {
			const char *text = entity->Child("error")->Child("text") ? entity->Child("error")->Child("text")->Data() : "Unknown";
			sprintf(buffer, "An error occurred when you tried sending a message to %s.  The reason is as follows:\n\n%s", entity->Attribute("from"), text);
			ModalAlertFactory::Alert(buffer, "Oh, well.", NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
		}
		
		//delete entity;
		
		return;
	}
	else if (!strcasecmp(entity->Attribute("type"), "headline"))
	{
		fprintf(stderr, "Tune message.\n");
		return;
	}
	else
	{
		type = ChatWindow::CHAT;
	}
	
	sender = string(entity->Attribute("from"));
	receiver = string(entity->Attribute("to"));
	group_username = UserID(sender).JabberResource();
	group_room = UserID(sender).JabberUsername();
	UserID sender_user(sender);

	ChatWindow *window = FindWindow(sender_user.JabberHandle());
		
	if (!window)
	{
		//JRoster::Instance()->Lock();
			
			  
		UserID *user = BlabberMainWindow::Instance()->fRosterView->
							fUsers[sender_user.JabberHandle()];
						//JRoster::Instance()->FindUser(&sender_user);
		
		if (!user)
		{
			user = new UserID(sender_user);
			
			fprintf(stderr, "Username: %s.\n", user->JabberUsername().c_str());
			fprintf(stderr, "Server: %s.\n", user->JabberServer().c_str());
			fprintf(stderr, "Resource: %s.\n", user->JabberResource().c_str());
			fprintf(stderr, "Valid?: %s.\n", user->WhyNotValidJabberHandle().c_str());
			if (user->JabberServer().empty())
			{
				user->SetFriendlyName(string("Server Chat"));
				user->SetJabberServer(sender);
				fprintf(stderr, "Username: %s.\n", user->JabberUsername().c_str());
				fprintf(stderr, "Server: %s.\n", user->JabberServer().c_str());
				fprintf(stderr, "Resource: %s.\n", user->JabberResource().c_str());
			}
			fprintf(stderr, "Not found incoming message user in roster.\n");
		}

		//JRoster::Instance()->Unlock();
			
		if (type==ChatWindow::CHAT /*|| type==ChatWindow::GROUP*/ )
		{
			window = CreateTalkSession(type, user);
			fprintf(stderr, "Create Window for incoming message.\n");
			window->jabber = jabber;
		} else {
			fprintf(stderr, "Unexisted Groupchat Window. No route\n");
			return;
		}
	}
	else
	{
		fprintf(stderr, "Redirected to Existed Window: %s.\n", sender_user.JabberHandle().c_str());
	}
				
	fprintf(stderr, "Lock. ");
	window->Lock();
	
	//fprintf(stderr, "Activate. ");
	//window->Activate();
			
	string body;
	string subject;
		
	if (entity->Child("body") && entity->Child("body")->Data())
		body = string(entity->Child("body")->Data());
		
	if (entity->Child("subject") && entity->Child("subject")->Data())
		subject = string(entity->Child("subject")->Data());
				
	if (type == ChatWindow::CHAT && !body.empty())
	{
		window->NewMessage(body);
	}
	else if (type == ChatWindow::GROUP && (!body.empty() || !subject.empty()))
	{
		//Accept exectly our JID in destinations 
		if (UserID(receiver).JabberHandle() != UserID(jabber->jid.String()).JabberHandle())
		{
			window->Unlock();
			fprintf(stderr, "Unlock.\n");
			delete entity;
			return;
		}
		
		if (group_username == "")
			window->AddToTalk(group_room, body, ChatWindow::MAIN_RECIPIENT); // channel messages
		else
		{
			if (!subject.empty())
			{
				window->AddToTalk(group_username, subject, ChatWindow::TOPIC); // topic messages
				fprintf(stderr, "Topic message.\n");
			}
			else			
				window->AddToTalk(group_username, body, ChatWindow::MAIN_RECIPIENT); // user messages
		}
	}
		
	window->Unlock();
	fprintf(stderr, "Unlock.\n");
	
}
void
JabberProtocol::ProcessPresence(XMLEntity *entity)
{
	

	int num_matches = 0;

	// verify we have a username
	if (entity->Attribute("from"))
	{
		// circumvent groupchat presences
		string room, server, user;
		
		if (entity->Child("x", "xmlns", "http://jabber.org/protocol/muc#user"))
		{
			UserID from = UserID(string(entity->Attribute("from")));
			room = from.JabberUsername();
			server = from.JabberServer();
			user = from.JabberResource();
			fprintf(stderr, "Group Presence in room %s from user %s.\n", 
				from.JabberHandle().c_str(), user.c_str());
						
			BMessage *msg = new BMessage(JAB_GROUP_CHATTER_ONLINE);
			msg->AddString("room", (room + '@' + server).c_str());
			msg->AddString("server", server.c_str());
			msg->AddString("username", user.c_str());
			
			if (!entity->Attribute("type") || !strcasecmp(entity->Attribute("type"), "available"))
			{
				if (entity->Child("show") && entity->Child("show")->Data())
				{
					msg->AddString("show", entity->Child("show")->Data());
				} else
					msg->AddString("show", "online");

				if (entity->Child("status") && entity->Child("status")->Data())
				{
					msg->AddString("status", entity->Child("status")->Data());
				} else
					msg->AddString("status", "");
				
				if (entity->Child("x")->Child("item") &&
					entity->Child("x")->Child("item")->Attribute("role"))
					msg->AddString("role", entity->Child("x")->Child("item")->Attribute("role"));
				else
					msg->AddString("role", "admin");
				
				if (entity->Child("x")->Child("item") &&
					entity->Child("x")->Child("item")->Attribute("affiliation"))
					msg->AddString("affiliation", entity->Child("x")->Child("item")->Attribute("affiliation"));
				else
					msg->AddString("affiliation", "none");
		
				msg->what = JAB_GROUP_CHATTER_ONLINE;
			}
			else if (!strcasecmp(entity->Attribute("type"), "unavailable"))
			{
				msg->what = JAB_GROUP_CHATTER_OFFLINE;
			}
			
			TalkManager::Instance()->Lock();
			
			ChatWindow *window = TalkManager::Instance()->FindWindow(from.JabberHandle());
			
			if (window != NULL)
			{
				fprintf(stderr, "Process group presence %s.\n", window->GetUserID()->JabberHandle().c_str());
				
				window->PostMessage(msg);
			}
			else
			{
				fprintf(stderr, "There is no window group presence route to.\n");
			}
			
			TalkManager::Instance()->Unlock();
			
			return;
		}		
		
		JRoster *roster = JRoster::Instance();
		
		roster->Lock();
		
		for (JRoster::ConstRosterIter i = roster->BeginIterator(); i != roster->EndIterator(); ++i)
		{
			UserID *user = NULL;

			if (!strcasecmp(UserID(entity->Attribute("from")).JabberHandle().c_str(),
					(*i)->JabberHandle().c_str()))
			{
				++num_matches;
				user = *i;
				ProcessUserPresence(user, entity);
				fprintf(stderr, "Process roster presence %s.\n", user->JabberHandle().c_str());
			}
		}
		
		if (num_matches == 0)
		{
			UserID user(string(entity->Attribute("from")));
			fprintf(stderr, "Process not in roster presence %s.\n", user.JabberHandle().c_str());
			ProcessUserPresence(&user, entity);
		}
			
		roster->Unlock();
		
		mainWindow->PostMessage(BLAB_UPDATE_ROSTER);			

	}
}