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 * ---------------------------------------------------
16 /* $ModDesc: Provides channel mode +J (delay rejoin after kick) */
18 inline int strtoint(const std::string &str)
20 std::istringstream ss(str);
26 typedef std::map<userrec*, time_t> delaylist;
28 /** Handles channel mode +J
30 class KickRejoin : public ModeHandler
33 KickRejoin(InspIRCd* Instance) : ModeHandler(Instance, 'J', 1, 0, false, MODETYPE_CHANNEL, false) { }
35 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
37 if (channel->IsModeSet('J'))
38 return std::make_pair(true, channel->GetModeParameter('J'));
40 return std::make_pair(false, parameter);
43 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
45 /* When TS is equal, the alphabetically later one wins */
46 return (their_param < our_param);
49 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
53 // Taking the mode off, we need to clean up.
56 if (channel->GetExt("norejoinusers", dl))
59 channel->Shrink("norejoinusers");
62 if (!channel->IsModeSet('J'))
64 return MODEACTION_DENY;
68 channel->SetMode('J', false);
69 return MODEACTION_ALLOW;
72 else if (atoi(parameter.c_str()) > 0)
74 if (!channel->IsModeSet('J'))
76 parameter = ConvToStr(atoi(parameter.c_str()));
77 channel->SetModeParam('J', parameter.c_str(), adding);
78 channel->SetMode('J', adding);
79 return MODEACTION_ALLOW;
83 std::string cur_param = channel->GetModeParameter('J');
84 if (cur_param == parameter)
86 // mode params match, don't change mode
87 return MODEACTION_DENY;
91 // new mode param, replace old with new
92 parameter = ConvToStr(atoi(parameter.c_str()));
93 cur_param = ConvToStr(atoi(cur_param.c_str()));
96 channel->SetModeParam('J', cur_param.c_str(), false);
97 channel->SetModeParam('J', parameter.c_str(), adding);
98 return MODEACTION_ALLOW;
102 /* Fix to jamie's fix, dont allow +J 0 on the new value! */
103 return MODEACTION_DENY;
110 return MODEACTION_DENY;
115 class ModuleKickNoRejoin : public Module
122 ModuleKickNoRejoin(InspIRCd* Me)
126 kr = new KickRejoin(ServerInstance);
127 if (!ServerInstance->AddMode(kr, 'J'))
128 throw ModuleException("Could not add new modes!");
131 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
136 if (chan->GetExt("norejoinusers", dl))
138 std::vector<userrec*> itemstoremove;
140 for (delaylist::iterator iter = dl->begin(); iter != dl->end(); iter++)
142 if (iter->second > time(NULL))
144 if (iter->first == user)
146 user->WriteServ( "495 %s %s :You cannot rejoin this channel yet after being kicked (+J)", user->nick, chan->name);
152 // Expired record, remove.
153 itemstoremove.push_back(iter->first);
157 for (unsigned int i = 0; i < itemstoremove.size(); i++)
158 dl->erase(itemstoremove[i]);
164 chan->Shrink("norejoinusers");
171 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
173 if (chan->IsModeSet('J') && (source != user))
176 if (!chan->GetExt("norejoinusers", dl))
179 chan->Extend("norejoinusers", dl);
181 (*dl)[user] = time(NULL) + strtoint(chan->GetModeParameter('J'));
185 virtual void OnChannelDelete(chanrec* chan)
189 if (chan->GetExt("norejoinusers", dl))
192 chan->Shrink("norejoinusers");
196 virtual void OnCleanup(int target_type, void* item)
198 if(target_type == TYPE_CHANNEL)
199 OnChannelDelete((chanrec*)item);
202 virtual void Implements(char* List)
204 List[I_OnCleanup] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserKick] = 1;
207 virtual ~ModuleKickNoRejoin()
209 ServerInstance->Modes->DelMode(kr);
213 virtual Version GetVersion()
215 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
220 MODULE_INIT(ModuleKickNoRejoin)