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)
64 std::string::size_type colon = parameter.find(':');
65 if (colon == std::string::npos)
66 return MODEACTION_DENY;
68 std::string duration = parameter.substr(colon+1);
69 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
70 return MODEACTION_DENY;
72 unsigned int len = ConvToInt(parameter.substr(0, colon));
73 int time = InspIRCd::Duration(duration);
74 if (len == 0 || time < 0)
75 return MODEACTION_DENY;
76 if (len > maxlines && IS_LOCAL(source))
77 return MODEACTION_DENY;
81 HistoryList* history = ext.get(channel);
84 // Shrink the list if the new line number limit is lower than the old one
85 if (len < history->lines.size())
86 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
88 history->maxlen = len;
89 history->maxtime = time;
90 history->param = parameter;
94 ext.set(channel, new HistoryList(len, time, parameter));
96 return MODEACTION_ALLOW;
99 void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
101 out.append(history->param);
105 class ModuleChanHistory : public Module
109 UserModeReference botmode;
112 ModuleChanHistory() : m(this), botmode(this, "bot")
116 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
118 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
119 m.maxlines = tag->getInt("maxlines", 50);
120 sendnotice = tag->getBool("notice", true);
121 dobots = tag->getBool("bots", true);
124 void OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList&, MessageType msgtype) CXX11_OVERRIDE
126 if ((target_type == TYPE_CHANNEL) && (status == 0) && (msgtype == MSG_PRIVMSG))
128 Channel* c = (Channel*)dest;
129 HistoryList* list = m.ext.get(c);
132 const std::string line = ":" + user->GetFullHost() + " PRIVMSG " + c->name + " :" + text;
133 list->lines.push_back(HistoryItem(line));
134 if (list->lines.size() > list->maxlen)
135 list->lines.pop_front();
140 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
142 if (IS_REMOTE(memb->user))
145 if (memb->user->IsModeSet(botmode) && !dobots)
148 HistoryList* list = m.ext.get(memb->chan);
153 mintime = ServerInstance->Time() - list->maxtime;
157 memb->user->WriteNotice("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history spanning up to " + ConvToStr(list->maxtime) + " seconds");
160 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
162 if (i->ts >= mintime)
163 memb->user->Write(i->line);
167 Version GetVersion() CXX11_OVERRIDE
169 return Version("Provides channel history replayed on join", VF_VENDOR);
173 MODULE_INIT(ModuleChanHistory)