1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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;
29 HistoryList(unsigned int Max) : max(Max) {}
32 class HistoryMode : public ModeHandler
35 SimpleExtItem<HistoryList> ext;
36 HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
37 ext("history", Creator) { }
39 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
41 int max = atoi(parameter.c_str());
42 if (adding && max == 0)
43 return MODEACTION_DENY;
46 ext.set(channel, new HistoryList(max));
47 channel->SetModeParam('H', parameter);
52 channel->SetModeParam('H', "");
54 return MODEACTION_ALLOW;
58 class ModuleChanHistory : public Module
64 ModuleChanHistory() : m(this)
66 if (!ServerInstance->Modes->AddMode(&m))
67 throw ModuleException("Could not add new modes!");
69 Implementation eventlist[] = { I_OnUserJoin, I_OnUserMessage };
70 ServerInstance->Modules->Attach(eventlist, this, 2);
73 void OnUserMessage(User* user,void* dest,int target_type, const std::string &text, char status, const CUList&)
75 if (target_type == TYPE_CHANNEL && status == 0)
77 Channel* c = (Channel*)dest;
78 HistoryList* list = m.ext.get(c);
82 snprintf(buf, MAXBUF, ":%s PRIVMSG %s :%s",
83 user->GetFullHost().c_str(), c->name.c_str(), text.c_str());
84 list->lines.push_back(HistoryItem(buf));
85 if (list->lines.size() > list->max)
86 list->lines.pop_front();
91 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& except_list)
93 HistoryList* list = m.ext.get(memb->chan);
96 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
97 memb->user->Write(i->line);
102 return Version("Provides channel history replayed on join", VF_COMMON | VF_VENDOR);
106 MODULE_INIT(ModuleChanHistory)