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"
27 std::string sourcemask;
29 HistoryItem(User* source, const std::string& Text)
30 : ts(ServerInstance->Time())
32 , sourcemask(source->GetFullHost())
39 std::deque<HistoryItem> lines;
40 unsigned int maxlen, maxtime;
43 HistoryList(unsigned int len, unsigned int time, const std::string& oparam)
44 : maxlen(len), maxtime(time), param(oparam) { }
47 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
49 bool IsValidDuration(const std::string& duration)
51 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
54 if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
57 if (duration_multi[c] == 1)
64 unsigned int maxlines;
65 HistoryMode(Module* Creator)
66 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
70 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
72 std::string::size_type colon = parameter.find(':');
73 if (colon == std::string::npos)
75 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
76 return MODEACTION_DENY;
79 std::string duration(parameter, colon+1);
80 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
82 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
83 return MODEACTION_DENY;
86 unsigned int len = ConvToInt(parameter.substr(0, colon));
87 unsigned int time = InspIRCd::Duration(duration);
88 if (len == 0 || (len > maxlines && IS_LOCAL(source)))
90 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
91 return MODEACTION_DENY;
96 HistoryList* history = ext.get(channel);
99 // Shrink the list if the new line number limit is lower than the old one
100 if (len < history->lines.size())
101 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
103 history->maxlen = len;
104 history->maxtime = time;
105 history->param = parameter;
109 ext.set(channel, new HistoryList(len, time, parameter));
111 return MODEACTION_ALLOW;
114 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
116 out.append(history->param);
120 class ModuleChanHistory : public Module
124 UserModeReference botmode;
126 IRCv3::ServerTime::API servertimemanager;
131 , botmode(this, "bot")
132 , servertimemanager(this)
136 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
138 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
139 m.maxlines = tag->getUInt("maxlines", 50, 1);
140 sendnotice = tag->getBool("notice", true);
141 dobots = tag->getBool("bots", true);
144 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
146 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
148 Channel* c = target.Get<Channel>();
149 HistoryList* list = m.ext.get(c);
152 list->lines.push_back(HistoryItem(user, details.text));
153 if (list->lines.size() > list->maxlen)
154 list->lines.pop_front();
159 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
161 LocalUser* localuser = IS_LOCAL(memb->user);
165 if (memb->user->IsModeSet(botmode) && !dobots)
168 HistoryList* list = m.ext.get(memb->chan);
173 mintime = ServerInstance->Time() - list->maxtime;
177 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
178 if (list->maxtime > 0)
179 message.append(" spanning up to " + ConvToStr(list->maxtime) + " seconds");
180 memb->WriteNotice(message);
183 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
185 const HistoryItem& item = *i;
186 if (item.ts >= mintime)
188 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, memb->chan, item.text);
189 if (servertimemanager)
190 servertimemanager->Set(msg, item.ts);
191 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
196 Version GetVersion() CXX11_OVERRIDE
198 return Version("Provides channel history replayed on join", VF_VENDOR);
202 MODULE_INIT(ModuleChanHistory)