]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_kicknorejoin.cpp
a38d260bb6938cae7b6cad756b95ee78ba547c05
[user/henk/code/inspircd.git] / src / modules / m_kicknorejoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <time.h>
15 #include <map>
16 #include <vector>
17 #include <sstream>
18 #include "users.h"
19 #include "channels.h"
20 #include "modules.h"
21 #include "inspircd.h"
22
23 /* $ModDesc: Provides channel mode +J (delay rejoin after kick) */
24
25
26
27 inline int strtoint(const std::string &str)
28 {
29         std::istringstream ss(str);
30         int result;
31         ss >> result;
32         return result;
33 }
34
35 typedef std::map<userrec*, time_t> delaylist;
36
37 /** Handles channel mode +J
38  */
39 class KickRejoin : public ModeHandler
40 {
41  public:
42         KickRejoin(InspIRCd* Instance) : ModeHandler(Instance, 'J', 1, 0, false, MODETYPE_CHANNEL, false) { }
43
44         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
45         {
46                 if (channel->IsModeSet('J'))
47                         return std::make_pair(true, channel->GetModeParameter('J'));
48                 else
49                         return std::make_pair(false, parameter);
50         } 
51
52         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
53         {
54                 /* When TS is equal, the alphabetically later one wins */
55                 return (their_param < our_param);
56         }
57         
58         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
59         {
60                 if (!adding)
61                 {
62                         // Taking the mode off, we need to clean up.
63                         delaylist* dl;
64                         
65                         if (channel->GetExt("norejoinusers", dl))
66                         {
67                                 DELETE(dl);
68                                 channel->Shrink("norejoinusers");
69                         }
70                         
71                         if (!channel->IsModeSet('J'))
72                         {
73                                 return MODEACTION_DENY;
74                         }
75                         else
76                         {
77                                 channel->SetMode('J', false);
78                                 return MODEACTION_ALLOW;
79                         }
80                 }
81                 else if (atoi(parameter.c_str()) > 0)
82                 {
83                         if (!channel->IsModeSet('J'))
84                         {
85                                 parameter = ConvToStr(atoi(parameter.c_str()));
86                                 channel->SetModeParam('J', parameter.c_str(), adding);
87                                 channel->SetMode('J', adding);
88                                 return MODEACTION_ALLOW;
89                         }
90                         else
91                         {
92                                 std::string cur_param = channel->GetModeParameter('J');
93                                 if (cur_param == parameter)
94                                 {
95                                         // mode params match, don't change mode
96                                         return MODEACTION_DENY;
97                                 }
98                                 else
99                                 {
100                                         // new mode param, replace old with new
101                                         parameter = ConvToStr(atoi(parameter.c_str()));
102                                         cur_param = ConvToStr(atoi(cur_param.c_str()));
103                                         if (parameter != "0")
104                                         {
105                                                 channel->SetModeParam('J', cur_param.c_str(), false);
106                                                 channel->SetModeParam('J', parameter.c_str(), adding);
107                                                 return MODEACTION_ALLOW;
108                                         }
109                                         else
110                                         {
111                                                 /* Fix to jamie's fix, dont allow +J 0 on the new value! */
112                                                 return MODEACTION_DENY;
113                                         }
114                                 }
115                         }
116                 }
117                 else
118                 {
119                         return MODEACTION_DENY;
120                 }
121         }
122 };
123
124 class ModuleKickNoRejoin : public Module
125 {
126         
127         KickRejoin* kr;
128         
129 public:
130  
131         ModuleKickNoRejoin(InspIRCd* Me)
132                 : Module(Me)
133         {
134                 
135                 kr = new KickRejoin(ServerInstance);
136                 if (!ServerInstance->AddMode(kr, 'J'))
137                         throw ModuleException("Could not add new modes!");
138         }
139
140         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
141         {
142                 if (chan)
143                 {
144                         delaylist* dl;
145                         if (chan->GetExt("norejoinusers", dl))
146                         {
147                                 std::vector<userrec*> itemstoremove;
148                         
149                                 for (delaylist::iterator iter = dl->begin(); iter != dl->end(); iter++)
150                                 {
151                                         if (iter->second > time(NULL))
152                                         {
153                                                 if (iter->first == user)                                        
154                                                 {
155                                                         user->WriteServ( "495 %s %s :You cannot rejoin this channel yet after being kicked (+J)", user->nick, chan->name);
156                                                         return 1;
157                                                 }
158                                         }
159                                         else
160                                         {
161                                                 // Expired record, remove.
162                                                 itemstoremove.push_back(iter->first);
163                                         }
164                                 }
165                                 
166                                 for (unsigned int i = 0; i < itemstoremove.size(); i++)
167                                         dl->erase(itemstoremove[i]);
168                                                                                                                                         
169                                 if (!dl->size())
170                                 {
171                                         // Now it's empty..
172                                         DELETE(dl);
173                                         chan->Shrink("norejoinusers");
174                                 }
175                         }
176                 }
177                 return 0;
178         }
179                 
180         virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
181         {
182                 if (chan->IsModeSet('J') && (source != user))
183                 {
184                         delaylist* dl;
185                         if (!chan->GetExt("norejoinusers", dl))
186                         {
187                                 dl = new delaylist;
188                                 chan->Extend("norejoinusers", dl);
189                         }
190                         (*dl)[user] = time(NULL) + strtoint(chan->GetModeParameter('J'));
191                 }
192         }
193         
194         virtual void OnChannelDelete(chanrec* chan)
195         {
196                 delaylist* dl;
197                         
198                 if (chan->GetExt("norejoinusers", dl))
199                 {
200                         DELETE(dl);
201                         chan->Shrink("norejoinusers");
202                 }
203         }
204         
205         virtual void OnCleanup(int target_type, void* item)
206         {
207                 if(target_type == TYPE_CHANNEL)
208                         OnChannelDelete((chanrec*)item);
209         }
210
211         virtual void Implements(char* List)
212         {
213                 List[I_OnCleanup] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserKick] = 1;
214         }
215
216         virtual ~ModuleKickNoRejoin()
217         {
218                 ServerInstance->Modes->DelMode(kr);
219                 DELETE(kr);
220         }
221         
222         virtual Version GetVersion()
223         {
224                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
225         }
226 };
227
228
229 class ModuleKickNoRejoinFactory : public ModuleFactory
230 {
231  public:
232         ModuleKickNoRejoinFactory()
233         {
234         }
235         
236         ~ModuleKickNoRejoinFactory()
237         {
238         }
239         
240         virtual Module * CreateModule(InspIRCd* Me)
241         {
242                 return new ModuleKickNoRejoin(Me);
243         }
244         
245 };
246
247
248 extern "C" DllExport void * init_module( void )
249 {
250         return new ModuleKickNoRejoinFactory;
251 }
252