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
41 SimpleExtItem<HistoryList> ext;
43 HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
44 ext("history", Creator) { }
46 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
50 std::string::size_type colon = parameter.find(':');
51 if (colon == std::string::npos)
52 return MODEACTION_DENY;
53 int len = atoi(parameter.substr(0, colon).c_str());
54 int time = ServerInstance->Duration(parameter.substr(colon+1));
55 if (len <= 0 || time < 0)
56 return MODEACTION_DENY;
57 if (len > maxlines && IS_LOCAL(source))
58 return MODEACTION_DENY;
61 if (parameter == channel->GetModeParameter(this))
62 return MODEACTION_DENY;
63 ext.set(channel, new HistoryList(len, time));
64 channel->SetModeParam('H', parameter);
68 if (!channel->IsModeSet('H'))
69 return MODEACTION_DENY;
71 channel->SetModeParam('H', "");
73 return MODEACTION_ALLOW;
77 class ModuleChanHistory : public Module
81 ModuleChanHistory() : m(this)
87 ServerInstance->Modules->AddService(m);
89 Implementation eventlist[] = { I_OnPostJoin, I_OnUserMessage, I_OnRehash };
90 ServerInstance->Modules->Attach(eventlist, this, 3);
96 m.maxlines = ServerInstance->Config->ConfValue("chanhistory")->getInt("maxlines", 50);
101 ServerInstance->Modes->DelMode(&m);
104 void OnUserMessage(User* user,void* dest,int target_type, const std::string &text, char status, const CUList&)
106 if (target_type == TYPE_CHANNEL && status == 0)
108 Channel* c = (Channel*)dest;
109 HistoryList* list = m.ext.get(c);
113 snprintf(buf, MAXBUF, ":%s PRIVMSG %s :%s",
114 user->GetFullHost().c_str(), c->name.c_str(), text.c_str());
115 list->lines.push_back(HistoryItem(buf));
116 if (list->lines.size() > list->maxlen)
117 list->lines.pop_front();
122 void OnPostJoin(Membership* memb)
124 HistoryList* list = m.ext.get(memb->chan);
129 mintime = ServerInstance->Time() - list->maxtime;
130 memb->user->WriteServ("NOTICE %s :Replaying up to %d lines of pre-join history spanning up to %d seconds",
131 memb->chan->name.c_str(), list->maxlen, list->maxtime);
132 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
134 if (i->ts >= mintime)
135 memb->user->Write(i->line);
141 return Version("Provides channel history replayed on join", VF_VENDOR);
145 MODULE_INIT(ModuleChanHistory)