2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-03-15 14:14:23 +03:00
|
|
|
#include "IRCChannel.h"
|
2019-03-15 20:25:51 +03:00
|
|
|
#include "IRCChannelMemberListModel.h"
|
2019-06-07 12:48:03 +03:00
|
|
|
#include "IRCClient.h"
|
2019-03-15 14:14:23 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
IRCChannel::IRCChannel(IRCClient& client, const String& name)
|
|
|
|
: m_client(client)
|
|
|
|
, m_name(name)
|
|
|
|
, m_log(IRCLogBuffer::create())
|
2019-03-20 05:27:07 +03:00
|
|
|
, m_member_model(IRCChannelMemberListModel::create(*this))
|
2019-03-15 14:14:23 +03:00
|
|
|
{
|
2019-03-16 03:45:49 +03:00
|
|
|
m_window = m_client.aid_create_window(this, IRCWindow::Channel, m_name);
|
|
|
|
m_window->set_log_buffer(*m_log);
|
2019-03-15 14:14:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IRCChannel::~IRCChannel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
NonnullRefPtr<IRCChannel> IRCChannel::create(IRCClient& client, const String& name)
|
2019-03-15 14:14:23 +03:00
|
|
|
{
|
2021-04-23 17:46:57 +03:00
|
|
|
return adopt_ref(*new IRCChannel(client, name));
|
2019-03-15 14:14:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRCChannel::add_member(const String& name, char prefix)
|
|
|
|
{
|
|
|
|
for (auto& member : m_members) {
|
|
|
|
if (member.name == name) {
|
|
|
|
member.prefix = prefix;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_members.append({ name, prefix });
|
2019-03-18 22:56:45 +03:00
|
|
|
m_member_model->update();
|
2019-03-15 14:14:23 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 22:56:45 +03:00
|
|
|
void IRCChannel::remove_member(const String& name)
|
2019-03-15 14:14:23 +03:00
|
|
|
{
|
2019-06-07 12:48:03 +03:00
|
|
|
m_members.remove_first_matching([&](auto& member) { return name == member.name; });
|
2019-03-18 22:56:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRCChannel::add_message(char prefix, const String& name, const String& text, Color color)
|
|
|
|
{
|
|
|
|
log().add_message(prefix, name, text, color);
|
2020-03-26 22:11:23 +03:00
|
|
|
window().did_add_message(name, text);
|
2019-03-18 22:56:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRCChannel::add_message(const String& text, Color color)
|
|
|
|
{
|
|
|
|
log().add_message(text, color);
|
2019-03-16 04:14:53 +03:00
|
|
|
window().did_add_message();
|
2019-03-15 14:14:23 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 19:37:13 +03:00
|
|
|
void IRCChannel::say(const String& text)
|
|
|
|
{
|
|
|
|
m_client.send_privmsg(m_name, text);
|
|
|
|
add_message(' ', m_client.nickname(), text);
|
|
|
|
}
|
2019-03-16 14:21:42 +03:00
|
|
|
|
|
|
|
void IRCChannel::handle_join(const String& nick, const String& hostmask)
|
|
|
|
{
|
2020-04-02 17:39:25 +03:00
|
|
|
if (nick == m_client.nickname()) {
|
2019-03-16 14:21:42 +03:00
|
|
|
m_open = true;
|
2020-04-02 17:39:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
add_member(nick, (char)0);
|
|
|
|
m_member_model->update();
|
2020-04-10 13:59:13 +03:00
|
|
|
if (m_client.show_join_part_messages())
|
2020-10-06 15:16:35 +03:00
|
|
|
add_message(String::formatted("*** {} [{}] has joined {}", nick, hostmask, m_name), Color::MidGreen);
|
2019-03-16 14:21:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRCChannel::handle_part(const String& nick, const String& hostmask)
|
|
|
|
{
|
2019-03-18 22:56:45 +03:00
|
|
|
if (nick == m_client.nickname()) {
|
2019-03-16 14:21:42 +03:00
|
|
|
m_open = false;
|
2019-03-18 22:56:45 +03:00
|
|
|
m_members.clear();
|
2019-07-07 22:51:08 +03:00
|
|
|
m_client.did_part_from_channel({}, *this);
|
2019-03-18 22:56:45 +03:00
|
|
|
} else {
|
|
|
|
remove_member(nick);
|
|
|
|
}
|
|
|
|
m_member_model->update();
|
2020-04-10 13:59:13 +03:00
|
|
|
if (m_client.show_join_part_messages())
|
2020-10-06 15:16:35 +03:00
|
|
|
add_message(String::formatted("*** {} [{}] has parted from {}", nick, hostmask, m_name), Color::MidGreen);
|
2019-03-16 14:21:42 +03:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:39:25 +03:00
|
|
|
void IRCChannel::handle_quit(const String& nick, const String& hostmask, const String& message)
|
|
|
|
{
|
|
|
|
if (nick == m_client.nickname()) {
|
|
|
|
m_open = false;
|
|
|
|
m_members.clear();
|
|
|
|
m_client.did_part_from_channel({}, *this);
|
|
|
|
} else {
|
|
|
|
remove_member(nick);
|
|
|
|
}
|
|
|
|
m_member_model->update();
|
2020-10-06 15:16:35 +03:00
|
|
|
add_message(String::formatted("*** {} [{}] has quit ({})", nick, hostmask, message), Color::MidGreen);
|
2020-04-02 17:39:25 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 14:21:42 +03:00
|
|
|
void IRCChannel::handle_topic(const String& nick, const String& topic)
|
|
|
|
{
|
2019-03-18 22:56:45 +03:00
|
|
|
if (nick.is_null())
|
2020-10-06 15:16:35 +03:00
|
|
|
add_message(String::formatted("*** Topic is \"{}\"", topic), Color::MidBlue);
|
2019-03-18 22:56:45 +03:00
|
|
|
else
|
2020-10-06 15:16:35 +03:00
|
|
|
add_message(String::formatted("*** {} set topic to \"{}\"", nick, topic), Color::MidBlue);
|
2019-03-20 06:21:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRCChannel::notify_nick_changed(const String& old_nick, const String& new_nick)
|
|
|
|
{
|
|
|
|
for (auto& member : m_members) {
|
|
|
|
if (member.name == old_nick) {
|
|
|
|
member.name = new_nick;
|
|
|
|
m_member_model->update();
|
2020-04-10 13:59:13 +03:00
|
|
|
if (m_client.show_nick_change_messages())
|
2020-10-06 15:16:35 +03:00
|
|
|
add_message(String::formatted("~ {} changed nickname to {}", old_nick, new_nick), Color::MidMagenta);
|
2019-03-20 06:21:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-03-16 14:21:42 +03:00
|
|
|
}
|