]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delaymsg.cpp
MetaData rework
[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 class DelayMsgMode : public ModeHandler
18 {
19  private:
20         CUList empty;
21         Module* Creator;
22  public:
23         DelayMsgMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, 'd', 1, 0, false, MODETYPE_CHANNEL, false, 0, '@'), Creator(Parent) {};
24
25         ModePair ModeSet(User*, User*, Channel* channel, const std::string &parameter)
26         {
27                 std::string climit = channel->GetModeParameter('d');
28                 if (!climit.empty())
29                 {
30                         return std::make_pair(true, climit);
31                 }
32                 else
33                 {
34                         return std::make_pair(false, parameter);
35                 }
36         }
37
38         bool CheckTimeStamp(std::string &their_param, const std::string &our_param, Channel*)
39         {
40                 return (atoi(their_param.c_str()) < atoi(our_param.c_str()));
41         }
42
43         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool);
44 };
45
46 class ModuleDelayMsg : public Module
47 {
48  private:
49         DelayMsgMode djm;
50  public:
51         ModuleDelayMsg(InspIRCd* Me) : Module(Me), djm(Me, this)
52         {
53                 if (!ServerInstance->Modes->AddMode(&djm))
54                         throw ModuleException("Could not add new modes!");
55                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnCleanup, I_OnUserPreMessage};
56                 ServerInstance->Modules->Attach(eventlist, this, 5);
57         }
58         virtual ~ModuleDelayMsg();
59         virtual Version GetVersion();
60         void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created);
61         void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
62         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent);
63         void OnCleanup(int target_type, void* item);
64         int OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list);
65 };
66
67 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
68
69 ModeAction DelayMsgMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
70 {
71         if (adding)
72         {
73                 /* Setting a new limit, sanity check */
74                 long limit = atoi(parameter.c_str());
75
76                 /* Wrap low values at 32768 */
77                 if (limit < 0)
78                         limit = 0x7FFF;
79
80                 parameter = ConvToStr(limit);
81         }
82         else
83         {
84                 /*
85                  * Clean up metadata
86                  */
87                 CUList* names = channel->GetUsers();
88                 for (CUListIter n = names->begin(); n != names->end(); ++n)
89                         n->first->Shrink("delaymsg_" + channel->name);
90         }
91         channel->SetModeParam('d', adding ? parameter : "");
92         return MODEACTION_ALLOW;
93 }
94
95 ModuleDelayMsg::~ModuleDelayMsg()
96 {
97         ServerInstance->Modes->DelMode(&djm);
98 }
99
100 Version ModuleDelayMsg::GetVersion()
101 {
102         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
103 }
104
105 void ModuleDelayMsg::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created)
106 {
107         if (channel->IsModeSet('d'))
108                 user->Extend("delaymsg_"+channel->name, reinterpret_cast<char*>(ServerInstance->Time()));
109 }
110
111 void ModuleDelayMsg::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
112 {
113         user->Shrink("delaymsg_"+channel->name);
114 }
115
116 void ModuleDelayMsg::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
117 {
118         user->Shrink("delaymsg_"+chan->name);
119 }
120
121 void ModuleDelayMsg::OnCleanup(int target_type, void* item)
122 {
123         if (target_type == TYPE_USER)
124         {
125                 User* user = (User*)item;
126                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
127                 {
128                         Channel* chan = f->first;
129                         user->Shrink("delaymsg_"+chan->name);
130                 }
131         }
132 }
133
134 int ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
135 {
136         /* Server origin */
137         if (!user)
138                 return false;
139
140         if (target_type != TYPE_CHANNEL)
141                 return false;
142
143         Channel* channel = (Channel*) dest;
144
145         void* jointime_as_ptr;
146
147         if (!user->GetExt("delaymsg_"+channel->name, jointime_as_ptr))
148                 return false;
149
150         time_t jointime = reinterpret_cast<time_t>(jointime_as_ptr);
151
152         std::string len = channel->GetModeParameter('d');
153
154         if (jointime + atoi(len.c_str()) > ServerInstance->Time())
155         {
156                 if (channel->GetStatus(user) < STATUS_VOICE)
157                 {
158                         user->WriteNumeric(404, "%s %s :You must wait %s seconds after joining to send to channel (+d)",
159                                 user->nick.c_str(), channel->name.c_str(), len.c_str());
160                         return true;
161                 }
162         }
163         else
164         {
165                 /* Timer has expired, we can stop checking now */
166                 user->Shrink("delaymsg_"+channel->name);
167         }
168         return false;
169 }
170
171 MODULE_INIT(ModuleDelayMsg)
172