]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Don't kill cloaking users when hash/md5 is missing.
[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-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>
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
66 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
67 {
68  public:
69         unsigned int maxlines;
70         HistoryMode(Module* Creator)
71                 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
72         {
73                 syntax = "<max-messages>:<max-duration>";
74         }
75
76         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
77         {
78                 std::string::size_type colon = parameter.find(':');
79                 if (colon == std::string::npos)
80                 {
81                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
82                         return MODEACTION_DENY;
83                 }
84
85                 std::string duration(parameter, colon+1);
86                 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
87                 {
88                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
89                         return MODEACTION_DENY;
90                 }
91
92                 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
93                 unsigned long time;
94                 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
95                 {
96                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
97                         return MODEACTION_DENY;
98                 }
99                 if (len > maxlines)
100                         len = maxlines;
101
102                 HistoryList* history = ext.get(channel);
103                 if (history)
104                 {
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));
108
109                         history->maxlen = len;
110                         history->maxtime = time;
111                 }
112                 else
113                 {
114                         ext.set(channel, new HistoryList(len, time));
115                 }
116                 return MODEACTION_ALLOW;
117         }
118
119         void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
120         {
121                 out.append(ConvToStr(history->maxlen));
122                 out.append(":");
123                 out.append(InspIRCd::DurationString(history->maxtime));
124         }
125 };
126
127 class ModuleChanHistory
128         : public Module
129         , public ServerProtocol::BroadcastEventListener
130 {
131  private:
132         HistoryMode m;
133         bool prefixmsg;
134         UserModeReference botmode;
135         bool dobots;
136         IRCv3::Batch::CapReference batchcap;
137         IRCv3::Batch::API batchmanager;
138         IRCv3::Batch::Batch batch;
139         IRCv3::ServerTime::API servertimemanager;
140         ClientProtocol::MessageTagEvent tagevent;
141
142         void AddTag(ClientProtocol::Message& msg, const std::string& tagkey, std::string& tagval)
143         {
144                 const Events::ModuleEventProvider::SubscriberList& list = tagevent.GetSubscribers();
145                 for (Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
146                 {
147                         ClientProtocol::MessageTagProvider* const tagprov = static_cast<ClientProtocol::MessageTagProvider*>(*i);
148                         const ModResult res = tagprov->OnProcessTag(ServerInstance->FakeClient, tagkey, tagval);
149                         if (res == MOD_RES_ALLOW)
150                                 msg.AddTag(tagkey, tagprov, tagval);
151                         else if (res == MOD_RES_DENY)
152                                 break;
153                 }
154         }
155
156         void SendHistory(LocalUser* user, Channel* channel, HistoryList* list, time_t mintime)
157         {
158                 if (batchmanager)
159                 {
160                         batchmanager->Start(batch);
161                         batch.GetBatchStartMessage().PushParamRef(channel->name);
162                 }
163
164                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
165                 {
166                         HistoryItem& item = *i;
167                         if (item.ts >= mintime)
168                         {
169                                 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, channel, item.text, item.type);
170                                 for (HistoryTagMap::iterator iter = item.tags.begin(); iter != item.tags.end(); ++iter)
171                                         AddTag(msg, iter->first, iter->second);
172                                 if (servertimemanager)
173                                         servertimemanager->Set(msg, item.ts);
174                                 batch.AddToBatch(msg);
175                                 user->Send(ServerInstance->GetRFCEvents().privmsg, msg);
176                         }
177                 }
178
179                 if (batchmanager)
180                         batchmanager->End(batch);
181         }
182
183  public:
184         ModuleChanHistory()
185                 : ServerProtocol::BroadcastEventListener(this)
186                 , m(this)
187                 , botmode(this, "bot")
188                 , batchcap(this)
189                 , batchmanager(this)
190                 , batch("chathistory")
191                 , servertimemanager(this)
192                 , tagevent(this)
193         {
194         }
195
196         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
197         {
198                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
199                 m.maxlines = tag->getUInt("maxlines", 50, 1);
200                 prefixmsg = tag->getBool("prefixmsg", tag->getBool("notice", true));
201                 dobots = tag->getBool("bots", true);
202         }
203
204         ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
205         {
206                 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
207         }
208
209         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
210         {
211                 std::string ctcpname;
212                 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (!details.IsCTCP(ctcpname) || irc::equals(ctcpname, "ACTION")))
213                 {
214                         Channel* c = target.Get<Channel>();
215                         HistoryList* list = m.ext.get(c);
216                         if (list)
217                         {
218                                 list->lines.push_back(HistoryItem(user, details));
219                                 if (list->lines.size() > list->maxlen)
220                                         list->lines.pop_front();
221                         }
222                 }
223         }
224
225         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
226         {
227                 LocalUser* localuser = IS_LOCAL(memb->user);
228                 if (!localuser)
229                         return;
230
231                 if (memb->user->IsModeSet(botmode) && !dobots)
232                         return;
233
234                 HistoryList* list = m.ext.get(memb->chan);
235                 if (!list)
236                         return;
237
238                 if ((prefixmsg) && (!batchcap.get(localuser)))
239                 {
240                         std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
241                         if (list->maxtime > 0)
242                                 message.append(" from the last " + InspIRCd::DurationString(list->maxtime));
243                         memb->WriteNotice(message);
244                 }
245
246                 time_t mintime = 0;
247                 if (list->maxtime)
248                         mintime = ServerInstance->Time() - list->maxtime;
249
250                 SendHistory(localuser, memb->chan, list, mintime);
251         }
252
253         Version GetVersion() CXX11_OVERRIDE
254         {
255                 return Version("Adds channel mode H (history) which allows message history to be viewed on joining the channel.", VF_VENDOR);
256         }
257 };
258
259 MODULE_INIT(ModuleChanHistory)