8 #include "configreader.h"
10 /* $ModDesc: Gives the /shun command, which stops a user executing all commands except PING and PONG. */
12 /* $ModAuthorMail: om@inspircd.org */
25 Shun(std::string bm, std::string sb, time_t so, long ln, std::string rs) : banmask(bm), set_by(sb), set_on(so), length(ln), reason(rs) { }
27 bool operator<(const Shun &ban2) const
29 return ((this->set_on + this->length) < (ban2.set_on + ban2.length));
33 typedef std::vector<Shun> shunlist;
38 /* shuns is declared here, as our type is right above. Don't try move it. */
43 std::string EncodeShun(const Shun &shun)
45 std::ostringstream stream;
46 stream << shun.banmask << " " << shun.set_by << " " << shun.set_on << " " << shun.length << " " << shun.reason;
50 Shun DecodeShun(const std::string &data)
53 std::istringstream stream(data);
54 stream >> res.banmask;
58 res.reason = stream.str();
65 while(shuns.size() && shuns.begin()->length && ((shuns.begin()->set_on + shuns.begin()->length) <= Srv->Time()))
67 shunlist::iterator iter = shuns.begin();
69 Srv->SNO->WriteToSnoMask('X', "*** %ld second shun on '%s' (%s) set by %s %ld seconds ago expired", iter->length, iter->banmask.c_str(), iter->reason.c_str(), iter->set_by.c_str(), Srv->Time() - iter->set_on);
75 class cmd_shun : public Command
82 cmd_shun(InspIRCd* Me, ModuleShunBase* b)
83 : Command(Me, "SHUN", "o", 1), Srv(Me), base(b)
85 this->source = "m_shun.so";
88 CmdResult Handle(const char* const*parameters, int pcnt, User *user)
90 /* syntax: SHUN nick!user@host time :reason goes here */
91 /* 'time' is a human-readable timestring, like 2d3h2s. */
97 /* form: SHUN mask removes a SHUN */
98 for(shunlist::iterator iter = base->shuns.begin(); iter != base->shuns.end(); iter++)
100 if(parameters[0] == iter->banmask)
102 Srv->SNO->WriteToSnoMask('X', "*** %s removed shun '%s', set %ld seconds ago with reason '%s'", user->nick, iter->banmask.c_str(), Srv->Time() - iter->set_on, iter->reason.c_str());
103 base->shuns.erase(iter);
108 user->WriteServ("NOTICE %s :*** The mask %s is not currently shunned, try /stats s", user->nick, parameters[0]);
113 /* full form to add a shun */
114 if(Srv->IsValidMask(parameters[0]))
116 // parameters[0] = Foamy!*@*
117 // parameters[1] = 1h3m2s
118 // parameters[2] = Tortoise abuser
119 for(shunlist::iterator iter = base->shuns.begin(); iter != base->shuns.end(); iter++)
121 if (parameters[0] == iter->banmask)
123 user->WriteServ("NOTICE %s :*** Shun on %s already exists", user->nick, parameters[0]);
128 long length = Srv->Duration(parameters[1]);
130 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
132 base->shuns.push_back(Shun(parameters[0], user->nick, Srv->Time(), length, reason));
134 std::sort(base->shuns.begin(), base->shuns.end());
137 Srv->SNO->WriteToSnoMask('X', "*** %s added %ld second shun on '%s' (%s)", user->nick, length, parameters[0], reason.c_str());
139 Srv->SNO->WriteToSnoMask('X', "*** %s added permanent shun on '%s' (%s)", user->nick, parameters[0], reason.c_str());
145 user->WriteServ("NOTICE %s :*** Invalid ban mask (%s)", user->nick, parameters[0]);
153 class ModuleShun : public Module, public ModuleShunBase
158 ModuleShun(InspIRCd* Me)
162 mycommand = new cmd_shun(Srv, this);
163 Srv->AddCommand(mycommand);
166 void Implements(char* List)
168 List[I_OnPreCommand] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
171 virtual int OnStats(char symbol, User* user, string_list& out)
177 for(shunlist::iterator iter = shuns.begin(); iter != shuns.end(); iter++)
179 std::ostringstream format;
180 format << Srv->Config->ServerName << " 223 " << user->nick << " :" << iter->banmask << " " << iter->set_on << " " << iter->length << " " << iter->set_by << " " << iter->reason;
181 out.push_back(format.str());
188 virtual int OnPreCommand(const std::string &command, const char* const*parameters, int pcnt, User* user, bool validated, const std::string &original_line)
190 if((command != "PONG") && (command != "PING"))
194 for(shunlist::iterator iter = shuns.begin(); iter != shuns.end(); iter++)
195 if(Srv->MatchText(user->GetFullHost(), iter->banmask) || Srv->MatchText(user->GetFullRealHost(), iter->banmask))
202 virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
204 for(shunlist::iterator iter = shuns.begin(); iter != shuns.end(); iter++)
206 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "shun", EncodeShun(*iter));
210 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
212 if((target_type == TYPE_OTHER) && (extname == "shun"))
214 shuns.push_back(DecodeShun(extdata));
215 std::sort(shuns.begin(), shuns.end());
219 virtual ~ModuleShun()
223 virtual Version GetVersion()
225 return Version(1,0,0,0,0,API_VERSION);
229 MODULE_INIT(ModuleShun)