]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
4bd230a7c649c1278717696dcb84feb4efc6d7df
[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 NoHistoryMode : public SimpleUserModeHandler
128 {
129 public:
130         NoHistoryMode(Module* Creator)
131                 : SimpleUserModeHandler(Creator, "nohistory", 'N')
132         {
133                 if (!ServerInstance->Config->ConfValue("chanhistory")->getBool("enableumode"))
134                         DisableAutoRegister();
135         }
136 };
137
138 class ModuleChanHistory
139         : public Module
140         , public ServerProtocol::BroadcastEventListener
141 {
142  private:
143         HistoryMode historymode;
144         NoHistoryMode nohistorymode;
145         bool prefixmsg;
146         UserModeReference botmode;
147         bool dobots;
148         IRCv3::Batch::CapReference batchcap;
149         IRCv3::Batch::API batchmanager;
150         IRCv3::Batch::Batch batch;
151         IRCv3::ServerTime::API servertimemanager;
152         ClientProtocol::MessageTagEvent tagevent;
153
154         void AddTag(ClientProtocol::Message& msg, const std::string& tagkey, std::string& tagval)
155         {
156                 const Events::ModuleEventProvider::SubscriberList& list = tagevent.GetSubscribers();
157                 for (Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
158                 {
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)
164                                 break;
165                 }
166         }
167
168         void SendHistory(LocalUser* user, Channel* channel, HistoryList* list, time_t mintime)
169         {
170                 if (batchmanager)
171                 {
172                         batchmanager->Start(batch);
173                         batch.GetBatchStartMessage().PushParamRef(channel->name);
174                 }
175
176                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
177                 {
178                         HistoryItem& item = *i;
179                         if (item.ts >= mintime)
180                         {
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);
188                         }
189                 }
190
191                 if (batchmanager)
192                         batchmanager->End(batch);
193         }
194
195  public:
196         ModuleChanHistory()
197                 : ServerProtocol::BroadcastEventListener(this)
198                 , historymode(this)
199                 , nohistorymode(this)
200                 , botmode(this, "bot")
201                 , batchcap(this)
202                 , batchmanager(this)
203                 , batch("chathistory")
204                 , servertimemanager(this)
205                 , tagevent(this)
206         {
207         }
208
209         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
210         {
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);
215         }
216
217         ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
218         {
219                 return channel->IsModeSet(historymode) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
220         }
221
222         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
223         {
224                 std::string ctcpname;
225                 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (!details.IsCTCP(ctcpname) || irc::equals(ctcpname, "ACTION")))
226                 {
227                         Channel* c = target.Get<Channel>();
228                         HistoryList* list = historymode.ext.get(c);
229                         if (list)
230                         {
231                                 list->lines.push_back(HistoryItem(user, details));
232                                 if (list->lines.size() > list->maxlen)
233                                         list->lines.pop_front();
234                         }
235                 }
236         }
237
238         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
239         {
240                 LocalUser* localuser = IS_LOCAL(memb->user);
241                 if (!localuser)
242                         return;
243
244                 if (memb->user->IsModeSet(botmode) && !dobots)
245                         return;
246
247                 if (memb->user->IsModeSet(nohistorymode))
248                         return;
249
250                 HistoryList* list = historymode.ext.get(memb->chan);
251                 if (!list)
252                         return;
253
254                 if ((prefixmsg) && (!batchcap.get(localuser)))
255                 {
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);
260                 }
261
262                 time_t mintime = 0;
263                 if (list->maxtime)
264                         mintime = ServerInstance->Time() - list->maxtime;
265
266                 SendHistory(localuser, memb->chan, list, mintime);
267         }
268
269         Version GetVersion() CXX11_OVERRIDE
270         {
271                 return Version("Adds channel mode H (history) which allows message history to be viewed on joining the channel.", VF_VENDOR);
272         }
273 };
274
275 MODULE_INIT(ModuleChanHistory)