From ef970fe35babae4da5227416cd285d42016b186b Mon Sep 17 00:00:00 2001 From: attilamolnar Date: Tue, 29 May 2012 20:56:54 +0200 Subject: m_spanningtree Add channel timestamp to FTOPIC in protocol version 1204 Fixes issue #148 --- src/modules/m_spanningtree/compat.cpp | 18 +++++ src/modules/m_spanningtree/ftopic.cpp | 87 ++++++++++++++++++++---- src/modules/m_spanningtree/netburst.cpp | 2 +- src/modules/m_spanningtree/protocolinterface.cpp | 1 + 4 files changed, 94 insertions(+), 14 deletions(-) diff --git a/src/modules/m_spanningtree/compat.cpp b/src/modules/m_spanningtree/compat.cpp index f04b69e8f..a7c46cdc6 100644 --- a/src/modules/m_spanningtree/compat.cpp +++ b/src/modules/m_spanningtree/compat.cpp @@ -229,6 +229,24 @@ void TreeSocket::WriteLine(std::string line) line.erase(c, d-c); } } + else if (proto_version < 1204 && command == "FTOPIC") + { + // Drop channel TS for FTOPIC + // :sid FTOPIC #target TS TopicTS ... + // A B C D + if (b == std::string::npos) + return; + std::string::size_type c = line.find(' ', b + 1); + if (c == std::string::npos) + return; + + std::string::size_type d = line.find(' ', c + 1); + if (d == std::string::npos) + return; + + ServerInstance->Logs->Log("m_spanningtree", DEBUG, "Stripping channel TS in FTOPIC for pre-1204-protocol server"); + line.erase(c, d-c); + } } ServerInstance->Logs->Log("m_spanningtree", RAWIO, "S[%d] O %s", this->GetFd(), line.c_str()); diff --git a/src/modules/m_spanningtree/ftopic.cpp b/src/modules/m_spanningtree/ftopic.cpp index d559c6ae5..a20a443eb 100644 --- a/src/modules/m_spanningtree/ftopic.cpp +++ b/src/modules/m_spanningtree/ftopic.cpp @@ -28,24 +28,85 @@ /** FTOPIC command */ CmdResult CommandFTopic::Handle(const std::vector& params, User *user) { - time_t ts = atoi(params[1].c_str()); Channel* c = ServerInstance->FindChan(params[0]); - if (c) + if ((!c) || (!IS_SERVER(user))) + return CMD_FAILURE; + + /** + * Here's how this works: + * + * In pre-1204 protocol version we had a syntax like + * + * FTOPIC #chan topicts setby :topic + * + * where topicts is the time when the topic was set, and setby is the n!u@h + * of the user who set it. If the topicts value was bigger (newer) than our topic + * TS (stored in Channel::topicset), we accepted the change, else we assumed + * we already had a more recent topic and did nothing. + * + * However this behavior meant that if the channel was recreated on a server + * during a netsplit, users there could set any topic and override ours with it + * at merge because their topic had a newer topic TS. + * Protocol version 1204 addresses this issue by adding the channel TS to FTOPIC: + * + * FTOPIC #chan chants topicts setby :topic + * + * so we notice when the channel has been recreated and discard the FTOPIC in that case. + * Apart from this the logic remains the same. For previous versions not supporting the + * new syntax we need to fall back to the old behavior. + * + */ + + // Determine the protocol version of the sender + SpanningTreeUtilities* Utils = ((ModuleSpanningTree*) (Module*) creator)->Utils; + TreeServer* srcserver = Utils->FindServer(user->server); + bool has_chants = (srcserver->Socket->proto_version >= 1204); + + // If we got a channel TS compare it with ours. If it's different, drop the command. + // Also drop the command if we are using 1204, but there aren't enough parameters. + if (has_chants) + { + if (params.size() < 5) + return CMD_FAILURE; + + // Only proceed if the TSes are equal. If their TS is newer the channel + // got recreated on their side, if it's older that means things are messed up, + // because they haven't sent us an FJOIN earlier which could lower the chan TS. + if (ConvToInt(params[1]) != c->age) + return CMD_FAILURE; + } + + // Now do things as usual but if required, apply an offset to the index when accessing params + unsigned int indexoffset = (has_chants ? 1 : 0); + time_t topicts = ConvToInt(params[1+indexoffset]); + + // See if the topic they sent is newer than ours (or we don't have a topic at all) + if ((topicts >= c->topicset) || (c->topic.empty())) { - if ((ts >= c->topicset) || (c->topic.empty())) + if (c->topic != params[3+indexoffset]) { - if (c->topic != params[3]) - { - // Update topic only when it differs from current topic - c->topic.assign(params[3], 0, ServerInstance->Config->Limits.MaxTopic); - c->WriteChannel(user, "TOPIC %s :%s", c->name.c_str(), c->topic.c_str()); - } - - // Always update setter and settime. - c->setby.assign(params[2], 0, 127); - c->topicset = ts; + // Update topic only when it differs from current topic + c->topic.assign(params[3+indexoffset], 0, ServerInstance->Config->Limits.MaxTopic); + c->WriteChannel(user, "TOPIC %s :%s", c->name.c_str(), c->topic.c_str()); } + + // Always update setter and settime. + c->setby.assign(params[2+indexoffset], 0, 127); + c->topicset = topicts; + } + else + { + // We got a newer topic than this one, keep ours and drop the command + return CMD_FAILURE; } + + // They haven't sent us a channel TS, add ours before passing the command on + if (!has_chants) + { + parameterlist& p = const_cast(params); + p.insert(p.begin()+1, ConvToStr(c->age)); + } + return CMD_SUCCESS; } diff --git a/src/modules/m_spanningtree/netburst.cpp b/src/modules/m_spanningtree/netburst.cpp index f71bfed7c..c4cc93af8 100644 --- a/src/modules/m_spanningtree/netburst.cpp +++ b/src/modules/m_spanningtree/netburst.cpp @@ -156,7 +156,7 @@ void TreeSocket::SendFJoins(Channel* c) if (!c->topic.empty()) { - snprintf(list,MAXBUF,":%s FTOPIC %s %lu %s :%s", ServerInstance->Config->GetSID().c_str(), c->name.c_str(), (unsigned long)c->topicset, c->setby.c_str(), c->topic.c_str()); + snprintf(list,MAXBUF,":%s FTOPIC %s %lu %lu %s :%s", ServerInstance->Config->GetSID().c_str(), c->name.c_str(), (unsigned long) c->age, (unsigned long) c->topicset, c->setby.c_str(), c->topic.c_str()); WriteLine(list); } diff --git a/src/modules/m_spanningtree/protocolinterface.cpp b/src/modules/m_spanningtree/protocolinterface.cpp index 97e7b4160..c73dc08a4 100644 --- a/src/modules/m_spanningtree/protocolinterface.cpp +++ b/src/modules/m_spanningtree/protocolinterface.cpp @@ -112,6 +112,7 @@ void SpanningTreeProtocolInterface::SendTopic(Channel* channel, std::string &top parameterlist params; params.push_back(channel->name); + params.push_back(ConvToStr(channel->age)); params.push_back(ConvToStr(ServerInstance->Time())); params.push_back(ServerInstance->Config->ServerName); params.push_back(":" + topic); -- cgit v1.3.1-10-gc9f91