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 ServerProtocol::BroadcastEventListener
120 UserModeReference botmode;
122 IRCv3::Batch::CapReference batchcap;
123 IRCv3::Batch::API batchmanager;
124 IRCv3::Batch::Batch batch;
125 IRCv3::ServerTime::API servertimemanager;
127 void SendHistory(LocalUser* user, Channel* channel, HistoryList* list, time_t mintime)
131 batchmanager->Start(batch);
132 batch.GetBatchStartMessage().PushParamRef(channel->name);
135 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
137 const HistoryItem& item = *i;
138 if (item.ts >= mintime)
140 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, channel, item.text);
141 if (servertimemanager)
142 servertimemanager->Set(msg, item.ts);
143 batch.AddToBatch(msg);
144 user->Send(ServerInstance->GetRFCEvents().privmsg, msg);
149 batchmanager->End(batch);
154 : ServerProtocol::BroadcastEventListener(this)
156 , botmode(this, "bot")
159 , batch("chathistory")
160 , servertimemanager(this)
164 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
166 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
167 m.maxlines = tag->getUInt("maxlines", 50, 1);
168 sendnotice = tag->getBool("notice", true);
169 dobots = tag->getBool("bots", true);
172 ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
174 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
177 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
179 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
181 Channel* c = target.Get<Channel>();
182 HistoryList* list = m.ext.get(c);
185 list->lines.push_back(HistoryItem(user, details.text));
186 if (list->lines.size() > list->maxlen)
187 list->lines.pop_front();
192 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
194 LocalUser* localuser = IS_LOCAL(memb->user);
198 if (memb->user->IsModeSet(botmode) && !dobots)
201 HistoryList* list = m.ext.get(memb->chan);
205 if ((sendnotice) && (!batchcap.get(localuser)))
207 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
208 if (list->maxtime > 0)
209 message.append(" spanning up to " + InspIRCd::DurationString(list->maxtime));
210 memb->WriteNotice(message);
215 mintime = ServerInstance->Time() - list->maxtime;
217 SendHistory(localuser, memb->chan, list, mintime);
220 Version GetVersion() CXX11_OVERRIDE
222 return Version("Provides channel mode +H, allows for the channel message history to be replayed on join", VF_VENDOR);
226 MODULE_INIT(ModuleChanHistory)