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"
28 std::string sourcemask;
30 HistoryItem(User* source, const std::string& Text)
31 : ts(ServerInstance->Time())
33 , sourcemask(source->GetFullHost())
40 std::deque<HistoryItem> lines;
41 unsigned int maxlen, maxtime;
44 HistoryList(unsigned int len, unsigned int time, const std::string& oparam)
45 : maxlen(len), maxtime(time), param(oparam) { }
48 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
50 bool IsValidDuration(const std::string& duration)
52 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
55 if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
58 if (duration_multi[c] == 1)
65 unsigned int maxlines;
66 HistoryMode(Module* Creator)
67 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
71 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
73 std::string::size_type colon = parameter.find(':');
74 if (colon == std::string::npos)
76 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
77 return MODEACTION_DENY;
80 std::string duration(parameter, colon+1);
81 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
83 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
84 return MODEACTION_DENY;
87 unsigned int len = ConvToInt(parameter.substr(0, colon));
88 unsigned int time = InspIRCd::Duration(duration);
89 if (len == 0 || (len > maxlines && IS_LOCAL(source)))
91 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
92 return MODEACTION_DENY;
97 HistoryList* history = ext.get(channel);
100 // Shrink the list if the new line number limit is lower than the old one
101 if (len < history->lines.size())
102 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
104 history->maxlen = len;
105 history->maxtime = time;
106 history->param = parameter;
110 ext.set(channel, new HistoryList(len, time, parameter));
112 return MODEACTION_ALLOW;
115 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
117 out.append(history->param);
121 class ModuleChanHistory : public Module
125 UserModeReference botmode;
127 IRCv3::Batch::CapReference batchcap;
128 IRCv3::Batch::API batchmanager;
129 IRCv3::Batch::Batch batch;
130 IRCv3::ServerTime::API servertimemanager;
135 , botmode(this, "bot")
138 , batch("chathistory")
139 , servertimemanager(this)
143 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
145 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
146 m.maxlines = tag->getUInt("maxlines", 50, 1);
147 sendnotice = tag->getBool("notice", true);
148 dobots = tag->getBool("bots", true);
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 " + ConvToStr(list->maxtime) + " seconds");
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 history replayed on join", VF_VENDOR);
219 MODULE_INIT(ModuleChanHistory)