1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides channel history for a given number of lines */
22 HistoryItem(const std::string& Line) : ts(ServerInstance->Time()), line(Line) {}
27 std::deque<HistoryItem> lines;
28 unsigned int maxlen, maxtime;
29 HistoryList(unsigned int len, unsigned int time) : maxlen(len), maxtime(time) {}
32 class HistoryMode : public ModeHandler
35 SimpleExtItem<HistoryList> ext;
37 HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
38 ext("history", Creator) { }
40 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
44 std::string::size_type colon = parameter.find(':');
45 if (colon == std::string::npos)
46 return MODEACTION_DENY;
47 int len = atoi(parameter.substr(0, colon).c_str());
48 int time = ServerInstance->Duration(parameter.substr(colon+1));
49 if (len <= 0 || time < 0)
50 return MODEACTION_DENY;
51 if (len > maxlines && IS_LOCAL(source))
52 return MODEACTION_DENY;
55 if (parameter == channel->GetModeParameter(this))
56 return MODEACTION_DENY;
57 ext.set(channel, new HistoryList(len, time));
58 channel->SetModeParam('H', parameter);
63 channel->SetModeParam('H', "");
65 return MODEACTION_ALLOW;
69 class ModuleChanHistory : public Module
73 ModuleChanHistory() : m(this)
79 ServerInstance->Modules->AddService(m);
81 Implementation eventlist[] = { I_OnPostJoin, I_OnUserMessage, I_OnRehash };
82 ServerInstance->Modules->Attach(eventlist, this, 3);
88 m.maxlines = ServerInstance->Config->ConfValue("chanhistory")->getInt("maxlines", 50);
93 ServerInstance->Modes->DelMode(&m);
96 void OnUserMessage(User* user,void* dest,int target_type, const std::string &text, char status, const CUList&)
98 if (target_type == TYPE_CHANNEL && status == 0)
100 Channel* c = (Channel*)dest;
101 HistoryList* list = m.ext.get(c);
105 snprintf(buf, MAXBUF, ":%s PRIVMSG %s :%s",
106 user->GetFullHost().c_str(), c->name.c_str(), text.c_str());
107 list->lines.push_back(HistoryItem(buf));
108 if (list->lines.size() > list->maxlen)
109 list->lines.pop_front();
114 void OnPostJoin(Membership* memb)
116 HistoryList* list = m.ext.get(memb->chan);
121 mintime = ServerInstance->Time() - list->maxtime;
122 memb->user->WriteServ("NOTICE %s :Replaying up to %d lines of pre-join history spanning up to %d seconds",
123 memb->chan->name.c_str(), list->maxlen, list->maxtime);
124 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
126 if (i->ts >= mintime)
127 memb->user->Write(i->line);
133 return Version("Provides channel history replayed on join", VF_VENDOR);
137 MODULE_INIT(ModuleChanHistory)