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;
42 unsigned int maxlen, maxtime;
45 HistoryList(unsigned int len, unsigned int time, const std::string& oparam)
46 : maxlen(len), maxtime(time), param(oparam) { }
49 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
52 unsigned int maxlines;
53 HistoryMode(Module* Creator)
54 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
58 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
60 std::string::size_type colon = parameter.find(':');
61 if (colon == std::string::npos)
63 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
64 return MODEACTION_DENY;
67 std::string duration(parameter, colon+1);
68 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
70 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
71 return MODEACTION_DENY;
74 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
76 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
78 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
79 return MODEACTION_DENY;
84 HistoryList* history = ext.get(channel);
87 // Shrink the list if the new line number limit is lower than the old one
88 if (len < history->lines.size())
89 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
91 history->maxlen = len;
92 history->maxtime = time;
93 history->param = parameter;
97 ext.set(channel, new HistoryList(len, time, parameter));
99 return MODEACTION_ALLOW;
102 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
104 out.append(history->param);
108 class ModuleChanHistory
110 , public ServerEventListener
114 UserModeReference botmode;
116 IRCv3::Batch::CapReference batchcap;
117 IRCv3::Batch::API batchmanager;
118 IRCv3::Batch::Batch batch;
119 IRCv3::ServerTime::API servertimemanager;
123 : ServerEventListener(this)
125 , botmode(this, "bot")
128 , batch("chathistory")
129 , servertimemanager(this)
133 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
135 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
136 m.maxlines = tag->getUInt("maxlines", 50, 1);
137 sendnotice = tag->getBool("notice", true);
138 dobots = tag->getBool("bots", true);
141 ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
143 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
146 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
148 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
150 Channel* c = target.Get<Channel>();
151 HistoryList* list = m.ext.get(c);
154 list->lines.push_back(HistoryItem(user, details.text));
155 if (list->lines.size() > list->maxlen)
156 list->lines.pop_front();
161 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
163 LocalUser* localuser = IS_LOCAL(memb->user);
167 if (memb->user->IsModeSet(botmode) && !dobots)
170 HistoryList* list = m.ext.get(memb->chan);
175 mintime = ServerInstance->Time() - list->maxtime;
177 if ((sendnotice) && (!batchcap.get(localuser)))
179 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
180 if (list->maxtime > 0)
181 message.append(" spanning up to " + ConvToStr(list->maxtime) + " seconds");
182 memb->WriteNotice(message);
187 batchmanager->Start(batch);
188 batch.GetBatchStartMessage().PushParamRef(memb->chan->name);
191 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
193 const HistoryItem& item = *i;
194 if (item.ts >= mintime)
196 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, memb->chan, item.text);
197 if (servertimemanager)
198 servertimemanager->Set(msg, item.ts);
199 batch.AddToBatch(msg);
200 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
205 batchmanager->End(batch);
208 Version GetVersion() CXX11_OVERRIDE
210 return Version("Provides channel mode +H, allows for the channel message history to be replayed on join", VF_VENDOR);
214 MODULE_INIT(ModuleChanHistory)