]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / src / modules / m_chanhistory.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
11  *
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.
15  *
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
19  * details.
20  *
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/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "modules/ircv3_servertime.h"
28 #include "modules/ircv3_batch.h"
29 #include "modules/server.h"
30
31 typedef insp::flat_map<std::string, std::string> HistoryTagMap;
32
33 struct HistoryItem
34 {
35         time_t ts;
36         std::string text;
37         MessageType type;
38         HistoryTagMap tags;
39         std::string sourcemask;
40
41         HistoryItem(User* source, const MessageDetails& details)
42                 : ts(ServerInstance->Time())
43                 , text(details.text)
44                 , type(details.type)
45                 , sourcemask(source->GetFullHost())
46         {
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;
50         }
51 };
52
53 struct HistoryList
54 {
55         std::deque<HistoryItem> lines;
56         unsigned int maxlen;
57         unsigned int maxtime;
58
59         HistoryList(unsigned int len, unsigned int time)
60                 : maxlen(len)
61                 , maxtime(time)
62         {
63         }
64
65         size_t Prune()
66         {
67                 // Prune expired entries from the list.
68                 if (maxtime)
69                 {
70                         time_t mintime = ServerInstance->Time() - maxtime;
71                         while (!lines.empty() && lines.front().ts < mintime)
72                                 lines.pop_front();
73                 }
74                 return lines.size();
75         }
76 };
77
78 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
79 {
80  public:
81         unsigned int maxlines;
82         HistoryMode(Module* Creator)
83                 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
84         {
85                 syntax = "<max-messages>:<max-duration>";
86         }
87
88         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
89         {
90                 std::string::size_type colon = parameter.find(':');
91                 if (colon == std::string::npos)
92                 {
93                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
94                         return MODEACTION_DENY;
95                 }
96
97                 std::string duration(parameter, colon+1);
98                 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
99                 {
100                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
101                         return MODEACTION_DENY;
102                 }
103
104                 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
105                 unsigned long time;
106                 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
107                 {
108                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
109                         return MODEACTION_DENY;
110                 }
111                 if (len > maxlines)
112                         len = maxlines;
113
114                 HistoryList* history = ext.get(channel);
115                 if (history)
116                 {
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));
120
121                         history->maxlen = len;
122                         history->maxtime = time;
123                         history->Prune();
124                 }
125                 else
126                 {
127                         ext.set(channel, new HistoryList(len, time));
128                 }
129                 return MODEACTION_ALLOW;
130         }
131
132         void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
133         {
134                 out.append(ConvToStr(history->maxlen));
135                 out.append(":");
136                 out.append(InspIRCd::DurationString(history->maxtime));
137         }
138 };
139
140 class NoHistoryMode : public SimpleUserModeHandler
141 {
142 public:
143         NoHistoryMode(Module* Creator)
144                 : SimpleUserModeHandler(Creator, "nohistory", 'N')
145         {
146                 if (!ServerInstance->Config->ConfValue("chanhistory")->getBool("enableumode"))
147                         DisableAutoRegister();
148         }
149 };
150
151 class ModuleChanHistory
152         : public Module
153         , public ServerProtocol::BroadcastEventListener
154 {
155  private:
156         HistoryMode historymode;
157         NoHistoryMode nohistorymode;
158         bool prefixmsg;
159         UserModeReference botmode;
160         bool dobots;
161         IRCv3::Batch::CapReference batchcap;
162         IRCv3::Batch::API batchmanager;
163         IRCv3::Batch::Batch batch;
164         IRCv3::ServerTime::API servertimemanager;
165         ClientProtocol::MessageTagEvent tagevent;
166
167         void AddTag(ClientProtocol::Message& msg, const std::string& tagkey, std::string& tagval)
168         {
169                 const Events::ModuleEventProvider::SubscriberList& list = tagevent.GetSubscribers();
170                 for (Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
171                 {
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)
177                                 break;
178                 }
179         }
180
181         void SendHistory(LocalUser* user, Channel* channel, HistoryList* list)
182         {
183                 if (batchmanager)
184                 {
185                         batchmanager->Start(batch);
186                         batch.GetBatchStartMessage().PushParamRef(channel->name);
187                 }
188
189                 for (std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
190                 {
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);
199                 }
200
201                 if (batchmanager)
202                         batchmanager->End(batch);
203         }
204
205  public:
206         ModuleChanHistory()
207                 : ServerProtocol::BroadcastEventListener(this)
208                 , historymode(this)
209                 , nohistorymode(this)
210                 , botmode(this, "bot")
211                 , batchcap(this)
212                 , batchmanager(this)
213                 , batch("chathistory")
214                 , servertimemanager(this)
215                 , tagevent(this)
216         {
217         }
218
219         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
220         {
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);
225         }
226
227         ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
228         {
229                 return channel->IsModeSet(historymode) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
230         }
231
232         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
233         {
234                 if (target.type != MessageTarget::TYPE_CHANNEL || target.status)
235                         return;
236
237                 std::string ctcpname;
238                 if (details.IsCTCP(ctcpname) && !irc::equals(ctcpname, "ACTION"))
239                         return;
240
241                 HistoryList* list = historymode.ext.get(target.Get<Channel>());
242                 if (!list)
243                         return;
244
245                 list->lines.push_back(HistoryItem(user, details));
246                 if (list->lines.size() > list->maxlen)
247                         list->lines.pop_front();
248         }
249
250         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
251         {
252                 LocalUser* localuser = IS_LOCAL(memb->user);
253                 if (!localuser)
254                         return;
255
256                 if (memb->user->IsModeSet(botmode) && !dobots)
257                         return;
258
259                 if (memb->user->IsModeSet(nohistorymode))
260                         return;
261
262                 HistoryList* list = historymode.ext.get(memb->chan);
263                 if (!list || !list->Prune())
264                         return;
265
266                 if ((prefixmsg) && (!batchcap.get(localuser)))
267                 {
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);
272                 }
273
274                 SendHistory(localuser, memb->chan, list);
275         }
276
277         Version GetVersion() CXX11_OVERRIDE
278         {
279                 return Version("Adds channel mode H (history) which allows message history to be viewed on joining the channel.", VF_VENDOR);
280         }
281 };
282
283 MODULE_INIT(ModuleChanHistory)