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