2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
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.
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
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/>.
21 #include "modules/ircv3_servertime.h"
22 #include "modules/ircv3_batch.h"
23 #include "modules/server.h"
25 typedef insp::flat_map<std::string, std::string> HistoryTagMap;
33 std::string sourcemask;
35 HistoryItem(User* source, const MessageDetails& details)
36 : ts(ServerInstance->Time())
39 , sourcemask(source->GetFullHost())
41 tags.reserve(details.tags_out.size());
42 for (ClientProtocol::TagMap::const_iterator iter = details.tags_out.begin(); iter != details.tags_out.end(); ++iter)
43 tags[iter->first] = iter->second.value;
49 std::deque<HistoryItem> lines;
53 HistoryList(unsigned int len, unsigned int time)
60 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
63 unsigned int maxlines;
64 HistoryMode(Module* Creator)
65 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
67 syntax = "<max-messages>:<max-duration>";
70 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
72 std::string::size_type colon = parameter.find(':');
73 if (colon == std::string::npos)
75 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
76 return MODEACTION_DENY;
79 std::string duration(parameter, colon+1);
80 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
82 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
83 return MODEACTION_DENY;
86 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
88 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
90 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
91 return MODEACTION_DENY;
96 HistoryList* history = ext.get(channel);
99 // Shrink the list if the new line number limit is lower than the old one
100 if (len < history->lines.size())
101 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
103 history->maxlen = len;
104 history->maxtime = time;
108 ext.set(channel, new HistoryList(len, time));
110 return MODEACTION_ALLOW;
113 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
115 out.append(ConvToStr(history->maxlen));
117 out.append(InspIRCd::DurationString(history->maxtime));
121 class ModuleChanHistory
123 , public ServerProtocol::BroadcastEventListener
128 UserModeReference botmode;
130 IRCv3::Batch::CapReference batchcap;
131 IRCv3::Batch::API batchmanager;
132 IRCv3::Batch::Batch batch;
133 IRCv3::ServerTime::API servertimemanager;
134 ClientProtocol::MessageTagEvent tagevent;
136 void AddTag(ClientProtocol::Message& msg, const std::string& tagkey, std::string& tagval)
138 const Events::ModuleEventProvider::SubscriberList& list = tagevent.GetSubscribers();
139 for (Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
141 ClientProtocol::MessageTagProvider* const tagprov = static_cast<ClientProtocol::MessageTagProvider*>(*i);
142 const ModResult res = tagprov->OnProcessTag(ServerInstance->FakeClient, tagkey, tagval);
143 if (res == MOD_RES_ALLOW)
144 msg.AddTag(tagkey, tagprov, tagval);
145 else if (res == MOD_RES_DENY)
150 void SendHistory(LocalUser* user, Channel* channel, HistoryList* list, time_t mintime)
154 batchmanager->Start(batch);
155 batch.GetBatchStartMessage().PushParamRef(channel->name);
158 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
160 HistoryItem& item = *i;
161 if (item.ts >= mintime)
163 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, channel, item.text, item.type);
164 for (HistoryTagMap::iterator iter = item.tags.begin(); iter != item.tags.end(); ++iter)
165 AddTag(msg, iter->first, iter->second);
166 if (servertimemanager)
167 servertimemanager->Set(msg, item.ts);
168 batch.AddToBatch(msg);
169 user->Send(ServerInstance->GetRFCEvents().privmsg, msg);
174 batchmanager->End(batch);
179 : ServerProtocol::BroadcastEventListener(this)
181 , botmode(this, "bot")
184 , batch("chathistory")
185 , servertimemanager(this)
190 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
192 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
193 m.maxlines = tag->getUInt("maxlines", 50, 1);
194 prefixmsg = tag->getBool("prefixmsg", tag->getBool("notice", true));
195 dobots = tag->getBool("bots", true);
198 ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
200 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
203 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
205 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && !details.IsCTCP())
207 Channel* c = target.Get<Channel>();
208 HistoryList* list = m.ext.get(c);
211 list->lines.push_back(HistoryItem(user, details));
212 if (list->lines.size() > list->maxlen)
213 list->lines.pop_front();
218 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
220 LocalUser* localuser = IS_LOCAL(memb->user);
224 if (memb->user->IsModeSet(botmode) && !dobots)
227 HistoryList* list = m.ext.get(memb->chan);
231 if ((prefixmsg) && (!batchcap.get(localuser)))
233 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
234 if (list->maxtime > 0)
235 message.append(" from the last " + InspIRCd::DurationString(list->maxtime));
236 memb->WriteNotice(message);
241 mintime = ServerInstance->Time() - list->maxtime;
243 SendHistory(localuser, memb->chan, list, mintime);
246 Version GetVersion() CXX11_OVERRIDE
248 return Version("Provides channel mode +H, allows for the channel message history to be replayed on join", VF_VENDOR);
252 MODULE_INIT(ModuleChanHistory)