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