]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delaymsg.cpp
Replace OnAccessCheck with OnPreMode to remove a number of redundant checks
[user/henk/code/inspircd.git] / src / modules / m_delaymsg.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 #include <stdarg.h>
16
17 /* $ModDesc: Provides channelmode +d <int>, to deny messages to a channel until <int> seconds. */
18
19 class DelayMsgMode : public ModeHandler
20 {
21  private:
22         CUList empty;
23  public:
24         LocalIntExt jointime;
25         DelayMsgMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, Parent, 'd', 1, 0, false, MODETYPE_CHANNEL, false, 0, '@'), jointime("delaymsg", Parent) {};
26
27         ModePair ModeSet(User*, User*, Channel* channel, const std::string &parameter)
28         {
29                 std::string climit = channel->GetModeParameter('d');
30                 if (!climit.empty())
31                 {
32                         return std::make_pair(true, climit);
33                 }
34                 else
35                 {
36                         return std::make_pair(false, parameter);
37                 }
38         }
39
40         bool CheckTimeStamp(std::string &their_param, const std::string &our_param, Channel*)
41         {
42                 return (atoi(their_param.c_str()) < atoi(our_param.c_str()));
43         }
44
45         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
46 };
47
48 class ModuleDelayMsg : public Module
49 {
50  private:
51         DelayMsgMode djm;
52  public:
53         ModuleDelayMsg(InspIRCd* Me) : Module(Me), djm(Me, this)
54         {
55                 if (!ServerInstance->Modes->AddMode(&djm))
56                         throw ModuleException("Could not add new modes!");
57                 Extensible::Register(&djm.jointime);
58                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPreMessage};
59                 ServerInstance->Modules->Attach(eventlist, this, 2);
60         }
61         ~ModuleDelayMsg();
62         Version GetVersion();
63         void OnUserJoin(Membership* memb, bool sync, bool created, CUList&);
64         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list);
65 };
66
67 ModeAction DelayMsgMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
68 {
69         if (adding)
70         {
71                 /* Setting a new limit, sanity check */
72                 long limit = atoi(parameter.c_str());
73
74                 /* Wrap low values at 32768 */
75                 if (limit < 0)
76                         limit = 0x7FFF;
77
78                 parameter = ConvToStr(limit);
79         }
80         else
81         {
82                 /*
83                  * Clean up metadata
84                  */
85                 const UserMembList* names = channel->GetUsers();
86                 for (UserMembCIter n = names->begin(); n != names->end(); ++n)
87                         jointime.set(n->second, 0);
88         }
89         channel->SetModeParam('d', adding ? parameter : "");
90         return MODEACTION_ALLOW;
91 }
92
93 ModuleDelayMsg::~ModuleDelayMsg()
94 {
95         ServerInstance->Modes->DelMode(&djm);
96 }
97
98 Version ModuleDelayMsg::GetVersion()
99 {
100         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
101 }
102
103 void ModuleDelayMsg::OnUserJoin(Membership* memb, bool sync, bool created, CUList&)
104 {
105         if (memb->chan->IsModeSet('d'))
106         {
107                 djm.jointime.set(memb, ServerInstance->Time());
108         }
109 }
110
111 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
112 {
113         /* Server origin */
114         if (!user)
115                 return MOD_RES_PASSTHRU;
116
117         if (target_type != TYPE_CHANNEL)
118                 return MOD_RES_PASSTHRU;
119
120         Channel* channel = (Channel*) dest;
121         Membership* memb = channel->GetUser(user);
122
123         if (!memb)
124                 return MOD_RES_PASSTHRU;
125         
126         time_t ts = djm.jointime.get(memb);
127
128         if (ts == 0)
129                 return MOD_RES_PASSTHRU;
130
131         std::string len = channel->GetModeParameter('d');
132
133         if (ts + atoi(len.c_str()) > ServerInstance->Time())
134         {
135                 if (channel->GetPrefixValue(user) < VOICE_VALUE)
136                 {
137                         user->WriteNumeric(404, "%s %s :You must wait %s seconds after joining to send to channel (+d)",
138                                 user->nick.c_str(), channel->name.c_str(), len.c_str());
139                         return MOD_RES_DENY;
140                 }
141         }
142         else
143         {
144                 /* Timer has expired, we can stop checking now */
145                 djm.jointime.set(memb, 0);
146         }
147         return MOD_RES_PASSTHRU;
148 }
149
150 MODULE_INIT(ModuleDelayMsg)
151