1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
23 /* $ModDesc: Provides channel mode +J (delay rejoin after kick) */
27 inline int strtoint(const std::string &str)
29 std::istringstream ss(str);
35 typedef std::map<userrec*, time_t> delaylist;
37 /** Handles channel mode +J
39 class KickRejoin : public ModeHandler
42 KickRejoin(InspIRCd* Instance) : ModeHandler(Instance, 'J', 1, 0, false, MODETYPE_CHANNEL, false) { }
44 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
46 if (channel->IsModeSet('J'))
47 return std::make_pair(true, channel->GetModeParameter('J'));
49 return std::make_pair(false, parameter);
52 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
54 /* When TS is equal, the alphabetically later one wins */
55 return (their_param < our_param);
58 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
62 // Taking the mode off, we need to clean up.
65 if (channel->GetExt("norejoinusers", dl))
68 channel->Shrink("norejoinusers");
71 if (!channel->IsModeSet('J'))
73 return MODEACTION_DENY;
77 channel->SetMode('J', false);
78 return MODEACTION_ALLOW;
81 else if (atoi(parameter.c_str()) > 0)
83 if (!channel->IsModeSet('J'))
85 parameter = ConvToStr(atoi(parameter.c_str()));
86 channel->SetModeParam('J', parameter.c_str(), adding);
87 channel->SetMode('J', adding);
88 return MODEACTION_ALLOW;
92 std::string cur_param = channel->GetModeParameter('J');
93 if (cur_param == parameter)
95 // mode params match, don't change mode
96 return MODEACTION_DENY;
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")
105 channel->SetModeParam('J', cur_param.c_str(), false);
106 channel->SetModeParam('J', parameter.c_str(), adding);
107 return MODEACTION_ALLOW;
111 /* Fix to jamie's fix, dont allow +J 0 on the new value! */
112 return MODEACTION_DENY;
119 return MODEACTION_DENY;
124 class ModuleKickNoRejoin : public Module
131 ModuleKickNoRejoin(InspIRCd* Me)
135 kr = new KickRejoin(ServerInstance);
136 if (!ServerInstance->AddMode(kr, 'J'))
137 throw ModuleException("Could not add new modes!");
140 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
145 if (chan->GetExt("norejoinusers", dl))
147 std::vector<userrec*> itemstoremove;
149 for (delaylist::iterator iter = dl->begin(); iter != dl->end(); iter++)
151 if (iter->second > time(NULL))
153 if (iter->first == user)
155 user->WriteServ( "495 %s %s :You cannot rejoin this channel yet after being kicked (+J)", user->nick, chan->name);
161 // Expired record, remove.
162 itemstoremove.push_back(iter->first);
166 for (unsigned int i = 0; i < itemstoremove.size(); i++)
167 dl->erase(itemstoremove[i]);
173 chan->Shrink("norejoinusers");
180 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
182 if (chan->IsModeSet('J') && (source != user))
185 if (!chan->GetExt("norejoinusers", dl))
188 chan->Extend("norejoinusers", dl);
190 (*dl)[user] = time(NULL) + strtoint(chan->GetModeParameter('J'));
194 virtual void OnChannelDelete(chanrec* chan)
198 if (chan->GetExt("norejoinusers", dl))
201 chan->Shrink("norejoinusers");
205 virtual void OnCleanup(int target_type, void* item)
207 if(target_type == TYPE_CHANNEL)
208 OnChannelDelete((chanrec*)item);
211 virtual void Implements(char* List)
213 List[I_OnCleanup] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserKick] = 1;
216 virtual ~ModuleKickNoRejoin()
218 ServerInstance->Modes->DelMode(kr);
222 virtual Version GetVersion()
224 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
229 class ModuleKickNoRejoinFactory : public ModuleFactory
232 ModuleKickNoRejoinFactory()
236 ~ModuleKickNoRejoinFactory()
240 virtual Module * CreateModule(InspIRCd* Me)
242 return new ModuleKickNoRejoin(Me);
248 extern "C" void * init_module( void )
250 return new ModuleKickNoRejoinFactory;