aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/m_spanningtree/ftopic.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/m_spanningtree/ftopic.cpp')
-rw-r--r--src/modules/m_spanningtree/ftopic.cpp87
1 files changed, 74 insertions, 13 deletions
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<std::string>& 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 ((ts >= c->topicset) || (c->topic.empty()))
- {
- 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());
- }
+ if (params.size() < 5)
+ return CMD_FAILURE;
- // Always update setter and settime.
- c->setby.assign(params[2], 0, 127);
- c->topicset = ts;
+ // 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 (c->topic != params[3+indexoffset])
+ {
+ // 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<parameterlist&>(params);
+ p.insert(p.begin()+1, ConvToStr(c->age));
+ }
+
return CMD_SUCCESS;
}