2021-05-24 01:47:21 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
|
|
|
* All rights reserved. Distributed under the terms of the MIT license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Conversation.h"
|
|
|
|
|
2021-05-24 14:48:25 -05:00
|
|
|
#include <DateTimeFormat.h>
|
|
|
|
#include <Locale.h>
|
2021-05-24 19:12:42 -05:00
|
|
|
#include <StringList.h>
|
2021-05-24 14:48:25 -05:00
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
#include "CayaPreferences.h"
|
|
|
|
#include "CayaProtocolMessages.h"
|
2021-05-28 22:26:32 -05:00
|
|
|
#include "CayaRenderView.h"
|
2021-05-24 14:20:57 -05:00
|
|
|
#include "CayaUtils.h"
|
2021-06-15 00:19:52 -05:00
|
|
|
#include "ChatCommand.h"
|
2021-05-27 11:15:30 -05:00
|
|
|
#include "ConversationItem.h"
|
2021-05-28 22:26:32 -05:00
|
|
|
#include "ConversationView.h"
|
2021-05-24 01:47:21 -05:00
|
|
|
#include "MainWindow.h"
|
2021-06-04 13:57:04 -05:00
|
|
|
#include "NotifyMessage.h"
|
2021-05-24 14:20:57 -05:00
|
|
|
#include "ProtocolLooper.h"
|
|
|
|
#include "ProtocolManager.h"
|
2021-06-15 14:40:28 -05:00
|
|
|
#include "RoomFlags.h"
|
2021-05-24 01:47:21 -05:00
|
|
|
#include "Server.h"
|
|
|
|
#include "TheApp.h"
|
|
|
|
|
|
|
|
|
|
|
|
Conversation::Conversation(BString id, BMessenger msgn)
|
|
|
|
:
|
|
|
|
fID(id),
|
|
|
|
fName(id),
|
|
|
|
fMessenger(msgn),
|
2021-05-28 22:26:32 -05:00
|
|
|
fChatView(NULL),
|
2021-05-24 14:48:25 -05:00
|
|
|
fLooper(NULL),
|
2021-06-04 13:57:04 -05:00
|
|
|
fIcon(NULL),
|
2021-06-13 01:16:30 -05:00
|
|
|
fDateFormatter(),
|
|
|
|
fRoomFlags(0),
|
|
|
|
fDisallowedFlags(0)
|
2021-05-24 01:47:21 -05:00
|
|
|
{
|
2021-05-27 11:15:30 -05:00
|
|
|
fConversationItem = new ConversationItem(fName.String(), this);
|
2021-06-03 23:39:50 -05:00
|
|
|
RegisterObserver(fConversationItem);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-11 20:33:28 -05:00
|
|
|
Conversation::~Conversation()
|
|
|
|
{
|
|
|
|
((TheApp*)be_app)->GetMainWindow()->RemoveConversation(this);
|
|
|
|
|
|
|
|
ProtocolLooper* looper = GetProtocolLooper();
|
|
|
|
if (looper != NULL)
|
|
|
|
looper->RemoveConversation(this);
|
|
|
|
|
|
|
|
delete fChatView;
|
|
|
|
delete fConversationItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
BString
|
|
|
|
Conversation::GetId() const
|
|
|
|
{
|
|
|
|
return fID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::ImMessage(BMessage* msg)
|
|
|
|
{
|
|
|
|
int32 im_what = msg->FindInt32("im_what");
|
|
|
|
|
|
|
|
switch(im_what)
|
|
|
|
{
|
|
|
|
case IM_MESSAGE_RECEIVED:
|
|
|
|
{
|
|
|
|
_EnsureUser(msg);
|
2021-05-24 14:20:57 -05:00
|
|
|
_LogChatMessage(msg);
|
2021-05-28 22:26:32 -05:00
|
|
|
GetView()->MessageReceived(msg);
|
2021-05-24 01:47:21 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-05-30 19:07:50 -05:00
|
|
|
case IM_MESSAGE_SENT:
|
2021-05-24 14:20:57 -05:00
|
|
|
{
|
|
|
|
_LogChatMessage(msg);
|
2021-05-30 19:07:50 -05:00
|
|
|
GetView()->MessageReceived(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_SEND_MESSAGE:
|
|
|
|
{
|
2021-06-15 00:19:52 -05:00
|
|
|
BString body;
|
|
|
|
if (msg->FindString("body", &body) != B_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (IsCommand(body.String()) == false) {
|
|
|
|
fMessenger.SendMessage(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
BString name = CommandName(body);
|
|
|
|
BString args = CommandArgs(body);
|
2021-06-15 00:59:00 -05:00
|
|
|
ChatCommand* cmd = _GetServer()->CommandById(name, fLooper->GetInstance());
|
2021-06-15 00:19:52 -05:00
|
|
|
|
|
|
|
if (cmd == NULL) {
|
2021-06-15 00:45:51 -05:00
|
|
|
_WarnUser(BString("That isn't a valid command. Try /help for a list."));
|
2021-06-15 00:19:52 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
BString error("");
|
|
|
|
if (cmd->Parse(args, &error, this) == false)
|
|
|
|
_WarnUser(error);
|
2021-05-24 14:20:57 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-06-13 01:16:30 -05:00
|
|
|
case IM_ROOM_METADATA:
|
|
|
|
{
|
|
|
|
BString name;
|
|
|
|
if (msg->FindString("chat_name", &name) == B_OK)
|
|
|
|
SetNotifyName(name.String());
|
|
|
|
|
|
|
|
BString subject;
|
|
|
|
if (msg->FindString("subject", &subject) == B_OK)
|
|
|
|
SetNotifySubject(subject.String());
|
|
|
|
|
|
|
|
int32 defaultFlags;
|
|
|
|
if (msg->FindInt32("room_default_flags", &defaultFlags) == B_OK)
|
|
|
|
if (fRoomFlags == 0)
|
|
|
|
fRoomFlags = defaultFlags;
|
|
|
|
|
|
|
|
int32 disabledFlags;
|
|
|
|
if (msg->FindInt32("room_disallowed_flags", &disabledFlags) == B_OK)
|
|
|
|
fDisallowedFlags = disabledFlags;
|
2021-06-13 02:34:11 -05:00
|
|
|
_CacheRoomFlags();
|
2021-06-13 01:16:30 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-06-07 00:03:15 -05:00
|
|
|
case IM_ROOM_PARTICIPANT_JOINED:
|
|
|
|
{
|
|
|
|
BString user_id;
|
|
|
|
if (msg->FindString("user_id", &user_id) != B_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (UserById(user_id) == NULL) {
|
|
|
|
_EnsureUser(msg);
|
|
|
|
GetView()->MessageReceived(msg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_ROOM_PARTICIPANT_LEFT:
|
|
|
|
case IM_ROOM_PARTICIPANT_KICKED:
|
|
|
|
case IM_ROOM_PARTICIPANT_BANNED:
|
|
|
|
{
|
|
|
|
BString user_id = msg->FindString("user_id");
|
|
|
|
User* user;
|
|
|
|
if (user_id.IsEmpty() == true || (user = UserById(user_id)) == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
GetView()->MessageReceived(msg);
|
|
|
|
RemoveUser(user);
|
|
|
|
break;
|
|
|
|
}
|
2021-05-30 19:07:50 -05:00
|
|
|
case IM_LOGS_RECEIVED:
|
2021-05-24 01:47:21 -05:00
|
|
|
default:
|
2021-05-28 22:26:32 -05:00
|
|
|
GetView()->MessageReceived(msg);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::ObserveString(int32 what, BString str)
|
|
|
|
{
|
2021-06-03 23:39:50 -05:00
|
|
|
GetView()->InvalidateUserList();
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-06-03 23:39:50 -05:00
|
|
|
Conversation::ObserveInteger(int32 what, int32 value)
|
2021-05-24 01:47:21 -05:00
|
|
|
{
|
2021-06-03 23:39:50 -05:00
|
|
|
GetView()->InvalidateUserList();
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-06-03 23:39:50 -05:00
|
|
|
Conversation::ObservePointer(int32 what, void* ptr)
|
2021-05-24 01:47:21 -05:00
|
|
|
{
|
2021-06-03 23:39:50 -05:00
|
|
|
GetView()->InvalidateUserList();
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-06 18:06:46 -05:00
|
|
|
void
|
|
|
|
Conversation::SetNotifyName(const char* name)
|
|
|
|
{
|
|
|
|
if (BString(name) == fName)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fName = name;
|
|
|
|
NotifyString(STR_ROOM_NAME, fName.String());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-04 13:57:04 -05:00
|
|
|
void
|
|
|
|
Conversation::SetNotifySubject(const char* subject)
|
|
|
|
{
|
|
|
|
if (BString(subject) == fSubject)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fSubject = subject;
|
|
|
|
NotifyString(STR_ROOM_SUBJECT, fSubject.String());
|
|
|
|
}
|
|
|
|
|
2021-06-03 23:39:50 -05:00
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
BMessenger
|
|
|
|
Conversation::Messenger() const
|
|
|
|
{
|
|
|
|
return fMessenger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::SetMessenger(BMessenger messenger)
|
|
|
|
{
|
|
|
|
fMessenger = messenger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ProtocolLooper*
|
|
|
|
Conversation::GetProtocolLooper() const
|
|
|
|
{
|
|
|
|
return fLooper;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::SetProtocolLooper(ProtocolLooper* looper)
|
|
|
|
{
|
|
|
|
fLooper = looper;
|
2021-06-13 02:34:11 -05:00
|
|
|
_LoadRoomFlags();
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-04 13:57:04 -05:00
|
|
|
BBitmap*
|
|
|
|
Conversation::ProtocolBitmap() const
|
|
|
|
{
|
|
|
|
CayaProtocol* protocol = fLooper->Protocol();
|
|
|
|
CayaProtocolAddOn* addOn
|
|
|
|
= ProtocolManager::Get()->ProtocolAddOn(protocol->Signature());
|
|
|
|
|
|
|
|
return addOn->ProtoIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
|
|
|
Conversation::IconBitmap() const
|
|
|
|
{
|
|
|
|
return fIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
BString
|
|
|
|
Conversation::GetName() const
|
|
|
|
{
|
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
ConversationView*
|
|
|
|
Conversation::GetView()
|
|
|
|
{
|
|
|
|
if (fChatView != NULL)
|
|
|
|
return fChatView;
|
|
|
|
|
|
|
|
fChatView = new ConversationView(this);
|
|
|
|
|
2021-06-15 14:40:28 -05:00
|
|
|
if (!(fRoomFlags & ROOM_POPULATE_LOGS))
|
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 fChatView;
|
|
|
|
|
2021-06-06 12:02:26 -05:00
|
|
|
BMessage logMsg;
|
|
|
|
if (_GetChatLogs(&logMsg) == B_OK)
|
|
|
|
fChatView->MessageReceived(&logMsg);
|
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
|
|
|
|
|
|
|
RegisterObserver(fChatView);
|
|
|
|
return fChatView;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::ShowView(bool typing, bool userAction)
|
|
|
|
{
|
|
|
|
((TheApp*)be_app)->GetMainWindow()->SetConversation(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ConversationItem*
|
|
|
|
Conversation::GetListItem()
|
|
|
|
{
|
|
|
|
return fConversationItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
UserMap
|
|
|
|
Conversation::Users()
|
|
|
|
{
|
|
|
|
return fUsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 11:56:45 -05:00
|
|
|
User*
|
2021-05-24 01:47:21 -05:00
|
|
|
Conversation::UserById(BString id)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
return fUsers.ValueFor(id, &found);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::AddUser(User* user)
|
|
|
|
{
|
|
|
|
BMessage msg;
|
|
|
|
msg.AddString("user_id", user->GetId());
|
2021-06-02 16:53:03 -05:00
|
|
|
msg.AddString("user_name", user->GetName());
|
2021-05-24 01:47:21 -05:00
|
|
|
_EnsureUser(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-02 16:53:03 -05:00
|
|
|
void
|
|
|
|
Conversation::RemoveUser(User* user)
|
|
|
|
{
|
|
|
|
fUsers.RemoveItemFor(user->GetId());
|
2021-06-03 23:39:50 -05:00
|
|
|
user->UnregisterObserver(this);
|
2021-06-02 16:53:03 -05:00
|
|
|
GetView()->UpdateUserList(fUsers);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
BString
|
Explicitly tie Conversations, Contacts, and Users to their ProtocolLoopers
Previously, all Conversations/Contacts/Users were stored in the Server,
each in their respective KeyMaps, identified solely by their
identifiers. This leads to the glaring problem of overlap― if the user
has multiple accounts, some users/rooms might be used or present in multiple
accounts at the same time.
Now, each accounts' Contacts, Conversations, and Users are stored in
its ProtocolLooper, making this overlap impossible. An oversight of only
allowing one user identifier to be stored (fMySelf) in Server was also fixed
this way.
This is the bulk of the work required for multi-account support― now,
the user can join the same XMPP room on two seperate accounts, and it
works perfectly.
2021-06-10 15:16:43 -05:00
|
|
|
Conversation::GetOwnId()
|
2021-05-30 20:45:24 -05:00
|
|
|
{
|
Explicitly tie Conversations, Contacts, and Users to their ProtocolLoopers
Previously, all Conversations/Contacts/Users were stored in the Server,
each in their respective KeyMaps, identified solely by their
identifiers. This leads to the glaring problem of overlap― if the user
has multiple accounts, some users/rooms might be used or present in multiple
accounts at the same time.
Now, each accounts' Contacts, Conversations, and Users are stored in
its ProtocolLooper, making this overlap impossible. An oversight of only
allowing one user identifier to be stored (fMySelf) in Server was also fixed
this way.
This is the bulk of the work required for multi-account support― now,
the user can join the same XMPP room on two seperate accounts, and it
works perfectly.
2021-06-10 15:16:43 -05:00
|
|
|
return fLooper->GetOwnId();
|
2021-05-30 20:45:24 -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
|
|
|
void
|
|
|
|
Conversation::SetRole(BString id, Role* role)
|
2021-05-24 01:47:21 -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* oldRole = fRoles.ValueFor(id);
|
|
|
|
if (oldRole != NULL) {
|
|
|
|
fRoles.RemoveItemFor(id);
|
|
|
|
delete oldRole;
|
|
|
|
}
|
2021-05-30 19:07:50 -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
|
|
|
fRoles.AddItem(id, role);
|
2021-05-24 01:47:21 -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*
|
|
|
|
Conversation::GetRole(BString id)
|
2021-05-30 20:45:24 -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
|
|
|
return fRoles.ValueFor(id);
|
2021-05-30 20:45:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 00:19:52 -05:00
|
|
|
void
|
|
|
|
Conversation::_WarnUser(BString message)
|
|
|
|
{
|
|
|
|
BMessage* warning = new BMessage(IM_MESSAGE);
|
|
|
|
warning->AddInt32("im_what", IM_MESSAGE_RECEIVED);
|
|
|
|
warning->AddString("body", message.Append('\n', 1).InsertChars("-- ", 0));
|
|
|
|
GetView()->MessageReceived(warning);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 14:20:57 -05:00
|
|
|
void
|
|
|
|
Conversation::_LogChatMessage(BMessage* msg)
|
|
|
|
{
|
2021-05-24 14:48:25 -05:00
|
|
|
BString date;
|
|
|
|
fDateFormatter.Format(date, time(0), B_SHORT_DATE_FORMAT, B_MEDIUM_TIME_FORMAT);
|
|
|
|
|
2021-05-24 14:20:57 -05:00
|
|
|
BString id = msg->FindString("user_id");
|
2021-06-06 12:02:26 -05:00
|
|
|
BString body = msg->FindString("body");
|
|
|
|
|
2021-06-15 00:45:51 -05:00
|
|
|
if (id.IsEmpty() == true)
|
|
|
|
return;
|
|
|
|
|
2021-06-06 12:02:26 -05:00
|
|
|
// Binary logs
|
|
|
|
// TODO: Don't hardcode 21, expose maximum as a setting
|
|
|
|
BStringList users, bodies;
|
2021-06-14 16:41:25 -05:00
|
|
|
int64 times[21] = { 0 };
|
|
|
|
times[0] = (int64)time(NULL);
|
2021-06-06 12:02:26 -05:00
|
|
|
|
|
|
|
BMessage logMsg;
|
|
|
|
if (_GetChatLogs(&logMsg) == B_OK) {
|
|
|
|
logMsg.FindStrings("body", &bodies);
|
|
|
|
logMsg.FindStrings("user_id", &users);
|
2021-06-14 16:41:25 -05:00
|
|
|
|
|
|
|
int64 found;
|
|
|
|
for (int i = 0; i < 21; i++)
|
|
|
|
if (logMsg.FindInt64("when", i, &found) == B_OK)
|
|
|
|
times[i + 1] = found;
|
|
|
|
|
2021-06-06 12:02:26 -05:00
|
|
|
bodies.Remove(21);
|
|
|
|
users.Remove(21);
|
|
|
|
bodies.Add(body, 0);
|
|
|
|
users.Add(id, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BMessage newLogMsg(IM_MESSAGE);
|
|
|
|
newLogMsg.AddInt32("im_what", IM_LOGS_RECEIVED);
|
|
|
|
newLogMsg.AddStrings("body", bodies);
|
|
|
|
newLogMsg.AddStrings("user_id", users);
|
2021-06-14 16:41:25 -05:00
|
|
|
newLogMsg.AddInt64("when", time(NULL));
|
|
|
|
for (int i = 0; i < 21; i++)
|
|
|
|
newLogMsg.AddInt64("when", times[i]);
|
2021-06-06 12:02:26 -05:00
|
|
|
|
2021-06-12 21:42:10 -05:00
|
|
|
BFile logFile(fCachePath.Path(), B_READ_WRITE | B_OPEN_AT_END | B_CREATE_FILE);
|
|
|
|
WriteAttributeMessage(&logFile, "Caya:logs", &newLogMsg);
|
2021-05-24 14:20:57 -05:00
|
|
|
|
2021-06-06 12:02:26 -05:00
|
|
|
// Plain-text logs
|
|
|
|
BString uname;
|
2021-05-24 14:20:57 -05:00
|
|
|
if (id.IsEmpty() == false)
|
|
|
|
uname = UserById(id)->GetName();
|
|
|
|
else
|
|
|
|
uname = "You";
|
|
|
|
|
2021-05-24 14:48:25 -05:00
|
|
|
BString logLine("[");
|
2021-06-11 20:33:28 -05:00
|
|
|
logLine << date << "] <" << uname << "> " << body << "\n";
|
2021-05-24 14:20:57 -05:00
|
|
|
|
2021-06-06 12:02:26 -05:00
|
|
|
logFile.Write(logLine.String(), logLine.Length());
|
2021-05-24 19:12:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-06 12:02:26 -05:00
|
|
|
status_t
|
|
|
|
Conversation::_GetChatLogs(BMessage* msg)
|
2021-05-24 19:12:42 -05:00
|
|
|
{
|
2021-06-12 21:42:10 -05:00
|
|
|
_EnsureCachePath();
|
2021-05-24 19:12:42 -05:00
|
|
|
|
2021-06-12 21:42:10 -05:00
|
|
|
BFile logFile(fCachePath.Path(), B_READ_WRITE | B_CREATE_FILE);
|
2021-05-24 19:12:42 -05:00
|
|
|
|
2021-06-14 16:41:25 -05:00
|
|
|
return ReadAttributeMessage(&logFile, "Caya:logs", msg);
|
2021-05-24 14:20:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-13 02:34:11 -05:00
|
|
|
void
|
|
|
|
Conversation::_CacheRoomFlags()
|
|
|
|
{
|
|
|
|
_EnsureCachePath();
|
|
|
|
BFile cacheFile(fCachePath.Path(), B_READ_WRITE | B_CREATE_FILE);
|
|
|
|
if (cacheFile.InitCheck() != B_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cacheFile.WriteAttr("Caya:flags", B_INT32_TYPE, 0, &fRoomFlags, sizeof(int32));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::_LoadRoomFlags()
|
|
|
|
{
|
|
|
|
_EnsureCachePath();
|
|
|
|
BFile cacheFile(fCachePath.Path(), B_READ_ONLY);
|
|
|
|
if (cacheFile.InitCheck() != B_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cacheFile.ReadAttr("Caya:flags", B_INT32_TYPE, 0, &fRoomFlags, sizeof(int32));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 14:20:57 -05:00
|
|
|
void
|
2021-06-12 21:42:10 -05:00
|
|
|
Conversation::_EnsureCachePath()
|
2021-05-24 14:20:57 -05:00
|
|
|
{
|
2021-06-12 21:42:10 -05:00
|
|
|
if (fCachePath.InitCheck() == B_OK)
|
2021-05-24 14:20:57 -05:00
|
|
|
return;
|
2021-06-12 21:42:10 -05:00
|
|
|
fCachePath.SetTo(CayaRoomCachePath(fLooper->Protocol()->GetName(),
|
|
|
|
fID.String()));
|
2021-05-24 14:20:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 11:56:45 -05:00
|
|
|
User*
|
2021-05-24 01:47:21 -05:00
|
|
|
Conversation::_EnsureUser(BMessage* msg)
|
|
|
|
{
|
|
|
|
BString id = msg->FindString("user_id");
|
2021-06-07 00:03:15 -05:00
|
|
|
BString name = msg->FindString("user_name");
|
2021-05-24 01:47:21 -05:00
|
|
|
if (id.IsEmpty() == true) return NULL;
|
|
|
|
|
2021-05-31 11:56:45 -05:00
|
|
|
User* user = UserById(id);
|
Explicitly tie Conversations, Contacts, and Users to their ProtocolLoopers
Previously, all Conversations/Contacts/Users were stored in the Server,
each in their respective KeyMaps, identified solely by their
identifiers. This leads to the glaring problem of overlap― if the user
has multiple accounts, some users/rooms might be used or present in multiple
accounts at the same time.
Now, each accounts' Contacts, Conversations, and Users are stored in
its ProtocolLooper, making this overlap impossible. An oversight of only
allowing one user identifier to be stored (fMySelf) in Server was also fixed
this way.
This is the bulk of the work required for multi-account support― now,
the user can join the same XMPP room on two seperate accounts, and it
works perfectly.
2021-06-10 15:16:43 -05:00
|
|
|
User* serverUser = fLooper->UserById(id);
|
2021-05-24 01:47:21 -05:00
|
|
|
|
2021-06-07 00:03:15 -05:00
|
|
|
// Not here, but found in server
|
2021-05-24 01:47:21 -05:00
|
|
|
if (user == NULL && serverUser != NULL) {
|
|
|
|
fUsers.AddItem(id, serverUser);
|
|
|
|
user = serverUser;
|
2021-05-31 10:50:43 -05:00
|
|
|
GetView()->UpdateUserList(fUsers);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
2021-06-07 00:03:15 -05:00
|
|
|
// Not anywhere; create user
|
2021-05-24 01:47:21 -05:00
|
|
|
else if (user == NULL) {
|
2021-06-15 00:19:52 -05:00
|
|
|
user = new User(id, _GetServer()->Looper());
|
2021-05-24 01:47:21 -05:00
|
|
|
user->SetProtocolLooper(fLooper);
|
|
|
|
|
Explicitly tie Conversations, Contacts, and Users to their ProtocolLoopers
Previously, all Conversations/Contacts/Users were stored in the Server,
each in their respective KeyMaps, identified solely by their
identifiers. This leads to the glaring problem of overlap― if the user
has multiple accounts, some users/rooms might be used or present in multiple
accounts at the same time.
Now, each accounts' Contacts, Conversations, and Users are stored in
its ProtocolLooper, making this overlap impossible. An oversight of only
allowing one user identifier to be stored (fMySelf) in Server was also fixed
this way.
This is the bulk of the work required for multi-account support― now,
the user can join the same XMPP room on two seperate accounts, and it
works perfectly.
2021-06-10 15:16:43 -05:00
|
|
|
fLooper->AddUser(user);
|
2021-05-24 01:47:21 -05:00
|
|
|
fUsers.AddItem(id, user);
|
2021-05-31 10:50:43 -05:00
|
|
|
GetView()->UpdateUserList(fUsers);
|
2021-06-10 16:44:35 -05:00
|
|
|
}
|
2021-06-07 00:03:15 -05:00
|
|
|
|
2021-06-10 16:44:35 -05:00
|
|
|
if (name.IsEmpty() == false) {
|
|
|
|
user->SetNotifyName(name);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
2021-06-03 23:39:50 -05:00
|
|
|
user->RegisterObserver(this);
|
2021-05-24 01:47:21 -05:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 00:19:52 -05:00
|
|
|
Server*
|
|
|
|
Conversation::_GetServer()
|
|
|
|
{
|
|
|
|
return ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|