]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
f440040b7a37e14de909f06af0f96f2e60ccab41
[user/henk/code/inspircd.git] / src / modules / m_chanhistory.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides channel history for a given number of lines */
17
18 struct HistoryItem
19 {
20         time_t ts;
21         std::string line;
22         HistoryItem(const std::string& Line) : ts(ServerInstance->Time()), line(Line) {}
23 };
24
25 struct HistoryList
26 {
27         std::deque<HistoryItem> lines;
28         unsigned int max;
29         HistoryList(unsigned int Max) : max(Max) {}
30 };
31
32 class HistoryMode : public ModeHandler
33 {
34  public:
35         SimpleExtItem<HistoryList> ext;
36         HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
37                 ext("history", Creator) { }
38
39         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
40         {
41                 int max = atoi(parameter.c_str());
42                 if (adding && max == 0)
43                         return MODEACTION_DENY;
44                 if (adding)
45                 {
46                         ext.set(channel, new HistoryList(max));
47                         channel->SetModeParam('H', parameter);
48                 }
49                 else
50                 {
51                         ext.unset(channel);
52                         channel->SetModeParam('H', "");
53                 }
54                 return MODEACTION_ALLOW;
55         }
56 };
57
58 class ModuleChanHistory : public Module
59 {
60         HistoryMode m;
61
62  public:
63
64         ModuleChanHistory() : m(this)
65         {
66                 if (!ServerInstance->Modes->AddMode(&m))
67                         throw ModuleException("Could not add new modes!");
68
69                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserMessage };
70                 ServerInstance->Modules->Attach(eventlist, this, 2);
71         }
72
73         void OnUserMessage(User* user,void* dest,int target_type, const std::string &text, char status, const CUList&)
74         {
75                 if (target_type == TYPE_CHANNEL && status == 0)
76                 {
77                         Channel* c = (Channel*)dest;
78                         HistoryList* list = m.ext.get(c);
79                         if (list)
80                         {
81                                 char buf[MAXBUF];
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();
87                         }
88                 }
89         }
90
91         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& except_list)
92         {
93                 HistoryList* list = m.ext.get(memb->chan);
94                 if (!list)
95                         return;
96                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
97                         memb->user->Write(i->line);
98         }
99
100         Version GetVersion()
101         {
102                 return Version("Provides channel history replayed on join", VF_COMMON | VF_VENDOR);
103         }
104 };
105
106 MODULE_INIT(ModuleChanHistory)