1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:37:35 +00:00

IRCClient: Colorize some channel messages (joins, parts, topics)

This commit is contained in:
Andreas Kling 2019-03-18 20:56:45 +01:00
parent 794c81626e
commit 55aa819077
11 changed files with 60 additions and 23 deletions

View file

@ -32,14 +32,24 @@ void IRCChannel::add_member(const String& name, char prefix)
} }
} }
m_members.append({ name, prefix }); m_members.append({ name, prefix });
dump(); m_member_model->update();
} }
void IRCChannel::add_message(char prefix, const String& name, const String& text) void IRCChannel::remove_member(const String& name)
{ {
m_members.remove_first_matching([&] (auto& member) { return name == member.name; });
}
void IRCChannel::add_message(char prefix, const String& name, const String& text, Color color)
{
log().add_message(prefix, name, text, color);
window().did_add_message();
}
void IRCChannel::add_message(const String& text, Color color)
{
log().add_message(text, color);
window().did_add_message(); window().did_add_message();
log().add_message(prefix, name, text);
dump();
} }
void IRCChannel::dump() const void IRCChannel::dump() const
@ -60,19 +70,25 @@ void IRCChannel::handle_join(const String& nick, const String& hostmask)
{ {
if (nick == m_client.nickname()) if (nick == m_client.nickname())
m_open = true; m_open = true;
add_message(' ', "", String::format("*** %s [%s] has joined %s", nick.characters(), hostmask.characters(), m_name.characters())); add_message(String::format("*** %s [%s] has joined %s", nick.characters(), hostmask.characters(), m_name.characters()), Color::DarkGreen);
} }
void IRCChannel::handle_part(const String& nick, const String& hostmask) void IRCChannel::handle_part(const String& nick, const String& hostmask)
{ {
if (nick == m_client.nickname()) if (nick == m_client.nickname()) {
m_open = false; m_open = false;
add_message(' ', "", String::format("*** %s [%s] has parted from %s", nick.characters(), hostmask.characters(), m_name.characters())); m_members.clear();
} else {
remove_member(nick);
}
m_member_model->update();
add_message(String::format("*** %s [%s] has parted from %s", nick.characters(), hostmask.characters(), m_name.characters()), Color::DarkGreen);
} }
void IRCChannel::handle_topic(const String& nick, const String& topic) void IRCChannel::handle_topic(const String& nick, const String& topic)
{ {
if (nick == m_client.nickname()) if (nick.is_null())
m_open = false; add_message(String::format("*** Topic is \"%s\"", topic.characters()), Color::DarkBlue);
add_message(' ', "", String::format("*** %s set topic to \"%s\"", nick.characters(), topic.characters())); else
add_message(String::format("*** %s set topic to \"%s\"", nick.characters(), topic.characters()), Color::DarkBlue);
} }

View file

@ -24,7 +24,8 @@ public:
void add_member(const String& name, char prefix); void add_member(const String& name, char prefix);
void remove_member(const String& name); void remove_member(const String& name);
void add_message(char prefix, const String& name, const String& text); void add_message(char prefix, const String& name, const String& text, Color = Color::Black);
void add_message(const String& text, Color = Color::Black);
void dump() const; void dump() const;

View file

@ -369,7 +369,7 @@ void IRCClient::handle_join(const Message& msg)
void IRCClient::handle_part(const Message& msg) void IRCClient::handle_part(const Message& msg)
{ {
if (msg.arguments.size() != 1) if (msg.arguments.size() < 1)
return; return;
auto prefix_parts = msg.prefix.split('!'); auto prefix_parts = msg.prefix.split('!');
if (prefix_parts.size() < 1) if (prefix_parts.size() < 1)
@ -393,12 +393,11 @@ void IRCClient::handle_topic(const Message& msg)
void IRCClient::handle_rpl_topic(const Message& msg) void IRCClient::handle_rpl_topic(const Message& msg)
{ {
if (msg.arguments.size() != 3) if (msg.arguments.size() < 3)
return; return;
auto& nick = msg.arguments[0];
auto& channel_name = msg.arguments[1]; auto& channel_name = msg.arguments[1];
auto& topic = msg.arguments[2]; auto& topic = msg.arguments[2];
ensure_channel(channel_name).handle_topic(nick, topic); ensure_channel(channel_name).handle_topic({ }, topic);
// FIXME: Handle RPL_TOPICWHOTIME so we can know who set it and when. // FIXME: Handle RPL_TOPICWHOTIME so we can know who set it and when.
} }
@ -501,7 +500,7 @@ void IRCClient::handle_rpl_topicwhotime(const Message& msg)
tm->tm_sec tm->tm_sec
); );
} }
ensure_channel(channel_name).add_message(0, "", String::format("Topic set by %s at %s", nick.characters(), setat.characters())); ensure_channel(channel_name).add_message(String::format("*** (set by %s at %s)", nick.characters(), setat.characters()), Color::DarkBlue);
} }
void IRCClient::register_subwindow(IRCWindow& subwindow) void IRCClient::register_subwindow(IRCWindow& subwindow)

View file

@ -17,9 +17,15 @@ IRCLogBuffer::~IRCLogBuffer()
{ {
} }
void IRCLogBuffer::add_message(char prefix, const String& name, const String& text) void IRCLogBuffer::add_message(char prefix, const String& name, const String& text, Color color)
{ {
m_messages.enqueue({ time(nullptr), prefix, name, text }); m_messages.enqueue({ time(nullptr), prefix, name, text, color });
m_model->update();
}
void IRCLogBuffer::add_message(const String& text, Color color)
{
m_messages.enqueue({ time(nullptr), '\0', String(), text, color });
m_model->update(); m_model->update();
} }

View file

@ -4,6 +4,7 @@
#include <AK/CircularQueue.h> #include <AK/CircularQueue.h>
#include <AK/Retainable.h> #include <AK/Retainable.h>
#include <AK/RetainPtr.h> #include <AK/RetainPtr.h>
#include <SharedGraphics/Color.h>
class IRCLogBufferModel; class IRCLogBufferModel;
@ -17,11 +18,13 @@ public:
char prefix { 0 }; char prefix { 0 };
String sender; String sender;
String text; String text;
Color color { Color::Black };
}; };
int count() const { return m_messages.size(); } int count() const { return m_messages.size(); }
const Message& at(int index) const { return m_messages.at(index); } const Message& at(int index) const { return m_messages.at(index); }
void add_message(char prefix, const String& name, const String& text); void add_message(char prefix, const String& name, const String& text, Color = Color::Black);
void add_message(const String& text, Color = Color::Black);
void dump() const; void dump() const;
const IRCLogBufferModel* model() const { return m_model; } const IRCLogBufferModel* model() const { return m_model; }

View file

@ -59,6 +59,10 @@ GVariant IRCLogBufferModel::data(const GModelIndex& index, Role role) const
case Column::Text: return entry.text; case Column::Text: return entry.text;
} }
} }
if (role == Role::ForegroundColor) {
if (index.column() == Column::Text)
return m_log_buffer->at(index.row()).color;
}
return { }; return { };
} }

View file

@ -27,11 +27,10 @@ void IRCQuery::dump() const
log().dump(); log().dump();
} }
void IRCQuery::add_message(char prefix, const String& name, const String& text) void IRCQuery::add_message(char prefix, const String& name, const String& text, Color color)
{ {
log().add_message(prefix, name, text, color);
window().did_add_message(); window().did_add_message();
log().add_message(prefix, name, text);
dump();
} }
void IRCQuery::say(const String& text) void IRCQuery::say(const String& text)

View file

@ -16,7 +16,7 @@ public:
~IRCQuery(); ~IRCQuery();
String name() const { return m_name; } String name() const { return m_name; }
void add_message(char prefix, const String& name, const String& text); void add_message(char prefix, const String& name, const String& text, Color = Color::Black);
void dump() const; void dump() const;

View file

@ -1,6 +1,7 @@
#include "IRCWindowListModel.h" #include "IRCWindowListModel.h"
#include "IRCWindow.h" #include "IRCWindow.h"
#include "IRCClient.h" #include "IRCClient.h"
#include "IRCChannel.h"
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
@ -58,6 +59,8 @@ GVariant IRCWindowListModel::data(const GModelIndex& index, Role role) const
auto& window = m_client.window_at(index.row()); auto& window = m_client.window_at(index.row());
if (window.unread_count()) if (window.unread_count())
return Color(Color::Red); return Color(Color::Red);
if (!window.channel().is_open())
return Color(Color::LightGray);
return Color(Color::Black); return Color(Color::Black);
} }
} }

View file

@ -20,6 +20,9 @@ Color::Color(NamedColor named)
case DarkGray: rgb = { 64, 64, 64 }; break; case DarkGray: rgb = { 64, 64, 64 }; break;
case MidGray: rgb = { 127, 127, 127 }; break; case MidGray: rgb = { 127, 127, 127 }; break;
case LightGray: rgb = { 192, 192, 192 }; break; case LightGray: rgb = { 192, 192, 192 }; break;
case DarkGreen: rgb = { 0, 128, 0 }; break;
case DarkBlue: rgb = { 0, 0, 128 }; break;
case DarkRed: rgb = { 128, 0, 0 }; break;
default: ASSERT_NOT_REACHED(); break; default: ASSERT_NOT_REACHED(); break;
} }

View file

@ -23,6 +23,9 @@ public:
DarkGray, DarkGray,
MidGray, MidGray,
LightGray, LightGray,
DarkGreen,
DarkBlue,
DarkRed,
}; };
Color() { } Color() { }