2010-05-07 04:47:10 -05:00
|
|
|
/*
|
2011-12-03 16:38:03 -06:00
|
|
|
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
|
|
|
|
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
|
2010-05-07 04:47:10 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Andrea Anzani, andrea.anzani@gmail.com
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
2011-12-14 17:36:27 -06:00
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* Dario Casalinuovo
|
2010-05-07 04:47:10 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Application.h>
|
2010-05-16 16:02:50 -05:00
|
|
|
#include <Debug.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <Entry.h>
|
2010-05-30 07:17:32 -05:00
|
|
|
#include <Notification.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <Path.h>
|
|
|
|
#include <TranslationUtils.h>
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "Account.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "AccountManager.h"
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "ProtocolLooper.h"
|
|
|
|
#include "CayaMessages.h"
|
|
|
|
#include "CayaProtocol.h"
|
2010-05-19 15:37:26 -05:00
|
|
|
#include "CayaProtocolMessages.h"
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "ChatWindow.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "ImageCache.h"
|
|
|
|
#include "ProtocolManager.h"
|
|
|
|
#include "RosterItem.h"
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "Server.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
|
|
|
|
Server::Server()
|
2010-05-19 17:28:26 -05:00
|
|
|
:
|
2012-02-27 18:46:15 -06:00
|
|
|
BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-19 17:28:26 -05:00
|
|
|
void
|
2010-05-07 04:47:10 -05:00
|
|
|
Server::Quit()
|
|
|
|
{
|
2010-05-28 17:38:16 -05:00
|
|
|
ContactLinker* linker = NULL;
|
2010-05-16 16:02:50 -05:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
while ((linker = fRosterMap.ValueAt(0))) {
|
|
|
|
linker->DeleteWindow();
|
|
|
|
linker->DeletePopUp();
|
|
|
|
fRosterMap.RemoveItemAt(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-16 16:02:50 -05:00
|
|
|
Server::AddProtocolLooper(bigtime_t instanceId, CayaProtocol* cayap)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
ProtocolLooper* looper = new ProtocolLooper(cayap);
|
|
|
|
fLoopers.AddItem(instanceId, looper);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-16 16:02:50 -05:00
|
|
|
Server::RemoveProtocolLooper(bigtime_t instanceId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Server::LoginAll()
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
for (uint32 i = 0; i < fLoopers.CountItems(); i++) {
|
|
|
|
ProtocolLooper* looper = fLoopers.ValueAt(i);
|
|
|
|
|
|
|
|
BMessage* msg = new BMessage(IM_MESSAGE);
|
2010-05-19 15:37:26 -05:00
|
|
|
msg->AddInt32("im_what", IM_SET_OWN_STATUS);
|
2010-05-16 16:02:50 -05:00
|
|
|
msg->AddInt32("status", CAYA_ONLINE);
|
|
|
|
looper->PostMessage(msg);
|
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
void
|
|
|
|
Server::SendProtocolMessage(BMessage* msg)
|
|
|
|
{
|
|
|
|
// Skip null messages
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check if message contains the instance field
|
|
|
|
bigtime_t id;
|
|
|
|
if (msg->FindInt64("instance", &id) == B_OK) {
|
|
|
|
bool found = false;
|
|
|
|
ProtocolLooper* looper
|
|
|
|
= fLoopers.ValueFor(id, &found);
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
looper->PostMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Server::SendAllProtocolMessage(BMessage* msg)
|
|
|
|
{
|
|
|
|
// Skip null messages
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Send message to all protocols
|
|
|
|
for (uint32 i = 0; i < fLoopers.CountItems(); i++) {
|
|
|
|
ProtocolLooper* looper = fLoopers.ValueAt(i);
|
|
|
|
looper->PostMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
filter_result
|
|
|
|
Server::Filter(BMessage* message, BHandler **target)
|
|
|
|
{
|
|
|
|
filter_result result = B_DISPATCH_MESSAGE;
|
|
|
|
|
2010-05-19 17:28:26 -05:00
|
|
|
switch (message->what) {
|
2010-05-16 16:02:50 -05:00
|
|
|
case IM_MESSAGE_RECEIVED:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
BString id = message->FindString("id");
|
|
|
|
if (id.Length() > 0) {
|
|
|
|
bool found = false;
|
|
|
|
ContactLinker* item = fRosterMap.ValueFor(id, &found);
|
|
|
|
if (found) {
|
|
|
|
ChatWindow* win = item->GetChatWindow();
|
|
|
|
item->ShowWindow();
|
|
|
|
win->PostMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = B_SKIP_MESSAGE;
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
|
|
|
}
|
2011-12-14 17:36:27 -06:00
|
|
|
case CAYA_CLOSE_CHAT_WINDOW:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
BString id = message->FindString("id");
|
2010-05-16 16:02:50 -05:00
|
|
|
if (id.Length() > 0) {
|
2010-05-07 04:47:10 -05:00
|
|
|
bool found = false;
|
2010-05-20 16:31:55 -05:00
|
|
|
ContactLinker* item = fRosterMap.ValueFor(id, &found);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
if (found)
|
2010-05-28 17:38:16 -05:00
|
|
|
item->HideWindow();
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
result = B_SKIP_MESSAGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_MESSAGE:
|
|
|
|
result = ImMessage(message);
|
|
|
|
break;
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
case CAYA_REPLICANT_MESSENGER:
|
|
|
|
{
|
2012-02-27 18:46:15 -06:00
|
|
|
BMessenger* messenger = new BMessenger();
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
status_t ret = message->FindMessenger(
|
2012-02-27 18:46:15 -06:00
|
|
|
"messenger", messenger);
|
2011-12-14 17:36:27 -06:00
|
|
|
|
2012-02-27 18:46:15 -06:00
|
|
|
if (ret != B_OK || !messenger->IsValid()) {
|
2011-12-14 17:36:27 -06:00
|
|
|
message->PrintToStream();
|
|
|
|
printf("err %s\n", strerror(ret));
|
2012-02-27 18:46:15 -06:00
|
|
|
break;
|
2011-12-14 17:36:27 -06:00
|
|
|
}
|
2012-02-27 18:46:15 -06:00
|
|
|
AccountManager* accountManager = AccountManager::Get();
|
|
|
|
accountManager->SetReplicantMessenger(messenger);
|
2011-12-14 17:36:27 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
default:
|
|
|
|
// Dispatch not handled messages to main window
|
|
|
|
break;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
2010-05-28 17:38:16 -05:00
|
|
|
return result;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RosterMap
|
|
|
|
Server::RosterItems() const
|
|
|
|
{
|
|
|
|
return fRosterMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-28 17:38:16 -05:00
|
|
|
RosterItem*
|
2010-05-07 04:47:10 -05:00
|
|
|
Server::RosterItemForId(BString id)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
ContactLinker* item = fRosterMap.ValueFor(id, &found);
|
2010-05-28 17:38:16 -05:00
|
|
|
return item ? item->GetRosterItem() : NULL;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
filter_result
|
|
|
|
Server::ImMessage(BMessage* msg)
|
2010-05-28 17:38:16 -05:00
|
|
|
{
|
2010-05-07 04:47:10 -05:00
|
|
|
filter_result result = B_DISPATCH_MESSAGE;
|
|
|
|
int32 im_what = msg->FindInt32("im_what");
|
|
|
|
|
|
|
|
switch (im_what) {
|
2010-05-19 15:37:26 -05:00
|
|
|
case IM_CONTACT_LIST:
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
BString id;
|
|
|
|
while (msg->FindString("id", i++, &id) == B_OK) {
|
|
|
|
bool found = false;
|
|
|
|
ContactLinker* item = fRosterMap.ValueFor(id, &found);
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
item = new ContactLinker(id.String(), Looper());
|
|
|
|
item->SetProtocolLooper(_LooperFromMessage(msg));
|
|
|
|
fRosterMap.AddItem(id, item);
|
|
|
|
}
|
|
|
|
result = B_SKIP_MESSAGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_OWN_STATUS_SET:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2011-12-14 17:36:27 -06:00
|
|
|
//msg->PrintToStream();
|
2010-05-07 04:47:10 -05:00
|
|
|
int32 status;
|
|
|
|
const char* protocol;
|
|
|
|
if (msg->FindInt32("status", &status) != B_OK)
|
|
|
|
return B_SKIP_MESSAGE;
|
|
|
|
if (msg->FindString("protocol", &protocol) != B_OK)
|
|
|
|
return B_SKIP_MESSAGE;
|
|
|
|
|
|
|
|
AccountManager* accountManager = AccountManager::Get();
|
|
|
|
accountManager->SetStatus((CayaStatus)status);
|
2011-12-14 17:36:27 -06:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
|
|
|
}
|
2010-05-19 15:37:26 -05:00
|
|
|
case IM_STATUS_SET:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
int32 status;
|
|
|
|
|
|
|
|
if (msg->FindInt32("status", &status) != B_OK)
|
|
|
|
return B_SKIP_MESSAGE;
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
ContactLinker* linker = _EnsureContactLinker(msg);
|
2010-05-29 23:28:04 -05:00
|
|
|
if (!linker)
|
|
|
|
break;
|
|
|
|
|
|
|
|
linker->SetNotifyStatus((CayaStatus)status);
|
|
|
|
linker->SetNotifyPersonalStatus(msg->FindString("message"));
|
2011-12-14 17:36:27 -06:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
|
|
|
}
|
2010-05-19 15:37:26 -05:00
|
|
|
case IM_CONTACT_INFO:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
ContactLinker* linker = _EnsureContactLinker(msg);
|
2010-05-29 23:28:04 -05:00
|
|
|
if (!linker)
|
|
|
|
break;
|
2010-05-16 16:02:50 -05:00
|
|
|
|
2010-05-19 12:11:22 -05:00
|
|
|
const char* name = NULL;
|
|
|
|
|
2010-05-28 16:31:53 -05:00
|
|
|
if ((msg->FindString("name", &name) == B_OK)
|
|
|
|
&& (strcmp(name, "") != 0))
|
|
|
|
linker->SetNotifyName(name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_EXTENDED_CONTACT_INFO:
|
|
|
|
{
|
|
|
|
ContactLinker* linker = _EnsureContactLinker(msg);
|
2010-05-29 23:28:04 -05:00
|
|
|
if (!linker)
|
|
|
|
break;
|
2010-05-28 16:31:53 -05:00
|
|
|
|
|
|
|
if (linker->GetName().Length() > 0)
|
2010-05-29 23:28:04 -05:00
|
|
|
break;
|
2010-05-28 16:31:53 -05:00
|
|
|
|
|
|
|
const char* name = NULL;
|
|
|
|
|
|
|
|
if ((msg->FindString("full name", &name) == B_OK)
|
|
|
|
&& (strcmp(name, "") != 0))
|
2010-05-19 12:11:22 -05:00
|
|
|
linker->SetNotifyName(name);
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
|
|
|
}
|
2010-05-19 15:37:26 -05:00
|
|
|
case IM_AVATAR_SET:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
ContactLinker* linker = _EnsureContactLinker(msg);
|
2010-05-29 23:28:04 -05:00
|
|
|
if (!linker)
|
|
|
|
break;
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
entry_ref ref;
|
2010-05-29 23:28:04 -05:00
|
|
|
|
|
|
|
if (msg->FindRef("ref", &ref) == B_OK) {
|
|
|
|
BBitmap* bitmap = BTranslationUtils::GetBitmap(&ref);
|
|
|
|
linker->SetNotifyAvatarBitmap(bitmap);
|
|
|
|
} else
|
|
|
|
linker->SetNotifyAvatarBitmap(NULL);
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
|
|
|
}
|
2010-05-16 16:02:50 -05:00
|
|
|
case IM_SEND_MESSAGE: {
|
|
|
|
// Route this message through the appropriate ProtocolLooper
|
|
|
|
ContactLinker* linker = _EnsureContactLinker(msg);
|
|
|
|
if (linker->GetProtocolLooper())
|
|
|
|
linker->GetProtocolLooper()->PostMessage(msg);
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
2010-05-16 16:02:50 -05:00
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
case IM_MESSAGE_RECEIVED:
|
2010-05-28 17:38:16 -05:00
|
|
|
case IM_CONTACT_STARTED_TYPING:
|
|
|
|
case IM_CONTACT_STOPPED_TYPING:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
BString id = msg->FindString("id");
|
2010-05-16 16:02:50 -05:00
|
|
|
if (id.Length() > 0) {
|
2010-05-07 04:47:10 -05:00
|
|
|
bool found = false;
|
|
|
|
ContactLinker* item = fRosterMap.ValueFor(id, &found);
|
|
|
|
if (found) {
|
|
|
|
ChatWindow* win = item->GetChatWindow();
|
|
|
|
item->ShowWindow();
|
|
|
|
win->PostMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = B_SKIP_MESSAGE;
|
|
|
|
break;
|
|
|
|
}
|
2010-05-30 07:17:32 -05:00
|
|
|
case IM_PROGRESS:
|
|
|
|
{
|
|
|
|
const char* protocol = NULL;
|
|
|
|
const char* title = NULL;
|
|
|
|
const char* message = NULL;
|
|
|
|
float progress = 0.0f;
|
|
|
|
|
|
|
|
if (msg->FindString("protocol", &protocol) != B_OK)
|
|
|
|
return result;
|
|
|
|
if (msg->FindString("title", &title) != B_OK)
|
|
|
|
return result;
|
|
|
|
if (msg->FindString("message", &message) != B_OK)
|
|
|
|
return result;
|
|
|
|
if (msg->FindFloat("progress", &progress) != B_OK)
|
|
|
|
return result;
|
2011-12-14 17:36:27 -06:00
|
|
|
#if 0
|
2010-05-30 07:17:32 -05:00
|
|
|
|
|
|
|
CayaProtocolAddOn* addOn
|
|
|
|
= ProtocolManager::Get()->ProtocolAddOn(protocol);
|
|
|
|
|
|
|
|
BNotification notification(B_PROGRESS_NOTIFICATION);
|
2010-07-03 02:55:23 -05:00
|
|
|
notification.SetGroup(BString("Caya"));
|
2010-05-30 07:17:32 -05:00
|
|
|
notification.SetTitle(title);
|
|
|
|
notification.SetIcon(addOn->Icon());
|
|
|
|
notification.SetContent(message);
|
|
|
|
notification.SetProgress(progress);
|
2010-07-03 02:55:23 -05:00
|
|
|
notification.Send();
|
|
|
|
#endif
|
2010-05-30 07:17:32 -05:00
|
|
|
break;
|
|
|
|
}
|
2010-05-30 13:56:24 -05:00
|
|
|
case IM_NOTIFICATION:
|
|
|
|
{
|
|
|
|
int32 type = (int32)B_INFORMATION_NOTIFICATION;
|
|
|
|
const char* protocol = NULL;
|
|
|
|
const char* title = NULL;
|
|
|
|
const char* message = NULL;
|
|
|
|
|
|
|
|
if (msg->FindString("protocol", &protocol) != B_OK)
|
|
|
|
return result;
|
|
|
|
if (msg->FindInt32("type", &type) != B_OK)
|
|
|
|
return result;
|
|
|
|
if (msg->FindString("title", &title) != B_OK)
|
|
|
|
return result;
|
|
|
|
if (msg->FindString("message", &message) != B_OK)
|
|
|
|
return result;
|
|
|
|
|
2011-12-14 17:36:27 -06:00
|
|
|
#if 0
|
2010-05-30 13:56:24 -05:00
|
|
|
CayaProtocolAddOn* addOn
|
|
|
|
= ProtocolManager::Get()->ProtocolAddOn(protocol);
|
|
|
|
|
|
|
|
BNotification notification((notification_type)type);
|
2010-07-03 02:55:23 -05:00
|
|
|
notification.SetGroup(BString("Caya"));
|
2010-05-30 13:56:24 -05:00
|
|
|
notification.SetTitle(title);
|
|
|
|
notification.SetIcon(addOn->Icon());
|
|
|
|
notification.SetContent(message);
|
2010-07-03 02:55:23 -05:00
|
|
|
notification.Send();
|
2010-07-03 03:40:04 -05:00
|
|
|
#endif
|
2010-05-30 13:56:24 -05:00
|
|
|
break;
|
|
|
|
}
|
2011-12-14 17:36:27 -06:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-28 17:38:16 -05:00
|
|
|
ContactLinker*
|
2010-05-16 16:02:50 -05:00
|
|
|
Server::GetOwnContact()
|
|
|
|
{
|
|
|
|
return fMySelf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ProtocolLooper*
|
|
|
|
Server::_LooperFromMessage(BMessage* message)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
if (!message)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bigtime_t identifier;
|
|
|
|
|
|
|
|
if (message->FindInt64("instance", &identifier) == B_OK) {
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
ProtocolLooper* looper = fLoopers.ValueFor(identifier, &found);
|
|
|
|
if (found)
|
|
|
|
return looper;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-28 17:38:16 -05:00
|
|
|
ContactLinker*
|
2010-05-16 16:02:50 -05:00
|
|
|
Server::_EnsureContactLinker(BMessage* message)
|
|
|
|
{
|
|
|
|
if (!message)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BString id = message->FindString("id");
|
2010-05-07 04:47:10 -05:00
|
|
|
ContactLinker* item = NULL;
|
2010-05-16 16:02:50 -05:00
|
|
|
|
|
|
|
if (id.Length() > 0) {
|
2010-05-07 04:47:10 -05:00
|
|
|
bool found = false;
|
|
|
|
item = fRosterMap.ValueFor(id, &found);
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
item = new ContactLinker(id.String(), Looper());
|
2010-05-16 16:02:50 -05:00
|
|
|
item->SetProtocolLooper(_LooperFromMessage(message));
|
2010-05-07 04:47:10 -05:00
|
|
|
fRosterMap.AddItem(id, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|