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 * ---------------------------------------------------
21 /* $ModDesc: Provides support for the /SILENCE command */
23 /* Improved drop-in replacement for the /SILENCE command
24 * syntax: /SILENCE [+|-]<mask> <p|c|i|n|t|a|x> as in <privatemessage|channelmessage|invites|privatenotice|channelnotice|all|exclude>
26 * example that blocks all except private messages
30 * example that blocks all invites except from channel services
32 * /SILENCE +chanserv!services@chatters.net ix
34 * example that blocks some bad dude from private, notice and inviting you
35 * /SILENCE +*!kiddie@lamerz.net pin
37 * TODO: possibly have add and remove check for existing host and only modify flags according to
38 * what's been changed instead of having to remove first, then add if you want to change
42 // pair of hostmask and flags
43 typedef std::pair<std::string, int> silenceset;
45 // deque list of pairs
46 typedef std::deque<silenceset> silencelist;
49 static int SILENCE_PRIVATE = 0x0001; /* p private messages */
50 static int SILENCE_CHANNEL = 0x0002; /* c channel messages */
51 static int SILENCE_INVITE = 0x0004; /* i invites */
52 static int SILENCE_NOTICE = 0x0008; /* n notices */
53 static int SILENCE_CNOTICE = 0x0010; /* t channel notices */
54 static int SILENCE_ALL = 0x0020; /* a all, (pcint) */
55 static int SILENCE_EXCLUDE = 0x0040; /* x exclude this pattern */
58 class cmd_silence : public command_t
60 unsigned int& maxsilence;
62 cmd_silence (InspIRCd* Instance, unsigned int &max) : command_t(Instance,"SILENCE", 0, 0), maxsilence(max)
64 this->source = "m_silence_ext.so";
65 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
68 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
72 // no parameters, show the current silence list.
73 // Use Extensible::GetExt to fetch the silence list
75 user->GetExt("silence_list", sl);
76 // if the user has a silence list associated with their user record, show it
79 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
81 user->WriteServ("271 %s %s %s %s",user->nick, user->nick,c->first.c_str(), DecompPattern(c->second).c_str());
84 user->WriteServ("272 %s :End of Silence List",user->nick);
90 // one or more parameters, add or delete entry from the list (only the first parameter is used)
91 std::string mask = parameters[0] + 1;
92 char action = *parameters[0];
93 // Default is private and notice so clients do not break
94 int pattern = CompilePattern("pn");
96 // if pattern supplied, use it
98 pattern = CompilePattern(parameters[1]);
103 // 'SILENCE +' or 'SILENCE -', assume *!*@*
107 ModeParser::CleanMask(mask);
111 // fetch their silence list
113 user->GetExt("silence_list", sl);
114 // does it contain any entries and does it exist?
117 for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
119 // search through for the item
120 irc::string listitem = i->first.c_str();
121 if (listitem == mask && i->second == pattern)
124 user->WriteServ("950 %s %s :Removed %s %s from silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
128 user->Shrink("silence_list");
134 user->WriteServ("952 %s %s :%s %s does not exist on your silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
136 else if (action == '+')
138 // fetch the user's current silence list
140 user->GetExt("silence_list", sl);
141 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
144 sl = new silencelist;
145 user->Extend("silence_list", sl);
147 if (sl->size() > maxsilence)
149 user->WriteServ("952 %s %s :Your silence list is full",user->nick, user->nick);
152 for (silencelist::iterator n = sl->begin(); n != sl->end(); n++)
154 irc::string listitem = n->first.c_str();
155 if (listitem == mask && n->second == pattern)
157 user->WriteServ("952 %s %s :%s %s is already on your silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
161 if (((pattern & SILENCE_EXCLUDE) > 0))
163 sl->push_front(silenceset(mask,pattern));
167 sl->push_back(silenceset(mask,pattern));
169 user->WriteServ("951 %s %s :Added %s %s to silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
170 return CMD_LOCALONLY;
173 return CMD_LOCALONLY;
176 /* turn the nice human readable pattern into a mask */
177 int CompilePattern(const char* pattern)
180 for (const char* n = pattern; *n; n++)
185 p |= SILENCE_PRIVATE;
188 p |= SILENCE_CHANNEL;
197 p |= SILENCE_CNOTICE;
203 p |= SILENCE_EXCLUDE;
212 /* turn the mask into a nice human readable format */
213 std::string DecompPattern (const int pattern)
216 if ((pattern & SILENCE_PRIVATE) > 0)
217 out += ",privatemessages";
218 if ((pattern & SILENCE_CHANNEL) > 0)
219 out += ",channelmessages";
220 if ((pattern & SILENCE_INVITE) > 0)
222 if ((pattern & SILENCE_NOTICE) > 0)
223 out += ",privatenotices";
224 if ((pattern & SILENCE_CNOTICE) > 0)
225 out += ",channelnotices";
226 if ((pattern & SILENCE_ALL) > 0)
228 if ((pattern & SILENCE_EXCLUDE) > 0)
230 return "<" + out.substr(1) + ">";
235 class ModuleSilence : public Module
237 cmd_silence* mycommand;
238 unsigned int maxsilence;
241 ModuleSilence(InspIRCd* Me)
242 : Module(Me), maxsilence(32)
245 mycommand = new cmd_silence(ServerInstance,maxsilence);
246 ServerInstance->AddCommand(mycommand);
249 virtual void OnRehash(userrec* user, const std::string ¶meter)
251 ConfigReader Conf(ServerInstance);
252 maxsilence = Conf.ReadInteger("silence", "maxentries", 0, true);
257 void Implements(char* List)
259 List[I_OnRehash] = List[I_OnBuildExemptList] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = List[I_OnUserPreInvite] = 1;
262 virtual void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
264 // when the user quits tidy up any silence list they might have just to keep things tidy
266 user->GetExt("silence_list", sl);
270 user->Shrink("silence_list");
274 virtual void On005Numeric(std::string &output)
276 // we don't really have a limit...
277 output = output + " ESILENCE SILENCE=" + ConvToStr(maxsilence);
280 virtual void OnBuildExemptList(MessageType message_type, chanrec* chan, userrec* sender, char status, CUList &exempt_list)
282 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
287 ulist = chan->GetOppedUsers();
290 ulist = chan->GetHalfoppedUsers();
293 ulist = chan->GetVoicedUsers();
296 ulist = chan->GetUsers();
300 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
302 if (IS_LOCAL(i->first))
304 if (MatchPattern(i->first, sender, public_silence) == 1)
306 exempt_list[i->first] = i->first->nick;
312 virtual int PreText(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
317 if (target_type == TYPE_USER)
319 return MatchPattern((userrec*)dest, user, silence_type);
321 else if (target_type == TYPE_CHANNEL)
323 chanrec* chan = (chanrec*)dest;
326 this->OnBuildExemptList((silence_type == SILENCE_PRIVATE ? MSG_PRIVMSG : MSG_NOTICE), chan, user, status, exempt_list);
332 virtual int OnUserPreMessage(userrec* 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 virtual int OnUserPreNotice(userrec* 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 virtual int OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel)
344 return MatchPattern(dest, source, SILENCE_INVITE);
347 int MatchPattern(userrec* dest, userrec* source, int pattern)
350 dest->GetExt("silence_list", sl);
353 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
355 if (((((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0)) && (ServerInstance->MatchText(source->GetFullHost(), c->first)))
356 return !(((c->second & SILENCE_EXCLUDE) > 0));
362 virtual ~ModuleSilence()
366 virtual Version GetVersion()
368 return Version(1, 1, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
372 MODULE_INIT(ModuleSilence)