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