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>
|
2021-06-02 16:53:03 -05:00
|
|
|
#include <StringList.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#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 "CayaMessages.h"
|
|
|
|
#include "CayaProtocol.h"
|
2012-03-08 16:55:13 -06:00
|
|
|
#include "CayaPreferences.h"
|
2010-05-19 15:37:26 -05:00
|
|
|
#include "CayaProtocolMessages.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "ImageCache.h"
|
2021-06-08 15:32:04 -05:00
|
|
|
#include "InviteDialogue.h"
|
|
|
|
#include "ProtocolLooper.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#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()
|
|
|
|
{
|
2021-05-24 01:47:21 -05:00
|
|
|
Contact* contact = NULL;
|
|
|
|
Conversation* conversation = NULL;
|
2010-05-16 16:02:50 -05:00
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
while (contact = fRosterMap.ValueAt(0)) {
|
|
|
|
contact->DeletePopUp();
|
2010-05-07 04:47:10 -05:00
|
|
|
fRosterMap.RemoveItemAt(0);
|
|
|
|
}
|
2021-05-24 01:47:21 -05:00
|
|
|
|
|
|
|
while (conversation = fChatMap.ValueAt(0)) {
|
|
|
|
fChatMap.RemoveItemAt(0);
|
2021-05-28 22:26:32 -05:00
|
|
|
delete conversation;
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2011-12-14 17:36:27 -06:00
|
|
|
case CAYA_CLOSE_CHAT_WINDOW:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2021-05-24 01:47:21 -05:00
|
|
|
BString id = message->FindString("chat_id");
|
2010-05-16 16:02:50 -05:00
|
|
|
if (id.Length() > 0) {
|
2010-05-07 04:47:10 -05:00
|
|
|
bool found = false;
|
2021-05-24 01:47:21 -05:00
|
|
|
Conversation* item = fChatMap.ValueFor(id, &found);
|
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
|
|
|
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);
|
2012-03-03 20:27:16 -06:00
|
|
|
accountManager->ReplicantStatusNotify(accountManager->Status());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2021-05-31 13:04:58 -05:00
|
|
|
while (msg->FindString("user_id", i++, &id) == B_OK)
|
|
|
|
_EnsureContact(msg);
|
2010-05-19 15:37:26 -05:00
|
|
|
result = B_SKIP_MESSAGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_OWN_STATUS_SET:
|
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;
|
|
|
|
|
2021-06-02 16:53:03 -05:00
|
|
|
User* user = _EnsureUser(msg);
|
|
|
|
if (!user)
|
2010-05-29 23:28:04 -05:00
|
|
|
break;
|
|
|
|
|
2021-06-02 16:53:03 -05:00
|
|
|
user->SetNotifyStatus((CayaStatus)status);
|
2012-03-07 18:47:00 -06:00
|
|
|
BString statusMsg;
|
|
|
|
if (msg->FindString("message", &statusMsg) == B_OK) {
|
2021-06-02 16:53:03 -05:00
|
|
|
user->SetNotifyPersonalStatus(statusMsg);
|
2012-03-07 18:47:00 -06:00
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-05-31 13:04:58 -05:00
|
|
|
case IM_OWN_CONTACT_INFO:
|
|
|
|
{
|
|
|
|
Contact* contact = _EnsureContact(msg);
|
|
|
|
if (contact != NULL) {
|
|
|
|
fMySelf = contact->GetId();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-05-19 15:37:26 -05:00
|
|
|
case IM_CONTACT_INFO:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2021-05-24 01:47:21 -05:00
|
|
|
Contact* contact = _EnsureContact(msg);
|
|
|
|
if (!contact)
|
2010-05-29 23:28:04 -05:00
|
|
|
break;
|
2010-05-16 16:02:50 -05:00
|
|
|
|
2010-05-19 12:11:22 -05:00
|
|
|
const char* name = NULL;
|
|
|
|
|
2021-06-07 11:45:30 -05:00
|
|
|
if ((msg->FindString("user_name", &name) == B_OK)
|
2010-05-28 16:31:53 -05:00
|
|
|
&& (strcmp(name, "") != 0))
|
2021-05-24 01:47:21 -05:00
|
|
|
contact->SetNotifyName(name);
|
2012-03-07 13:22:09 -06:00
|
|
|
|
|
|
|
BString status;
|
2021-06-07 00:03:15 -05:00
|
|
|
if (msg->FindString("message", &status) == B_OK)
|
2021-05-24 01:47:21 -05:00
|
|
|
contact->SetNotifyPersonalStatus(status);
|
2010-05-28 16:31:53 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_EXTENDED_CONTACT_INFO:
|
|
|
|
{
|
2021-05-24 01:47:21 -05:00
|
|
|
Contact* contact = _EnsureContact(msg);
|
|
|
|
if (!contact)
|
2010-05-29 23:28:04 -05:00
|
|
|
break;
|
2010-05-28 16:31:53 -05:00
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
if (contact->GetName().Length() > 0)
|
2010-05-29 23:28:04 -05:00
|
|
|
break;
|
2010-05-28 16:31:53 -05:00
|
|
|
|
|
|
|
const char* name = NULL;
|
|
|
|
|
2021-06-07 11:45:30 -05:00
|
|
|
if ((msg->FindString("full_name", &name) == B_OK)
|
2010-05-28 16:31:53 -05:00
|
|
|
&& (strcmp(name, "") != 0))
|
2021-05-24 01:47:21 -05:00
|
|
|
contact->SetNotifyName(name);
|
2021-06-07 11:45:30 -05:00
|
|
|
else if ((msg->FindString("user_name", &name) == B_OK)
|
|
|
|
&& (strcmp(name, "") != 0))
|
|
|
|
contact->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
|
|
|
{
|
2021-05-31 11:56:45 -05:00
|
|
|
User* user = _EnsureUser(msg);
|
|
|
|
if (!user)
|
2010-05-29 23:28:04 -05:00
|
|
|
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);
|
2021-05-31 11:56:45 -05:00
|
|
|
user->SetNotifyAvatarBitmap(bitmap);
|
2010-05-29 23:28:04 -05:00
|
|
|
} else
|
2021-05-31 11:56:45 -05:00
|
|
|
user->SetNotifyAvatarBitmap(NULL);
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-05-26 07:48:25 -05:00
|
|
|
case IM_CREATE_CHAT:
|
|
|
|
{
|
|
|
|
BString user_id = msg->FindString("user_id");
|
|
|
|
if (user_id.IsEmpty() == false) {
|
|
|
|
User* user = ContactById(user_id);
|
|
|
|
user->GetProtocolLooper()->PostMessage(msg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_CHAT_CREATED:
|
|
|
|
{
|
|
|
|
Conversation* chat = _EnsureConversation(msg);
|
2021-05-31 11:56:45 -05:00
|
|
|
User* user = _EnsureUser(msg);
|
2021-05-26 07:48:25 -05:00
|
|
|
|
|
|
|
if (chat != NULL && user != NULL) {
|
|
|
|
chat->AddUser(user);
|
2021-05-28 22:26:32 -05:00
|
|
|
chat->ShowView(false, true);
|
2021-05-26 07:48:25 -05:00
|
|
|
}
|
2021-05-27 11:15:30 -05:00
|
|
|
|
2021-05-26 07:48:25 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-06-01 21:43:19 -05:00
|
|
|
case IM_JOIN_ROOM:
|
|
|
|
{
|
|
|
|
SendProtocolMessage(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_ROOM_JOINED:
|
|
|
|
{
|
2021-06-04 16:32:18 -05:00
|
|
|
_EnsureConversation(msg);
|
2021-06-01 21:43:19 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-06-02 16:53:03 -05:00
|
|
|
case IM_ROOM_PARTICIPANTS:
|
|
|
|
{
|
|
|
|
Conversation* chat = _EnsureConversation(msg);
|
|
|
|
BStringList ids;
|
|
|
|
BStringList name;
|
|
|
|
|
|
|
|
msg->FindStrings("user_name", &name);
|
|
|
|
if (msg->FindStrings("user_id", &ids) != B_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ProtocolLooper* protoLooper = _LooperFromMessage(msg);
|
|
|
|
|
|
|
|
for (int i = 0; i < ids.CountStrings(); i++) {
|
|
|
|
User* user = _EnsureUser(ids.StringAt(i), protoLooper);
|
|
|
|
|
|
|
|
if (name.CountStrings() >= i) {
|
|
|
|
user->SetNotifyName(name.StringAt(i));
|
|
|
|
}
|
|
|
|
chat->AddUser(user);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-06-07 00:03:15 -05:00
|
|
|
case IM_ROOM_PARTICIPANT_JOINED:
|
2021-06-02 16:53:03 -05:00
|
|
|
case IM_ROOM_PARTICIPANT_LEFT:
|
2021-06-07 00:03:15 -05:00
|
|
|
case IM_ROOM_PARTICIPANT_BANNED:
|
|
|
|
case IM_ROOM_PARTICIPANT_KICKED:
|
2021-06-02 16:53:03 -05:00
|
|
|
{
|
|
|
|
Conversation* chat = _EnsureConversation(msg);
|
2021-06-07 00:03:15 -05:00
|
|
|
if (chat == NULL)
|
2021-06-02 16:53:03 -05:00
|
|
|
break;
|
2021-06-07 00:03:15 -05:00
|
|
|
chat->ImMessage(msg);
|
2021-06-02 16:53:03 -05:00
|
|
|
break;
|
2021-06-04 13:57:04 -05:00
|
|
|
}
|
2021-06-07 11:45:30 -05:00
|
|
|
case IM_ROOM_ROLECHANGED:
|
Support for "Roles" (user, moderator, admin, etc.)
Add scaffodling support for arbitrary roles and permission-based (and
varying!) UI.
A new class, Role, represents a user's role in a given room, with three
values:
* The role's title
* The role's permission-set
* The role's priority
The permission set is a bitmask value for various permissions (e.g.,
PERM_WRITE, PERM_BAN, etc), and priority is position in the hierarchy.
A user with higher priority (and PERM_BAN) can ban a user with lower
priority, but not vice-versa. Two users with the same priority can't
ban/kick/mute each other, etc.
These permissions should be used to determine what UI elements are
displayed― if the user doesn't have permission to ban users, then a
"Ban" button shouldn't exist. If the user is muted, they shouldn't be
able to type. So on and so forth.
For now, permissions are sent with a IM_ROLECHANGE message and stored
by the Conversation, but aren't really in use yet.
This system should be flexible groundwork to account for the varying
administrative hierarchies and norms of different protocols.
2021-06-06 00:41:45 -05:00
|
|
|
{
|
|
|
|
Conversation* chat = _EnsureConversation(msg);
|
|
|
|
BString user_id;
|
|
|
|
Role* role = _GetRole(msg);
|
|
|
|
|
|
|
|
if (chat == NULL || msg->FindString("user_id", &user_id) != B_OK
|
|
|
|
|| role == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
chat->SetRole(user_id, role);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-07 11:45:30 -05:00
|
|
|
case IM_ROOM_NAME_SET:
|
2021-06-06 18:06:46 -05:00
|
|
|
{
|
|
|
|
BString name;
|
|
|
|
Conversation* chat = _EnsureConversation(msg);
|
|
|
|
if (msg->FindString("chat_name", &name) != B_OK || chat == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
chat->SetNotifyName(name.String());
|
|
|
|
break;
|
|
|
|
}
|
2021-06-07 11:45:30 -05:00
|
|
|
case IM_ROOM_SUBJECT_SET:
|
2021-06-04 13:57:04 -05:00
|
|
|
{
|
|
|
|
BString subject;
|
|
|
|
Conversation* chat = _EnsureConversation(msg);
|
|
|
|
if (msg->FindString("subject", &subject) != B_OK || chat == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
chat->SetNotifySubject(subject.String());
|
|
|
|
break;
|
2021-06-02 16:53:03 -05:00
|
|
|
}
|
2012-03-11 10:11:29 -05:00
|
|
|
case IM_SEND_MESSAGE:
|
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
// Route this message through the appropriate ProtocolLooper
|
2021-05-24 01:47:21 -05:00
|
|
|
Conversation* conversation = _EnsureConversation(msg);
|
|
|
|
if (conversation->GetProtocolLooper())
|
|
|
|
conversation->GetProtocolLooper()->PostMessage(msg);
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
2010-05-16 16:02:50 -05:00
|
|
|
}
|
2021-05-30 19:07:50 -05:00
|
|
|
case IM_MESSAGE_SENT:
|
2010-05-07 04:47:10 -05:00
|
|
|
case IM_MESSAGE_RECEIVED:
|
2012-03-08 18:35:02 -06:00
|
|
|
{
|
2021-05-24 01:47:21 -05:00
|
|
|
Conversation* item = _EnsureConversation(msg);
|
|
|
|
item->ImMessage(msg);
|
2021-05-27 11:15:30 -05:00
|
|
|
|
2012-03-08 18:35:02 -06:00
|
|
|
break;
|
|
|
|
}
|
2021-06-08 15:32:04 -05:00
|
|
|
case IM_ROOM_INVITE_RECEIVED:
|
|
|
|
{
|
|
|
|
BString chat_id;
|
|
|
|
User* user = _EnsureUser(msg);
|
|
|
|
BString user_id = msg->FindString("user_id");
|
|
|
|
BString user_name = user_id;
|
|
|
|
BString chat_name = msg->FindString("chat_name");
|
|
|
|
BString body = msg->FindString("body");
|
|
|
|
ProtocolLooper* looper = _LooperFromMessage(msg);
|
|
|
|
|
|
|
|
if (msg->FindString("chat_id", &chat_id) != B_OK || looper == NULL)
|
2021-06-08 19:59:22 -05:00
|
|
|
{
|
2021-06-08 15:32:04 -05:00
|
|
|
result = B_SKIP_MESSAGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chat_name.IsEmpty() == true)
|
|
|
|
chat_name = chat_id;
|
|
|
|
|
|
|
|
if (user != NULL)
|
|
|
|
user_name = user->GetName();
|
|
|
|
|
|
|
|
BString alertBody("You've been invited to %room%.");
|
|
|
|
if (user_id.IsEmpty() == false)
|
|
|
|
alertBody = "%user% has invited you to %room%.";
|
|
|
|
if (body.IsEmpty() == false)
|
|
|
|
alertBody << "\n\n\"%body%\"";
|
|
|
|
|
|
|
|
alertBody.ReplaceAll("%user%", user_name);
|
|
|
|
alertBody.ReplaceAll("%room%", chat_name);
|
|
|
|
alertBody.ReplaceAll("%body%", body);
|
|
|
|
|
2021-06-08 19:59:22 -05:00
|
|
|
BMessage* accept = new BMessage(IM_MESSAGE);
|
|
|
|
accept->AddInt32("im_what", IM_ROOM_INVITE_ACCEPT);
|
2021-06-08 15:32:04 -05:00
|
|
|
accept->AddString("chat_id", chat_id);
|
2021-06-08 19:59:22 -05:00
|
|
|
BMessage* reject = new BMessage(IM_MESSAGE);
|
|
|
|
accept->AddInt32("im_what", IM_ROOM_INVITE_REFUSE);
|
2021-06-08 15:32:04 -05:00
|
|
|
reject->AddString("chat_id", chat_id);
|
|
|
|
|
|
|
|
InviteDialogue* invite = new InviteDialogue(BMessenger(looper),
|
|
|
|
"Invitation received",
|
|
|
|
alertBody.String(), accept, reject);
|
|
|
|
invite->Go();
|
|
|
|
break;
|
|
|
|
}
|
2021-06-07 13:19:58 -05:00
|
|
|
case IM_USER_STARTED_TYPING:
|
|
|
|
case IM_USER_STOPPED_TYPING:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2021-06-08 15:32:04 -05:00
|
|
|
// User* user = _EnsureUser();
|
|
|
|
// Conversation* chat = _EnsureConversation();
|
2010-05-07 04:47:10 -05:00
|
|
|
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;
|
2012-03-08 16:55:13 -06:00
|
|
|
|
|
|
|
if (!CayaPreferences::Item()->NotifyProtocolStatus)
|
|
|
|
break;
|
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);
|
2021-05-22 12:05:44 -05:00
|
|
|
notification.SetIcon(addOn->ProtoIcon());
|
2010-05-30 07:17:32 -05:00
|
|
|
notification.SetContent(message);
|
|
|
|
notification.SetProgress(progress);
|
2010-07-03 02:55:23 -05:00
|
|
|
notification.Send();
|
2012-03-08 16:55:13 -06:00
|
|
|
|
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;
|
|
|
|
|
2012-03-08 16:55:13 -06:00
|
|
|
if (!CayaPreferences::Item()->NotifyProtocolStatus)
|
|
|
|
break;
|
|
|
|
|
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);
|
2021-05-22 12:05:44 -05:00
|
|
|
notification.SetIcon(addOn->ProtoIcon());
|
2010-05-30 13:56:24 -05:00
|
|
|
notification.SetContent(message);
|
2010-07-03 02:55:23 -05:00
|
|
|
notification.Send();
|
2012-03-08 16:55:13 -06:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 13:04:58 -05:00
|
|
|
void
|
|
|
|
Server::AddProtocolLooper(bigtime_t instanceId, CayaProtocol* cayap)
|
|
|
|
{
|
|
|
|
ProtocolLooper* looper = new ProtocolLooper(cayap);
|
|
|
|
fLoopers.AddItem(instanceId, looper);
|
2021-06-01 21:43:19 -05:00
|
|
|
fAccounts.AddItem(cayap->GetName(), instanceId);
|
2021-05-31 13:04:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Server::RemoveProtocolLooper(bigtime_t instanceId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-01 21:43:19 -05:00
|
|
|
ProtocolLooper*
|
|
|
|
Server::GetProtocolLooper(bigtime_t instanceId)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
return fLoopers.ValueFor(instanceId, &found);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AccountInstances
|
|
|
|
Server::GetAccounts()
|
|
|
|
{
|
|
|
|
return fAccounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 13:04:58 -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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RosterMap
|
|
|
|
Server::Contacts() const
|
|
|
|
{
|
|
|
|
return fRosterMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-23 14:39:07 -05:00
|
|
|
Contact*
|
2021-05-31 13:04:58 -05:00
|
|
|
Server::ContactById(BString id)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
return fRosterMap.ValueFor(id, &found);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Server::AddContact(Contact* contact)
|
|
|
|
{
|
|
|
|
fRosterMap.AddItem(contact->GetId(), contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UserMap
|
|
|
|
Server::Users() const
|
|
|
|
{
|
|
|
|
UserMap users = fUserMap;
|
|
|
|
|
|
|
|
for (int i = 0; i < fRosterMap.CountItems(); i++) {
|
|
|
|
User* user = (User*)fRosterMap.ValueAt(i);
|
|
|
|
users.AddItem(user->GetId(), user);
|
|
|
|
}
|
|
|
|
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
User*
|
|
|
|
Server::UserById(BString id)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
User* user = ContactById(id);
|
|
|
|
if (user == NULL)
|
|
|
|
user = fUserMap.ValueFor(id, &found);
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Server::AddUser(User* user)
|
|
|
|
{
|
|
|
|
fUserMap.AddItem(user->GetId(), user);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ChatMap
|
|
|
|
Server::Conversations() const
|
|
|
|
{
|
|
|
|
return fChatMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Conversation*
|
|
|
|
Server::ConversationById(BString id)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
return fChatMap.ValueFor(id, &found);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Server::AddConversation(Conversation* chat)
|
|
|
|
{
|
|
|
|
fChatMap.AddItem(chat->GetId(), chat);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-04 16:32:18 -05:00
|
|
|
void
|
|
|
|
Server::RemoveConversation(Conversation* chat)
|
|
|
|
{
|
|
|
|
fChatMap.RemoveItemFor(chat->GetId());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 13:04:58 -05:00
|
|
|
BString
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-23 14:39:07 -05:00
|
|
|
Contact*
|
2021-05-31 11:56:45 -05:00
|
|
|
Server::_EnsureContact(BMessage* message)
|
2010-05-16 16:02:50 -05:00
|
|
|
{
|
2021-05-24 01:47:21 -05:00
|
|
|
BString id = message->FindString("user_id");
|
2021-05-31 11:56:45 -05:00
|
|
|
Contact* contact = ContactById(id);
|
2010-05-16 16:02:50 -05:00
|
|
|
|
2021-05-31 11:56:45 -05:00
|
|
|
if (contact == NULL && id.IsEmpty() == false) {
|
|
|
|
contact = new Contact(id, Looper());
|
|
|
|
contact->SetProtocolLooper(_LooperFromMessage(message));
|
2021-05-31 13:04:58 -05:00
|
|
|
AddContact(contact);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
2021-05-31 11:56:45 -05:00
|
|
|
return contact;
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 11:56:45 -05:00
|
|
|
User*
|
|
|
|
Server::_EnsureUser(BMessage* message)
|
2021-05-24 01:47:21 -05:00
|
|
|
{
|
|
|
|
BString id = message->FindString("user_id");
|
2021-06-02 16:53:03 -05:00
|
|
|
return _EnsureUser(id, _LooperFromMessage(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
User*
|
|
|
|
Server::_EnsureUser(BString id, ProtocolLooper* protoLooper)
|
|
|
|
{
|
2021-05-31 11:56:45 -05:00
|
|
|
User* user = UserById(id);
|
2021-05-24 01:47:21 -05:00
|
|
|
|
2021-05-31 11:56:45 -05:00
|
|
|
if (user == NULL && id.IsEmpty() == false) {
|
|
|
|
user = new User(id, Looper());
|
2021-06-02 16:53:03 -05:00
|
|
|
user->SetProtocolLooper(protoLooper);
|
2021-05-31 13:04:58 -05:00
|
|
|
AddUser(user);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
2021-05-31 11:56:45 -05:00
|
|
|
return user;
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Conversation*
|
|
|
|
Server::_EnsureConversation(BMessage* message)
|
|
|
|
{
|
|
|
|
if (!message)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BString chat_id = message->FindString("chat_id");
|
|
|
|
Conversation* item = NULL;
|
|
|
|
|
|
|
|
if (chat_id.IsEmpty() == false) {
|
|
|
|
bool found = false;
|
|
|
|
item = fChatMap.ValueFor(chat_id, &found);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
if (!found) {
|
2021-05-24 01:47:21 -05:00
|
|
|
item = new Conversation(chat_id, Looper());
|
2010-05-16 16:02:50 -05:00
|
|
|
item->SetProtocolLooper(_LooperFromMessage(message));
|
2021-05-31 13:04:58 -05:00
|
|
|
item->AddUser(ContactById(fMySelf));
|
2021-05-24 01:47:21 -05:00
|
|
|
fChatMap.AddItem(chat_id, item);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
2021-05-23 14:39:07 -05:00
|
|
|
|
|
|
|
|
Support for "Roles" (user, moderator, admin, etc.)
Add scaffodling support for arbitrary roles and permission-based (and
varying!) UI.
A new class, Role, represents a user's role in a given room, with three
values:
* The role's title
* The role's permission-set
* The role's priority
The permission set is a bitmask value for various permissions (e.g.,
PERM_WRITE, PERM_BAN, etc), and priority is position in the hierarchy.
A user with higher priority (and PERM_BAN) can ban a user with lower
priority, but not vice-versa. Two users with the same priority can't
ban/kick/mute each other, etc.
These permissions should be used to determine what UI elements are
displayed― if the user doesn't have permission to ban users, then a
"Ban" button shouldn't exist. If the user is muted, they shouldn't be
able to type. So on and so forth.
For now, permissions are sent with a IM_ROLECHANGE message and stored
by the Conversation, but aren't really in use yet.
This system should be flexible groundwork to account for the varying
administrative hierarchies and norms of different protocols.
2021-06-06 00:41:45 -05:00
|
|
|
Role*
|
|
|
|
Server::_GetRole(BMessage* msg)
|
|
|
|
{
|
|
|
|
if (!msg)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BString title;
|
2021-06-06 01:49:11 -05:00
|
|
|
int32 perms;
|
|
|
|
int32 priority;
|
Support for "Roles" (user, moderator, admin, etc.)
Add scaffodling support for arbitrary roles and permission-based (and
varying!) UI.
A new class, Role, represents a user's role in a given room, with three
values:
* The role's title
* The role's permission-set
* The role's priority
The permission set is a bitmask value for various permissions (e.g.,
PERM_WRITE, PERM_BAN, etc), and priority is position in the hierarchy.
A user with higher priority (and PERM_BAN) can ban a user with lower
priority, but not vice-versa. Two users with the same priority can't
ban/kick/mute each other, etc.
These permissions should be used to determine what UI elements are
displayed― if the user doesn't have permission to ban users, then a
"Ban" button shouldn't exist. If the user is muted, they shouldn't be
able to type. So on and so forth.
For now, permissions are sent with a IM_ROLECHANGE message and stored
by the Conversation, but aren't really in use yet.
This system should be flexible groundwork to account for the varying
administrative hierarchies and norms of different protocols.
2021-06-06 00:41:45 -05:00
|
|
|
|
|
|
|
if (msg->FindString("role_title", &title) != B_OK
|
2021-06-06 01:49:11 -05:00
|
|
|
|| msg->FindInt32("role_perms", &perms) != B_OK
|
|
|
|
|| msg->FindInt32("role_priority", &priority) != B_OK)
|
Support for "Roles" (user, moderator, admin, etc.)
Add scaffodling support for arbitrary roles and permission-based (and
varying!) UI.
A new class, Role, represents a user's role in a given room, with three
values:
* The role's title
* The role's permission-set
* The role's priority
The permission set is a bitmask value for various permissions (e.g.,
PERM_WRITE, PERM_BAN, etc), and priority is position in the hierarchy.
A user with higher priority (and PERM_BAN) can ban a user with lower
priority, but not vice-versa. Two users with the same priority can't
ban/kick/mute each other, etc.
These permissions should be used to determine what UI elements are
displayed― if the user doesn't have permission to ban users, then a
"Ban" button shouldn't exist. If the user is muted, they shouldn't be
able to type. So on and so forth.
For now, permissions are sent with a IM_ROLECHANGE message and stored
by the Conversation, but aren't really in use yet.
This system should be flexible groundwork to account for the varying
administrative hierarchies and norms of different protocols.
2021-06-06 00:41:45 -05:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return new Role(title, perms, priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
|