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 HistoryItem(const std::string& Line) : ts(ServerInstance->Time()), line(Line) {}
31 std::deque<HistoryItem> lines;
32 unsigned int maxlen, maxtime;
35 HistoryList(unsigned int len, unsigned int time, const std::string& oparam)
36 : maxlen(len), maxtime(time), param(oparam) { }
39 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
41 bool IsValidDuration(const std::string& duration)
43 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
46 if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
49 if (duration_multi[c] == 1)
56 unsigned int maxlines;
57 HistoryMode(Module* Creator)
58 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
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) || (!IsValidDuration(duration))))
74 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
75 return MODEACTION_DENY;
78 unsigned int len = ConvToInt(parameter.substr(0, colon));
79 unsigned int time = InspIRCd::Duration(duration);
80 if (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;
97 history->param = parameter;
101 ext.set(channel, new HistoryList(len, time, parameter));
103 return MODEACTION_ALLOW;
106 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
108 out.append(history->param);
112 class ModuleChanHistory : public Module
116 UserModeReference botmode;
119 ModuleChanHistory() : m(this), botmode(this, "bot")
123 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
125 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
126 m.maxlines = tag->getInt("maxlines", 50, 1);
127 sendnotice = tag->getBool("notice", true);
128 dobots = tag->getBool("bots", true);
131 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
133 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
135 Channel* c = target.Get<Channel>();
136 HistoryList* list = m.ext.get(c);
139 const std::string line = ":" + user->GetFullHost() + " PRIVMSG " + c->name + " :" + details.text;
140 list->lines.push_back(HistoryItem(line));
141 if (list->lines.size() > list->maxlen)
142 list->lines.pop_front();
147 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
149 if (IS_REMOTE(memb->user))
152 if (memb->user->IsModeSet(botmode) && !dobots)
155 HistoryList* list = m.ext.get(memb->chan);
160 mintime = ServerInstance->Time() - list->maxtime;
164 std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
165 if (list->maxtime > 0)
166 message.append(" spanning up to " + ConvToStr(list->maxtime) + " seconds");
167 memb->WriteNotice(message);
170 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
172 if (i->ts >= mintime)
173 memb->user->Write(i->line);
177 Version GetVersion() CXX11_OVERRIDE
179 return Version("Provides channel history replayed on join", VF_VENDOR);
183 MODULE_INIT(ModuleChanHistory)