]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
m_spanningtree Add TreeServer::GetChildren() that replaces ChildCount() and GetChild()
[user/henk/code/inspircd.git] / src / modules / m_chanhistory.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #include "inspircd.h"
21
22 struct HistoryItem
23 {
24         time_t ts;
25         std::string line;
26         HistoryItem(const std::string& Line) : ts(ServerInstance->Time()), line(Line) {}
27 };
28
29 struct HistoryList
30 {
31         std::deque<HistoryItem> lines;
32         unsigned int maxlen, maxtime;
33         HistoryList(unsigned int len, unsigned int time) : maxlen(len), maxtime(time) {}
34 };
35
36 class HistoryMode : public ModeHandler
37 {
38         bool IsValidDuration(const std::string& duration)
39         {
40                 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
41                 {
42                         unsigned char c = *i;
43                         if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
44                                 continue;
45
46                         if (duration_multi[c] == 1)
47                                 return false;
48                 }
49                 return true;
50         }
51
52  public:
53         SimpleExtItem<HistoryList> ext;
54         unsigned int maxlines;
55         HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
56                 ext("history", Creator) { }
57
58         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
59         {
60                 if (adding)
61                 {
62                         std::string::size_type colon = parameter.find(':');
63                         if (colon == std::string::npos)
64                                 return MODEACTION_DENY;
65
66                         std::string duration = parameter.substr(colon+1);
67                         if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
68                                 return MODEACTION_DENY;
69
70                         unsigned int len = ConvToInt(parameter.substr(0, colon));
71                         int time = InspIRCd::Duration(duration);
72                         if (len == 0 || time < 0)
73                                 return MODEACTION_DENY;
74                         if (len > maxlines && IS_LOCAL(source))
75                                 return MODEACTION_DENY;
76                         if (len > maxlines)
77                                 len = maxlines;
78                         if (parameter == channel->GetModeParameter(this))
79                                 return MODEACTION_DENY;
80
81                         HistoryList* history = ext.get(channel);
82                         if (history)
83                         {
84                                 // Shrink the list if the new line number limit is lower than the old one
85                                 if (len < history->lines.size())
86                                         history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
87
88                                 history->maxlen = len;
89                                 history->maxtime = time;
90                         }
91                         else
92                         {
93                                 ext.set(channel, new HistoryList(len, time));
94                         }
95                 }
96                 else
97                 {
98                         if (!channel->IsModeSet(this))
99                                 return MODEACTION_DENY;
100                         ext.unset(channel);
101                 }
102                 return MODEACTION_ALLOW;
103         }
104 };
105
106 class ModuleChanHistory : public Module
107 {
108         HistoryMode m;
109         bool sendnotice;
110  public:
111         ModuleChanHistory() : m(this)
112         {
113         }
114
115         void init() CXX11_OVERRIDE
116         {
117                 ServerInstance->Modules->AddService(m);
118                 ServerInstance->Modules->AddService(m.ext);
119
120                 OnRehash(NULL);
121         }
122
123         void OnRehash(User*) CXX11_OVERRIDE
124         {
125                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
126                 m.maxlines = tag->getInt("maxlines", 50);
127                 sendnotice = tag->getBool("notice", true);
128         }
129
130         void OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList&, MessageType msgtype) CXX11_OVERRIDE
131         {
132                 if ((target_type == TYPE_CHANNEL) && (status == 0) && (msgtype == MSG_PRIVMSG))
133                 {
134                         Channel* c = (Channel*)dest;
135                         HistoryList* list = m.ext.get(c);
136                         if (list)
137                         {
138                                 const std::string line = ":" + user->GetFullHost() + " PRIVMSG " + c->name + " :" + text;
139                                 list->lines.push_back(HistoryItem(line));
140                                 if (list->lines.size() > list->maxlen)
141                                         list->lines.pop_front();
142                         }
143                 }
144         }
145
146         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
147         {
148                 if (IS_REMOTE(memb->user))
149                         return;
150
151                 HistoryList* list = m.ext.get(memb->chan);
152                 if (!list)
153                         return;
154                 time_t mintime = 0;
155                 if (list->maxtime)
156                         mintime = ServerInstance->Time() - list->maxtime;
157
158                 if (sendnotice)
159                 {
160                         memb->user->WriteNotice("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history spanning up to " + ConvToStr(list->maxtime) + " seconds");
161                 }
162
163                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
164                 {
165                         if (i->ts >= mintime)
166                                 memb->user->Write(i->line);
167                 }
168         }
169
170         Version GetVersion() CXX11_OVERRIDE
171         {
172                 return Version("Provides channel history replayed on join", VF_VENDOR);
173         }
174 };
175
176 MODULE_INIT(ModuleChanHistory)