1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides support for the /SILENCE command */
18 /* Improved drop-in replacement for the /SILENCE command
19 * syntax: /SILENCE [+|-]<mask> <p|c|i|n|t|a|x> as in <privatemessage|channelmessage|invites|privatenotice|channelnotice|all|exclude>
21 * example that blocks all except private messages
25 * example that blocks all invites except from channel services
27 * /SILENCE +chanserv!services@chatters.net ix
29 * example that blocks some bad dude from private, notice and inviting you
30 * /SILENCE +*!kiddie@lamerz.net pin
32 * TODO: possibly have add and remove check for existing host and only modify flags according to
33 * what's been changed instead of having to remove first, then add if you want to change
37 // pair of hostmask and flags
38 typedef std::pair<std::string, int> silenceset;
40 // deque list of pairs
41 typedef std::deque<silenceset> silencelist;
44 static int SILENCE_PRIVATE = 0x0001; /* p private messages */
45 static int SILENCE_CHANNEL = 0x0002; /* c channel messages */
46 static int SILENCE_INVITE = 0x0004; /* i invites */
47 static int SILENCE_NOTICE = 0x0008; /* n notices */
48 static int SILENCE_CNOTICE = 0x0010; /* t channel notices */
49 static int SILENCE_ALL = 0x0020; /* a all, (pcint) */
50 static int SILENCE_EXCLUDE = 0x0040; /* x exclude this pattern */
53 class CommandSVSSilence : public Command
56 CommandSVSSilence(InspIRCd* Instance, Module* Creator) : Command(Instance, Creator,"SVSSILENCE", 0, 2)
58 syntax = "<target> {[+|-]<mask> <p|c|i|n|t|a|x>}";
59 TRANSLATE3(TR_NICK, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
62 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
65 * XXX: thought occurs to me
66 * We may want to change the syntax of this command to
67 * SVSSILENCE <flagsora+> +<nick> -<nick> +<nick>
68 * style command so services can modify lots of entries at once.
69 * leaving it backwards compatible for now as it's late. -- w
71 if (!ServerInstance->ULine(user->server))
74 User *u = ServerInstance->FindNick(parameters[0]);
80 ServerInstance->Parser->CallHandler("SILENCE", std::vector<std::string>(++parameters.begin(), parameters.end()), u);
86 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
88 return ROUTE_BROADCAST;
92 class CommandSilence : public Command
94 unsigned int& maxsilence;
96 SimpleExtItem<silencelist> ext;
97 CommandSilence (InspIRCd* Instance, Module* Creator, unsigned int &max) : Command(Instance, Creator, "SILENCE", 0, 0),
98 maxsilence(max), ext("silence_list", Creator)
100 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
101 TRANSLATE3(TR_TEXT, TR_TEXT, TR_END);
104 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
106 if (!parameters.size())
108 // no parameters, show the current silence list.
109 silencelist* sl = ext.get(user);
110 // if the user has a silence list associated with their user record, show it
113 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
115 user->WriteNumeric(271, "%s %s %s %s",user->nick.c_str(), user->nick.c_str(),c->first.c_str(), DecompPattern(c->second).c_str());
118 user->WriteNumeric(272, "%s :End of Silence List",user->nick.c_str());
122 else if (parameters.size() > 0)
124 // one or more parameters, add or delete entry from the list (only the first parameter is used)
125 std::string mask = parameters[0].substr(1);
126 char action = parameters[0][0];
127 // Default is private and notice so clients do not break
128 int pattern = CompilePattern("pn");
130 // if pattern supplied, use it
131 if (parameters.size() > 1) {
132 pattern = CompilePattern(parameters[1].c_str());
137 // 'SILENCE +' or 'SILENCE -', assume *!*@*
141 ModeParser::CleanMask(mask);
145 // fetch their silence list
146 silencelist* sl = ext.get(user);
147 // does it contain any entries and does it exist?
150 for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
152 // search through for the item
153 irc::string listitem = i->first.c_str();
154 if (listitem == mask && i->second == pattern)
157 user->WriteNumeric(950, "%s %s :Removed %s %s from silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), DecompPattern(pattern).c_str());
166 user->WriteNumeric(952, "%s %s :%s %s does not exist on your silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), DecompPattern(pattern).c_str());
168 else if (action == '+')
170 // fetch the user's current silence list
171 silencelist* sl = ext.get(user);
174 sl = new silencelist;
177 if (sl->size() > maxsilence)
179 user->WriteNumeric(952, "%s %s :Your silence list is full",user->nick.c_str(), user->nick.c_str());
182 for (silencelist::iterator n = sl->begin(); n != sl->end(); n++)
184 irc::string listitem = n->first.c_str();
185 if (listitem == mask && n->second == pattern)
187 user->WriteNumeric(952, "%s %s :%s %s is already on your silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), DecompPattern(pattern).c_str());
191 if (((pattern & SILENCE_EXCLUDE) > 0))
193 sl->push_front(silenceset(mask,pattern));
197 sl->push_back(silenceset(mask,pattern));
199 user->WriteNumeric(951, "%s %s :Added %s %s to silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), DecompPattern(pattern).c_str());
206 /* turn the nice human readable pattern into a mask */
207 int CompilePattern(const char* pattern)
210 for (const char* n = pattern; *n; n++)
215 p |= SILENCE_PRIVATE;
218 p |= SILENCE_CHANNEL;
227 p |= SILENCE_CNOTICE;
234 p |= SILENCE_EXCLUDE;
243 /* turn the mask into a nice human readable format */
244 std::string DecompPattern (const int pattern)
247 if ((pattern & SILENCE_PRIVATE) > 0)
248 out += ",privatemessages";
249 if ((pattern & SILENCE_CHANNEL) > 0)
250 out += ",channelmessages";
251 if ((pattern & SILENCE_INVITE) > 0)
253 if ((pattern & SILENCE_NOTICE) > 0)
254 out += ",privatenotices";
255 if ((pattern & SILENCE_CNOTICE) > 0)
256 out += ",channelnotices";
257 if ((pattern & SILENCE_ALL) > 0)
259 if ((pattern & SILENCE_EXCLUDE) > 0)
261 return "<" + out.substr(1) + ">";
266 class ModuleSilence : public Module
268 unsigned int maxsilence;
269 CommandSilence cmdsilence;
270 CommandSVSSilence cmdsvssilence;
273 ModuleSilence(InspIRCd* Me)
274 : Module(Me), maxsilence(32), cmdsilence(Me, this, maxsilence), cmdsvssilence(Me, this)
277 ServerInstance->AddCommand(&cmdsilence);
278 ServerInstance->AddCommand(&cmdsvssilence);
280 Implementation eventlist[] = { I_OnRehash, I_OnBuildExemptList, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnUserPreInvite };
281 ServerInstance->Modules->Attach(eventlist, this, 6);
284 void OnRehash(User* user)
286 ConfigReader Conf(ServerInstance);
287 maxsilence = Conf.ReadInteger("silence", "maxentries", 0, true);
292 void On005Numeric(std::string &output)
294 // we don't really have a limit...
295 output = output + " ESILENCE SILENCE=" + ConvToStr(maxsilence);
298 void OnBuildExemptList(MessageType message_type, Channel* chan, User* sender, char status, CUList &exempt_list, const std::string &text)
300 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
301 const UserMembList *ulist = chan->GetUsers();
303 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
305 if (IS_LOCAL(i->first))
307 if (MatchPattern(i->first, sender, public_silence) == MOD_RES_ALLOW)
309 exempt_list.insert(i->first);
315 ModResult PreText(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
317 if (target_type == TYPE_USER && IS_LOCAL(((User*)dest)))
319 return MatchPattern((User*)dest, user, silence_type);
321 else if (target_type == TYPE_CHANNEL)
323 Channel* chan = (Channel*)dest;
326 this->OnBuildExemptList((silence_type == SILENCE_PRIVATE ? MSG_PRIVMSG : MSG_NOTICE), chan, user, status, exempt_list, "");
329 return MOD_RES_PASSTHRU;
332 ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
334 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
337 ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
339 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
342 ModResult OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout)
344 return MatchPattern(dest, source, SILENCE_INVITE);
347 ModResult MatchPattern(User* dest, User* source, int pattern)
350 if (!source || !dest)
351 return MOD_RES_ALLOW;
353 silencelist* sl = cmdsilence.ext.get(dest);
356 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
358 if (((((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0)) && (InspIRCd::Match(source->GetFullHost(), c->first)))
359 return (c->second & SILENCE_EXCLUDE) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
362 return MOD_RES_PASSTHRU;
371 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
375 MODULE_INIT(ModuleSilence)