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;
33 HistoryList(unsigned int len, unsigned int time) : maxlen(len), maxtime(time) {}
36 class HistoryMode : public ModeHandler
38 bool IsValidDuration(const std::string& duration)
40 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
43 if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
46 if (duration_multi[c] == 1)
53 SimpleExtItem<HistoryList> ext;
54 unsigned int maxlines;
55 HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
56 ext("history", Creator) { }
58 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
62 std::string::size_type colon = parameter.find(':');
63 if (colon == std::string::npos)
64 return MODEACTION_DENY;
66 std::string duration = parameter.substr(colon+1);
67 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
68 return MODEACTION_DENY;
70 unsigned int len = ConvToInt(parameter.substr(0, colon));
71 int time = InspIRCd::Duration(duration);
72 if (len == 0 || time < 0)
73 return MODEACTION_DENY;
74 if (len > maxlines && IS_LOCAL(source))
75 return MODEACTION_DENY;
78 if (parameter == channel->GetModeParameter(this))
79 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;
93 ext.set(channel, new HistoryList(len, time));
98 if (!channel->IsModeSet(this))
99 return MODEACTION_DENY;
102 return MODEACTION_ALLOW;
106 class ModuleChanHistory : public Module
110 UserModeReference botmode;
113 ModuleChanHistory() : m(this), botmode(this, "bot")
117 void init() CXX11_OVERRIDE
119 ServerInstance->Modules->AddService(m);
120 ServerInstance->Modules->AddService(m.ext);
123 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
125 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
126 m.maxlines = tag->getInt("maxlines", 50);
127 sendnotice = tag->getBool("notice", true);
128 dobots = tag->getBool("bots", true);
131 void OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList&, MessageType msgtype) CXX11_OVERRIDE
133 if ((target_type == TYPE_CHANNEL) && (status == 0) && (msgtype == MSG_PRIVMSG))
135 Channel* c = (Channel*)dest;
136 HistoryList* list = m.ext.get(c);
139 const std::string line = ":" + user->GetFullHost() + " PRIVMSG " + c->name + " :" + 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 memb->user->WriteNotice("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history spanning up to " + ConvToStr(list->maxtime) + " seconds");
167 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
169 if (i->ts >= mintime)
170 memb->user->Write(i->line);
174 Version GetVersion() CXX11_OVERRIDE
176 return Version("Provides channel history replayed on join", VF_VENDOR);
180 MODULE_INIT(ModuleChanHistory)