]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Fix the chanhistory module not replaying message tags.
[user/henk/code/inspircd.git] / src / modules / m_chanhistory.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/ircv3_servertime.h"
22 #include "modules/ircv3_batch.h"
23 #include "modules/server.h"
24
25 typedef insp::flat_map<std::string, std::string> HistoryTagMap;
26
27 struct HistoryItem
28 {
29         time_t ts;
30         std::string text;
31         HistoryTagMap tags;
32         std::string sourcemask;
33
34         HistoryItem(User* source, const std::string& Text, const ClientProtocol::TagMap& Tags)
35                 : ts(ServerInstance->Time())
36                 , text(Text)
37                 , sourcemask(source->GetFullHost())
38         {
39                 tags.reserve(Tags.size());
40                 for (ClientProtocol::TagMap::const_iterator iter = Tags.begin(); iter != Tags.end(); ++iter)
41                         tags[iter->first] = iter->second.value;
42         }
43 };
44
45 struct HistoryList
46 {
47         std::deque<HistoryItem> lines;
48         unsigned int maxlen;
49         unsigned int maxtime;
50
51         HistoryList(unsigned int len, unsigned int time)
52                 : maxlen(len)
53                 , maxtime(time)
54         {
55         }
56 };
57
58 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
59 {
60  public:
61         unsigned int maxlines;
62         HistoryMode(Module* Creator)
63                 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
64         {
65                 syntax = "<max-messages>:<max-duration>";
66         }
67
68         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
69         {
70                 std::string::size_type colon = parameter.find(':');
71                 if (colon == std::string::npos)
72                 {
73                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
74                         return MODEACTION_DENY;
75                 }
76
77                 std::string duration(parameter, colon+1);
78                 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
79                 {
80                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
81                         return MODEACTION_DENY;
82                 }
83
84                 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
85                 unsigned long time;
86                 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
87                 {
88                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
89                         return MODEACTION_DENY;
90                 }
91                 if (len > maxlines)
92                         len = maxlines;
93
94                 HistoryList* history = ext.get(channel);
95                 if (history)
96                 {
97                         // Shrink the list if the new line number limit is lower than the old one
98                         if (len < history->lines.size())
99                                 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
100
101                         history->maxlen = len;
102                         history->maxtime = time;
103                 }
104                 else
105                 {
106                         ext.set(channel, new HistoryList(len, time));
107                 }
108                 return MODEACTION_ALLOW;
109         }
110
111         void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
112         {
113                 out.append(ConvToStr(history->maxlen));
114                 out.append(":");
115                 out.append(InspIRCd::DurationString(history->maxtime));
116         }
117 };
118
119 class ModuleChanHistory
120         : public Module
121         , public ServerProtocol::BroadcastEventListener
122 {
123  private:
124         HistoryMode m;
125         bool sendnotice;
126         UserModeReference botmode;
127         bool dobots;
128         IRCv3::Batch::CapReference batchcap;
129         IRCv3::Batch::API batchmanager;
130         IRCv3::Batch::Batch batch;
131         IRCv3::ServerTime::API servertimemanager;
132         ClientProtocol::MessageTagEvent tagevent;
133
134         void AddTag(ClientProtocol::Message& msg, const std::string& tagkey, std::string& tagval)
135         {
136                 const Events::ModuleEventProvider::SubscriberList& list = tagevent.GetSubscribers();
137                 for (Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
138                 {
139                         ClientProtocol::MessageTagProvider* const tagprov = static_cast<ClientProtocol::MessageTagProvider*>(*i);
140                         const ModResult res = tagprov->OnProcessTag(ServerInstance->FakeClient, tagkey, tagval);
141                         if (res == MOD_RES_ALLOW)
142                                 msg.AddTag(tagkey, tagprov, tagval);
143                         else if (res == MOD_RES_DENY)
144                                 break;
145                 }
146         }
147
148         void SendHistory(LocalUser* user, Channel* channel, HistoryList* list, time_t mintime)
149         {
150                 if (batchmanager)
151                 {
152                         batchmanager->Start(batch);
153                         batch.GetBatchStartMessage().PushParamRef(channel->name);
154                 }
155
156                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
157                 {
158                         HistoryItem& item = *i;
159                         if (item.ts >= mintime)
160                         {
161                                 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, channel, item.text);
162                                 for (HistoryTagMap::iterator iter = item.tags.begin(); iter != item.tags.end(); ++iter)
163                                         AddTag(msg, iter->first, iter->second);
164                                 if (servertimemanager)
165                                         servertimemanager->Set(msg, item.ts);
166                                 batch.AddToBatch(msg);
167                                 user->Send(ServerInstance->GetRFCEvents().privmsg, msg);
168                         }
169                 }
170
171                 if (batchmanager)
172                         batchmanager->End(batch);
173         }
174
175  public:
176         ModuleChanHistory()
177                 : ServerProtocol::BroadcastEventListener(this)
178                 , m(this)
179                 , botmode(this, "bot")
180                 , batchcap(this)
181                 , batchmanager(this)
182                 , batch("chathistory")
183                 , servertimemanager(this)
184                 , tagevent(this)
185         {
186         }
187
188         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
189         {
190                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
191                 m.maxlines = tag->getUInt("maxlines", 50, 1);
192                 sendnotice = tag->getBool("notice", true);
193                 dobots = tag->getBool("bots", true);
194         }
195
196         ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
197         {
198                 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
199         }
200
201         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
202         {
203                 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
204                 {
205                         Channel* c = target.Get<Channel>();
206                         HistoryList* list = m.ext.get(c);
207                         if (list)
208                         {
209                                 list->lines.push_back(HistoryItem(user, details.text, details.tags_out));
210                                 if (list->lines.size() > list->maxlen)
211                                         list->lines.pop_front();
212                         }
213                 }
214         }
215
216         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
217         {
218                 LocalUser* localuser = IS_LOCAL(memb->user);
219                 if (!localuser)
220                         return;
221
222                 if (memb->user->IsModeSet(botmode) && !dobots)
223                         return;
224
225                 HistoryList* list = m.ext.get(memb->chan);
226                 if (!list)
227                         return;
228
229                 if ((sendnotice) && (!batchcap.get(localuser)))
230                 {
231                         std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
232                         if (list->maxtime > 0)
233                                 message.append(" spanning up to " + InspIRCd::DurationString(list->maxtime));
234                         memb->WriteNotice(message);
235                 }
236
237                 time_t mintime = 0;
238                 if (list->maxtime)
239                         mintime = ServerInstance->Time() - list->maxtime;
240
241                 SendHistory(localuser, memb->chan, list, mintime);
242         }
243
244         Version GetVersion() CXX11_OVERRIDE
245         {
246                 return Version("Provides channel mode +H, allows for the channel message history to be replayed on join", VF_VENDOR);
247         }
248 };
249
250 MODULE_INIT(ModuleChanHistory)