]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Initial support for listening on UNIX socket endpoints.
[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         std::string param;
34
35         HistoryList(unsigned int len, unsigned int time, const std::string& oparam)
36                 : maxlen(len), maxtime(time), param(oparam) { }
37 };
38
39 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
40 {
41         bool IsValidDuration(const std::string& duration)
42         {
43                 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
44                 {
45                         unsigned char c = *i;
46                         if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
47                                 continue;
48
49                         if (duration_multi[c] == 1)
50                                 return false;
51                 }
52                 return true;
53         }
54
55  public:
56         unsigned int maxlines;
57         HistoryMode(Module* Creator)
58                 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
59         {
60         }
61
62         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
63         {
64                 std::string::size_type colon = parameter.find(':');
65                 if (colon == std::string::npos)
66                 {
67                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
68                         return MODEACTION_DENY;
69                 }
70
71                 std::string duration(parameter, colon+1);
72                 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
73                 {
74                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
75                         return MODEACTION_DENY;
76                 }
77
78                 unsigned int len = ConvToInt(parameter.substr(0, colon));
79                 unsigned int time = InspIRCd::Duration(duration);
80                 if (len == 0 || (len > maxlines && IS_LOCAL(source)))
81                 {
82                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
83                         return MODEACTION_DENY;
84                 }
85                 if (len > maxlines)
86                         len = maxlines;
87
88                 HistoryList* history = ext.get(channel);
89                 if (history)
90                 {
91                         // Shrink the list if the new line number limit is lower than the old one
92                         if (len < history->lines.size())
93                                 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
94
95                         history->maxlen = len;
96                         history->maxtime = time;
97                         history->param = parameter;
98                 }
99                 else
100                 {
101                         ext.set(channel, new HistoryList(len, time, parameter));
102                 }
103                 return MODEACTION_ALLOW;
104         }
105
106         void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
107         {
108                 out.append(history->param);
109         }
110 };
111
112 class ModuleChanHistory : public Module
113 {
114         HistoryMode m;
115         bool sendnotice;
116         UserModeReference botmode;
117         bool dobots;
118  public:
119         ModuleChanHistory() : m(this), botmode(this, "bot")
120         {
121         }
122
123         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
124         {
125                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
126                 m.maxlines = tag->getUInt("maxlines", 50, 1);
127                 sendnotice = tag->getBool("notice", true);
128                 dobots = tag->getBool("bots", true);
129         }
130
131         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
132         {
133                 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
134                 {
135                         Channel* c = target.Get<Channel>();
136                         HistoryList* list = m.ext.get(c);
137                         if (list)
138                         {
139                                 const std::string line = ":" + user->GetFullHost() + " PRIVMSG " + c->name + " :" + details.text;
140                                 list->lines.push_back(HistoryItem(line));
141                                 if (list->lines.size() > list->maxlen)
142                                         list->lines.pop_front();
143                         }
144                 }
145         }
146
147         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
148         {
149                 if (IS_REMOTE(memb->user))
150                         return;
151
152                 if (memb->user->IsModeSet(botmode) && !dobots)
153                         return;
154
155                 HistoryList* list = m.ext.get(memb->chan);
156                 if (!list)
157                         return;
158                 time_t mintime = 0;
159                 if (list->maxtime)
160                         mintime = ServerInstance->Time() - list->maxtime;
161
162                 if (sendnotice)
163                 {
164                         std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
165                         if (list->maxtime > 0)
166                                 message.append(" spanning up to " + ConvToStr(list->maxtime) + " seconds");
167                         memb->WriteNotice(message);
168                 }
169
170                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
171                 {
172                         if (i->ts >= mintime)
173                                 memb->user->Write(i->line);
174                 }
175         }
176
177         Version GetVersion() CXX11_OVERRIDE
178         {
179                 return Version("Provides channel history replayed on join", VF_VENDOR);
180         }
181 };
182
183 MODULE_INIT(ModuleChanHistory)