(irc) Write-lock and responding to pings

This commit is contained in:
Jaidyn Ann 2021-08-10 12:53:20 -05:00
parent 340aa405f3
commit 35cd5cbc8f
2 changed files with 43 additions and 13 deletions

View File

@ -36,7 +36,8 @@ IrcProtocol::IrcProtocol()
fSocket(NULL), fSocket(NULL),
fNick(NULL), fNick(NULL),
fIdent(NULL), fIdent(NULL),
fReady(false) fReady(false),
fWriteLocked(false)
{ {
} }
@ -152,10 +153,18 @@ IrcProtocol::Process(BMessage* msg)
{ {
BString chat_id; BString chat_id;
if (msg->FindString("chat_id", &chat_id) == B_OK) { if (msg->FindString("chat_id", &chat_id) == B_OK) {
if (_IsChannelName(chat_id) == true) {
BString cmd = "PART "; BString cmd = "PART ";
cmd << chat_id << " * :" << fPartText << "\n"; cmd << chat_id << " * :" << fPartText << "\n";
_SendIrc(cmd); _SendIrc(cmd);
} }
else {
BMessage left(IM_MESSAGE);
left.AddInt32("im_what", IM_ROOM_LEFT);
left.AddString("chat_id", chat_id);
_SendMsg(&left);
}
}
break; break;
} }
case IM_GET_ROOM_METADATA: case IM_GET_ROOM_METADATA:
@ -166,7 +175,7 @@ IrcProtocol::Process(BMessage* msg)
meta.AddInt32("im_what", IM_ROOM_METADATA); meta.AddInt32("im_what", IM_ROOM_METADATA);
meta.AddString("chat_id", chat_id); meta.AddString("chat_id", chat_id);
meta.AddInt32("room_default_flags", meta.AddInt32("room_default_flags",
ROOM_AUTOJOIN | ROOM_LOG_LOCALLY | ROOM_POPULATE_LOGS); ROOM_LOG_LOCALLY | ROOM_POPULATE_LOGS);
_SendMsg(&meta); _SendMsg(&meta);
} }
break; break;
@ -343,7 +352,7 @@ IrcProtocol::_ProcessCommand(BString command, BString sender,
if (fReady == false && _SenderNick(sender) == fNick) if (fReady == false && _SenderNick(sender) == fNick)
_MakeReady(_SenderNick(sender), _SenderIdent(sender)); _MakeReady(_SenderNick(sender), _SenderIdent(sender));
if (command == "PING") if (sender == "PING")
{ {
BString cmd = "PONG "; BString cmd = "PONG ";
cmd << params.Last() << "\n"; cmd << params.Last() << "\n";
@ -353,7 +362,7 @@ IrcProtocol::_ProcessCommand(BString command, BString sender,
{ {
BString chat_id = params.First(); BString chat_id = params.First();
BString user_id = _SenderIdent(sender); BString user_id = _SenderIdent(sender);
if (params.First() == fNick) if (_IsChannelName(chat_id) == false)
chat_id = _SenderNick(sender); chat_id = _SenderNick(sender);
BMessage chat(IM_MESSAGE); BMessage chat(IM_MESSAGE);
@ -439,15 +448,20 @@ IrcProtocol::_ProcessCommand(BString command, BString sender,
} }
else if (command == "NICK") else if (command == "NICK")
{ {
BString ident = _SenderIdent(sender);
BString user_name = params.Last();
BMessage nick(IM_MESSAGE); BMessage nick(IM_MESSAGE);
nick.AddString("user_name", params.Last()); nick.AddString("user_name", user_name);
if (_SenderIdent(sender) == fIdent) { if (ident == fIdent) {
nick.AddInt32("im_what", IM_OWN_NICKNAME_SET); nick.AddInt32("im_what", IM_OWN_NICKNAME_SET);
fNick = params.Last(); fNick = user_name;
} }
else { else {
nick.AddInt32("im_what", IM_USER_NICKNAME_SET); nick.AddInt32("im_what", IM_USER_NICKNAME_SET);
nick.AddString("user_id", _SenderIdent(sender)); nick.AddString("user_id", ident);
fIdentNicks.RemoveItemFor(ident);
fIdentNicks.AddItem(ident, user_name);
} }
_SendMsg(&nick); _SendMsg(&nick);
} }
@ -482,7 +496,7 @@ IrcProtocol::_LineSender(BStringList words)
{ {
BString sender; BString sender;
if (words.CountStrings() > 1) if (words.CountStrings() > 1)
sender = words.First().RemoveChars(0, 1); sender = words.First().RemoveFirst(":");
return sender; return sender;
} }
@ -532,8 +546,13 @@ IrcProtocol::_SendMsg(BMessage* msg)
void void
IrcProtocol::_SendIrc(BString cmd) IrcProtocol::_SendIrc(BString cmd)
{ {
if (fSocket != NULL && fSocket->IsConnected() == true) if (fSocket != NULL && fSocket->IsConnected() == true) {
while (fWriteLocked == true)
snooze(1000);
fWriteLocked = true;
fSocket->Write(cmd.String(), cmd.CountChars()); fSocket->Write(cmd.String(), cmd.CountChars());
fWriteLocked = false;
}
else { else {
BMessage disable(IM_MESSAGE); BMessage disable(IM_MESSAGE);
disable.AddInt32("im_what", IM_PROTOCOL_DISABLE); disable.AddInt32("im_what", IM_PROTOCOL_DISABLE);
@ -570,6 +589,14 @@ IrcProtocol::_IdentNick(BString ident)
} }
bool
IrcProtocol::_IsChannelName(BString name)
{
return (name.StartsWith("!") || name.StartsWith("&") || name.StartsWith("#")
|| name.StartsWith("+"));
}
BString BString
IrcProtocol::_ReadUntilNewline(BDataIO* io, BString* extraBuffer) IrcProtocol::_ReadUntilNewline(BDataIO* io, BString* extraBuffer)
{ {

View File

@ -76,6 +76,8 @@ private:
BString _IdentNick(BString ident); BString _IdentNick(BString ident);
bool _IsChannelName(BString name);
// Read a data stream until newline found; if data found past // Read a data stream until newline found; if data found past
// newline, append to given buffer for later use // newline, append to given buffer for later use
BString _ReadUntilNewline(BDataIO* data, BString* extraBuffer); BString _ReadUntilNewline(BDataIO* data, BString* extraBuffer);
@ -98,6 +100,7 @@ private:
// WHOREPLY is requested by the add-on to populate the user-list, but the // WHOREPLY is requested by the add-on to populate the user-list, but the
// user might also use the /who command― if the user does, this is true // user might also use the /who command― if the user does, this is true
bool fWhoRequested; bool fWhoRequested;
bool fWriteLocked;
StringMap fIdentNicks; // User ident → nick StringMap fIdentNicks; // User ident → nick