1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012-2014, 2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
9  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 
25 #include "inspircd.h"
26 #include "commands.h"
27 
28 /** FTOPIC command */
Handle(User * user,Params & params)29 CmdResult CommandFTopic::Handle(User* user, Params& params)
30 {
31 	Channel* c = ServerInstance->FindChan(params[0]);
32 	if (!c)
33 		return CMD_FAILURE;
34 
35 	if (c->age < ServerCommand::ExtractTS(params[1]))
36 		// Our channel TS is older, nothing to do
37 		return CMD_FAILURE;
38 
39 	// Channel::topicset is initialized to 0 on channel creation, so their ts will always win if we never had a topic
40 	time_t ts = ServerCommand::ExtractTS(params[2]);
41 	if (ts < c->topicset)
42 		return CMD_FAILURE;
43 
44 	// The topic text is always the last parameter
45 	const std::string& newtopic = params.back();
46 
47 	// If there is a setter in the message use that, otherwise use the message source
48 	const std::string& setter = ((params.size() > 4) ? params[3] : (ServerInstance->Config->FullHostInTopic ? user->GetFullHost() : user->nick));
49 
50 	/*
51 	 * If the topics were updated at the exact same second, accept
52 	 * the remote only when it's "bigger" than ours as defined by
53 	 * string comparison, so non-empty topics always override
54 	 * empty topics if their timestamps are equal
55 	 *
56 	 * Similarly, if the topic texts are equal too, keep one topic
57 	 * setter and discard the other
58 	 */
59 	if (ts == c->topicset)
60 	{
61 		// Discard if their topic text is "smaller"
62 		if (c->topic > newtopic)
63 			return CMD_FAILURE;
64 
65 		// If the texts are equal in addition to the timestamps, decide which setter to keep
66 		if ((c->topic == newtopic) && (c->setby >= setter))
67 			return CMD_FAILURE;
68 	}
69 
70 	c->SetTopic(user, newtopic, ts, &setter);
71 	return CMD_SUCCESS;
72 }
73 
74 // Used when bursting and in reply to RESYNC, contains topic setter as the 4th parameter
Builder(Channel * chan)75 CommandFTopic::Builder::Builder(Channel* chan)
76 	: CmdBuilder("FTOPIC")
77 {
78 	push(chan->name);
79 	push_int(chan->age);
80 	push_int(chan->topicset);
81 	push(chan->setby);
82 	push_last(chan->topic);
83 }
84 
85 // Used when changing the topic, the setter is the message source
Builder(User * user,Channel * chan)86 CommandFTopic::Builder::Builder(User* user, Channel* chan)
87 	: CmdBuilder(user, "FTOPIC")
88 {
89 	push(chan->name);
90 	push_int(chan->age);
91 	push_int(chan->topicset);
92 	push_last(chan->topic);
93 }
94