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> >
51 bool IsValidDuration(const std::string& duration)
53 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
56 if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
59 if (duration_multi[c] == 1)
66 unsigned int maxlines;
67 HistoryMode(Module* Creator)
68 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
72 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
74 std::string::size_type colon = parameter.find(':');
75 if (colon == std::string::npos)
77 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
78 return MODEACTION_DENY;
81 std::string duration(parameter, colon+1);
82 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
84 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
85 return MODEACTION_DENY;
88 unsigned int len = ConvToInt(parameter.substr(0, colon));
89 unsigned int time = InspIRCd::Duration(duration);
90 if (len == 0 || (len > maxlines && IS_LOCAL(source)))
92 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
93 return MODEACTION_DENY;
98 HistoryList* history = ext.get(channel);
101 // Shrink the list if the new line number limit is lower than the old one
102 if (len < history->lines.size())
103 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
105 history->maxlen = len;
106 history->maxtime = time;
107 history->param = parameter;
111 ext.set(channel, new HistoryList(len, time, parameter));
113 return MODEACTION_ALLOW;
116 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
118 out.append(history->param);
122 class ModuleChanHistory
124 , public ServerEventListener
128 UserModeReference botmode;
130 IRCv3::Batch::CapReference batchcap;
131 IRCv3::Batch::API batchmanager;
132 IRCv3::Batch::Batch batch;
133 IRCv3::ServerTime::API servertimemanager;
137 : ServerEventListener(this)
139 , botmode(this, "bot")
142 , batch("chathistory")
143 , servertimemanager(this)
147 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
149 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
150 m.maxlines = tag->getUInt("maxlines", 50, 1);
151 sendnotice = tag->getBool("notice", true);
152 dobots = tag->getBool("bots", true);
155 ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
157 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
160 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
162 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
164 Channel* c = target.Get<Channel>();
165 HistoryList* list = m.ext.get(c);
168 list->lines.push_back(HistoryItem(user, details.text));
169 if (list->lines.size() > list->maxlen)
170 list->lines.pop_front();
175 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
177 LocalUser* localuser = IS_LOCAL(memb->user);
181 if (memb->user->IsModeSet(botmode) && !dobots)
184 HistoryList* list = m.ext.get(memb->chan);
189 mintime = ServerInstance->Time() - list->maxtime;
191 if ((sendnotice) && (!batchcap.get(localuser)))
193 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
194 if (list->maxtime > 0)
195 message.append(" spanning up to " + ConvToStr(list->maxtime) + " seconds");
196 memb->WriteNotice(message);
201 batchmanager->Start(batch);
202 batch.GetBatchStartMessage().PushParamRef(memb->chan->name);
205 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
207 const HistoryItem& item = *i;
208 if (item.ts >= mintime)
210 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, memb->chan, item.text);
211 if (servertimemanager)
212 servertimemanager->Set(msg, item.ts);
213 batch.AddToBatch(msg);
214 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
219 batchmanager->End(batch);
222 Version GetVersion() CXX11_OVERRIDE
224 return Version("Provides channel history replayed on join", VF_VENDOR);
228 MODULE_INIT(ModuleChanHistory)