]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
m_shun Fix a couple of things, namely
[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 /* $ModDesc: Provides channel history for a given number of lines */
23
24 struct HistoryItem
25 {
26         time_t ts;
27         std::string line;
28         HistoryItem(const std::string& Line) : ts(ServerInstance->Time()), line(Line) {}
29 };
30
31 struct HistoryList
32 {
33         std::deque<HistoryItem> lines;
34         unsigned int maxlen, maxtime;
35         HistoryList(unsigned int len, unsigned int time) : maxlen(len), maxtime(time) {}
36 };
37
38 class HistoryMode : public ModeHandler
39 {
40  public:
41         SimpleExtItem<HistoryList> ext;
42         int maxlines;
43         HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
44                 ext("history", Creator) { }
45
46         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
47         {
48                 if (adding)
49                 {
50                         std::string::size_type colon = parameter.find(':');
51                         if (colon == std::string::npos)
52                                 return MODEACTION_DENY;
53                         int len = atoi(parameter.substr(0, colon).c_str());
54                         int time = ServerInstance->Duration(parameter.substr(colon+1));
55                         if (len <= 0 || time < 0)
56                                 return MODEACTION_DENY;
57                         if (len > maxlines && IS_LOCAL(source))
58                                 return MODEACTION_DENY;
59                         if (len > maxlines)
60                                 len = maxlines;
61                         if (parameter == channel->GetModeParameter(this))
62                                 return MODEACTION_DENY;
63                         ext.set(channel, new HistoryList(len, time));
64                         channel->SetModeParam('H', parameter);
65                 }
66                 else
67                 {
68                         if (!channel->IsModeSet('H'))
69                                 return MODEACTION_DENY;
70                         ext.unset(channel);
71                         channel->SetModeParam('H', "");
72                 }
73                 return MODEACTION_ALLOW;
74         }
75 };
76
77 class ModuleChanHistory : public Module
78 {
79         HistoryMode m;
80  public:
81         ModuleChanHistory() : m(this)
82         {
83         }
84
85         void init()
86         {
87                 ServerInstance->Modules->AddService(m);
88
89                 Implementation eventlist[] = { I_OnPostJoin, I_OnUserMessage, I_OnRehash };
90                 ServerInstance->Modules->Attach(eventlist, this, 3);
91                 OnRehash(NULL);
92         }
93
94         void OnRehash(User*)
95         {
96                 m.maxlines = ServerInstance->Config->ConfValue("chanhistory")->getInt("maxlines", 50);
97         }
98
99         ~ModuleChanHistory()
100         {
101                 ServerInstance->Modes->DelMode(&m);
102         }
103
104         void OnUserMessage(User* user,void* dest,int target_type, const std::string &text, char status, const CUList&)
105         {
106                 if (target_type == TYPE_CHANNEL && status == 0)
107                 {
108                         Channel* c = (Channel*)dest;
109                         HistoryList* list = m.ext.get(c);
110                         if (list)
111                         {
112                                 char buf[MAXBUF];
113                                 snprintf(buf, MAXBUF, ":%s PRIVMSG %s :%s",
114                                         user->GetFullHost().c_str(), c->name.c_str(), text.c_str());
115                                 list->lines.push_back(HistoryItem(buf));
116                                 if (list->lines.size() > list->maxlen)
117                                         list->lines.pop_front();
118                         }
119                 }
120         }
121
122         void OnPostJoin(Membership* memb)
123         {
124                 HistoryList* list = m.ext.get(memb->chan);
125                 if (!list)
126                         return;
127                 time_t mintime = 0;
128                 if (list->maxtime)
129                         mintime = ServerInstance->Time() - list->maxtime;
130                 memb->user->WriteServ("NOTICE %s :Replaying up to %d lines of pre-join history spanning up to %d seconds",
131                         memb->chan->name.c_str(), list->maxlen, list->maxtime);
132                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
133                 {
134                         if (i->ts >= mintime)
135                                 memb->user->Write(i->line);
136                 }
137         }
138
139         Version GetVersion()
140         {
141                 return Version("Provides channel history replayed on join", VF_VENDOR);
142         }
143 };
144
145 MODULE_INIT(ModuleChanHistory)