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/>.
22 /* $ModDesc: Provides channel history for a given number of lines */
28 HistoryItem(const std::string& Line) : ts(ServerInstance->Time()), line(Line) {}
33 std::deque<HistoryItem> lines;
34 unsigned int maxlen, maxtime;
35 HistoryList(unsigned int len, unsigned int time) : maxlen(len), maxtime(time) {}
38 class HistoryMode : public ModeHandler
40 bool IsValidDuration(const std::string& duration)
42 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
45 if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
48 if (duration_multi[c] == 1)
55 SimpleExtItem<HistoryList> ext;
56 unsigned int maxlines;
57 HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
58 ext("history", Creator) { }
60 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
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;
80 if (parameter == channel->GetModeParameter(this))
81 return MODEACTION_DENY;
83 HistoryList* history = ext.get(channel);
86 // Shrink the list if the new line number limit is lower than the old one
87 if (len < history->lines.size())
88 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
90 history->maxlen = len;
91 history->maxtime = time;
95 ext.set(channel, new HistoryList(len, time));
97 channel->SetModeParam('H', parameter);
101 if (!channel->IsModeSet('H'))
102 return MODEACTION_DENY;
104 channel->SetModeParam('H', "");
106 return MODEACTION_ALLOW;
110 class ModuleChanHistory : public Module
115 ModuleChanHistory() : m(this)
121 ServerInstance->Modules->AddService(m);
122 ServerInstance->Modules->AddService(m.ext);
124 Implementation eventlist[] = { I_OnPostJoin, I_OnUserMessage, I_OnRehash };
125 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
131 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
132 m.maxlines = tag->getInt("maxlines", 50);
133 sendnotice = tag->getBool("notice", true);
136 void OnUserMessage(User* user,void* dest,int target_type, const std::string &text, char status, const CUList&)
138 if (target_type == TYPE_CHANNEL && status == 0)
140 Channel* c = (Channel*)dest;
141 HistoryList* list = m.ext.get(c);
145 snprintf(buf, MAXBUF, ":%s PRIVMSG %s :%s",
146 user->GetFullHost().c_str(), c->name.c_str(), text.c_str());
147 list->lines.push_back(HistoryItem(buf));
148 if (list->lines.size() > list->maxlen)
149 list->lines.pop_front();
154 void OnPostJoin(Membership* memb)
156 if (IS_REMOTE(memb->user))
159 HistoryList* list = m.ext.get(memb->chan);
164 mintime = ServerInstance->Time() - list->maxtime;
168 memb->user->WriteServ("NOTICE %s :Replaying up to %d lines of pre-join history spanning up to %d seconds",
169 memb->chan->name.c_str(), list->maxlen, list->maxtime);
172 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
174 if (i->ts >= mintime)
175 memb->user->Write(i->line);
181 return Version("Provides channel history replayed on join", VF_VENDOR);
185 MODULE_INIT(ModuleChanHistory)