]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Change to Duration for second param
[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 maxlen, maxtime;
29         HistoryList(unsigned int len, unsigned int time) : maxlen(len), maxtime(time) {}
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                 if (adding)
42                 {
43                         std::string::size_type colon = parameter.find(':');
44                         if (colon == std::string::npos)
45                                 return MODEACTION_DENY;
46                         int len = atoi(parameter.substr(0, colon).c_str());
47                         int time = ServerInstance->Duration(parameter.substr(colon+1));
48                         if (len <= 0 || time < 0 || len > 50)
49                                 return MODEACTION_DENY;
50                         ext.set(channel, new HistoryList(len, time));
51                         channel->SetModeParam('H', parameter);
52                 }
53                 else
54                 {
55                         ext.unset(channel);
56                         channel->SetModeParam('H', "");
57                 }
58                 return MODEACTION_ALLOW;
59         }
60 };
61
62 class ModuleChanHistory : public Module
63 {
64         HistoryMode m;
65  public:
66         ModuleChanHistory() : m(this)
67         {
68                 if (!ServerInstance->Modes->AddMode(&m))
69                         throw ModuleException("Could not add new modes!");
70
71                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserMessage };
72                 ServerInstance->Modules->Attach(eventlist, this, 2);
73         }
74
75         ~ModuleChanHistory()
76         {
77                 ServerInstance->Modes->DelMode(&m);
78         }
79
80         void OnUserMessage(User* user,void* dest,int target_type, const std::string &text, char status, const CUList&)
81         {
82                 if (target_type == TYPE_CHANNEL && status == 0)
83                 {
84                         Channel* c = (Channel*)dest;
85                         HistoryList* list = m.ext.get(c);
86                         if (list)
87                         {
88                                 char buf[MAXBUF];
89                                 snprintf(buf, MAXBUF, ":%s PRIVMSG %s :%s",
90                                         user->GetFullHost().c_str(), c->name.c_str(), text.c_str());
91                                 list->lines.push_back(HistoryItem(buf));
92                                 if (list->lines.size() > list->maxlen)
93                                         list->lines.pop_front();
94                         }
95                 }
96         }
97
98         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& except_list)
99         {
100                 HistoryList* list = m.ext.get(memb->chan);
101                 if (!list)
102                         return;
103                 time_t mintime = 0;
104                 if (list->maxtime)
105                         mintime = ServerInstance->Time() - list->maxtime;
106                 memb->user->WriteServ("NOTICE %s :Replaying up to %d lines of pre-join history spanning up to %d seconds",
107                         memb->chan->name.c_str(), list->maxlen, list->maxtime);
108                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
109                 {
110                         if (i->ts >= mintime)
111                                 memb->user->Write(i->line);
112                 }
113         }
114
115         Version GetVersion()
116         {
117                 return Version("Provides channel history replayed on join", VF_COMMON | VF_VENDOR);
118         }
119 };
120
121 MODULE_INIT(ModuleChanHistory)