2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2013, 2017-2021 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
7 * Copyright (C) 2012-2015, 2018 Attila Molnar <attilamolnar@hush.com>
8 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
10 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "modules/ircv3_servertime.h"
28 #include "modules/ircv3_batch.h"
29 #include "modules/server.h"
31 typedef insp::flat_map<std::string, std::string> HistoryTagMap;
39 std::string sourcemask;
41 HistoryItem(User* source, const MessageDetails& details)
42 : ts(ServerInstance->Time())
45 , sourcemask(source->GetFullHost())
47 tags.reserve(details.tags_out.size());
48 for (ClientProtocol::TagMap::const_iterator iter = details.tags_out.begin(); iter != details.tags_out.end(); ++iter)
49 tags[iter->first] = iter->second.value;
55 std::deque<HistoryItem> lines;
59 HistoryList(unsigned int len, unsigned int time)
67 // Prune expired entries from the list.
70 time_t mintime = ServerInstance->Time() - maxtime;
71 while (!lines.empty() && lines.front().ts < mintime)
78 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
81 unsigned int maxlines;
82 HistoryMode(Module* Creator)
83 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
85 syntax = "<max-messages>:<max-duration>";
88 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
90 std::string::size_type colon = parameter.find(':');
91 if (colon == std::string::npos)
93 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
94 return MODEACTION_DENY;
97 std::string duration(parameter, colon+1);
98 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
100 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
101 return MODEACTION_DENY;
104 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
106 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
108 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
109 return MODEACTION_DENY;
114 HistoryList* history = ext.get(channel);
117 // Shrink the list if the new line number limit is lower than the old one
118 if (len < history->lines.size())
119 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
121 history->maxlen = len;
122 history->maxtime = time;
127 ext.set(channel, new HistoryList(len, time));
129 return MODEACTION_ALLOW;
132 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
134 out.append(ConvToStr(history->maxlen));
136 out.append(InspIRCd::DurationString(history->maxtime));
140 class NoHistoryMode : public SimpleUserModeHandler
143 NoHistoryMode(Module* Creator)
144 : SimpleUserModeHandler(Creator, "nohistory", 'N')
146 if (!ServerInstance->Config->ConfValue("chanhistory")->getBool("enableumode"))
147 DisableAutoRegister();
151 class ModuleChanHistory
153 , public ServerProtocol::BroadcastEventListener
156 HistoryMode historymode;
157 NoHistoryMode nohistorymode;
159 UserModeReference botmode;
161 IRCv3::Batch::CapReference batchcap;
162 IRCv3::Batch::API batchmanager;
163 IRCv3::Batch::Batch batch;
164 IRCv3::ServerTime::API servertimemanager;
165 ClientProtocol::MessageTagEvent tagevent;
167 void AddTag(ClientProtocol::Message& msg, const std::string& tagkey, std::string& tagval)
169 const Events::ModuleEventProvider::SubscriberList& list = tagevent.GetSubscribers();
170 for (Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
172 ClientProtocol::MessageTagProvider* const tagprov = static_cast<ClientProtocol::MessageTagProvider*>(*i);
173 const ModResult res = tagprov->OnProcessTag(ServerInstance->FakeClient, tagkey, tagval);
174 if (res == MOD_RES_ALLOW)
175 msg.AddTag(tagkey, tagprov, tagval);
176 else if (res == MOD_RES_DENY)
181 void SendHistory(LocalUser* user, Channel* channel, HistoryList* list)
185 batchmanager->Start(batch);
186 batch.GetBatchStartMessage().PushParamRef(channel->name);
189 for (std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
191 HistoryItem& item = *i;
192 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, channel, item.text, item.type);
193 for (HistoryTagMap::iterator iter = item.tags.begin(); iter != item.tags.end(); ++iter)
194 AddTag(msg, iter->first, iter->second);
195 if (servertimemanager)
196 servertimemanager->Set(msg, item.ts);
197 batch.AddToBatch(msg);
198 user->Send(ServerInstance->GetRFCEvents().privmsg, msg);
202 batchmanager->End(batch);
207 : ServerProtocol::BroadcastEventListener(this)
209 , nohistorymode(this)
210 , botmode(this, "bot")
213 , batch("chathistory")
214 , servertimemanager(this)
219 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
221 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
222 historymode.maxlines = tag->getUInt("maxlines", 50, 1);
223 prefixmsg = tag->getBool("prefixmsg", tag->getBool("notice", true));
224 dobots = tag->getBool("bots", true);
227 ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
229 return channel->IsModeSet(historymode) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
232 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
234 if (target.type != MessageTarget::TYPE_CHANNEL || target.status)
237 std::string ctcpname;
238 if (details.IsCTCP(ctcpname) && !irc::equals(ctcpname, "ACTION"))
241 HistoryList* list = historymode.ext.get(target.Get<Channel>());
245 list->lines.push_back(HistoryItem(user, details));
246 if (list->lines.size() > list->maxlen)
247 list->lines.pop_front();
250 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
252 LocalUser* localuser = IS_LOCAL(memb->user);
256 if (memb->user->IsModeSet(botmode) && !dobots)
259 if (memb->user->IsModeSet(nohistorymode))
262 HistoryList* list = historymode.ext.get(memb->chan);
263 if (!list || !list->Prune())
266 if ((prefixmsg) && (!batchcap.get(localuser)))
268 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
269 if (list->maxtime > 0)
270 message.append(" from the last " + InspIRCd::DurationString(list->maxtime));
271 memb->WriteNotice(message);
274 SendHistory(localuser, memb->chan, list);
277 Version GetVersion() CXX11_OVERRIDE
279 return Version("Adds channel mode H (history) which allows message history to be viewed on joining the channel.", VF_VENDOR);
283 MODULE_INIT(ModuleChanHistory)