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-07-19 09:54:27 -05:00
|
|
|
#include <Catalog.h>
|
2021-05-24 14:48:25 -05:00
|
|
|
#include <DateTimeFormat.h>
|
|
|
|
#include <Locale.h>
|
2021-07-16 16:28:58 -05:00
|
|
|
#include <Notification.h>
|
|
|
|
#include <StringFormat.h>
|
2021-05-24 19:12:42 -05:00
|
|
|
#include <StringList.h>
|
2021-05-24 14:48:25 -05:00
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "AppPreferences.h"
|
2021-07-16 16:28:58 -05:00
|
|
|
#include "Cardie.h"
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "ChatProtocolMessages.h"
|
|
|
|
#include "RenderView.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-06-30 14:27:58 -05:00
|
|
|
#include "Flags.h"
|
2021-07-27 19:51:55 -05:00
|
|
|
#include "ImageCache.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-05-24 01:47:21 -05:00
|
|
|
#include "Server.h"
|
|
|
|
#include "TheApp.h"
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "Utils.h"
|
2021-05-24 01:47:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
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-07-27 19:51:55 -05:00
|
|
|
fIcon(ImageCache::Get()->GetImage("kChatIcon")),
|
2021-06-13 01:16:30 -05:00
|
|
|
fDateFormatter(),
|
|
|
|
fRoomFlags(0),
|
2021-07-16 16:28:58 -05:00
|
|
|
fDisallowedFlags(0),
|
2021-07-17 00:23:56 -05:00
|
|
|
fNotifyMessageCount(0),
|
2021-07-27 19:51:55 -05:00
|
|
|
fNotifyMentionCount(0),
|
|
|
|
fUserIcon(false)
|
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:
|
|
|
|
{
|
2021-07-19 09:54:27 -05:00
|
|
|
#undef B_TRANSLATION_CONTEXT
|
|
|
|
#define B_TRANSLATION_CONTEXT "Conversation ― Notifications"
|
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
_EnsureUser(msg);
|
2021-05-24 14:20:57 -05:00
|
|
|
_LogChatMessage(msg);
|
2021-05-28 22:26:32 -05:00
|
|
|
GetView()->MessageReceived(msg);
|
2021-07-16 16:28:58 -05:00
|
|
|
|
|
|
|
BString text = msg->FindString("body");
|
|
|
|
Contact* contact = GetOwnContact();
|
2021-07-17 00:23:56 -05:00
|
|
|
BWindow* win = fChatView->Window();
|
|
|
|
|
|
|
|
bool winFocused = (win != NULL &&
|
|
|
|
(win->IsFront() && !(win->IsMinimized())));
|
2021-07-16 16:28:58 -05:00
|
|
|
bool mentioned = ((text.IFindFirst(contact->GetName()) != B_ERROR)
|
|
|
|
|| (text.IFindFirst(contact->GetName()) != B_ERROR));
|
|
|
|
|
2021-07-17 00:23:56 -05:00
|
|
|
// Send a notification, if appropriate
|
2021-07-28 19:10:09 -05:00
|
|
|
if (winFocused == false && AppPreferences::Get()->NotifyNewMessage
|
2021-07-17 00:23:56 -05:00
|
|
|
&& (fUsers.CountItems() <= 2 || mentioned == true))
|
2021-07-16 16:28:58 -05:00
|
|
|
{
|
2021-07-19 09:54:27 -05:00
|
|
|
BString notifyTitle = B_TRANSLATE("New mention");
|
|
|
|
BString notifyText = B_TRANSLATE("You've been summoned from "
|
|
|
|
"%source%.");
|
2021-07-16 16:28:58 -05:00
|
|
|
|
|
|
|
if (mentioned == false) {
|
2021-07-17 00:23:56 -05:00
|
|
|
fNotifyMessageCount++;
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
notifyTitle.SetTo(B_TRANSLATE("New message"));
|
2021-07-16 16:28:58 -05:00
|
|
|
notifyText.SetTo("");
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
BStringFormat pmFormat(B_TRANSLATE("{0, plural,"
|
2021-07-16 16:28:58 -05:00
|
|
|
"=1{You've got a new message from %source%.}"
|
2021-07-19 09:54:27 -05:00
|
|
|
"other{You've got # new messages from %source%.}}"));
|
2021-07-17 00:23:56 -05:00
|
|
|
pmFormat.Format(notifyText, fNotifyMessageCount);
|
2021-07-16 16:28:58 -05:00
|
|
|
}
|
2021-07-17 00:23:56 -05:00
|
|
|
else
|
|
|
|
fNotifyMentionCount++;
|
|
|
|
|
2021-07-16 16:28:58 -05:00
|
|
|
notifyText.ReplaceAll("%source%", GetName());
|
|
|
|
|
|
|
|
BBitmap* icon = IconBitmap();
|
|
|
|
if (icon == NULL)
|
|
|
|
icon = ProtocolBitmap();
|
|
|
|
|
|
|
|
|
|
|
|
BNotification notification(B_INFORMATION_NOTIFICATION);
|
|
|
|
notification.SetGroup(BString(APP_NAME));
|
|
|
|
notification.SetTitle(notifyTitle);
|
|
|
|
notification.SetIcon(icon);
|
|
|
|
notification.SetContent(notifyText);
|
|
|
|
notification.SetMessageID(fID);
|
|
|
|
notification.Send();
|
|
|
|
}
|
2021-07-17 00:23:56 -05:00
|
|
|
|
|
|
|
// If unattached, highlight the ConversationItem
|
|
|
|
if (win == NULL && mentioned == true)
|
|
|
|
NotifyInteger(INT_NEW_MENTION, fNotifyMentionCount);
|
|
|
|
else if (win == NULL)
|
|
|
|
NotifyInteger(INT_NEW_MESSAGE, fNotifyMessageCount);
|
|
|
|
|
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-07-19 09:54:27 -05:00
|
|
|
#undef B_TRANSLATION_CONTEXT
|
|
|
|
#define B_TRANSLATION_CONTEXT "Conversation ― Command info"
|
|
|
|
|
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-07-19 09:54:27 -05:00
|
|
|
_WarnUser(BString(B_TRANSLATE("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-07-29 22:00:01 -05:00
|
|
|
case IM_ROOM_ROLECHANGED:
|
|
|
|
{
|
|
|
|
BString user_id;
|
|
|
|
Role* role = _GetRole(msg);
|
|
|
|
if (msg->FindString("user_id", &user_id) != B_OK || role == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
SetRole(user_id, role);
|
|
|
|
GetView()->MessageReceived(msg);
|
|
|
|
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-07-17 00:23:56 -05:00
|
|
|
if (what == INT_WINDOW_FOCUSED) {
|
|
|
|
fNotifyMessageCount = 0;
|
|
|
|
fNotifyMentionCount = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
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-07-27 19:51:55 -05:00
|
|
|
bool
|
|
|
|
Conversation::SetIconBitmap(BBitmap* icon)
|
|
|
|
{
|
|
|
|
if (icon != NULL) {
|
|
|
|
fIcon = icon;
|
|
|
|
GetView()->UpdateIcon();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2021-06-20 12:44:20 -05:00
|
|
|
ChatProtocol* protocol = fLooper->Protocol();
|
|
|
|
ChatProtocolAddOn* addOn
|
2021-06-04 13:57:04 -05:00
|
|
|
= 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-21 02:32:49 -05:00
|
|
|
BString
|
|
|
|
Conversation::GetSubject() const
|
|
|
|
{
|
|
|
|
return fSubject;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-07-17 00:23:56 -05:00
|
|
|
fChatView->RegisterObserver(fConversationItem);
|
|
|
|
fChatView->RegisterObserver(this);
|
|
|
|
RegisterObserver(fChatView);
|
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
|
|
|
|
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
|
|
|
|
|
|
|
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-07-25 14:42:38 -05:00
|
|
|
_SortConversationList();
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2021-07-25 14:42:38 -05:00
|
|
|
_SortConversationList();
|
2021-06-02 16:53:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-06 14:46:28 -05:00
|
|
|
Contact*
|
|
|
|
Conversation::GetOwnContact()
|
2021-05-30 20:45:24 -05:00
|
|
|
{
|
2021-07-06 14:46:28 -05:00
|
|
|
return fLooper->GetOwnContact();
|
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;
|
|
|
|
}
|
|
|
|
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);
|
2021-06-20 12:44:20 -05:00
|
|
|
WriteAttributeMessage(&logFile, "Chat: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-20 12:44:20 -05:00
|
|
|
return ReadAttributeMessage(&logFile, "Chat: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;
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
cacheFile.WriteAttr("Chat:flags", B_INT32_TYPE, 0, &fRoomFlags, sizeof(int32));
|
2021-06-13 02:34:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::_LoadRoomFlags()
|
|
|
|
{
|
|
|
|
_EnsureCachePath();
|
|
|
|
BFile cacheFile(fCachePath.Path(), B_READ_ONLY);
|
|
|
|
if (cacheFile.InitCheck() != B_OK)
|
|
|
|
return;
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
cacheFile.ReadAttr("Chat:flags", B_INT32_TYPE, 0, &fRoomFlags, sizeof(int32));
|
2021-06-13 02:34:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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-20 12:44:20 -05:00
|
|
|
fCachePath.SetTo(RoomCachePath(fLooper->Protocol()->GetName(),
|
2021-06-12 21:42:10 -05:00
|
|
|
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-07-27 19:51:55 -05:00
|
|
|
|
2021-07-29 22:00:01 -05:00
|
|
|
_AdoptUserIcon(user);
|
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-07-27 19:51:55 -05:00
|
|
|
|
2021-07-29 22:00:01 -05:00
|
|
|
_AdoptUserIcon(user);
|
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-07-29 22:00:01 -05:00
|
|
|
Role*
|
|
|
|
Conversation::_GetRole(BMessage* msg)
|
|
|
|
{
|
|
|
|
if (!msg)
|
|
|
|
return NULL;
|
|
|
|
BString title;
|
|
|
|
int32 perms;
|
|
|
|
int32 priority;
|
|
|
|
|
|
|
|
if (msg->FindString("role_title", &title) != B_OK
|
|
|
|
|| msg->FindInt32("role_perms", &perms) != B_OK
|
|
|
|
|| msg->FindInt32("role_priority", &priority) != B_OK)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return new Role(title, perms, priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 19:51:55 -05:00
|
|
|
void
|
2021-07-29 22:00:01 -05:00
|
|
|
Conversation::_AdoptUserIcon(User* user)
|
2021-07-27 19:51:55 -05:00
|
|
|
{
|
|
|
|
// If it's a one-on-one chat without custom icon, steal a user's
|
|
|
|
if ((fUsers.CountItems() <= 2 && user->GetId() != GetOwnContact()->GetId())
|
|
|
|
&& (fIcon == ImageCache::Get()->GetImage("kChatIcon")
|
|
|
|
|| fIcon == NULL))
|
|
|
|
fUserIcon = SetIconBitmap(user->AvatarBitmap());
|
|
|
|
|
|
|
|
// If it's no longer one-on-one, revert
|
|
|
|
if (fUsers.CountItems() > 2 && fUserIcon == true) {
|
|
|
|
SetIconBitmap(ImageCache::Get()->GetImage("kChatIcon"));
|
|
|
|
fUserIcon = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-25 14:42:38 -05:00
|
|
|
void
|
|
|
|
Conversation::_SortConversationList()
|
|
|
|
{
|
|
|
|
if (fUsers.CountItems() <= 2 || fUsers.CountItems() == 3)
|
|
|
|
((TheApp*)be_app)->GetMainWindow()->SortConversation(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 00:19:52 -05:00
|
|
|
Server*
|
|
|
|
Conversation::_GetServer()
|
|
|
|
{
|
|
|
|
return ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
|
|
|
}
|