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"
29 std::string sourcemask;
31 HistoryItem(User* source, const std::string& Text)
32 : ts(ServerInstance->Time())
34 , sourcemask(source->GetFullHost())
41 std::deque<HistoryItem> lines;
45 HistoryList(unsigned int len, unsigned int time)
52 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
55 unsigned int maxlines;
56 HistoryMode(Module* Creator)
57 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
59 syntax = "<max-messages>:<max-duration>";
62 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
64 std::string::size_type colon = parameter.find(':');
65 if (colon == std::string::npos)
67 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
68 return MODEACTION_DENY;
71 std::string duration(parameter, colon+1);
72 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
74 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
75 return MODEACTION_DENY;
78 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
80 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
82 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
83 return MODEACTION_DENY;
88 HistoryList* history = ext.get(channel);
91 // Shrink the list if the new line number limit is lower than the old one
92 if (len < history->lines.size())
93 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
95 history->maxlen = len;
96 history->maxtime = time;
100 ext.set(channel, new HistoryList(len, time));
102 return MODEACTION_ALLOW;
105 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
107 out.append(ConvToStr(history->maxlen));
109 out.append(InspIRCd::DurationString(history->maxtime));
113 class ModuleChanHistory
115 , public ServerEventListener
119 UserModeReference botmode;
121 IRCv3::Batch::CapReference batchcap;
122 IRCv3::Batch::API batchmanager;
123 IRCv3::Batch::Batch batch;
124 IRCv3::ServerTime::API servertimemanager;
128 : ServerEventListener(this)
130 , botmode(this, "bot")
133 , batch("chathistory")
134 , servertimemanager(this)
138 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
140 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
141 m.maxlines = tag->getUInt("maxlines", 50, 1);
142 sendnotice = tag->getBool("notice", true);
143 dobots = tag->getBool("bots", true);
146 ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
148 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
151 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
153 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
155 Channel* c = target.Get<Channel>();
156 HistoryList* list = m.ext.get(c);
159 list->lines.push_back(HistoryItem(user, details.text));
160 if (list->lines.size() > list->maxlen)
161 list->lines.pop_front();
166 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
168 LocalUser* localuser = IS_LOCAL(memb->user);
172 if (memb->user->IsModeSet(botmode) && !dobots)
175 HistoryList* list = m.ext.get(memb->chan);
180 mintime = ServerInstance->Time() - list->maxtime;
182 if ((sendnotice) && (!batchcap.get(localuser)))
184 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
185 if (list->maxtime > 0)
186 message.append(" spanning up to " + InspIRCd::DurationString(list->maxtime));
187 memb->WriteNotice(message);
192 batchmanager->Start(batch);
193 batch.GetBatchStartMessage().PushParamRef(memb->chan->name);
196 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
198 const HistoryItem& item = *i;
199 if (item.ts >= mintime)
201 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, memb->chan, item.text);
202 if (servertimemanager)
203 servertimemanager->Set(msg, item.ts);
204 batch.AddToBatch(msg);
205 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
210 batchmanager->End(batch);
213 Version GetVersion() CXX11_OVERRIDE
215 return Version("Provides channel mode +H, allows for the channel message history to be replayed on join", VF_VENDOR);
219 MODULE_INIT(ModuleChanHistory)