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