2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2013, 2017-2020 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)
66 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
69 unsigned int maxlines;
70 HistoryMode(Module* Creator)
71 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
73 syntax = "<max-messages>:<max-duration>";
76 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
78 std::string::size_type colon = parameter.find(':');
79 if (colon == std::string::npos)
81 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
82 return MODEACTION_DENY;
85 std::string duration(parameter, colon+1);
86 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
88 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
89 return MODEACTION_DENY;
92 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
94 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
96 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
97 return MODEACTION_DENY;
102 HistoryList* history = ext.get(channel);
105 // Shrink the list if the new line number limit is lower than the old one
106 if (len < history->lines.size())
107 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
109 history->maxlen = len;
110 history->maxtime = time;
114 ext.set(channel, new HistoryList(len, time));
116 return MODEACTION_ALLOW;
119 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
121 out.append(ConvToStr(history->maxlen));
123 out.append(InspIRCd::DurationString(history->maxtime));
127 class NoHistoryMode : public SimpleUserModeHandler
130 NoHistoryMode(Module* Creator)
131 : SimpleUserModeHandler(Creator, "nohistory", 'N')
133 if (!ServerInstance->Config->ConfValue("chanhistory")->getBool("enableumode"))
134 DisableAutoRegister();
138 class ModuleChanHistory
140 , public ServerProtocol::BroadcastEventListener
143 HistoryMode historymode;
144 NoHistoryMode nohistorymode;
146 UserModeReference botmode;
148 IRCv3::Batch::CapReference batchcap;
149 IRCv3::Batch::API batchmanager;
150 IRCv3::Batch::Batch batch;
151 IRCv3::ServerTime::API servertimemanager;
152 ClientProtocol::MessageTagEvent tagevent;
154 void AddTag(ClientProtocol::Message& msg, const std::string& tagkey, std::string& tagval)
156 const Events::ModuleEventProvider::SubscriberList& list = tagevent.GetSubscribers();
157 for (Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
159 ClientProtocol::MessageTagProvider* const tagprov = static_cast<ClientProtocol::MessageTagProvider*>(*i);
160 const ModResult res = tagprov->OnProcessTag(ServerInstance->FakeClient, tagkey, tagval);
161 if (res == MOD_RES_ALLOW)
162 msg.AddTag(tagkey, tagprov, tagval);
163 else if (res == MOD_RES_DENY)
168 void SendHistory(LocalUser* user, Channel* channel, HistoryList* list, time_t mintime)
172 batchmanager->Start(batch);
173 batch.GetBatchStartMessage().PushParamRef(channel->name);
176 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
178 HistoryItem& item = *i;
179 if (item.ts >= mintime)
181 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, channel, item.text, item.type);
182 for (HistoryTagMap::iterator iter = item.tags.begin(); iter != item.tags.end(); ++iter)
183 AddTag(msg, iter->first, iter->second);
184 if (servertimemanager)
185 servertimemanager->Set(msg, item.ts);
186 batch.AddToBatch(msg);
187 user->Send(ServerInstance->GetRFCEvents().privmsg, msg);
192 batchmanager->End(batch);
197 : ServerProtocol::BroadcastEventListener(this)
199 , nohistorymode(this)
200 , botmode(this, "bot")
203 , batch("chathistory")
204 , servertimemanager(this)
209 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
211 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
212 historymode.maxlines = tag->getUInt("maxlines", 50, 1);
213 prefixmsg = tag->getBool("prefixmsg", tag->getBool("notice", true));
214 dobots = tag->getBool("bots", true);
217 ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
219 return channel->IsModeSet(historymode) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
222 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
224 std::string ctcpname;
225 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (!details.IsCTCP(ctcpname) || irc::equals(ctcpname, "ACTION")))
227 Channel* c = target.Get<Channel>();
228 HistoryList* list = historymode.ext.get(c);
231 list->lines.push_back(HistoryItem(user, details));
232 if (list->lines.size() > list->maxlen)
233 list->lines.pop_front();
238 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
240 LocalUser* localuser = IS_LOCAL(memb->user);
244 if (memb->user->IsModeSet(botmode) && !dobots)
247 if (memb->user->IsModeSet(nohistorymode))
250 HistoryList* list = historymode.ext.get(memb->chan);
254 if ((prefixmsg) && (!batchcap.get(localuser)))
256 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
257 if (list->maxtime > 0)
258 message.append(" from the last " + InspIRCd::DurationString(list->maxtime));
259 memb->WriteNotice(message);
264 mintime = ServerInstance->Time() - list->maxtime;
266 SendHistory(localuser, memb->chan, list, mintime);
269 Version GetVersion() CXX11_OVERRIDE
271 return Version("Adds channel mode H (history) which allows message history to be viewed on joining the channel.", VF_VENDOR);
275 MODULE_INIT(ModuleChanHistory)