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/>.
26 std::string sourcemask;
28 HistoryItem(User* source, const std::string& Text)
29 : ts(ServerInstance->Time())
31 , sourcemask(source->GetFullHost())
38 std::deque<HistoryItem> lines;
39 unsigned int maxlen, maxtime;
42 HistoryList(unsigned int len, unsigned int time, const std::string& oparam)
43 : maxlen(len), maxtime(time), param(oparam) { }
46 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
48 bool IsValidDuration(const std::string& duration)
50 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
53 if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
56 if (duration_multi[c] == 1)
63 unsigned int maxlines;
64 HistoryMode(Module* Creator)
65 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
69 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
71 std::string::size_type colon = parameter.find(':');
72 if (colon == std::string::npos)
74 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
75 return MODEACTION_DENY;
78 std::string duration(parameter, colon+1);
79 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
81 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
82 return MODEACTION_DENY;
85 unsigned int len = ConvToInt(parameter.substr(0, colon));
86 unsigned int time = InspIRCd::Duration(duration);
87 if (len == 0 || (len > maxlines && IS_LOCAL(source)))
89 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
90 return MODEACTION_DENY;
95 HistoryList* history = ext.get(channel);
98 // Shrink the list if the new line number limit is lower than the old one
99 if (len < history->lines.size())
100 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
102 history->maxlen = len;
103 history->maxtime = time;
104 history->param = parameter;
108 ext.set(channel, new HistoryList(len, time, parameter));
110 return MODEACTION_ALLOW;
113 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
115 out.append(history->param);
119 class ModuleChanHistory : public Module
123 UserModeReference botmode;
126 ModuleChanHistory() : m(this), botmode(this, "bot")
130 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
132 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
133 m.maxlines = tag->getUInt("maxlines", 50, 1);
134 sendnotice = tag->getBool("notice", true);
135 dobots = tag->getBool("bots", true);
138 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
140 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
142 Channel* c = target.Get<Channel>();
143 HistoryList* list = m.ext.get(c);
146 list->lines.push_back(HistoryItem(user, details.text));
147 if (list->lines.size() > list->maxlen)
148 list->lines.pop_front();
153 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
155 LocalUser* localuser = IS_LOCAL(memb->user);
159 if (memb->user->IsModeSet(botmode) && !dobots)
162 HistoryList* list = m.ext.get(memb->chan);
167 mintime = ServerInstance->Time() - list->maxtime;
171 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
172 if (list->maxtime > 0)
173 message.append(" spanning up to " + ConvToStr(list->maxtime) + " seconds");
174 memb->WriteNotice(message);
177 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
179 const HistoryItem& item = *i;
180 if (item.ts >= mintime)
182 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, memb->chan, item.text);
183 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
188 Version GetVersion() CXX11_OVERRIDE
190 return Version("Provides channel history replayed on join", VF_VENDOR);
194 MODULE_INIT(ModuleChanHistory)