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;
32 std::string sourcemask;
34 HistoryItem(User* source, const std::string& Text, const ClientProtocol::TagMap& Tags)
35 : ts(ServerInstance->Time())
37 , sourcemask(source->GetFullHost())
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;
47 std::deque<HistoryItem> lines;
51 HistoryList(unsigned int len, unsigned int time)
58 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
61 unsigned int maxlines;
62 HistoryMode(Module* Creator)
63 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
65 syntax = "<max-messages>:<max-duration>";
68 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
70 std::string::size_type colon = parameter.find(':');
71 if (colon == std::string::npos)
73 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
74 return MODEACTION_DENY;
77 std::string duration(parameter, colon+1);
78 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
80 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
81 return MODEACTION_DENY;
84 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
86 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
88 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
89 return MODEACTION_DENY;
94 HistoryList* history = ext.get(channel);
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));
101 history->maxlen = len;
102 history->maxtime = time;
106 ext.set(channel, new HistoryList(len, time));
108 return MODEACTION_ALLOW;
111 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
113 out.append(ConvToStr(history->maxlen));
115 out.append(InspIRCd::DurationString(history->maxtime));
119 class ModuleChanHistory
121 , public ServerProtocol::BroadcastEventListener
126 UserModeReference botmode;
128 IRCv3::Batch::CapReference batchcap;
129 IRCv3::Batch::API batchmanager;
130 IRCv3::Batch::Batch batch;
131 IRCv3::ServerTime::API servertimemanager;
132 ClientProtocol::MessageTagEvent tagevent;
134 void AddTag(ClientProtocol::Message& msg, const std::string& tagkey, std::string& tagval)
136 const Events::ModuleEventProvider::SubscriberList& list = tagevent.GetSubscribers();
137 for (Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
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)
148 void SendHistory(LocalUser* user, Channel* channel, HistoryList* list, time_t mintime)
152 batchmanager->Start(batch);
153 batch.GetBatchStartMessage().PushParamRef(channel->name);
156 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
158 HistoryItem& item = *i;
159 if (item.ts >= mintime)
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);
172 batchmanager->End(batch);
177 : ServerProtocol::BroadcastEventListener(this)
179 , botmode(this, "bot")
182 , batch("chathistory")
183 , servertimemanager(this)
188 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
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);
196 ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
198 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
201 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
203 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
205 Channel* c = target.Get<Channel>();
206 HistoryList* list = m.ext.get(c);
209 list->lines.push_back(HistoryItem(user, details.text, details.tags_out));
210 if (list->lines.size() > list->maxlen)
211 list->lines.pop_front();
216 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
218 LocalUser* localuser = IS_LOCAL(memb->user);
222 if (memb->user->IsModeSet(botmode) && !dobots)
225 HistoryList* list = m.ext.get(memb->chan);
229 if ((sendnotice) && (!batchcap.get(localuser)))
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);
239 mintime = ServerInstance->Time() - list->maxtime;
241 SendHistory(localuser, memb->chan, list, mintime);
244 Version GetVersion() CXX11_OVERRIDE
246 return Version("Provides channel mode +H, allows for the channel message history to be replayed on join", VF_VENDOR);
250 MODULE_INIT(ModuleChanHistory)